Documentation
¶
Index ¶
- func WithAllowTokenHeader[T any](header string) option.Option[Management[T]]
- func WithDecryptKey(decryptKey string) option.Option[Options]
- func WithExposeAccessHeader[T any](header string) option.Option[Management[T]]
- func WithExposeRefreshHeader[T any](header string) option.Option[Management[T]]
- func WithGenIDFunc(fn func() string) option.Option[Options]
- func WithIssuer(issuer string) option.Option[Options]
- func WithMethod(method jwt.SigningMethod) option.Option[Options]
- func WithNowFunc[T any](nowFunc func() time.Time) option.Option[Management[T]]
- func WithRefreshJWTOptions[T any](refreshOpts Options) option.Option[Management[T]]
- func WithRotateRefreshToken[T any](isRotate bool) option.Option[Management[T]]
- type Management
- func (m *Management[T]) GenerateAccessToken(data T) (string, error)
- func (m *Management[T]) GenerateRefreshToken(data T) (string, error)
- func (m *Management[T]) MiddlewareBuilder() *MiddlewareBuilder[T]
- func (m *Management[T]) Refresh(ctx *gin.Context)
- func (m *Management[T]) SetClaims(ctx *gin.Context, claims RegisteredClaims[T])
- func (m *Management[T]) VerifyAccessToken(token string, opts ...jwt.ParserOption) (RegisteredClaims[T], error)
- func (m *Management[T]) VerifyRefreshToken(token string, opts ...jwt.ParserOption) (RegisteredClaims[T], error)
- type Manager
- type MiddlewareBuilder
- type Options
- type RegisteredClaims
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithAllowTokenHeader ¶
func WithAllowTokenHeader[T any](header string) option.Option[Management[T]]
WithAllowTokenHeader 设置允许 token 的请求头.
func WithDecryptKey ¶
WithDecryptKey 设置解密密钥.
func WithExposeAccessHeader ¶
func WithExposeAccessHeader[T any](header string) option.Option[Management[T]]
WithExposeAccessHeader 设置公开资源令牌的请求头.
func WithExposeRefreshHeader ¶
func WithExposeRefreshHeader[T any](header string) option.Option[Management[T]]
WithExposeRefreshHeader 设置公开刷新令牌的请求头.
func WithGenIDFunc ¶
WithGenIDFunc 设置生成 JWT ID 的函数. 可以设置成 WithGenIDFunc(uuid.NewString).
func WithMethod ¶
func WithMethod(method jwt.SigningMethod) option.Option[Options]
WithMethod 设置 JWT 的签名方法.
func WithNowFunc ¶
WithNowFunc 设置当前时间. 一般用于测试固定 jwt.
func WithRefreshJWTOptions ¶
func WithRefreshJWTOptions[T any](refreshOpts Options) option.Option[Management[T]]
WithRefreshJWTOptions 设置刷新令牌相关的配置.
func WithRotateRefreshToken ¶
func WithRotateRefreshToken[T any](isRotate bool) option.Option[Management[T]]
WithRotateRefreshToken 设置轮换刷新令牌.
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]) GenerateRefreshToken ¶
func (m *Management[T]) GenerateRefreshToken(data T) (string, error)
GenerateRefreshToken 生成刷新 token. 需要设置 refreshJWTOptions 否则返回 errEmptyRefreshOpts 错误.
func (*Management[T]) MiddlewareBuilder ¶
func (m *Management[T]) MiddlewareBuilder() *MiddlewareBuilder[T]
MiddlewareBuilder 登录认证的中间件.
func (*Management[T]) Refresh ¶
func (m *Management[T]) Refresh(ctx *gin.Context)
Refresh 刷新 token 的 gin.HandlerFunc.
func (*Management[T]) SetClaims ¶
func (m *Management[T]) SetClaims(ctx *gin.Context, claims RegisteredClaims[T])
SetClaims 设置 claims 到 key=`claims` 的 gin.Context 中.
func (*Management[T]) VerifyAccessToken ¶
func (m *Management[T]) VerifyAccessToken(token string, opts ...jwt.ParserOption) (RegisteredClaims[T], error)
VerifyAccessToken 校验资源 token.
func (*Management[T]) VerifyRefreshToken ¶
func (m *Management[T]) VerifyRefreshToken(token string, opts ...jwt.ParserOption) (RegisteredClaims[T], error)
VerifyRefreshToken 校验刷新 token. 需要设置 refreshJWTOptions 否则返回 errEmptyRefreshOpts 错误.
type Manager ¶
type Manager[T any] interface { // MiddlewareBuilder 创建登录认证的中间件. MiddlewareBuilder() *MiddlewareBuilder[T] // GenerateAccessToken 生成资源 token. GenerateAccessToken(data T) (string, error) // VerifyAccessToken 校验资源 token. VerifyAccessToken(token string, opts ...jwt.ParserOption) (RegisteredClaims[T], error) // GenerateRefreshToken 生成刷新 token. // 需要设置 refreshJWTOptions 否则返回 errEmptyRefreshOpts 错误. GenerateRefreshToken(data T) (string, error) // VerifyRefreshToken 校验刷新 token. // 需要设置 refreshJWTOptions 否则返回 errEmptyRefreshOpts 错误. VerifyRefreshToken(token string, opts ...jwt.ParserOption) (RegisteredClaims[T], error) // SetClaims 设置 claims 到 key=`claims` 的 gin.Context 中. SetClaims(ctx *gin.Context, claims RegisteredClaims[T]) }
Manager jwt 管理器.
type MiddlewareBuilder ¶
type MiddlewareBuilder[T any] struct { // contains filtered or unexported fields }
MiddlewareBuilder 创建一个校验登录的 middleware ignorePath: 默认使用 func(path string) bool { return false } 也就是全部不忽略.
func (*MiddlewareBuilder[T]) Build ¶
func (m *MiddlewareBuilder[T]) Build() gin.HandlerFunc
func (*MiddlewareBuilder[T]) IgnorePath ¶
func (m *MiddlewareBuilder[T]) IgnorePath(path ...string) *MiddlewareBuilder[T]
func (*MiddlewareBuilder[T]) IgnorePathFunc ¶
func (m *MiddlewareBuilder[T]) IgnorePathFunc(fn func(path string) bool) *MiddlewareBuilder[T]
IgnorePathFunc 设置忽略资源令牌认证的路径.
type Options ¶
type RegisteredClaims ¶
type RegisteredClaims[T any] struct { Data T `json:"data"` jwt.RegisteredClaims }