jwt

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithDecryptKey

func WithDecryptKey(decryptKey string) option.Option[Options]

WithDecryptKey 设置解密密钥.

func WithGenIDFunc

func WithGenIDFunc(fn func() string) option.Option[Options]

WithGenIDFunc 设置生成 JWT ID 的函数. 可以设置成 WithGenIDFunc(uuid.NewString).

func WithIssuer

func WithIssuer(issuer string) option.Option[Options]

WithIssuer 设置签发人.

func WithMethod

func WithMethod(method jwt.SigningMethod) option.Option[Options]

WithMethod 设置 JWT 的签名方法.

func WithNowFunc

func WithNowFunc[T any](nowFunc func() time.Time) option.Option[Management[T]]

WithNowFunc 设置当前时间. 一般用于测试固定 jwt.

Types

type Management

type Management[T any] struct {
	// contains filtered or unexported fields
}

func NewManagement

func NewManagement[T any](accessJWTOptions Options,
	opts ...option.Option[Management[T]]) *Management[T]

NewManagement 定义一个 Management. allowTokenHeader: 默认使用 authorization 为认证请求头. exposeAccessHeader: 默认使用 x-access-token 为暴露外部的资源请求头. exposeRefreshHeader: 默认使用 x-refresh-token 为暴露外部的刷新请求头. refreshJWTOptions: 默认使用 nil 为刷新 token 的配置, 如要使用 refresh 相关功能则需要使用 WithRefreshJWTOptions 添加相关配置. rotateRefreshToken: 默认不轮换刷新令牌. 该配置需要设置 refreshJWTOptions 才有效.

func (*Management[T]) GenerateAccessToken

func (m *Management[T]) GenerateAccessToken(data T) (string, error)

GenerateAccessToken 生成资源 token.

func (*Management[T]) VerifyAccessToken

func (m *Management[T]) VerifyAccessToken(token string, opts ...jwt.ParserOption) (RegisteredClaims[T], error)

VerifyAccessToken 校验资源 token.

type Manager

type Manager[T any] interface {
	// GenerateAccessToken 生成资源 token.
	GenerateAccessToken(data T) (string, error)

	// VerifyAccessToken 校验资源 token.
	VerifyAccessToken(token string, opts ...jwt.ParserOption) (RegisteredClaims[T], error)
}

Manager jwt 管理器.

type Options

type Options struct {
	Expire        time.Duration     // 有效期
	EncryptionKey string            // 加密密钥
	DecryptKey    string            // 解密密钥
	Method        jwt.SigningMethod // 签名方式
	Issuer        string            // 签发人
	// contains filtered or unexported fields
}

func NewOptions

func NewOptions(expire time.Duration, encryptionKey string,
	opts ...option.Option[Options]) Options

NewOptions 定义一个 JWT 配置. DecryptKey: 默认与 EncryptionKey 相同. Method: 默认使用 jwt.SigningMethodHS256 签名方式.

type RegisteredClaims

type RegisteredClaims[T any] struct {
	Data T `json:"data"`
	jwt.RegisteredClaims
}

Jump to

Keyboard shortcuts

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