Update templates, remove default base URL

This commit is contained in:
Till Faelligen 2022-03-07 09:45:24 +01:00
parent 7c6a162c0f
commit c7d2254698
8 changed files with 41 additions and 33 deletions

View file

@ -86,7 +86,7 @@ global:
enabled: false enabled: false
# The base URL this homeserver will serve clients on, e.g. https://matrix.org # The base URL this homeserver will serve clients on, e.g. https://matrix.org
base_url: http://localhost base_url: http://localhost
# Randomly generated string to be used to calculate the HMAC # Randomly generated string (e.g. by using "pwgen -sy 32") to be used to calculate the HMAC
form_secret: "superSecretRandomlyGeneratedSecret" form_secret: "superSecretRandomlyGeneratedSecret"
# Require consent when user registers for the first time # Require consent when user registers for the first time
require_at_registration: false require_at_registration: false

View file

@ -12,13 +12,13 @@
<p> <p>
Please give your consent to keep using this homeserver. Please give your consent to keep using this homeserver.
</p> </p>
{{ if not .PublicVersion }} {{ if not .ReadOnly }}
<!-- The variables used here are only provided when the 'u' param is given to the homeserver --> <!-- The variables used here are only provided when the 'u' param is given to the homeserver -->
<form method="post" action="consent"> <form method="post" action="consent">
<input type="hidden" name="v" value="{{ .Version }}"/> <input type="hidden" name="v" value="{{ .Version }}"/>
<input type="hidden" name="u" value="{{ .User }}"/> <input type="hidden" name="u" value="{{ .UserID }}"/>
<input type="hidden" name="h" value="{{ .UserHMAC }}"/> <input type="hidden" name="h" value="{{ .UserHMAC }}"/>
<input type="submit" value="Sure thing!"/> <input type="submit" value="I consent"/>
</form> </form>
{{ end }} {{ end }}
{{ end }} {{ end }}

View file

@ -25,6 +25,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/http/httputil" "net/http/httputil"
"net/url"
"os" "os"
"strings" "strings"
"sync" "sync"
@ -119,14 +120,14 @@ func MakeAuthAPI(
} }
func checkConsent(ctx context.Context, userID string, userAPI userapi.UserInternalAPI, userConsentCfg config.UserConsentOptions) *util.JSONResponse { func checkConsent(ctx context.Context, userID string, userAPI userapi.UserInternalAPI, userConsentCfg config.UserConsentOptions) *util.JSONResponse {
localPart, _, err := gomatrixserverlib.SplitID('@', userID) localpart, _, err := gomatrixserverlib.SplitID('@', userID)
if err != nil { if err != nil {
return nil return nil
} }
// check which version of the policy the user accepted // check which version of the policy the user accepted
res := &userapi.QueryPolicyVersionResponse{} res := &userapi.QueryPolicyVersionResponse{}
err = userAPI.QueryPolicyVersion(ctx, &userapi.QueryPolicyVersionRequest{ err = userAPI.QueryPolicyVersion(ctx, &userapi.QueryPolicyVersionRequest{
Localpart: localPart, Localpart: localpart,
}, res) }, res)
if err != nil { if err != nil {
return &util.JSONResponse{ return &util.JSONResponse{
@ -166,18 +167,20 @@ func checkConsent(ctx context.Context, userID string, userAPI userapi.UserIntern
} }
// getConsentURL constructs the URL shown to users to accept the TOS // getConsentURL constructs the URL shown to users to accept the TOS
func getConsentURL(username string, config config.UserConsentOptions) (string, error) { func getConsentURL(userID string, config config.UserConsentOptions) (string, error) {
mac := hmac.New(sha256.New, []byte(config.FormSecret)) mac := hmac.New(sha256.New, []byte(config.FormSecret))
_, err := mac.Write([]byte(username)) _, err := mac.Write([]byte(userID))
if err != nil { if err != nil {
return "", err return "", err
} }
hmac := hex.EncodeToString(mac.Sum(nil)) hmac := hex.EncodeToString(mac.Sum(nil))
return fmt.Sprintf( params := url.Values{}
"%s/_matrix/client/consent?u=%s&h=%s&v=%s", params.Add("u", userID)
config.BaseURL, username, hmac, config.Version, params.Add("h", string(hmac))
), nil params.Add("v", config.Version)
return fmt.Sprintf("%s/_matrix/client/consent?%s", config.BaseURL, params.Encode()), nil
} }
// MakeExternalAPI turns a util.JSONRequestHandler function into an http.Handler. // MakeExternalAPI turns a util.JSONRequestHandler function into an http.Handler.

View file

@ -59,14 +59,14 @@ func Setup(
PathToResult: map[string]*types.ThumbnailGenerationResult{}, PathToResult: map[string]*types.ThumbnailGenerationResult{},
} }
uploadHandler := httputil.MakeAuthAPI("upload", userAPI, cfg.Matrix.UserConsentOptions, false, func(req *http.Request, dev *userapi.Device) util.JSONResponse { uploadHandler := httputil.MakeAuthAPI("upload", userAPI, func(req *http.Request, dev *userapi.Device) util.JSONResponse {
if r := rateLimits.Limit(req); r != nil { if r := rateLimits.Limit(req); r != nil {
return *r return *r
} }
return Upload(req, cfg, dev, db, activeThumbnailGeneration) return Upload(req, cfg, dev, db, activeThumbnailGeneration)
}) })
configHandler := httputil.MakeAuthAPI("config", userAPI, cfg.Matrix.UserConsentOptions, false, func(req *http.Request, device *userapi.Device) util.JSONResponse { configHandler := httputil.MakeAuthAPI("config", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse {
if r := rateLimits.Limit(req); r != nil { if r := rateLimits.Limit(req); r != nil {
return *r return *r
} }

View file

@ -275,7 +275,6 @@ func (c *UserConsentOptions) Defaults() {
c.PolicyName = "Privacy Policy" c.PolicyName = "Privacy Policy"
c.Version = "1.0" c.Version = "1.0"
c.TemplateDir = "./templates/privacy" c.TemplateDir = "./templates/privacy"
c.BaseURL = "http://localhost"
} }
func (c *UserConsentOptions) Verify(configErrors *ConfigErrors, isMonolith bool) { func (c *UserConsentOptions) Verify(configErrors *ConfigErrors, isMonolith bool) {
@ -284,6 +283,7 @@ func (c *UserConsentOptions) Verify(configErrors *ConfigErrors, isMonolith bool)
checkNotEmpty(configErrors, "version", c.Version) checkNotEmpty(configErrors, "version", c.Version)
checkNotEmpty(configErrors, "policy_name", c.PolicyName) checkNotEmpty(configErrors, "policy_name", c.PolicyName)
checkNotEmpty(configErrors, "form_secret", c.FormSecret) checkNotEmpty(configErrors, "form_secret", c.FormSecret)
checkNotEmpty(configErrors, "base_url", c.BaseURL)
if len(*configErrors) > 0 { if len(*configErrors) > 0 {
return return
} }

View file

@ -82,6 +82,8 @@ func TestUserConsentOptions_Verify(t *testing.T) {
TemplateDir: "./testdata/privacy", TemplateDir: "./testdata/privacy",
Version: "1.0", Version: "1.0",
PolicyName: "Privacy policy", PolicyName: "Privacy policy",
FormSecret: "helloWorld",
BaseURL: "http://localhost",
}, },
args: struct { args: struct {
configErrors *ConfigErrors configErrors *ConfigErrors
@ -93,6 +95,9 @@ func TestUserConsentOptions_Verify(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
c := &UserConsentOptions{ c := &UserConsentOptions{
Enabled: true,
BaseURL: tt.fields.BaseURL,
FormSecret: tt.fields.FormSecret,
RequireAtRegistration: tt.fields.RequireAtRegistration, RequireAtRegistration: tt.fields.RequireAtRegistration,
PolicyName: tt.fields.PolicyName, PolicyName: tt.fields.PolicyName,
Version: tt.fields.Version, Version: tt.fields.Version,
@ -102,7 +107,7 @@ func TestUserConsentOptions_Verify(t *testing.T) {
BlockEventsError: tt.fields.BlockEventsError, BlockEventsError: tt.fields.BlockEventsError,
} }
c.Verify(tt.args.configErrors, tt.args.isMonolith) c.Verify(tt.args.configErrors, tt.args.isMonolith)
if tt.wantErr && tt.args.configErrors == nil { if !tt.wantErr && len(*tt.args.configErrors) > 0 {
t.Errorf("expected no errors, got '%+v'", tt.args.configErrors) t.Errorf("expected no errors, got '%+v'", tt.args.configErrors)
} }
}) })

View file

@ -1,26 +1,26 @@
<!doctype html> <!doctype html>
<html lang="en"> <html lang="en">
<head> <head>
<title>Matrix.org Privacy policy</title> <title>Privacy policy</title>
</head> </head>
<body> <body>
{{ if .HasConsented }} {{ if .HasConsented }}
<p> <p>
Your base already belong to us. You have already given your consent.
</p> </p>
{{ else }} {{ else }}
<p> <p>
All your base are belong to us. Please give your consent to keep using this homeserver.
</p> </p>
{{ if not .PublicVersion }} {{ if not .ReadOnly }}
<!-- The variables used here are only provided when the 'u' param is given to the homeserver --> <!-- The variables used here are only provided when the 'u' param is given to the homeserver -->
<form method="post" action="consent"> <form method="post" action="consent">
<input type="hidden" name="v" value="{{ .Version }}"/> <input type="hidden" name="v" value="{{ .Version }}"/>
<input type="hidden" name="u" value="{{ .User }}"/> <input type="hidden" name="u" value="{{ .UserID }}"/>
<input type="hidden" name="h" value="{{ .UserHMAC }}"/> <input type="hidden" name="h" value="{{ .UserHMAC }}"/>
<input type="submit" value="Sure thing!"/> <input type="submit" value="I consent"/>
</form> </form>
{{ end }} {{ end }}
{{ end }} {{ end }}
</body> </body>
</html> </html>

View file

@ -126,7 +126,7 @@ func Enable(
}) })
base.PublicClientAPIMux.Handle("/unstable/event_relationships", base.PublicClientAPIMux.Handle("/unstable/event_relationships",
httputil.MakeAuthAPI("eventRelationships", userAPI, base.Cfg.Global.UserConsentOptions, false, eventRelationshipHandler(db, rsAPI, fsAPI)), httputil.MakeAuthAPI("eventRelationships", userAPI, eventRelationshipHandler(db, rsAPI, fsAPI)),
).Methods(http.MethodPost, http.MethodOptions) ).Methods(http.MethodPost, http.MethodOptions)
base.PublicFederationAPIMux.Handle("/unstable/event_relationships", httputil.MakeExternalAPI( base.PublicFederationAPIMux.Handle("/unstable/event_relationships", httputil.MakeExternalAPI(