Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
156 lines
4.3 KiB
Go
156 lines
4.3 KiB
Go
// Copyright © 2023 Ory Corp
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package jwtmiddleware
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/ory/herodot"
|
|
|
|
jwtmiddleware "github.com/auth0/go-jwt-middleware/v2"
|
|
"github.com/urfave/negroni"
|
|
|
|
"github.com/ory/x/jwksx"
|
|
)
|
|
|
|
type Middleware struct {
|
|
o *middlewareOptions
|
|
wku string
|
|
jm *jwtmiddleware.JWTMiddleware
|
|
}
|
|
|
|
type middlewareOptions struct {
|
|
Debug bool
|
|
ExcludePaths []string
|
|
SigningMethod jwt.SigningMethod
|
|
ErrorWriter herodot.Writer
|
|
}
|
|
|
|
type MiddlewareOption func(*middlewareOptions)
|
|
|
|
func SessionFromContext(ctx context.Context) (json.RawMessage, error) {
|
|
raw := ctx.Value(jwtmiddleware.ContextKey{})
|
|
if raw == nil {
|
|
return nil, errors.WithStack(herodot.ErrUnauthorized().WithReasonf("Could not find credentials in the request."))
|
|
}
|
|
|
|
token, ok := raw.(*jwt.Token)
|
|
if !ok {
|
|
return nil, errors.WithStack(herodot.ErrInternalServerError().WithDebugf(`Expected context key "%T" to transport value of type *jwt.MapClaims but got type: %T`, jwtmiddleware.ContextKey{}, raw))
|
|
}
|
|
|
|
session, err := json.Marshal(token.Claims)
|
|
if err != nil {
|
|
return nil, errors.WithStack(herodot.ErrInternalServerError().WithDebugf("Unable to encode session data: %s", err))
|
|
}
|
|
|
|
return session, nil
|
|
}
|
|
|
|
func MiddlewareDebugEnabled() MiddlewareOption {
|
|
return func(o *middlewareOptions) {
|
|
o.Debug = true
|
|
}
|
|
}
|
|
|
|
func MiddlewareExcludePaths(paths ...string) MiddlewareOption {
|
|
return func(o *middlewareOptions) {
|
|
o.ExcludePaths = append(o.ExcludePaths, paths...)
|
|
}
|
|
}
|
|
|
|
func MiddlewareAllowSigningMethod(method jwt.SigningMethod) MiddlewareOption {
|
|
return func(o *middlewareOptions) {
|
|
o.SigningMethod = method
|
|
}
|
|
}
|
|
|
|
func MiddlewareErrorWriter(w herodot.Writer) MiddlewareOption {
|
|
return func(o *middlewareOptions) {
|
|
o.ErrorWriter = w
|
|
}
|
|
}
|
|
|
|
func NewMiddleware(
|
|
wellKnownURL string,
|
|
opts ...MiddlewareOption,
|
|
) *Middleware {
|
|
c := &middlewareOptions{
|
|
SigningMethod: jwt.SigningMethodES256,
|
|
ErrorWriter: herodot.NewJSONWriter(nil),
|
|
}
|
|
|
|
for _, o := range opts {
|
|
o(c)
|
|
}
|
|
jc := jwksx.NewFetcher(wellKnownURL)
|
|
return &Middleware{
|
|
o: c,
|
|
wku: wellKnownURL,
|
|
jm: jwtmiddleware.New(
|
|
func(ctx context.Context, rawToken string) (any, error) {
|
|
return jwt.NewParser(
|
|
jwt.WithValidMethods([]string{c.SigningMethod.Alg()}),
|
|
).Parse(rawToken, func(token *jwt.Token) (interface{}, error) {
|
|
if raw, ok := token.Header["kid"]; !ok {
|
|
return nil, errors.New(`jwt from authorization HTTP header is missing value for "kid" in token header`)
|
|
} else if kid, ok := raw.(string); !ok {
|
|
return nil, fmt.Errorf(`jwt from authorization HTTP header is expecting string value for "kid" in tokenWithoutKid header but got: %T`, raw)
|
|
} else if k, err := jc.GetKey(kid); err != nil {
|
|
return nil, err
|
|
} else {
|
|
return k.Key, nil
|
|
}
|
|
})
|
|
},
|
|
jwtmiddleware.WithCredentialsOptional(false),
|
|
jwtmiddleware.WithTokenExtractor(func(r *http.Request) (string, error) {
|
|
// wrapping the extractor to get a herodot.ErrorContainer
|
|
token, err := jwtmiddleware.AuthHeaderTokenExtractor(r)
|
|
if err != nil {
|
|
return "", herodot.ErrUnauthorized().WithReason(err.Error())
|
|
}
|
|
return token, nil
|
|
}),
|
|
jwtmiddleware.WithErrorHandler(func(w http.ResponseWriter, r *http.Request, err error) {
|
|
switch {
|
|
case errors.Is(err, jwtmiddleware.ErrJWTInvalid):
|
|
reason := "The token is invalid or expired."
|
|
if err := errors.Unwrap(err); err != nil {
|
|
reason = err.Error()
|
|
}
|
|
c.ErrorWriter.WriteError(w, r, errors.WithStack(herodot.ErrUnauthorized().WithReason(reason)))
|
|
case errors.Is(err, jwtmiddleware.ErrJWTMissing):
|
|
c.ErrorWriter.WriteError(w, r, errors.WithStack(herodot.ErrUnauthorized().WithReason("The token is missing.")))
|
|
default:
|
|
c.ErrorWriter.WriteError(w, r, err)
|
|
}
|
|
}),
|
|
),
|
|
}
|
|
}
|
|
|
|
// Deprecated: use Middleware as a negroni.Handler directly instead.
|
|
func (h *Middleware) NegroniHandler() negroni.Handler {
|
|
return negroni.HandlerFunc(h.ServeHTTP)
|
|
}
|
|
|
|
func (h *Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
|
for _, excluded := range h.o.ExcludePaths {
|
|
if strings.HasPrefix(r.URL.Path, excluded) {
|
|
next(w, r)
|
|
return
|
|
}
|
|
}
|
|
|
|
h.jm.CheckJWT(next).ServeHTTP(w, r)
|
|
}
|