Documentation
¶
Overview ¶
Package jwt is a JSON Web Token signer, verifier and validator.
Index ¶
- Variables
- func Ed25519PrivateKey(priv ed25519.PrivateKey) func(*Ed25519)
- func Ed25519PublicKey(pub ed25519.PublicKey) func(*Ed25519)
- func Sign[T WithRegisteredClaims](claims T, alg Algorithm, opts *SignOptions) ([]byte, error)
- type Algorithm
- type Audience
- type Ed25519
- type HMACSHA
- type Header
- type JWT
- type None
- type RegisteredClaims
- type SignOptions
- type Time
- type Validator
- func (Validator) Audience(aud Audience) ValidatorFunc
- func (Validator) ExpirationTime(now time.Time) ValidatorFunc
- func (Validator) ID(jti string) ValidatorFunc
- func (Validator) IssuedAt(now time.Time) ValidatorFunc
- func (Validator) Issuer(iss string) ValidatorFunc
- func (Validator) NotBefore(now time.Time) ValidatorFunc
- func (Validator) Subject(sub string) ValidatorFunc
- type ValidatorFunc
- type VerifyOptions
- type WithRegisteredClaims
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEd25519NilPrivKey is the error for trying to sign a JWT with a nil private key. ErrEd25519NilPrivKey = errors.New("jwt: Ed25519 private key is nil") // ErrEd25519NilPubKey is the error for trying to verify a JWT with a nil public key. ErrEd25519NilPubKey = errors.New("jwt: Ed25519 public key is nil") // ErrEd25519Verification is the error for when verification with Ed25519 fails. ErrEd25519Verification = errors.New("jwt: Ed25519 verification failed") )
var ( // ErrHMACMissingKey is the error for trying to sign or verify a JWT with an empty key. ErrHMACMissingKey = errors.New("jwt: HMAC key is empty") // ErrHMACVerification is the error for an invalid signature. ErrHMACVerification = errors.New("jwt: HMAC verification failed") )
var ( ErrMalformed = errors.New("jwt: token is malformed") ErrAlgValidation = errors.New(`jwt: "alg" header doesn't match chosen algorithm`) )
var ( // ErrAudValidation is the error for an invalid "aud" claim. ErrAudValidation = errors.New("jwt: aud claim is invalid") // ErrExpValidation is the error for an invalid "exp" claim. ErrExpValidation = errors.New("jwt: exp claim is invalid") // ErrIatValidation is the error for an invalid "iat" claim. ErrIatValidation = errors.New("jwt: iat claim is invalid") // ErrIssValidation is the error for an invalid "iss" claim. ErrIssValidation = errors.New("jwt: iss claim is invalid") // ErrJtiValidation is the error for an invalid "jti" claim. ErrJtiValidation = errors.New("jwt: jti claim is invalid") // ErrNbfValidation is the error for an invalid "nbf" claim. ErrNbfValidation = errors.New("jwt: nbf claim is invalid") // ErrSubValidation is the error for an invalid "sub" claim. ErrSubValidation = errors.New("jwt: sub claim is invalid") )
Functions ¶
func Ed25519PrivateKey ¶
func Ed25519PrivateKey(priv ed25519.PrivateKey) func(*Ed25519)
Ed25519PrivateKey is an option to set a private key to the Ed25519 algorithm.
func Ed25519PublicKey ¶
Ed25519PublicKey is an option to set a public key to the Ed25519 algorithm.
func Sign ¶
func Sign[T WithRegisteredClaims](claims T, alg Algorithm, opts *SignOptions) ([]byte, error)
Types ¶
type Algorithm ¶
type Algorithm interface { Name() string Sign(headerPayload []byte) ([]byte, error) Size() int Verify(headerPayload, sig []byte) error }
Algorithm is an algorithm for both signing and verifying a JWT.
type Audience ¶
type Audience []string
Audience is a special claim that may either be a single string or an array of strings, as per the RFC 7519.
func (Audience) MarshalJSON ¶
MarshalJSON implements a marshaling function for "aud" claim.
func (*Audience) UnmarshalJSON ¶
UnmarshalJSON implements an unmarshaling function for "aud" claim.
type Ed25519 ¶
type Ed25519 struct {
// contains filtered or unexported fields
}
Ed25519 is an algorithm that uses EdDSA to sign SHA-512 hashes.
func NewEd25519 ¶
NewEd25519 creates a new algorithm using EdDSA and SHA-512.
type HMACSHA ¶
type HMACSHA struct {
// contains filtered or unexported fields
}
HMACSHA is an algorithm that uses HMAC to sign SHA hashes.
type Header ¶
type Header struct { Algorithm string `json:"alg,omitempty"` ContentType string `json:"cty,omitempty"` KeyID string `json:"kid,omitempty"` Type string `json:"typ,omitempty"` }
Header is a JOSE header narrowed down to the JWT specification from RFC 7519.
Parameters are ordered according to the RFC 7515.
type JWT ¶
type JWT[T WithRegisteredClaims] struct { Header Header Claims T }
func Verify ¶
func Verify[T WithRegisteredClaims](token []byte, alg Algorithm, opts *VerifyOptions[T]) (*JWT[T], error)
type None ¶
type None struct{}
None is an unsecured algorithm.
type RegisteredClaims ¶
type RegisteredClaims struct { Issuer string `json:"iss,omitempty"` Subject string `json:"sub,omitempty"` Audience Audience `json:"aud,omitempty"` Expiration *Time `json:"exp,omitempty"` NotBefore *Time `json:"nbf,omitempty"` IssuedAt *Time `json:"iat,omitempty"` JWTID string `json:"jti,omitempty"` }
RegisteredClaims represents the set of registered claims according to the RFC 7519.
func (RegisteredClaims) RegisteredClaimsSet ¶
func (c RegisteredClaims) RegisteredClaimsSet() RegisteredClaims
type SignOptions ¶
type Time ¶
Time is the allowed format for time, as per the RFC 7519.
func (Time) MarshalJSON ¶
MarshalJSON implements a marshaling function for time-related claims.
func (*Time) UnmarshalJSON ¶
UnmarshalJSON implements an unmarshaling function for time-related claims.
type Validator ¶
type Validator struct{}
func (Validator) Audience ¶
func (Validator) Audience(aud Audience) ValidatorFunc
Audience validates the "aud" claim. It checks if at least one of the audiences in the JWT's payload is listed in aud.
func (Validator) ExpirationTime ¶
func (Validator) ExpirationTime(now time.Time) ValidatorFunc
ExpirationTime validates the "exp" claim.
func (Validator) IssuedAt ¶
func (Validator) IssuedAt(now time.Time) ValidatorFunc
IssuedAt validates the "iat" claim.
func (Validator) Issuer ¶
func (Validator) Issuer(iss string) ValidatorFunc
Issuer validates the "iss" claim.
func (Validator) NotBefore ¶
func (Validator) NotBefore(now time.Time) ValidatorFunc
NotBefore validates the "nbf" claim.
func (Validator) Subject ¶
func (Validator) Subject(sub string) ValidatorFunc
Subject validates the "sub" claim.
type ValidatorFunc ¶
type ValidatorFunc func(RegisteredClaims) error
ValidatorFunc is a function that validates a Payload pointer.
type VerifyOptions ¶
type VerifyOptions[T WithRegisteredClaims] struct { CheckAlgorithm bool ReplaceAlgorithm func(*JWT[T]) Algorithm Validators []ValidatorFunc }
type WithRegisteredClaims ¶
type WithRegisteredClaims interface {
RegisteredClaimsSet() RegisteredClaims
}