From 1fb3950e6b59be83305095683e30c74bda8904b7 Mon Sep 17 00:00:00 2001 From: "Crom (Thibaut CHARLES)" Date: Sun, 10 Dec 2017 15:47:17 +0100 Subject: [PATCH] User registration return M_USER_IN_USE when username is already taken --- .../dendrite/clientapi/jsonerror/jsonerror.go | 6 ++++++ .../dendrite/clientapi/routing/register.go | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror.go b/src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror.go index 07fe90304..747924188 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror.go +++ b/src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror.go @@ -103,6 +103,12 @@ func InvalidUsername(msg string) *MatrixError { return &MatrixError{"M_INVALID_USERNAME", msg} } +// InvalidUsername is an error returned when the client tries to register an +// username that already exists +func UserInUse(msg string) *MatrixError { + return &MatrixError{"M_USER_IN_USE", msg} +} + // GuestAccessForbidden is an error which is returned when the client is // forbidden from accessing a resource as a guest. func GuestAccessForbidden(msg string) *MatrixError { 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 7bd5820f9..73282d47b 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/register.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/register.go @@ -457,6 +457,19 @@ func completeRegistration( } } + avail, err := accountDB.CheckAccountAvailability(ctx, username) + if err == nil && avail == false { + return util.JSONResponse{ + Code: 400, + JSON: jsonerror.UserInUse("Desired user ID is already taken."), + } + } else if err != nil { + return util.JSONResponse{ + Code: 500, + JSON: jsonerror.Unknown("Failed to check account availability: " + err.Error()), + } + } + acc, err := accountDB.CreateAccount(ctx, username, password) if err != nil { return util.JSONResponse{