Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrAuthorizationHeader = "authorization header malformed" ErrAuthorizationHeaderStatus = http.StatusUnauthorized ErrAuthorizationScheme = "authorization scheme not supported" ErrAuthorizationSchemeStatus = http.StatusUnauthorized ErrBodyMissingKey = "body missing refresh token key" ErrBodyMissingKeyStatus = http.StatusUnprocessableEntity ErrMethodNotAllowed = "method not allowed" ErrMethodNotAllowedStatus = http.StatusMethodNotAllowed ErrRequestMalformed = "request malformed" ErrRequestMalformedStatus = http.StatusBadRequest ErrRouteNotFound = "route not found" ErrRouteNotFoundStatus = http.StatusNotFound ErrTokenExpired = "token expired" ErrTokenExpiredStatus = http.StatusUnauthorized ErrTokenInvalid = "token invalid" ErrTokenInvalidStatus = http.StatusUnauthorized ErrTokenInvalidIssuedAt = "token invalid issued at" ErrTokenInvalidIssuedAtStatus = http.StatusUnauthorized ErrTokenNotYetValid = "token not yet valid" ErrTokenNotYetValidStatus = http.StatusUnauthorized )
View Source
var DefaultConfig = Config{ Skipper: middleware.DefaultSkipper, ExemptRoutes: map[string][]string{"/login": {http.MethodPost}}, ExemptMethods: []string{http.MethodOptions}, OptionalRoutes: map[string][]string{}, ParseTokenFunc: parseToken, Options: []jwt.ParseOption{jwt.WithValidate(true)}, ContextKey: "token", CookieKey: "access_token", AuthHeader: "Authorization", AuthScheme: "Bearer", UseRefreshToken: false, RefreshToken: &RefreshToken{ ContextKey: "refresh_token", ContextKeyEncoded: "refresh_token_encoded", CookieKey: "refresh_token", BodyMIMEType: echo.MIMEApplicationJSON, BodyKey: "refresh_token", Routes: map[string][]string{ "/auth/refresh": {http.MethodPost}, "/auth/logout": {http.MethodPost}, }, }, }
Functions ¶
func JWT ¶
func JWT(key any) echo.MiddlewareFunc
func JWTWithConfig ¶
func JWTWithConfig(config Config) echo.MiddlewareFunc
Types ¶
type Config ¶
type Config struct {
// Skipper defines a function to skip middleware.
Skipper middleware.Skipper
// Key defines the RSA key used to verify tokens.
// Required.
Key any
// ExemptRoutes defines routes and methods that don't require tokens.
// Optional. Defaults to /login [POST].
ExemptRoutes map[string][]string
// ExemptMethods defines methods that don't require tokens.
// Optional. Defaults to [OPTIONS].
ExemptMethods []string
// OptionalRoutes defines routes and methods that
// can optionally require a token.
// Optional.
OptionalRoutes map[string][]string
// ParseTokenFunc defines a function used to decode tokens.
// Optional.
ParseTokenFunc func(string, []jwt.ParseOption) (jwt.Token, error)
// AfterParseFunc defines a function that will run after
// the ParseTokenFunc has successfully run.
// Optional.
AfterParseFunc func(echo.Context, jwt.Token, string, TokenSource) *echo.HTTPError
// Options defines jwt.ParseOption options for parsing tokens.
// Optional. Defaults [jwt.WithValidate(true)].
Options []jwt.ParseOption
// ContextKey defines the key that will be used to store the token
// on the echo.Context when the token is successfully parsed.
// Optional. Defaults to "token".
ContextKey string
// CookieKey defines the key that will be used to read the token
// from an HTTP cookie.
// Optional. Defaults to "access_token".
CookieKey string
// AuthHeader defines the HTTP header that will be used to
// read the token from.
// Optional. Defaults to "Authorization".
AuthHeader string
// AuthScheme defines the authorization scheme in the AuthHeader.
// Optional. Defaults to "Bearer".
AuthScheme string
// UseRefreshToken controls whether refresh tokens are used or not.
// Optional. Defaults to false.
UseRefreshToken bool
// RefreshToken holds the configuration related to refresh tokens.
// Optional.
RefreshToken *RefreshToken
}
type RefreshToken ¶ added in v0.4.0
type RefreshToken struct {
// ContextKey defines the key that will be used to store the refresh token
// on the echo.Context when the token is successfully parsed.
// Optional. Defaults to "refresh_token".
ContextKey string
// ContextKeyEncoded defines the key that will be used to store the encoded
// refresh token on the echo.Context when the token is successfully parsed.
// Optional. Defaults to "refresh_token_encoded".
ContextKeyEncoded string
// CookieKey defines the key that will be used to read the refresh token
// from an HTTP cookie.
// Optional. Defaults to "refresh_token".
CookieKey string
// BodyMIMEType defines the expected MIME type of the request body.
// Returns a 400 Bad Request if the request's Content-Type header does not match.
// Optional. Defaults to "application/json".
BodyMIMEType string
// BodyKey defines the key that will be used to read the refresh token
// from the request's body.
// Returns a 422 UnprocessableEntity if the request's body key is missing.
// Optional. Defaults to "refresh_token".
BodyKey string
// Routes defines routes and methods that require a refresh token.
// Optional. Defaults to /auth/refresh [POST] and /auth/logout [POST].
Routes map[string][]string
}
type TokenSource ¶ added in v0.6.0
type TokenSource int
const ( Unset TokenSource = iota Cookie Header )
func (TokenSource) String ¶ added in v0.6.0
func (s TokenSource) String() string
Click to show internal directories.
Click to hide internal directories.