jwt

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2023 License: MIT Imports: 3 Imported by: 1

README

jwt

token generation and validation.


Example of use

    import "github.com/zhufuyi/sponge/pkg/gwt"

	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"
	// generate token
	token, err := jwt.GenerateToken(uid)

    // verify token
	v, err := jwt.VerifyToken(token)
	if v.Uid != uid{
	    return
	}

Documentation

Overview

Package jwt is token generation and validation.

Index

Constants

This section is empty.

Variables

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

Functions

func GenerateToken

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

GenerateToken generate token

func GenerateTokenStandard

func GenerateTokenStandard() (string, error)

GenerateTokenStandard generate token

func Init

func Init(opts ...Option)

Init initialize jwt

func VerifyTokenStandard

func VerifyTokenStandard(tokenString string) error

VerifyTokenStandard verify token

Types

type CustomClaims

type CustomClaims struct {
	UID  string `json:"uid"`
	Role string `json:"role"`
	jwt.StandardClaims
}

CustomClaims add custom fields to StandardClaims' payload

func VerifyToken

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

VerifyToken verify token

type Option

type Option func(*options)

Option set the jwt options.

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