jwtKit

package
v3.0.62 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// KeyNotBefore 生效时间
	KeyNotBefore = "nbf"

	// KeyExpirationTime 过期时间
	KeyExpirationTime = "exp"

	// KeyIssuedAt 签发时间
	KeyIssuedAt = "iat"

	// KeyAudience 受众
	KeyAudience = "aud"

	// KeyIssuer 签发人
	KeyIssuer = "iss"

	// KeySubject 主题
	KeySubject = "sub"

	// KeyJwtId 编号
	KeyJwtId = "jti"
)

Variables

This section is empty.

Functions

func IsTokenExpiredError

func IsTokenExpiredError(err error) bool

Types

type JWT

type JWT struct {
	Key []byte
	// contains filtered or unexported fields
}

func NewJWT

func NewJWT(key []byte, shiftArgs ...int) *JWT

func (*JWT) New

func (j *JWT) New(method jwt.SigningMethod, claims jwt.MapClaims, options ...jwt.TokenOption) (string, error)

New

@param Key 密钥 @param method e.g. jwt.SigningMethodHS256 || jwt.SigningMethodHS384 || jwt.SigningMethodHS512

func (*JWT) NewComplexly

func (j *JWT) NewComplexly(method jwt.SigningMethod, claims jwt.MapClaims, options ...jwt.TokenOption) (cipherText string, err error)

func (*JWT) Parse

func (j *JWT) Parse(tokenString string, keyFunc jwt.Keyfunc, options ...jwt.ParserOption) (jwt.MapClaims, error)

Parse

PS: 如果 token 过期(根据"exp",有的话),会返回 error(可以通过 IsTokenExpiredError 判断).

@param keyFunc e.g.

func(token *jwt.Token) (interface{}, error) {
	// Don't forget to validate the alg is what you expect:
	if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
		return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
	}

	// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
	return Key, nil
}

func (*JWT) ParseComplexly

func (j *JWT) ParseComplexly(cipherText string, keyFunc jwt.Keyfunc, options ...jwt.ParserOption) (jwt.MapClaims, error)

Jump to

Keyboard shortcuts

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