jwt

package
v0.0.0-...-9dc0809 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Parser 默认锁定 HMAC 算法族做深度防御(本包签发一律 HS256,密钥为对称 []byte,
	// 不允许被换成其它算法家族);业务可用 SetOptions(jwt.WithValidMethods(...)) 进一步收紧。
	Parser          = jwt.NewParser(jwt.WithValidMethods([]string{"HS256", "HS384", "HS512"}))
	ErrInvalidToken = errors.New("invalid token")
)

Functions

func GenerateToken

func GenerateToken(claims jwt.Claims, secret interface{}) (string, error)

GenerateToken signs arbitrary jwt.Claims with HS256 using the provided secret.

func ParseToken

func ParseToken(claims jwt.Claims, token string, secret []byte) (*jwt.Token, error)

ParseToken parses and validates a JWT string, populating claims on success.

func ParseTokenWithKeyFunc

func ParseTokenWithKeyFunc(claims jwt.Claims, token string, f jwt.Keyfunc) (*jwt.Token, error)

ParseTokenWithKeyFunc parses a JWT using a custom key-lookup function.

func SetOptions

func SetOptions(options ...jwt.ParserOption)

SetOptions applies additional parser options to the global JWT Parser. 只应在启动早期(任何请求处理之前)调用:Parser 是无锁全局变量。

Types

type AuthInfo

type AuthInfo interface {
	GetId() string
}

AuthInfo is implemented by auth payloads that carry a string identity.

type Claims

type Claims[T any] struct {
	Auth T `json:"auth,omitempty"`
	jwt.RegisteredClaims
}

Claims holds typed authentication data alongside standard JWT registered claims. The Auth field should store immutable user info; refresh the token whenever the user changes credentials.

func Auth

func Auth[A AuthInfo](ctx context.Context, token string, secret []byte) (*Claims[A], error)

Auth parses and validates the token, attaches the raw auth payload to the request metadata, and returns the typed claims.

func NewClaims

func NewClaims[T any](data T, maxAge int64, sign string) *Claims[T]

NewClaims creates a Claims value with the given auth data, expiry duration (as time.Duration nanoseconds), and issuer.

func (*Claims[T]) GenerateToken

func (c *Claims[T]) GenerateToken(secret interface{}) (string, error)

GenerateToken signs the claims with HS256 using the provided secret.

type ClaimsWithRaw

type ClaimsWithRaw[A AuthInfo] struct {
	Claims[A]
	Raw []byte `json:"-"`
}

ClaimsWithRaw embeds Claims and captures the raw JSON bytes during unmarshalling, allowing the original token payload to be stored in request metadata.

func (*ClaimsWithRaw[A]) ParseToken

func (x *ClaimsWithRaw[A]) ParseToken(token string, secret []byte) error

ParseToken validates the JWT string, populates the claims, and sets the auth ID.

func (*ClaimsWithRaw[A]) UnmarshalJSON

func (x *ClaimsWithRaw[A]) UnmarshalJSON(data []byte) error

UnmarshalJSON saves the raw bytes before delegating to the embedded Claims.

Jump to

Keyboard shortcuts

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