token

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package token 令牌管理

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Install

func Install(mod string, db *orm.DB)

Types

type BuildResponseFunc

type BuildResponseFunc = func() jwt.Responser

type Claims

type Claims interface {
	jwt.Claims

	// UserID 获取用户的唯一编号
	//
	// 可以关联某一用户的所有登录令牌。
	UserID() string

	// BaseToken 关联的基础令牌
	//
	// 仅在刷新令牌中,此值不为空。
	BaseToken() string
}

Claims 自定义 Claims 需要实现的接口

func BuildClaims

func BuildClaims() Claims

func NewClaims

func NewClaims(uid string) Claims

NewClaims 返回默认的 Claims 实现

uid 为用户的唯一编号。

type Config

type Config struct {
	// expires 表示令牌的过期时间,单位为秒;
	Expired int `xml:"expired,attr" json:"expired" yaml:"expired"`

	// 表示刷新令牌的过期时间,单位为秒,要长于 expired。
	// 同时也被作为丢弃令牌的回收时间。
	// 如果为零,则自动赋值为 Expired * 2。
	Refreshed int `xml:"refreshed,attr" json:"refreshed" yaml:"refreshed"`

	HMAC []*hmac `xml:"hmac" json:"hmac" yaml:"hmac"`
	// contains filtered or unexported fields
}

func (*Config) SanitizeConfig

func (cnf *Config) SanitizeConfig() *app.ConfigError

type Tokens

type Tokens[T Claims] struct {
	// contains filtered or unexported fields
}

Tokens 令牌管理

func NewTokens

func NewTokens[T Claims](mod string, s *web.Server, db *orm.DB, bc jwt.BuildClaimsFunc[T], cnf *Config, jobTitle string) (*Tokens[T], error)

NewTokens 声明令牌管理对象

func (*Tokens[T]) BlockToken

func (tks *Tokens[T]) BlockToken(token string) error

BlockToken 丢弃令牌

时长为 New 中传递的 expired 的两倍。

func (*Tokens[T]) BlockUID

func (tks *Tokens[T]) BlockUID(uid string) error

BlockUID 丢弃 UserID 关联的所有令牌

时长为 New 中传递的 expires 的两倍。 包括后续生成的令牌,一般用于禁止用户登录等操作。

func (*Tokens[T]) ClaimsIsBlocked

func (tks *Tokens[T]) ClaimsIsBlocked(c T) bool

func (*Tokens[T]) GetToken

func (tks *Tokens[T]) GetToken(ctx *web.Context) string

func (*Tokens[T]) GetValue

func (tks *Tokens[T]) GetValue(ctx *web.Context) (T, bool)

func (*Tokens[T]) Middleware

func (tks *Tokens[T]) Middleware(next web.HandlerFunc) web.HandlerFunc

func (*Tokens[T]) New

func (tks *Tokens[T]) New(ctx *web.Context, status int, access T) web.Responser

New 签发新的令牌

access 普通的访问令牌; refresh 刷新令牌;

func (*Tokens[T]) RecoverUID

func (tks *Tokens[T]) RecoverUID(uid string) error

RecoverUID 恢复该用户的登录权限

func (*Tokens[T]) TokenIsBlocked

func (tks *Tokens[T]) TokenIsBlocked(token string) bool

Jump to

Keyboard shortcuts

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