From b6ee34918c79cbd82ab9320660fb9387d90ee758 Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Mon, 14 Feb 2022 13:41:21 +0100 Subject: [PATCH] Add consent tracking endpoint --- clientapi/clientapi.go | 3 +- clientapi/routing/consent_tracking.go | 47 +++++++++++++++++++++++++++ clientapi/routing/routing.go | 5 ++- 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 clientapi/routing/consent_tracking.go diff --git a/clientapi/clientapi.go b/clientapi/clientapi.go index d678ada96..a6b811300 100644 --- a/clientapi/clientapi.go +++ b/clientapi/clientapi.go @@ -36,6 +36,7 @@ import ( func AddPublicRoutes( router *mux.Router, synapseAdminRouter *mux.Router, + consentAPIMux *mux.Router, cfg *config.ClientAPI, accountsDB accounts.Database, federation *gomatrixserverlib.FederationClient, @@ -57,7 +58,7 @@ func AddPublicRoutes( } routing.Setup( - router, synapseAdminRouter, cfg, eduInputAPI, rsAPI, asAPI, + router, synapseAdminRouter, consentAPIMux, cfg, eduInputAPI, rsAPI, asAPI, accountsDB, userAPI, federation, syncProducer, transactionsCache, fsAPI, keyAPI, extRoomsProvider, mscCfg, ) diff --git a/clientapi/routing/consent_tracking.go b/clientapi/routing/consent_tracking.go new file mode 100644 index 000000000..5947855d4 --- /dev/null +++ b/clientapi/routing/consent_tracking.go @@ -0,0 +1,47 @@ +package routing + +import ( + "net/http" + + "github.com/matrix-org/dendrite/setup/config" + userapi "github.com/matrix-org/dendrite/userapi/api" + "github.com/sirupsen/logrus" +) + +// The data used to populate the /consent request +type constentTemplateData struct { + User string + Version string + UserHMAC string + HasConsented bool + PublicVersion bool +} + +func consent(userAPI userapi.UserInternalAPI, cfg *config.ClientAPI) http.HandlerFunc { + consentCfg := cfg.Matrix.UserConsentOptions + return func(writer http.ResponseWriter, req *http.Request) { + if !consentCfg.Enabled() { + writer.WriteHeader(http.StatusBadRequest) + _, _ = writer.Write([]byte("consent tracking is disabled")) + return + } + switch req.Method { + case http.MethodGet: + // The data used to populate the /consent request + data := constentTemplateData{ + User: req.FormValue("u"), + Version: req.FormValue("v"), + UserHMAC: req.FormValue("h"), + } + // display the privacy policy without a form + data.PublicVersion = data.User == "" || data.UserHMAC == "" || data.Version == "" + + err := consentCfg.Templates.ExecuteTemplate(writer, consentCfg.Version+".gohtml", data) + if err != nil { + logrus.WithError(err).Error("unable to print consent template") + } + case http.MethodPost: + + } + } +} diff --git a/clientapi/routing/routing.go b/clientapi/routing/routing.go index 732066166..d10817d1b 100644 --- a/clientapi/routing/routing.go +++ b/clientapi/routing/routing.go @@ -47,7 +47,7 @@ import ( // applied: // nolint: gocyclo func Setup( - publicAPIMux, synapseAdminRouter *mux.Router, cfg *config.ClientAPI, + publicAPIMux, synapseAdminRouter, consentAPIMux *mux.Router, cfg *config.ClientAPI, eduAPI eduServerAPI.EDUServerInputAPI, rsAPI roomserverAPI.RoomserverInternalAPI, asAPI appserviceAPI.AppServiceQueryAPI, @@ -117,6 +117,9 @@ func Setup( ).Methods(http.MethodGet, http.MethodPost, http.MethodOptions) } + // unspecced consent tracking + consentAPIMux.HandleFunc("/consent", consent(userAPI, cfg)).Methods(http.MethodGet, http.MethodPost, http.MethodOptions) + r0mux := publicAPIMux.PathPrefix("/r0").Subrouter() unstableMux := publicAPIMux.PathPrefix("/unstable").Subrouter()