From 2382d363ab2449e53191fdb301407969ec2e5612 Mon Sep 17 00:00:00 2001 From: Andrew Morgan <1342360+anoadragon453@users.noreply.github.com> Date: Mon, 20 Aug 2018 02:23:01 -0700 Subject: [PATCH] Include appservice namespace in username available check (#504) Signed-off-by: Andrew Morgan --- .../dendrite/clientapi/routing/register.go | 14 +++++++++++++- .../dendrite/clientapi/routing/routing.go | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/register.go b/src/github.com/matrix-org/dendrite/clientapi/routing/register.go index ef8577709..d37976343 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/register.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/register.go @@ -866,6 +866,7 @@ type availableResponse struct { // RegisterAvailable checks if the username is already taken or invalid. func RegisterAvailable( req *http.Request, + cfg config.Dendrite, accountDB *accounts.Database, ) util.JSONResponse { username := req.URL.Query().Get("username") @@ -877,6 +878,17 @@ func RegisterAvailable( return *err } + // Check if this username is reserved by an application service + userID := userutil.MakeUserID(username, cfg.Matrix.ServerName) + for _, appservice := range cfg.Derived.ApplicationServices { + if appservice.IsInterestedInUserID(userID) { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.UserInUse("Desired user ID is reserved by an application service."), + } + } + } + availability, availabilityErr := accountDB.CheckAccountAvailability(req.Context(), username) if availabilityErr != nil { return util.JSONResponse{ @@ -887,7 +899,7 @@ func RegisterAvailable( if !availability { return util.JSONResponse{ Code: http.StatusBadRequest, - JSON: jsonerror.InvalidUsername("A different user ID has already been registered for this session"), + JSON: jsonerror.UserInUse("Desired User ID is already taken."), } } diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go index ced000bb1..6081fc4a8 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go @@ -139,7 +139,7 @@ func Setup( })).Methods(http.MethodPost, http.MethodOptions) r0mux.Handle("/register/available", common.MakeExternalAPI("registerAvailable", func(req *http.Request) util.JSONResponse { - return RegisterAvailable(req, accountDB) + return RegisterAvailable(req, cfg, accountDB) })).Methods(http.MethodGet, http.MethodOptions) r0mux.Handle("/directory/room/{roomAlias}",