Documentation ¶
Overview ¶
Package jwt provides a easy to use JSON Web Token and blacklisting library
Index ¶
- Constants
- Variables
- func NewExp(d time.Duration) int64
- func NewNbf(t time.Time) int64
- func Now() int64
- type Blacklist
- type BlacklistMap
- type Claims
- func (c Claims) Copy() (n Claims)
- func (c Claims) Delete(key string)
- func (c Claims) Get(key string) (v interface{}, ok bool)
- func (c Claims) GetBool(key string) (b bool)
- func (c Claims) GetFloat64(key string) (f float64)
- func (c Claims) GetInt(key string) (i int)
- func (c Claims) GetInt64(key string) (i int64)
- func (c Claims) GetString(key string) (s string)
- func (c Claims) Set(key string, v interface{})
- type HS256
- type HS384
- type HS512
- type Hash
- type JWT
- type MemBlacklist
- type Token
Constants ¶
const ( HS256Name = "HS256" HS384Name = "HS384" HS512Name = "HS512" )
const ( // HTTPHeader is the default HTTP Authorization header name HTTPHeader = "Authorization" // DefaultExpiry is the default token expiration time DefaultExpiry = time.Hour * 12 // KeySize is the secret key size KeySize = 64 // Typ is the JWT type Typ = "JWT" // TypClaim is the typ claim name TypClaim = "typ" // AlgClaim is the alg claim name AlgClaim = "alg" // ExpClaim is the exp claim name ExpClaim = "exp" // NbfClaim is the nbf claim name NbfClaim = "nbf" // TokenSeparator is the tokens separator char TokenSeparator = "." )
Variables ¶
var ( ErrNoJWT = errors.New(jwtErr + "not a json web token") ErrEmptyToken = errors.New(jwtErr + "token is empty") ErrUnsupportedAlg = errors.New(jwtErr + "unsupported algorithm") ErrInvalid = errors.New(jwtErr + "token validation failed") ErrBlacklisted = errors.New(jwtErr + "token blacklisted") ErrBlacklistNotEnabled = errors.New(jwtErr + "blacklisting is not enabled") ErrNbf = errors.New(jwtErr + "token not valid yet") ErrExp = errors.New(jwtErr + "token expired") ErrMissingNbf = errors.New(jwtErr + "missing nbf claim") ErrMissingExp = errors.New(jwtErr + "missing exp claim") ErrMissingTokenParts = errors.New(jwtErr + "missing token parts") ErrEmptySignature = errors.New(jwtErr + "token signature is empty") ErrTokenIsNil = errors.New(jwtErr + "token is nil") ErrInvalidKeySize = errors.New(jwtErr + "invalid secret key size") )
var DefaultSecretReader = rand.Reader
DefaultSecretReader is the default secret key generator
Functions ¶
Types ¶
type Blacklist ¶
type Blacklist interface { Add(string, int64) error Remove(string) error Check(string) bool Map() (BlacklistMap, error) }
Blacklist is the blacklisting storage interface
type Claims ¶
type Claims map[string]interface{}
Claims is the claim type of the token The Claims map is not goroutine safe
func (Claims) GetFloat64 ¶
GetFloat64 returns a float64 from the claims map
type JWT ¶
type JWT struct {
// contains filtered or unexported fields
}
JWT represents the JSON Web Token signing and blacklisting infrastructure
func New ¶
New returns a new JWT object with the given expiry timeout. If the timeout is less or equal to zero the default expiry (12 hours) is used. The secret key size needs to be at least 64 bytes. If secret is nil the DefaultSecretReader is used. If blacklisting is enabled, the JWT object leaks a goroutine to garbage-collect expired blacklisted tokens. Call the Stop() method to exit the goroutine.
func (*JWT) Invalidate ¶
Invalidate checks if a token is already blacklisted If the token is not blacklisted, it will get blacklisted
type MemBlacklist ¶
type MemBlacklist struct { // protects list sync.RWMutex // contains filtered or unexported fields }
MemBlacklist implements the Blacklist interface
func NewMemBlacklist ¶
func NewMemBlacklist() *MemBlacklist
NewMemBlacklist implements the Blacklist interface using an in-memory map
func (*MemBlacklist) Add ¶
func (mb *MemBlacklist) Add(sig string, exp int64) error
Add adds a new token signature with expiration time to the blacklist
func (*MemBlacklist) Check ¶
func (mb *MemBlacklist) Check(sig string) (ok bool)
Check returns true if a token signature is blacklisted and false otherwise
func (*MemBlacklist) Map ¶
func (mb *MemBlacklist) Map() (list BlacklistMap, err error)
Map returns the blacklist in the form of a iterable map structure for cleanup
func (*MemBlacklist) Remove ¶
func (mb *MemBlacklist) Remove(sig string) error
Remove deletes a token signature from the blacklist
type Token ¶
Token represents a JWT token
func DecodeToken ¶
DecodeToken decodes a raw string token into a *Token object
func NewToken ¶
NewToken returns a new *Token using the provided hash algorithm and claims If claims is nil, an empty map is used If hash is nil, HS256 is used