Files

96 lines
3.2 KiB
Go
Raw Permalink Normal View History

2021-07-09 15:15:20 +00:00
package middleware
import (
"net/http"
gmmetadata "go-micro.dev/v4/metadata"
"google.golang.org/grpc/metadata"
2022-05-04 14:49:59 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/account"
2022-10-13 15:59:30 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/log"
opkgm "github.com/owncloud/ocis/v2/ocis-pkg/middleware"
"github.com/owncloud/ocis/v2/services/graph/pkg/errorcode"
2025-03-04 16:02:36 +01:00
"github.com/owncloud/reva/v2/pkg/auth/scope"
ctxpkg "github.com/owncloud/reva/v2/pkg/ctx"
revactx "github.com/owncloud/reva/v2/pkg/ctx"
"github.com/owncloud/reva/v2/pkg/token/manager/jwt"
2021-07-09 15:15:20 +00:00
)
// authOptions initializes the available default options.
func authOptions(opts ...account.Option) account.Options {
opt := account.Options{}
for _, o := range opts {
o(&opt)
}
return opt
}
2021-09-17 05:21:38 -04:00
// Auth provides a middleware to authenticate requests using the x-access-token header value
2021-07-09 15:15:20 +00:00
// and write it to the context. If there is no x-access-token the middleware prevents access and renders a json document.
func Auth(opts ...account.Option) func(http.Handler) http.Handler {
2022-05-24 15:34:25 +02:00
// Note: This largely duplicates what ocis-pkg/middleware/account.go already does (apart from a slightly different error
// handling). Ideally we should merge both middlewares.
2021-07-09 15:15:20 +00:00
opt := authOptions(opts...)
tokenManager, err := jwt.New(map[string]interface{}{
"secret": opt.JWTSecret,
"expires": int64(24 * 60 * 60),
2021-07-09 15:15:20 +00:00
})
if err != nil {
opt.Logger.Fatal().Err(err).Msgf("Could not initialize token-manager")
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
t := r.Header.Get("x-access-token")
if t == "" {
errorcode.InvalidAuthenticationToken.Render(w, r, http.StatusUnauthorized, "Access token is empty.")
/* msgraph error for GET https://graph.microsoft.com/v1.0/me
{
"error":
{
"code":"InvalidAuthenticationToken",
"message":"Access token is empty.",
"innerError":{
"date":"2021-07-09T14:40:51",
"request-id":"bb12f7db-b4c4-43a9-ba4b-31676aeed019",
"client-request-id":"bb12f7db-b4c4-43a9-ba4b-31676aeed019"
}
}
}
*/
return
}
u, tokenScope, err := tokenManager.DismantleToken(r.Context(), t)
if err != nil {
errorcode.InvalidAuthenticationToken.Render(w, r, http.StatusUnauthorized, "invalid token")
return
}
2021-10-15 17:26:48 +02:00
if ok, err := scope.VerifyScope(ctx, tokenScope, r); err != nil || !ok {
2022-10-13 15:59:30 +02:00
opt.Logger.Error().Str(log.RequestIDString, r.Header.Get("X-Request-ID")).Err(err).Msg("verifying scope failed")
2021-07-09 15:15:20 +00:00
errorcode.InvalidAuthenticationToken.Render(w, r, http.StatusUnauthorized, "verifying scope failed")
return
}
2021-08-11 20:33:27 +00:00
ctx = revactx.ContextSetToken(ctx, t)
ctx = revactx.ContextSetUser(ctx, u)
2024-04-23 15:06:23 +02:00
ctx = gmmetadata.Set(ctx, opkgm.AccountID, u.GetId().GetOpaqueId())
if m := u.GetOpaque().GetMap(); m != nil {
if roles, ok := m["roles"]; ok {
ctx = gmmetadata.Set(ctx, opkgm.RoleIDs, string(roles.GetValue()))
2022-05-30 16:01:43 +02:00
}
}
2021-08-11 20:33:27 +00:00
ctx = metadata.AppendToOutgoingContext(ctx, revactx.TokenHeader, t)
2021-07-09 15:15:20 +00:00
2024-04-23 15:06:23 +02:00
initiatorID := r.Header.Get(ctxpkg.InitiatorHeader)
if initiatorID != "" {
ctx = ctxpkg.ContextSetInitiator(ctx, initiatorID)
ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.InitiatorHeader, initiatorID)
}
2021-07-09 15:15:20 +00:00
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}