Documentation
¶
Overview ¶
Package jwt is token generation and validation.
Index ¶
- Variables
- func GenerateCustomToken(kv map[string]interface{}) (string, error)
- func GenerateToken(uid string, name string, kvs ...map[string]any) (string, error)
- func Init(opts ...Option)
- func RefreshCustomToken(tokenString string) (string, error)
- func RefreshToken(tokenString string) (string, error)
- type Claims
- type CustomClaims
- type CustomRegisteredClaims
- type KV
- type Option
- func WithAudience(audience []string) Option
- func WithExpire(d time.Duration) Option
- func WithID(id string) Option
- func WithIssuer(issuer string) Option
- func WithNotBefore(notBefore time.Time) Option
- func WithSigningKey(key string) Option
- func WithSigningMethod(sm *jwt.SigningMethodHMAC) Option
- func WithSubject(subject string) Option
Constants ¶
This section is empty.
Variables ¶
var ( // HS256 Method HS256 = jwt.SigningMethodHS256 // HS384 Method HS384 = jwt.SigningMethodHS384 // HS512 Method HS512 = jwt.SigningMethodHS512 )
var ErrTokenExpired = jwt.ErrTokenExpired
ErrTokenExpired expired
Functions ¶
func GenerateCustomToken ¶
GenerateCustomToken generate token by custom fields, use CustomClaims
func GenerateToken ¶
GenerateToken generate token by uid and name, use universal Claims
func RefreshCustomToken ¶
RefreshCustomToken refresh custom token
func RefreshToken ¶
RefreshToken refresh token
Types ¶
type Claims ¶
type Claims struct {
UID string `json:"uid"`
Name string `json:"name"`
Fields map[string]any `json:"data"`
CustomRegisteredClaims
}
Claims standard claims, include uid, name, and CustomRegisteredClaims
func ParseToken ¶
ParseToken parse token, return universal Claims
type CustomClaims ¶
type CustomClaims struct {
Fields KV `json:"data"`
CustomRegisteredClaims
}
CustomClaims custom fields claims
func ParseCustomToken ¶
func ParseCustomToken(tokenString string) (*CustomClaims, error)
ParseCustomToken parse token, return CustomClaims
func (*CustomClaims) Get ¶
func (c *CustomClaims) Get(key string) (val interface{}, isExist bool)
Get custom field value by key, if not found, return false
func (*CustomClaims) GetInt ¶ added in v1.0.49
func (c *CustomClaims) GetInt(key string) (int, bool)
GetInt custom field value by key, if not found, return false
type CustomRegisteredClaims ¶ added in v1.3.44
type CustomRegisteredClaims struct {
jwt.RegisteredClaims
Subject any `json:"sub,omitempty"`
}
CustomRegisteredClaims custom registered claims
type Option ¶
type Option func(*options)
Option set the jwt options.
func WithAudience ¶ added in v1.4.0
WithAudience set audience value
func WithNotBefore ¶ added in v1.4.0
WithNotBefore set not before value
func WithSigningMethod ¶
func WithSigningMethod(sm *jwt.SigningMethodHMAC) Option
WithSigningMethod set signing method value
func WithSubject ¶ added in v1.4.0
WithSubject set subject value