Documentation ¶
Overview ¶
JWT token generation and verification logic.
Index ¶
- Constants
- func ExpiresAt(t time.Time) option
- func ExpiresIn(d time.Duration) option
- func JWTAuthMiddleware(key HMACBase64Key, contextKey any, cc ...ClaimsAuthorizer) func(http.HandlerFunc) http.HandlerFunc
- func WithAudience(aud string) option
- func WithSubject(sub string) option
- type Claims
- type ClaimsAuthorizer
- type HMACBase64Key
Constants ¶
const (
Issuer = "scrape"
)
Variables ¶
This section is empty.
Functions ¶
func JWTAuthMiddleware ¶
func JWTAuthMiddleware( key HMACBase64Key, contextKey any, cc ...ClaimsAuthorizer, ) func(http.HandlerFunc) http.HandlerFunc
Checks the Authorization header for a JWT token and verifies it using the provided key. The token is always validated against the HMAC key, the issuer, and the Claims.Validate function.
The ClaimsAuthorizer functions, if any, are called in order. If any of them return an error, the request is rejected with a 401 Unauthorized status and the error message is written to the response body.
If the token is valid, the claims are added to the request context at the key value specified by contextKey.
func WithAudience ¶
func WithAudience(aud string) option
func WithSubject ¶
func WithSubject(sub string) option
Types ¶
type Claims ¶
type Claims struct {
jwt.RegisteredClaims
}
func VerifyToken ¶
func VerifyToken(key HMACBase64Key, tokenString string) (*Claims, error)
VerifyToken verifies the token string using the provided key. In the case where the token's signature is invalid, the function will not return any claims.
type ClaimsAuthorizer ¶
type HMACBase64Key ¶
type HMACBase64Key []byte
func MustNewHS256SigningKey ¶
func MustNewHS256SigningKey() HMACBase64Key
func NewHS256SigningKey ¶
func NewHS256SigningKey() (HMACBase64Key, error)
func (HMACBase64Key) MarshalText ¶
func (b HMACBase64Key) MarshalText() ([]byte, error)
func (HMACBase64Key) String ¶ added in v0.8.5
func (b HMACBase64Key) String() string
func (*HMACBase64Key) UnmarshalText ¶
func (b *HMACBase64Key) UnmarshalText(text []byte) error