Documentation ¶
Index ¶
- Constants
- Variables
- func Authenticator(next phi.HandlerFunc) phi.HandlerFunc
- func EpochNow() int64
- func ExpireIn(tm time.Duration) int64
- func FromContext(ctx *fasthttp.RequestCtx) (*jwt.Token, jwt.MapClaims, error)
- func SetExpiry(claims jwt.MapClaims, tm time.Time)
- func SetExpiryIn(claims jwt.MapClaims, tm time.Duration)
- func SetIssuedAt(claims jwt.MapClaims, tm time.Time)
- func SetIssuedNow(claims jwt.MapClaims)
- func TokenFromCookie(r *fasthttp.RequestCtx) string
- func TokenFromHeader(r *fasthttp.RequestCtx) string
- func TokenFromQuery(r *fasthttp.RequestCtx) string
- func UnixTime(tm time.Time) int64
- func Verifier(ja *JWTAuth) phi.Middleware
- func Verify(ja *JWTAuth, findTokenFns ...func(r *fasthttp.RequestCtx) string) func(phi.Handler) phi.HandlerFunc
- func VerifyRequest(ja *JWTAuth, r *fasthttp.RequestCtx, ...) (*jwt.Token, error)
- type JWTAuth
Constants ¶
const ( TokenCtxKey = "Token" ErrorCtxKey = "Error" )
Context keys
Variables ¶
var ( ErrExpired = errors.New("jwtauth: token is expired") ErrNBFInvalid = errors.New("jwtauth: token nbf validation failed") ErrIATInvalid = errors.New("jwtauth: token iat validation failed") ErrNoTokenFound = errors.New("jwtauth: no token found") ErrAlgoInvalid = errors.New("jwtauth: algorithm mismatch") )
Library errors
Functions ¶
func Authenticator ¶
func Authenticator(next phi.HandlerFunc) phi.HandlerFunc
Authenticator is a default authentication middleware to enforce access from the Verifier middleware request context values. The Authenticator sends a 401 Unauthorized response for any unverified tokens and passes the good ones through. It's just fine until you decide to write something similar and customize your client response.
func EpochNow ¶
func EpochNow() int64
EpochNow is a helper function that returns the NumericDate time value used by the spec
func ExpireIn ¶
ExpireIn is a helper function to return calculated time in the future for "exp" claim
func FromContext ¶
func SetExpiryIn ¶
Set expiry ("exp") in the claims to some duration from the present time
func SetIssuedAt ¶
Set issued at ("iat") to specified time in the claims
func SetIssuedNow ¶
Set issued at ("iat") to present time in the claims
func TokenFromCookie ¶
func TokenFromCookie(r *fasthttp.RequestCtx) string
TokenFromCookie tries to retreive the token string from a cookie named "jwt".
func TokenFromHeader ¶
func TokenFromHeader(r *fasthttp.RequestCtx) string
TokenFromHeader tries to retreive the token string from the "Authorization" reqeust header: "Authorization: BEARER T".
func TokenFromQuery ¶
func TokenFromQuery(r *fasthttp.RequestCtx) string
TokenFromQuery tries to retreive the token string from the "jwt" URI query parameter.
func Verifier ¶
func Verifier(ja *JWTAuth) phi.Middleware
Verifier http middleware handler will verify a JWT string from a http request.
Verifier will search for a JWT token in a http request, in the order:
- 'jwt' URI query parameter
- 'Authorization: BEARER T' request header
- Cookie 'jwt' value
The first JWT string that is found as a query parameter, authorization header or cookie header is then decoded by the `jwt-go` library and a *jwt.Token object is set on the request context. In the case of a signature decoding error the Verifier will also set the error on the request context.
The Verifier always calls the next http handler in sequence, which can either be the generic `jwtauth.Authenticator` middleware or your own custom handler which checks the request context jwt token and error to prepare a custom http response.
func Verify ¶
func Verify(ja *JWTAuth, findTokenFns ...func(r *fasthttp.RequestCtx) string) func(phi.Handler) phi.HandlerFunc
func VerifyRequest ¶
func VerifyRequest(ja *JWTAuth, r *fasthttp.RequestCtx, findTokenFns ...func(r *fasthttp.RequestCtx) string) (*jwt.Token, error)
Types ¶
type JWTAuth ¶
type JWTAuth struct {
// contains filtered or unexported fields
}
func New ¶
New creates a JWTAuth authenticator instance that provides middleware handlers and encoding/decoding functions for JWT signing.