jwt

package
v1.4.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 3 Imported by: 0

README

jwt

Generate and parse token based on jwt library.


Example of use

Example 1: common fields jwt

    import "github.com/18721889353/sunshine/pkg/jwt"

	jwt.Init(
		// jwt.WithSigningKey("123456"),   // key
		// jwt.WithExpire(time.Hour), // expiry time
		// jwt.WithSigningMethod(jwt.HS512), // encryption method, default is HS256, can be set to HS384, HS512
	)

	uid := "123"
	name := "admin"

	// generate token
	token, err := jwt.GenerateToken(uid, name)
	// handle err

	// parse token
	claims, err := jwt.ParseToken(token)
	// handle err

	// verify
	if claims.Uid != uid || claims.Name != name {
		print("verify failed")
	    return
	}

Example 2: custom fields jwt

    import "github.com/18721889353/sunshine/pkg/jwt"

	jwt.Init(
		// jwt.WithSigningKey("123456"),   // key
		// jwt.WithExpire(time.Hour), // expiry time
		// jwt.WithSigningMethod(jwt.HS512), // encryption method, default is HS256, can be set to HS384, HS512
	)

	fields := jwt.KV{"id": 123, "foo": "bar"}

	// generate token
	token, err := jwt.GenerateCustomToken(fields)
	// handle err

	// parse token
	claims, err := jwt.ParseCustomToken(token)
	// handle err

	// verify
	id, isExist1 := claims.Get("id")
	foo, isExist2 := claims.Get("foo")
	if !isExist1 || !isExist2 || int(id.(float64)) != fields["id"].(int) || foo.(string) != fields["foo"].(string) {
	    print("verify failed")
	    return
	}

Documentation

Overview

Package jwt is token generation and validation.

Index

Constants

This section is empty.

Variables

View Source
var (
	// HS256 Method
	HS256 = jwt.SigningMethodHS256
	// HS384 Method
	HS384 = jwt.SigningMethodHS384
	// HS512 Method
	HS512 = jwt.SigningMethodHS512
)
View Source
var ErrTokenExpired = jwt.ErrTokenExpired

ErrTokenExpired expired

Functions

func GenerateCustomToken

func GenerateCustomToken(kv map[string]interface{}) (string, error)

GenerateCustomToken generate token by custom fields, use CustomClaims

func GenerateToken

func GenerateToken(uid string, name string, kvs ...map[string]any) (string, error)

GenerateToken generate token by uid and name, use universal Claims

func Init

func Init(opts ...Option)

Init initialize jwt

func RefreshCustomToken

func RefreshCustomToken(tokenString string) (string, error)

RefreshCustomToken refresh custom token

func RefreshToken

func RefreshToken(tokenString string) (string, error)

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

func ParseToken(tokenString string) (*Claims, error)

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

func (*CustomClaims) GetString added in v1.0.49

func (c *CustomClaims) GetString(key string) (string, bool)

GetString custom field value by key, if not found, return false

func (*CustomClaims) GetUint64 added in v1.0.49

func (c *CustomClaims) GetUint64(key string) (uint64, bool)

GetUint64 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 KV

type KV = map[string]any

KV map type

type Option

type Option func(*options)

Option set the jwt options.

func WithAudience added in v1.4.0

func WithAudience(audience []string) Option

WithAudience set audience value

func WithExpire

func WithExpire(d time.Duration) Option

WithExpire set expire value

func WithID added in v1.4.0

func WithID(id string) Option

WithID set JWT ID value

func WithIssuer

func WithIssuer(issuer string) Option

WithIssuer set issuer value

func WithNotBefore added in v1.4.0

func WithNotBefore(notBefore time.Time) Option

WithNotBefore set not before value

func WithSigningKey

func WithSigningKey(key string) Option

WithSigningKey set signing key value

func WithSigningMethod

func WithSigningMethod(sm *jwt.SigningMethodHMAC) Option

WithSigningMethod set signing method value

func WithSubject added in v1.4.0

func WithSubject(subject string) Option

WithSubject set subject value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL