diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/auth.go b/src/github.com/matrix-org/dendrite/clientapi/auth/auth.go index 927e294c5..cb809dcd3 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/auth.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/auth.go @@ -6,17 +6,24 @@ import ( "strings" "github.com/matrix-org/dendrite/clientapi/jsonerror" + "github.com/matrix-org/util" ) // VerifyAccessToken verifies that an access token was supplied in the given HTTP request -// and returns the user ID it corresponds to. Returns an error if there is no access token -// or the token is invalid. -func VerifyAccessToken(req *http.Request) (userID string, err error) { - _, tokenErr := extractAccessToken(req) +// and returns the user ID it corresponds to. Returns err if there was a fatal problem checking +// the token. Returns resErr (an error response which can be sent to the client) if the token is invalid. +func VerifyAccessToken(req *http.Request) (userID string, resErr *util.JSONResponse, err error) { + token, tokenErr := extractAccessToken(req) if tokenErr != nil { - err = jsonerror.MissingToken(tokenErr.Error()) + resErr = &util.JSONResponse{ + Code: 401, + JSON: jsonerror.MissingToken(tokenErr.Error()), + } return } + if token == "fail" { + err = fmt.Errorf("Fatal error") + } // TODO: Check the token against the database return } 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 a0111197c..ea64896db 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror.go +++ b/src/github.com/matrix-org/dendrite/clientapi/jsonerror/jsonerror.go @@ -1,6 +1,9 @@ package jsonerror -import "fmt" +import ( + "fmt" + "github.com/matrix-org/util" +) // MatrixError represents the "standard error response" in Matrix. // http://matrix.org/docs/spec/client_server/r0.2.0.html#api-standards @@ -13,6 +16,20 @@ func (e *MatrixError) Error() string { return fmt.Sprintf("%s: %s", e.ErrCode, e.Err) } +// InternalServerError returns a 500 Internal Server Error in a matrix-compliant +// format. +func InternalServerError() util.JSONResponse { + return util.JSONResponse{ + Code: 500, + JSON: Unknown("Internal Server Error"), + } +} + +// Unknown is an unexpected error +func Unknown(msg string) *MatrixError { + return &MatrixError{"M_UNKNOWN", msg} +} + // Forbidden is an error when the client tries to access a resource // they are not allowed to access. func Forbidden(msg string) *MatrixError { diff --git a/src/github.com/matrix-org/dendrite/clientapi/readers/sync.go b/src/github.com/matrix-org/dendrite/clientapi/readers/sync.go index 8db717103..05def8148 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/readers/sync.go +++ b/src/github.com/matrix-org/dendrite/clientapi/readers/sync.go @@ -4,18 +4,20 @@ import ( "net/http" "github.com/matrix-org/dendrite/clientapi/auth" + "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/util" ) // Sync implements /sync func Sync(req *http.Request) util.JSONResponse { logger := util.GetLogger(req.Context()) - userID, err := auth.VerifyAccessToken(req) + userID, resErr, err := auth.VerifyAccessToken(req) if err != nil { - return util.JSONResponse{ - Code: 403, - JSON: err, - } + logger.WithError(err).Error("Failed to verify access token") + return jsonerror.InternalServerError() + } + if resErr != nil { + return *resErr } logger.WithField("userID", userID).Info("Doing stuff...") diff --git a/src/github.com/matrix-org/dendrite/clientapi/writers/sendmessage.go b/src/github.com/matrix-org/dendrite/clientapi/writers/sendmessage.go index d6713b1af..4b8ad9052 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/writers/sendmessage.go +++ b/src/github.com/matrix-org/dendrite/clientapi/writers/sendmessage.go @@ -5,18 +5,20 @@ import ( log "github.com/Sirupsen/logrus" "github.com/matrix-org/dendrite/clientapi/auth" + "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/util" ) // SendMessage implements /rooms/{roomID}/send/{eventType} func SendMessage(req *http.Request, roomID, eventType string) util.JSONResponse { logger := util.GetLogger(req.Context()) - userID, err := auth.VerifyAccessToken(req) + userID, resErr, err := auth.VerifyAccessToken(req) if err != nil { - return util.JSONResponse{ - Code: 403, - JSON: err, - } + logger.WithError(err).Error("Failed to verify access token") + return jsonerror.InternalServerError() + } + if resErr != nil { + return *resErr } logger.WithFields(log.Fields{ "roomID": roomID,