Documentation
¶
Index ¶
- Constants
- Variables
- func Sign(claims, headerClaims Claims, signer Signer) ([]byte, error)
- func SignHS256String(claims, headerClaims Claims, key []byte) string
- func SignRS25String(claims, headerClaims Claims, key *rsa.PrivateKey) string
- func SignString(claims, headerClaims Claims, signer Signer) (string, error)
- type Algorithm
- type Claims
- func DecodeHS256String(rawToken string, tolerance time.Duration, key []byte) (Claims, error)
- func DecodeHS256StringAt(rawToken string, key []byte, tolerance time.Duration, now time.Time) (Claims, error)
- func DecodeRS256String(rawToken string, tolerance time.Duration, pubKey *rsa.PublicKey) (Claims, error)
- func DecodeRS256StringAt(rawToken string, pubKey *rsa.PublicKey, tolerance time.Duration, now time.Time) (Claims, error)
- func DecodeStringAt(rawToken string, verifier Verifier, tolerance time.Duration, now time.Time) (Claims, error)
- func New(subject string, validity time.Duration) Claims
- func NewAt(subject string, validity time.Duration, now time.Time) Claims
- func (c Claims) ExpiresAt() time.Time
- func (c Claims) Int64(key string) (int64, bool)
- func (c Claims) Issuer() string
- func (c Claims) KeyID() string
- func (c Claims) String(key string) string
- func (c Claims) Subject() string
- func (c Claims) Time(key string) time.Time
- func (c Claims) TokenID() string
- func (c Claims) ValidateTime(tolerance time.Duration) error
- func (c Claims) ValidateTimeAt(tolerance time.Duration, now time.Time) error
- type HS256Signer
- type HS256Verifier
- type RS256Signer
- type RS256Verifier
- type Signer
- type Token
- func (t *Token) Alg() Algorithm
- func (t *Token) Claims() Claims
- func (t *Token) KeyID() string
- func (token *Token) Parse(rawToken []byte) error
- func (token *Token) ParseString(rawToken string) error
- func (token *Token) ValidateHS256(key []byte) error
- func (token *Token) ValidateRS256(key *rsa.PublicKey) error
- func (t *Token) ValidateWith(verifier Verifier) error
- type Verifier
Constants ¶
const ( TokenID = "jti" // TokenID is a unique identifier for this token. Issuer = "iss" // Issuer is the principal that issued the token Audience = "aud" // Audience identifies the recipients the token is intended for Subject = "sub" // Subject is the user/account/etc. that this token authorizes IssuedAt = "iat" // IssuedAt is a Unix timestamp for when the token was issued ExpiresAt = "exp" // ExpiresAt is a Unix timestamp for when the token should expire NotBeforeAt = "nbf" // NotBeforeAt is a timestamp this token should not be accepted until Alg = "alg" // Alg is a header field identifying the signing algorithm Typ = "typ" // Typ is a header field that must be set to "JWT" KeyID = "kid" // KeyID is a header field, an opaque string identifying the key used Forever time.Duration = 1<<63 - 1 // Forever is a token validity that never expires HS256 Algorithm = "HS256" RS256 Algorithm = "RS256" MinHS256KeyLen = 32 MaxHS256KeyLen = 64 // anything longer is hashed to 32 bytes )
Variables ¶
var ( ErrCorrupted = errors.New("token corrupted") ErrAlg = errors.New("token uses a wrong algorithm") ErrExpired = errors.New("token expired") ErrNotYetValid = errors.New("token not valid yet") ErrTooLong = errors.New("token too long") ErrSignature = errors.New("token signature invalid") ErrSignatureCorrupted = errors.New("token signature corrupted") MaxTokenLen = 8000 // MaxTokenLen is the safety limit to avoid decoding very long data ExpectedClaimCount = 10 // ExpectedClaimCount is a starting size for the claims map )
Functions ¶
func SignHS256String ¶
func SignRS25String ¶ added in v0.3.27
func SignRS25String(claims, headerClaims Claims, key *rsa.PrivateKey) string
Types ¶
type Claims ¶
func DecodeHS256String ¶
func DecodeHS256StringAt ¶
func DecodeRS256String ¶ added in v0.3.27
func DecodeRS256StringAt ¶ added in v0.3.27
func DecodeStringAt ¶ added in v0.3.27
type HS256Signer ¶ added in v0.3.27
type HS256Signer struct {
// contains filtered or unexported fields
}
HS256Signer signs using HMAC-SHA256 with a symmetric key.
func NewHS256Signer ¶ added in v0.3.27
func NewHS256Signer(key []byte, keyID string) *HS256Signer
NewHS256Signer returns an HS256 signer using the given key and optional keyID.
func (*HS256Signer) Algorithm ¶ added in v0.3.27
func (s *HS256Signer) Algorithm() Algorithm
func (*HS256Signer) KeyID ¶ added in v0.3.27
func (s *HS256Signer) KeyID() string
type HS256Verifier ¶ added in v0.3.27
type HS256Verifier struct {
// contains filtered or unexported fields
}
func NewHS256Verifier ¶ added in v0.3.27
func NewHS256Verifier(key []byte, keyID string) *HS256Verifier
NewHS256Verifier constructs an HS256 verifier with the given secret `key` and optional `keyID`.
func (*HS256Verifier) Algorithm ¶ added in v0.3.27
func (v *HS256Verifier) Algorithm() Algorithm
func (*HS256Verifier) KeyID ¶ added in v0.3.27
func (v *HS256Verifier) KeyID() string
func (*HS256Verifier) SigLen ¶ added in v0.3.27
func (v *HS256Verifier) SigLen() int
func (*HS256Verifier) Verify ¶ added in v0.3.27
func (v *HS256Verifier) Verify(data, signature []byte) error
Verify checks that `signature` (raw bytes) matches the HMAC-SHA256 of `data`.
type RS256Signer ¶ added in v0.3.27
type RS256Signer struct {
// contains filtered or unexported fields
}
func NewRS256Signer ¶ added in v0.3.27
func NewRS256Signer(privKey *rsa.PrivateKey, keyID string) *RS256Signer
NewRS256Signer returns an RS256 signer using the given RSA private key.
func (*RS256Signer) Algorithm ¶ added in v0.3.27
func (s *RS256Signer) Algorithm() Algorithm
func (*RS256Signer) KeyID ¶ added in v0.3.27
func (s *RS256Signer) KeyID() string
type RS256Verifier ¶ added in v0.3.27
type RS256Verifier struct {
// contains filtered or unexported fields
}
func NewRS256Verifier ¶ added in v0.3.27
func NewRS256Verifier(pubKey *rsa.PublicKey, keyID string) *RS256Verifier
NewRS256Verifier constructs an RS256 verifier with the given `pubKey` and optional `keyID`.
func (*RS256Verifier) Algorithm ¶ added in v0.3.27
func (v *RS256Verifier) Algorithm() Algorithm
func (*RS256Verifier) KeyID ¶ added in v0.3.27
func (v *RS256Verifier) KeyID() string
func (*RS256Verifier) SigLen ¶ added in v0.3.27
func (v *RS256Verifier) SigLen() int
func (*RS256Verifier) Verify ¶ added in v0.3.27
func (v *RS256Verifier) Verify(data, signature []byte) error
Verify checks that `signature` (raw bytes) matches the RSA-SHA256 of `data`.
type Token ¶
type Token struct {
// contains filtered or unexported fields
}
Token is the result of parsing a JWT token.
func ParseString ¶
ParseString decodes JWT parts of a token.
func (*Token) ParseString ¶
ParseString decodes JWT parts of a token.
func (*Token) ValidateHS256 ¶
func (*Token) ValidateRS256 ¶ added in v0.3.27
func (*Token) ValidateWith ¶ added in v0.3.27
ValidateWith verifies that this token’s algorithm matches the verifier, then checks the signature with verifier.Verify().