graph: Move token middleware and return proper JSON response on error.

Fixes #5661
This commit is contained in:
Daniel Swärd
2023-03-06 10:24:44 +01:00
committed by Ralf Haferkamp
parent e3d9b810bd
commit c17add76c4
4 changed files with 8 additions and 6 deletions
+2 -1
View File
@@ -8,6 +8,7 @@ import (
"github.com/justinas/alice"
"github.com/owncloud/ocis/v2/ocis-pkg/cors"
"github.com/owncloud/ocis/v2/ocis-pkg/middleware"
graphMiddleware "github.com/owncloud/ocis/v2/services/graph/pkg/middleware"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opencensus.io/zpages"
)
@@ -18,7 +19,7 @@ func NewService(opts ...Option) *http.Server {
mux := http.NewServeMux()
mux.Handle("/metrics", alice.New(
middleware.Token(
graphMiddleware.Token(
dopts.Token,
),
).Then(
@@ -3,13 +3,14 @@ package middleware
import (
"crypto/sha256"
"crypto/subtle"
"errors"
"net/http"
"github.com/owncloud/ocis/v2/services/graph/pkg/service/v0/errorcode"
)
var (
// ErrInvalidToken is returned when the request token is invalid.
ErrInvalidToken = errors.New("invalid or missing token")
ErrInvalidToken = "invalid or missing token"
)
// Token provides a middleware to check access secured by a static token.
@@ -26,7 +27,7 @@ func Token(token string) func(http.Handler) http.Handler {
header := r.Header.Get("Authorization")
if header == "" {
http.Error(w, ErrInvalidToken.Error(), http.StatusUnauthorized)
errorcode.InvalidAuthenticationToken.Render(w, r, http.StatusUnauthorized, ErrInvalidToken)
return
}
@@ -34,7 +35,7 @@ func Token(token string) func(http.Handler) http.Handler {
providedTokenHash := h.Sum([]byte(header))
if subtle.ConstantTimeCompare(requiredTokenHash, providedTokenHash) == 0 {
http.Error(w, ErrInvalidToken.Error(), http.StatusUnauthorized)
errorcode.InvalidAuthenticationToken.Render(w, r, http.StatusUnauthorized, ErrInvalidToken)
return
}
+1 -1
View File
@@ -114,7 +114,7 @@ func Server(opts ...Option) (http.Service, error) {
return http.Service{}, errors.Wrap(err, "could not initialize gateway client")
}
} else {
middlewares = append(middlewares, middleware.Token(options.Config.HTTP.APIToken))
middlewares = append(middlewares, graphMiddleware.Token(options.Config.HTTP.APIToken))
// use a dummy admin middleware for the chi router
requireAdminMiddleware = func(next stdhttp.Handler) stdhttp.Handler {
return next