Documentation
¶
Index ¶
- Variables
- func GenerateToken(claims jwt.Claims, secret interface{}) (string, error)
- func ParseToken(claims jwt.Claims, token string, secret []byte) (*jwt.Token, error)
- func ParseTokenWithKeyFunc(claims jwt.Claims, token string, f jwt.Keyfunc) (*jwt.Token, error)
- func SetOptions(options ...jwt.ParserOption)
- type AuthInfo
- type Claims
- type ClaimsWithRaw
Constants ¶
This section is empty.
Variables ¶
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 ¶
GenerateToken signs arbitrary jwt.Claims with HS256 using the provided secret.
func ParseToken ¶
ParseToken parses and validates a JWT string, populating claims on success.
func ParseTokenWithKeyFunc ¶
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 ¶
Auth parses and validates the token, attaches the raw auth payload to the request metadata, and returns the typed claims.
func NewClaims ¶
NewClaims creates a Claims value with the given auth data, expiry duration (as time.Duration nanoseconds), and issuer.
func (*Claims[T]) GenerateToken ¶
GenerateToken signs the claims with HS256 using the provided secret.
type ClaimsWithRaw ¶
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.