jwtKit

package
v3.0.920 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: Apache-2.0 Imports: 6 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

func Sign added in v3.0.67

func Sign(key []byte, method jwt.SigningMethod, claims jwt.MapClaims, options ...jwt.TokenOption) (string, error)

Sign 生成JWT字符串.

@param key 密钥(secret) @param method e.g. jwt.SigningMethodHS256 || jwt.SigningMethodHS384 || jwt.SigningMethodHS512

func SignWithCaesar added in v3.0.67

func SignWithCaesar(caesarShift int, key []byte, method jwt.SigningMethod, claims jwt.MapClaims, options ...jwt.TokenOption) (cipherText string, err error)

func Verify added in v3.0.67

func Verify(tokenString string, keyFunc jwt.Keyfunc, options ...jwt.ParserOption) (jwt.MapClaims, error)

Verify 验证JWT字符串.

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

@param keyFunc 此函数的第一个返回值为密钥(secret)

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 VerifyWithCaesar added in v3.0.67

func VerifyWithCaesar(caesarShift int, cipherText string, keyFunc jwt.Keyfunc, options ...jwt.ParserOption) (jwt.MapClaims, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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