From 2936c49b0b932ae48d0fe3e2296b80123ef02cfa Mon Sep 17 00:00:00 2001 From: "Andrew Morgan (https://amorgan.xyz)" Date: Wed, 7 Feb 2018 19:17:09 -0800 Subject: [PATCH] Error cleanup and minor fixes --- .../auth/storage/accounts/accounts_table.go | 4 ++-- .../clientapi/auth/storage/accounts/storage.go | 2 +- .../dendrite/clientapi/jsonerror/jsonerror.go | 9 +++++---- .../dendrite/clientapi/routing/register.go | 12 ++++++------ 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/accounts_table.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/accounts_table.go index a3961a305..57408296e 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/accounts_table.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/accounts_table.go @@ -33,7 +33,7 @@ CREATE TABLE IF NOT EXISTS account_accounts ( created_ts BIGINT NOT NULL, -- The password hash for this account. Can be NULL if this is a passwordless account. password_hash TEXT, - -- Identifies which Application Service this account belongs to. + -- Identifies which Application Service this account belongs to, if any. appservice_id TEXT -- TODO: -- is_guest, is_admin, upgraded_ts, devices, any email reset stuff? @@ -84,7 +84,7 @@ func (s *accountsStatements) insertAccount( ) (*authtypes.Account, error) { createdTimeMS := time.Now().UnixNano() / 1000000 stmt := s.insertAccountStmt - if _, err := stmt.ExecContext(ctx, localpart, createdTimeMS, hash, appserviceID); err != nil { + if _, err := stmt.ExecContext(ctx, localpart, createdTimeMS, hash, appserviceID ? appserviceID : nil); err != nil { return nil, err } return &authtypes.Account{ 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 875c56741..571482739 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 @@ -127,7 +127,7 @@ func (d *Database) CreateAccount( // Generate a password hash if this is not a password-less user hash := "" - if appserviceID == "" && plaintextPassword != "" { + if plaintextPassword != "" { hash, err = hashPassword(plaintextPassword) if err != nil { return nil, err 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 fe2823485..571ed49b3 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror.go +++ b/src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror.go @@ -86,7 +86,7 @@ func MissingToken(msg string) *MatrixError { } // UnknownToken is an error when the client tries to access a resource which -// requires authentication and supplies a valid, but out-of-date token. +// requires authentication and supplies an unrecognized token func UnknownToken(msg string) *MatrixError { return &MatrixError{"M_UNKNOWN_TOKEN", msg} } @@ -109,9 +109,10 @@ func UserInUse(msg string) *MatrixError { return &MatrixError{"M_USER_IN_USE", msg} } -// Exclusive is an error returned when an application service tries to register -// an username that is outside of its registered namespace -func Exclusive(msg string) *MatrixError { +// ASExclusive is an error returned when an application service tries to +// register an username that is outside of its registered namespace, or if a +// user attempts to register a username within an exclusive namespace +func ASExclusive(msg string) *MatrixError { return &MatrixError{"M_EXCLUSIVE", msg} } 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 71921026c..107344c3a 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/register.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/register.go @@ -312,7 +312,7 @@ func validateApplicationService( if matchedApplicationService != nil { return "", &util.JSONResponse{ Code: 401, - JSON: jsonerror.BadJSON("Supplied access_token does not match any known application service"), + JSON: jsonerror.UnknownToken("Supplied access_token does not match any known application service"), } } @@ -321,8 +321,8 @@ func validateApplicationService( // If we didn't find any matches, return M_EXCLUSIVE return "", &util.JSONResponse{ Code: 401, - JSON: jsonerror.Exclusive("Supplied username " + username + - " did not match any namespaces for application service ID: " + matchedApplicationService.ID), + JSON: jsonerror.ASExclusive(fmt.Sprintf( + "Supplied username %s did not match any namespaces for application service ID: %s", username, matchedApplicationService.ID)), } } @@ -330,8 +330,8 @@ func validateApplicationService( if UsernameMatchesMultipleExclusiveNamespaces(cfg, username) { return "", &util.JSONResponse{ Code: 401, - JSON: jsonerror.Exclusive("Supplied username " + username + - " matches multiple exclusive application service namespaces. Only 1 match allowed"), + JSON: jsonerror.ASExclusive(fmt.Sprintf( + "Supplied username %s matches multiple exclusive application service namespaces. Only 1 match allowed", username)), } } @@ -386,7 +386,7 @@ func Register( cfg.Derived.ExclusiveApplicationServicesUsernameRegexp.MatchString(r.Username) { return util.JSONResponse{ Code: 400, - JSON: jsonerror.Exclusive("This username is registered by an application service."), + JSON: jsonerror.ASExclusive("This username is reserved by an application service."), } }