jwt

package
v1.14.4 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2025 License: MIT Imports: 3 Imported by: 0

README

jwt

Note: This package is deprecated, please use github.com/go-dev-frame/pkg/jwt instead.

jwt is a library for generating and parsing token based on jwt.


Example of use

Default jwt

    import "github.com/go-dev-frame/sponge/pkg/jwt/old_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
    }

Custom jwt

    import "github.com/go-dev-frame/sponge/pkg/jwt/old_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 := map[string]interface{}{"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.GetInt("id")
    if !isExist1 || id != fields["id"].(int) {
        print("verify failed")
    }
    foo, isExist2 := claims.GetString("foo")
    if !isExist1 || foo != fields["foo"].(string) {
        print("verify failed")
        return
    }

Documentation

Overview

Package jwt is deprecated, old package path is "github.com/go-dev-frame/sponge/pkg/jwt/old_jwt" Please use new jwt package instead, new package path is "github.com/go-dev-frame/sponge/pkg/jwt"

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 Deprecated: use "github.com/go-dev-frame/sponge/pkg/jwt" GenerateToken instead

func GenerateToken

func GenerateToken(uid string, name ...string) (string, error)

GenerateToken generate token by uid and name, use universal Claims Deprecated: use "github.com/go-dev-frame/sponge/pkg/jwt" GenerateToken instead

func Init

func Init(opts ...Option)

Init initialize jwt Deprecated: build jwt.Init() before use

func RefreshCustomToken

func RefreshCustomToken(tokenString string) (string, error)

RefreshCustomToken refresh custom token Deprecated: use "github.com/go-dev-frame/sponge/pkg/jwt" RefreshToken instead

func RefreshToken

func RefreshToken(tokenString string) (string, error)

RefreshToken refresh token Deprecated: use "github.com/go-dev-frame/sponge/pkg/jwt" RefreshToken instead

Types

type Claims

type Claims struct {
	UID  string `json:"uid"`
	Name string `json:"name"`
	jwt.RegisteredClaims
}

Claims standard claims, include uid, name, and RegisteredClaims

func ParseToken

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

ParseToken parse token, return universal Claims Deprecated: use "github.com/go-dev-frame/sponge/pkg/jwt" ValidateToken instead

type CustomClaims

type CustomClaims struct {
	Fields KV `json:"fields"`
	jwt.RegisteredClaims
}

CustomClaims custom fields claims

func ParseCustomToken

func ParseCustomToken(tokenString string) (*CustomClaims, error)

ParseCustomToken parse token, return CustomClaims Deprecated: use "github.com/go-dev-frame/sponge/pkg/jwt" ValidateToken instead

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

func (c *CustomClaims) GetInt(key string) (int, bool)

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

func (*CustomClaims) GetString

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

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

func (*CustomClaims) GetUint64

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

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

type KV

type KV = map[string]interface{}

KV map type

type Option

type Option func(*options)

Option set the jwt options. Deprecated: use "github.com/go-dev-frame/sponge/pkg/jwt" RegisteredClaimsOption instead.

func WithExpire

func WithExpire(d time.Duration) Option

WithExpire set expire value

func WithIssuer

func WithIssuer(issuer string) Option

WithIssuer set issuer 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

Jump to

Keyboard shortcuts

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