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 d19c97b1a..958f1c2bd 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/register.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/register.go @@ -279,16 +279,10 @@ func LegacyRegister( cfg *config.Dendrite, ) util.JSONResponse { var r legacyRegisterRequest - resErr := httputil.UnmarshalJSONRequest(req, &r) + resErr := parseAndValidateLegacyLogin(req, &r) if resErr != nil { return *resErr } - if resErr = validateUserName(r.Username); resErr != nil { - return *resErr - } - if resErr = validatePassword(r.Password); resErr != nil { - return *resErr - } logger := util.GetLogger(req.Context()) logger.WithFields(log.Fields{ @@ -296,14 +290,6 @@ func LegacyRegister( "auth.type": r.Type, }).Info("Processing registration request") - // All registration requests must specify what auth they are using to perform this request - if r.Type == "" { - return util.JSONResponse{ - Code: 400, - JSON: jsonerror.BadJSON("invalid type"), - } - } - if cfg.Matrix.RegistrationDisabled && r.Type != authtypes.LoginTypeSharedSecret { return util.MessageResponse(403, "Registration has been disabled") } @@ -335,6 +321,31 @@ func LegacyRegister( } } +// parseAndValidateLegacyLogin parses the request into r and checks that the +// request is valid (e.g. valid user names, etc) +func parseAndValidateLegacyLogin(req *http.Request, r *legacyRegisterRequest) *util.JSONResponse { + resErr := httputil.UnmarshalJSONRequest(req, &r) + if resErr != nil { + return resErr + } + if resErr = validateUserName(r.Username); resErr != nil { + return resErr + } + if resErr = validatePassword(r.Password); resErr != nil { + return resErr + } + + // All registration requests must specify what auth they are using to perform this request + if r.Type == "" { + return &util.JSONResponse{ + Code: 400, + JSON: jsonerror.BadJSON("invalid type"), + } + } + + return nil +} + func completeRegistration( ctx context.Context, accountDB *accounts.Database,