From 42407623316b5f31f405d798655fd21a9d1d71c7 Mon Sep 17 00:00:00 2001 From: MTRNord Date: Sat, 7 Oct 2017 13:18:13 +0200 Subject: [PATCH] Remove OPTION method on "/register/available", Add CheckAccountAvailability method Signed-off-by: MTRNord --- .../clientapi/auth/storage/accounts/storage.go | 10 ++++++++++ .../matrix-org/dendrite/clientapi/readers/register.go | 9 ++++++++- .../matrix-org/dendrite/clientapi/routing/routing.go | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go index 16474ec6b..5449df5c5 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go @@ -314,3 +314,13 @@ func (d *Database) GetThreePIDsForLocalpart( ) (threepids []authtypes.ThreePID, err error) { return d.threepids.selectThreePIDsForLocalpart(ctx, localpart) } + +// CheckAccountAvailability checks if the username/localpart is already present in the database. +// If the DB returns sql.ErrNoRows the Localpart isn't taken. +func (d *Database) CheckAccountAvailability(ctx context.Context, localpart string) (bool, error) { + _, err := d.accounts.selectAccountByLocalpart(ctx, localpart) + if err == sql.ErrNoRows { + return true, nil + } + return false, err +} diff --git a/src/github.com/matrix-org/dendrite/clientapi/readers/register.go b/src/github.com/matrix-org/dendrite/clientapi/readers/register.go index f1a35d4d2..c0a67f799 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/readers/register.go +++ b/src/github.com/matrix-org/dendrite/clientapi/readers/register.go @@ -50,7 +50,14 @@ func RegisterAvailable( return *resErr } - if _, resErr := accountDB.GetProfileByLocalpart(req.Context(), username); resErr == nil { + availability, availabilityErr := accountDB.CheckAccountAvailability(req.Context(), username) + if availabilityErr != nil { + return util.JSONResponse{ + Code: 500, + JSON: jsonerror.Unknown("failed to check availability: " + availabilityErr.Error()), + } + } + if !availability { return util.JSONResponse{ Code: 400, JSON: jsonerror.InvalidUsername("A different user ID has already been registered for this session"), 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 2fd59b999..858a57ab9 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go @@ -133,7 +133,7 @@ func Setup( r0mux.Handle("/register/available", common.MakeExternalAPI("registerAvailable", func(req *http.Request) util.JSONResponse { return readers.RegisterAvailable(req, accountDB) - })).Methods("GET", "OPTIONS") + })).Methods("GET") r0mux.Handle("/directory/room/{roomAlias}", common.MakeAuthAPI("directory_room", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {