Documentation
¶
Overview ¶
Binary WEB Token
Index ¶
- Constants
- Variables
- func Decode(s string) ([]byte, error)
- func Encode(b []byte) string
- func KeyAs[T any](key any) (T, error)
- func KeyID(alg string, publicKeyMaterial []byte) string
- func ListAlgorithms() []string
- func MapValue[V any](m map[string]any, key string) (empty V)
- func ParseUnverified(tokenString string, claims Claims) (token *Token, raw Raw, err error)
- func RegisterAlgorithm(alg Algorithm)
- type Algorithm
- type Claims
- type ClaimsMap
- func (c ClaimsMap) GetAudience() []string
- func (c ClaimsMap) GetExpirationTime() time.Time
- func (c ClaimsMap) GetIssuedAt() time.Time
- func (c ClaimsMap) GetIssuer() string
- func (c ClaimsMap) GetKeyID() string
- func (c ClaimsMap) GetNotBefore() time.Time
- func (c ClaimsMap) GetSubject() string
- func (c ClaimsMap) Str(key string) string
- func (c ClaimsMap) Strs(key string) []string
- func (c ClaimsMap) Time(key string) time.Time
- type ClaimsValidator
- type Key
- type Keyfunc
- type Parser
- type PrivateKey
- type Raw
- type RegisteredClaims
- func (c RegisteredClaims) GetAudience() []string
- func (c RegisteredClaims) GetExpirationTime() time.Time
- func (c RegisteredClaims) GetIssuedAt() time.Time
- func (c RegisteredClaims) GetIssuer() string
- func (c RegisteredClaims) GetKeyID() string
- func (c RegisteredClaims) GetNotBefore() time.Time
- func (c RegisteredClaims) GetSubject() string
- type Token
- type Validator
- type ValidatorFunc
- type ValidatorOption
- func WithLeeway(leeway time.Duration) ValidatorOption
- func WithRequireAudience(required bool, audience string) ValidatorOption
- func WithTimeFunc(f func() time.Time) ValidatorOption
- func WithValidator(f ValidatorFunc) ValidatorOption
- func WithVerifyExpiration(required bool) ValidatorOption
- func WithVerifyIssuedAt(required bool) ValidatorOption
- func WithVerifyIssuer(required bool, issuer string) ValidatorOption
- func WithVerifyNotBefore(required bool) ValidatorOption
- func WithVerifySubject(required bool, subject string) ValidatorOption
Constants ¶
const ( ClaimsKeyKeyID = "kid" ClaimsKeyExpirationTime = "exp" ClaimsKeyIssuedAt = "iat" ClaimsKeyNotBefore = "nbf" ClaimsKeyIssuer = "iss" ClaimsKeySubject = "sub" ClaimsKeyAudience = "aud" )
well-known claim keys
const Type = "BWT"
Type is "BWT"
Variables ¶
var ( ErrInvalidKeyType = errors.New("key is of invalid type") ErrInvalidKey = errors.New("invalid key") ErrTagInvalid = errors.New("auth tag is invalid") ErrWrongTag = errors.New("auth tag is wrong") )
var ( HS256 *hmacAlgo HS384 *hmacAlgo HS512 *hmacAlgo )
SHA3 based
var ( ErrTokenMalformed = errors.New("token is malformed") ErrTokenUnverifiable = errors.New("token is unverifiable") ErrTokenTagInvalid = errors.New("token tag is invalid") ErrTokenInvalidClaims = errors.New("token has invalid claims") )
var ( ErrTokenRequiredClaimMissing = errors.New("token is missing required claim") ErrTokenInvalidAudience = errors.New("token has invalid audience") ErrTokenExpired = errors.New("token is expired") ErrTokenUsedBeforeIssued = errors.New("token used before issued") ErrTokenInvalidIssuer = errors.New("token has invalid issuer") ErrTokenInvalidSubject = errors.New("token has invalid subject") ErrTokenNotValidYet = errors.New("token is not valid yet") )
var EdDSA *algoEd25519
EdDSA algorithm. Expects ed25519.PrivateKey for authentication and ed25519.PublicKey for verification.
HashUnavailable returns true if the requested hash function is unavailable.
Functions ¶
func KeyID ¶
KeyID computes the KeyID for the given algorithm and public key material and returns the base64 encoded string.
func ListAlgorithms ¶
func ListAlgorithms() []string
ListAlgorithms returns a list of registered algorithm names.
func ParseUnverified ¶
ParseUnverified parses the token but doesn't verify the tag.
func RegisterAlgorithm ¶
func RegisterAlgorithm(alg Algorithm)
Types ¶
type Algorithm ¶
type Algorithm interface { Name() string Auth(prefix string, body []byte, key PrivateKey) ([]byte, error) Verify(prefix string, body []byte, key Key, tag []byte) error }
Algorithm represents a signing/authentication algorithm.
func GetAlgorithm ¶
GetAlgorithm retrieves an auth algorithm from an "alg" string.
type Claims ¶
type Claims interface { GetKeyID() string GetExpirationTime() time.Time GetIssuedAt() time.Time GetNotBefore() time.Time GetIssuer() string GetSubject() string GetAudience() []string }
Claims represents the token claims. Must be msgpack de/encodable and be pointer.
type ClaimsMap ¶
ClaimsMap is a map of claims.
func ClaimsAsMap ¶
ClaimsAsMap returns the Claims as a ClaimsMap dereferencing the pointer if needed.
func (ClaimsMap) GetAudience ¶
func (ClaimsMap) GetExpirationTime ¶
func (ClaimsMap) GetIssuedAt ¶
func (ClaimsMap) GetNotBefore ¶
func (ClaimsMap) GetSubject ¶
type ClaimsValidator ¶
ClaimsValidator is an interface that can be implemented by custom claims to perform custom validation.
type Key ¶
Key represents a public or secret key for verifying a token's signature or authentication.
type Keyfunc ¶
Keyfunc is a callback function to supply the key for verification. The function receives the parsed, but unverified Token.
func KeyfuncFrom ¶
KeyfuncFrom returns a Keyfunc that always returns the same key.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is used to parse, validate, and verify BWTs.
type PrivateKey ¶
type PrivateKey interface { crypto.PrivateKey | []byte }
PrivateKey represents a private or secret key for signing or authenticating a token.
type RegisteredClaims ¶
type RegisteredClaims struct { KeyID string `msgpack:"kid,omitempty"` ExpirationTime time.Time `msgpack:"exp,omitempty"` IssuedAt time.Time `msgpack:"iat,omitempty"` NotBefore time.Time `msgpack:"nbf,omitempty"` Issuer string `msgpack:"iss,omitempty"` Subject string `msgpack:"sub,omitempty"` Audience []string `msgpack:"aud,omitempty"` }
RegisteredClaims contains well-known claims.
func (RegisteredClaims) GetAudience ¶
func (c RegisteredClaims) GetAudience() []string
func (RegisteredClaims) GetExpirationTime ¶
func (c RegisteredClaims) GetExpirationTime() time.Time
func (RegisteredClaims) GetIssuedAt ¶
func (c RegisteredClaims) GetIssuedAt() time.Time
func (RegisteredClaims) GetIssuer ¶
func (c RegisteredClaims) GetIssuer() string
func (RegisteredClaims) GetKeyID ¶
func (c RegisteredClaims) GetKeyID() string
func (RegisteredClaims) GetNotBefore ¶
func (c RegisteredClaims) GetNotBefore() time.Time
func (RegisteredClaims) GetSubject ¶
func (c RegisteredClaims) GetSubject() string
type Token ¶
Token represents a BWT token.
func ParseWithClaims ¶
func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc, v ...*Validator) (*Token, error)
ParseWithClaims is a shortcut for NewParser().ParseWithClaims().
func (*Token) Authenticate ¶
func (t *Token) Authenticate(key PrivateKey) (string, error)
Authenticate creates an authentication tag and returns the encoded token.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator is a validator that can be used to validate already parsed claims.
func NewValidator ¶
func NewValidator(opts ...ValidatorOption) *Validator
NewValidator can be used to create a validator with the supplied options. This validator can then be used to validate already parsed claims.
type ValidatorFunc ¶
ValidatorFunc is a function to validate claims.
type ValidatorOption ¶
type ValidatorOption func(*Validator)
ValidatorOption is a functional option that can be used to configure the Validator.
func WithLeeway ¶
func WithLeeway(leeway time.Duration) ValidatorOption
WithLeeway returns the ValidatorOption for specifying the leeway window.
func WithRequireAudience ¶
func WithRequireAudience(required bool, audience string) ValidatorOption
WithRequireAudience returns the ValidatorOption for specifying the verification of the audience.
func WithTimeFunc ¶
func WithTimeFunc(f func() time.Time) ValidatorOption
WithTimeFunc returns the ValidatorOption for specifying the time func.
func WithValidator ¶
func WithValidator(f ValidatorFunc) ValidatorOption
WithValidator returns the ValidatorOption to add the validator func.
func WithVerifyExpiration ¶
func WithVerifyExpiration(required bool) ValidatorOption
WithVerifyExpiration returns the ValidatorOption for specifying the verification of the expiration time.
func WithVerifyIssuedAt ¶
func WithVerifyIssuedAt(required bool) ValidatorOption
WithVerifyIssuedAt returns the ValidatorOption for specifying the verification of the issued at time.
func WithVerifyIssuer ¶
func WithVerifyIssuer(required bool, issuer string) ValidatorOption
WithVerifyIssuer returns the ValidatorOption for specifying the verification of the issuer.
func WithVerifyNotBefore ¶
func WithVerifyNotBefore(required bool) ValidatorOption
WithVerifyNotBefore returns the ValidatorOption for specifying the verification of the not before time.
func WithVerifySubject ¶
func WithVerifySubject(required bool, subject string) ValidatorOption
WithVerifySubject returns the ValidatorOption for specifying the verification of the subject.