Documentation
¶
Overview ¶
Package jwt provides a parser, generator and a middleware to checks if a jwt-token is valid. If not, a StatusUnauthorized (401) will return.
Claims must implement the jwt.Claimer interface. A standard Claim is defined which can get embedded in your struct to avoid rewriting all of the functions.
Config struct for a simple token configuration is provided.
Generate: will set the CookieRefresh, the Claim gets generated and calls the CallbackGenerate function. After that, the token gets signed and the CookieJWT gets set.
Parse: will check the CookieJWT and parses the string. The claim will be checked if its valid. If the claim is expired, the CallbackRefresh function will be called, to check if a new token should be generated. On success the request.Context CLAIM will be set.
A refresh token will only be generated if a refresh callback is set and the CookieJWT and CookieRefresh is available.
Index ¶
- Constants
- Variables
- func Cookie(r *http.Request, name string) (string, error)
- func NewCookie(w http.ResponseWriter, name string, value string, ttl time.Duration)
- type Claim
- func (c *Claim) Aud() string
- func (c *Claim) Exp() int64
- func (c *Claim) Iat() int64
- func (c *Claim) Iss() string
- func (c *Claim) Jid() string
- func (c *Claim) Nbf() int64
- func (c *Claim) Render() interface{}
- func (c *Claim) SetAud(aud string)
- func (c *Claim) SetExp(exp int64)
- func (c *Claim) SetIat(iat int64)
- func (c *Claim) SetIss(iss string)
- func (c *Claim) SetJid(id string)
- func (c *Claim) SetNbf(nbf int64)
- func (c *Claim) SetSub(sub string)
- func (c *Claim) Sub() string
- func (c *Claim) Valid() error
- type Claimer
- type Config
- type RefreshConfig
- type Token
Constants ¶
const ( CookieJWT = "JWT" CookieRefresh = "REFRESH" )
Cookie constants
const ( HS256 = "HS256" HS384 = "HS384" HS512 = "HS512" )
allowed algorithms.
const CLAIM = "JWT"
CLAIM key for the request ctx.
Variables ¶
var ( ErrConfigNotValid = errors.New("jwt: config is not valid") ErrSigningMethod = "jwt: unexpected signing method: %v" ErrInvalidClaim = "jwt: claim is not valid %s: %#v" ErrTokenExpired = errors.New("jwt: token is expired") )
Error messages.
Functions ¶
Types ¶
type Claim ¶
type Claim struct {
jwt.StandardClaims
}
Claim type implements the Claimer interface and extends the jwt.StandardClaims.
type Claimer ¶
type Claimer interface {
Jid() string
SetJid(string)
Iss() string
SetIss(string)
Aud() string
SetAud(string)
Sub() string
SetSub(string)
Iat() int64
SetIat(int64)
Exp() int64
SetExp(int64)
Nbf() int64
SetNbf(int64)
// UserID should return the user id.
UserID() interface{}
// Render should return the needed data for the frontend.
Render() interface{}
// Valid is defined in the jwt-go package but can get overwritten here.
Valid() error
}
Claimer interface.
type Config ¶
type Config struct {
Alg string // algorithm (HS256, HS384, HS512)
Issuer string // issuer
Audience string // audience
Subject string // subject
Expiration time.Duration // the ttl of the token (suggested short lived 15 Minutes). 0 is not allowed.
SignKey string // the sign key. atm only a key, later on it can also be a file path
RefreshToken RefreshConfig // true if a refresh token should get created
}
Config of the jwt token.
type RefreshConfig ¶
RefreshConfig config.
type Token ¶
type Token struct {
// should be used to check if the refresh token is still valid. Error should return if not.
CallbackRefresh func(http.ResponseWriter, *http.Request, Claimer) error
// should be used to check user data and update the claim, before the token gets generated.
CallbackGenerate func(http.ResponseWriter, *http.Request, Claimer, string) error
// contains filtered or unexported fields
}
Token struct.
func (*Token) Generate ¶
Generate a new token. Refresh cookie will be set, a new Claim generated and passed to the callback function - if defined. The JWT token gets signed and set as JTW cookie. Error will return if the token could not get signed or the callback function returns an error.
func (*Token) MW ¶
func (t *Token) MW(h http.HandlerFunc) http.HandlerFunc
MW will be passed to the middleware.
func (*Token) Parse ¶
Parse the JWT cookie. The Claim will be checked if its valid. If the Claim is expired, the refresh Callback will be called to generate a new Token. The Claim will be set as request context JWT. A refresh token will only be generated if the CookieJWT (expired) and CookieRefresh is set.