コンテンツにスキップ

JWT

JWT ミドルウェアは JSON Web Token(JWT)認証を提供します。これは別 module にあります: github.com/labstack/echo-jwt。

挙動:

  • 有効な token の場合、コンテキストにユーザーを設定して次のハンドラを呼び出します。
  • 無効な token の場合、401 Unauthorized レスポンスを送信します。
  • Authorization header がない、または無効な場合、400 Bad Request レスポンスを送信します。
import "github.com/labstack/echo-jwt/v5"
e.Use(echojwt.JWT([]byte("secret")))
e.Use(echojwt.WithConfig(echojwt.Config{
SigningKey: []byte("secret"),
}))

Config · github.com/labstack/echo-jwt/[email protected]

フィールドの説明は英語のソースコードのコメントから生成されています。

パッケージのソースコードのフィールド
フィールド型説明ソース
Skipper middleware.Skipper Skipper defines a function to skip middleware. L19
BeforeFunc middleware.BeforeFunc BeforeFunc defines a function which is executed just before the middleware. L22
SuccessHandler func(c *echo.Context) error SuccessHandler defines a function which is executed for a valid token. In case SuccessHandler error the middleware stops handler chain execution and returns error. L27
ErrorHandler func(c *echo.Context, err error) error ErrorHandler defines a function which is executed when all lookups have been done and none of them passed Validator function. ErrorHandler is executed with last missing (ErrExtractionValueMissing) or an invalid key. It may be used to define a custom JWT error. Note: when error handler swallows the error (returns nil) middleware continues handler chain execution towards handler. This is useful in cases when portion of your site/api is publicly accessible and has extra features for authorized users In that case you can use ErrorHandler to set default public JWT token value to request and continue with handler chain. L36
ContinueOnIgnoredError bool ContinueOnIgnoredError allows the next middleware/handler to be called when ErrorHandler decides to ignore the error (by returning `nil`). This is useful when parts of your site/api allow public access and some authorized routes provide extra functionality. In that case you can use ErrorHandler to set a default public JWT token value in the request context and continue. Some logic down the remaining execution chain needs to check that (public) token value then. L43
ContextKey string Context key to store user information from the token into context. Optional. Default value "user". L47
SigningKey interface{} Signing key to validate token. This is one of the three options to provide a token validation key. The order of precedence is a user-defined KeyFunc, SigningKeys and SigningKey. Required if neither user-defined KeyFunc nor SigningKeys is provided. L53
SigningKeys map[string]interface{} Map of signing keys to validate token with kid field usage. This is one of the three options to provide a token validation key. The order of precedence is a user-defined KeyFunc, SigningKeys and SigningKey. Required if neither user-defined KeyFunc nor SigningKey is provided. L59
SigningMethod string Signing method used to check the token's signing algorithm. SigningMethod is not checked when a user-defined KeyFunc is provided. Optional. Default value HS256. L64
KeyFunc jwt.Keyfunc KeyFunc defines a user-defined function that supplies the public key for a token validation. The function shall take care of verifying the signing algorithm and selecting the proper key. A user-defined KeyFunc can be useful if tokens are issued by an external party. Used by default ParseTokenFunc implementation. When a user-defined KeyFunc is provided, SigningKey, SigningKeys, and SigningMethod are ignored. This is one of the three options to provide a token validation key. The order of precedence is a user-defined KeyFunc, SigningKeys and SigningKey. Required if neither SigningKeys nor SigningKey is provided. Not used if custom ParseTokenFunc is set. Default to an internal implementation verifying the signing algorithm and selecting the proper key. L77
TokenLookup string TokenLookup is a string in the form of "<source>:<name>" or "<source>:<name>,<source>:<name>" that is used to extract token from the request. Optional. Default value "header:Authorization". Possible values: - "header:<name>" or "header:<name>:<cut-prefix>" `<cut-prefix>` is argument value to cut/trim prefix of the extracted value. This is useful if header value has static prefix like `Authorization: <auth-scheme> <authorisation-parameters>` where part that we want to cut is `<auth-scheme> ` note the space at the end. In case of JWT tokens `Authorization: Bearer <token>` prefix we cut is `Bearer `. If prefix is left empty the whole value is returned. - "query:<name>" - "param:<name>" - "cookie:<name>" - "form:<name>" Multiple sources example: - "header:Authorization:Bearer ,cookie:myowncookie" L95
TokenLookupFuncs []middleware.ValuesExtractor TokenLookupFuncs defines a list of user-defined functions that extract JWT token from the given context. This is one of the two options to provide a token extractor. The order of precedence is user-defined TokenLookupFuncs, and TokenLookup. You can also provide both if you want. L101
ParseTokenFunc func(c *echo.Context, auth string) (interface{}, error) ParseTokenFunc defines a user-defined function that parses token from given auth. Returns an error when token parsing fails or parsed token is invalid. Defaults to implementation using `github.com/golang-jwt/jwt` as JWT implementation library L106
NewClaimsFunc func(c *echo.Context) jwt.Claims Claims are extendable claims data defining token content. Used by default ParseTokenFunc implementation. Not used if custom ParseTokenFunc is set. Optional. Defaults to function returning jwt.MapClaims L111
パッケージのソースコードの関数
型ソース
func JWT(signingKey interface{}) echo.MiddlewareFunc L168
func WithConfig(config Config) echo.MiddlewareFunc L178

完全な例は JWT cookbook を参照してください。