forked from lestrrat-go/echo-middleware-jwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
103 lines (84 loc) · 3.73 KB
/
interface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package jwx
import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/lestrrat-go/jwx/jwa"
"github.com/lestrrat-go/jwx/jwk"
"github.com/lestrrat-go/jwx/jwt"
)
var ErrJWTInvalid = middleware.ErrJWTInvalid
var ErrJWTMissing = middleware.ErrJWTMissing
type BeforeFunc = middleware.BeforeFunc
type JWTErrorHandler = middleware.JWTErrorHandler
type JWTErrorHandlerWithContext = middleware.JWTErrorHandlerWithContext
type JWTSuccessHandler = middleware.JWTSuccessHandler
type Skipper = middleware.Skipper
type jwtExtractor func(echo.Context) (string, error)
// Config defines the config for JWT middleware (using github.com/lestrrat-go/jwx/jwt).
type Config struct {
// Skipper defines a function to skip middleware.
Skipper Skipper
// BeforeFunc defines a function which is executed just before the middleware.
BeforeFunc BeforeFunc
// SuccessHandler defines a function which is executed for a valid token.
SuccessHandler JWTSuccessHandler
// Context key to store user information from the token into context.
// Optional. Default value "user".
ContextKey string
// ErrorHandler defines a function which is executed for an invalid token.
// It may be used to define a custom JWT error.
ErrorHandler JWTErrorHandler
// ErrorHandlerWithContext is almost identical to ErrorHandler, but it's passed the current context.
ErrorHandlerWithContext JWTErrorHandlerWithContext
// TokenLookup is a string in the form of "<source>:<name>" that is used
// to extract token from the request.
// Optional. Default value "header:Authorization".
// Possible values:
// - "header:<name>"
// - "query:<name>"
// - "param:<name>"
// - "cookie:<name>"
// - "form:<name>"
TokenLookup string
// AuthScheme to be used in the Authorization header.
// Optional. Default value "Bearer".
AuthScheme string
// KeySet defines the JWKS that is used to verify the keys against.
//
// Each key in the JWKS must have a valid "alg" field.
//
// If the JWS message contains a "kid" field, one of the keys in the JWKS must have a matching "kid" (on top of the "alg" field) for the verification to succeed
// If the key needs periodic rotation, use jwk.AutoRefresh
KeySet jwk.Set
// KeyFunc is a user-defined function that supplies the key or key set for
// token verification.
//
// If you simply want to refresh the key(s) to verify the token with, consider using
// `github.com/lestrrat-go/jwx/jwk.AutoRefresh`, and set the key set in the KeySet field.
KeyFunc func(echo.Context) (interface{}, error)
// ValidateOptions defines the set of options to pass to jwt.Validate() in order to validate the JWT.
//
// See github.com/lestrrat-go/jwx/jwt for the various options available.
ValidateOptions []jwt.ValidateOption
// TokenFactory is a function that creates a new instance of a token.
// Use it to tell jwx to use a different underlying token type (such as github.com/lestrrat-go/jwx/jwt/openid)
//
// Optional. Default function always creates a new token using jwt.New
TokenFactory func(echo.Context) jwt.Token
// Signing key to verify the token.
//
// If the key contains the "alg" header, its value is used when verifying the token.
// Otherwise, the value in config.SignatureAlgorithm will be used.
// If neither values are properly initialized, verification of the tokens will always fail.
//
// This is one of the three options to provide a token validation key.
// The order of precedence is a user-defined KeyFunc, KeySet and Key.
// Required if neither user-defined KeyFunc nor Keys is provided.
Key jwk.Key
// Signing algorithm used to verify the signature of the token
// Optional. Default value HS256.
SignatureAlgorithm jwa.SignatureAlgorithm
}
func DefaultSkipper(c echo.Context) bool {
return middleware.DefaultSkipper(c)
}