mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-03-21 17:14:28 -05:00
three-value returns
This commit is contained in:
parent
e0c6f9b50e
commit
4137b6185d
|
@ -6,17 +6,24 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
"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
|
// 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
|
// and returns the user ID it corresponds to. Returns err if there was a fatal problem checking
|
||||||
// or the token is invalid.
|
// 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, err error) {
|
func VerifyAccessToken(req *http.Request) (userID string, resErr *util.JSONResponse, err error) {
|
||||||
_, tokenErr := extractAccessToken(req)
|
token, tokenErr := extractAccessToken(req)
|
||||||
if tokenErr != nil {
|
if tokenErr != nil {
|
||||||
err = jsonerror.MissingToken(tokenErr.Error())
|
resErr = &util.JSONResponse{
|
||||||
|
Code: 401,
|
||||||
|
JSON: jsonerror.MissingToken(tokenErr.Error()),
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if token == "fail" {
|
||||||
|
err = fmt.Errorf("Fatal error")
|
||||||
|
}
|
||||||
// TODO: Check the token against the database
|
// TODO: Check the token against the database
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package jsonerror
|
package jsonerror
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/matrix-org/util"
|
||||||
|
)
|
||||||
|
|
||||||
// MatrixError represents the "standard error response" in Matrix.
|
// MatrixError represents the "standard error response" in Matrix.
|
||||||
// http://matrix.org/docs/spec/client_server/r0.2.0.html#api-standards
|
// 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)
|
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
|
// Forbidden is an error when the client tries to access a resource
|
||||||
// they are not allowed to access.
|
// they are not allowed to access.
|
||||||
func Forbidden(msg string) *MatrixError {
|
func Forbidden(msg string) *MatrixError {
|
||||||
|
|
|
@ -4,18 +4,20 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth"
|
"github.com/matrix-org/dendrite/clientapi/auth"
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Sync implements /sync
|
// Sync implements /sync
|
||||||
func Sync(req *http.Request) util.JSONResponse {
|
func Sync(req *http.Request) util.JSONResponse {
|
||||||
logger := util.GetLogger(req.Context())
|
logger := util.GetLogger(req.Context())
|
||||||
userID, err := auth.VerifyAccessToken(req)
|
userID, resErr, err := auth.VerifyAccessToken(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return util.JSONResponse{
|
logger.WithError(err).Error("Failed to verify access token")
|
||||||
Code: 403,
|
return jsonerror.InternalServerError()
|
||||||
JSON: err,
|
}
|
||||||
}
|
if resErr != nil {
|
||||||
|
return *resErr
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.WithField("userID", userID).Info("Doing stuff...")
|
logger.WithField("userID", userID).Info("Doing stuff...")
|
||||||
|
|
|
@ -5,18 +5,20 @@ import (
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth"
|
"github.com/matrix-org/dendrite/clientapi/auth"
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SendMessage implements /rooms/{roomID}/send/{eventType}
|
// SendMessage implements /rooms/{roomID}/send/{eventType}
|
||||||
func SendMessage(req *http.Request, roomID, eventType string) util.JSONResponse {
|
func SendMessage(req *http.Request, roomID, eventType string) util.JSONResponse {
|
||||||
logger := util.GetLogger(req.Context())
|
logger := util.GetLogger(req.Context())
|
||||||
userID, err := auth.VerifyAccessToken(req)
|
userID, resErr, err := auth.VerifyAccessToken(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return util.JSONResponse{
|
logger.WithError(err).Error("Failed to verify access token")
|
||||||
Code: 403,
|
return jsonerror.InternalServerError()
|
||||||
JSON: err,
|
}
|
||||||
}
|
if resErr != nil {
|
||||||
|
return *resErr
|
||||||
}
|
}
|
||||||
logger.WithFields(log.Fields{
|
logger.WithFields(log.Fields{
|
||||||
"roomID": roomID,
|
"roomID": roomID,
|
||||||
|
|
Loading…
Reference in a new issue