three-value returns

This commit is contained in:
Kegan Dougal 2017-02-24 15:14:47 +00:00
parent e0c6f9b50e
commit 4137b6185d
4 changed files with 44 additions and 16 deletions

View file

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

View file

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

View file

@ -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...")

View file

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