Refactor LegacyRegister

This commit is contained in:
Erik Johnston 2017-11-29 18:07:07 +00:00
parent 82a34b1029
commit 431e1678d1

View file

@ -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,