token

package
v0.0.0-...-ef5db53 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2025 License: MIT Imports: 12 Imported by: 0

README

令牌管理

Documentation

Index

Constants

View Source
const (
	ACCESS_TOKEN_HEADER_NAME          = "Authorization"
	ACCESS_TOKEN_COOKIE_NAME          = "access_token"
	REFRESH_TOKEN_HEADER_NAME         = "X-REFRESH-TOKEN"
	ACCESS_TOKEN_RESPONSE_HEADER_NAME = "X-OAUTH-TOKEN"
)
View Source
const (
	ISSUER_LADP          = "ldap"
	ISSUER_FEISHU        = "feishu"
	ISSUER_PASSWORD      = "password"
	ISSUER_PRIVATE_TOKEN = "private_token"
)
View Source
const (
	APP_NAME = "token"
)

Variables

View Source
var (
	CTX_TOKEN_KEY  = tokenContextKey{}
	CookieNotFound = exception.NewUnauthorized("cookie %s not found", ACCESS_TOKEN_COOKIE_NAME)
)

Functions

func GetAccessTokenFromHTTP

func GetAccessTokenFromHTTP(r *http.Request) string

func GetIssueParameterValue

func GetIssueParameterValue[T any](p IssueParameter, key string) T

泛型函数,用于从 IssueParameter 中获取指定键的值

func GetRefreshTokenFromHTTP

func GetRefreshTokenFromHTTP(r *http.Request) string

func MakeBearer

func MakeBearer(lenth int) string

随机生成token

func RegistryIssuer

func RegistryIssuer(name string, p Issuer)

Types

type ChangeNamespaceRequest

type ChangeNamespaceRequest struct{}

type DESCRIBE_BY

type DESCRIBE_BY int
const (
	DESCRIBE_BY_ACCESS_TOKEN DESCRIBE_BY = iota
)

type DescribeTokenRequest

type DescribeTokenRequest struct {
	DescribeBy    DESCRIBE_BY `json:"describe_by"`
	DescribeValue string      `json:"describe_value"`
}

func NewDescribeTokenRequest

func NewDescribeTokenRequest(accessToken string) *DescribeTokenRequest

type IssueParameter

type IssueParameter map[string]any

参数类型

func NewIssuerParameter

func NewIssuerParameter() IssueParameter

func (IssueParameter) AccessToken

func (p IssueParameter) AccessToken() string

func (IssueParameter) ExpireTTL

func (p IssueParameter) ExpireTTL() time.Duration

func (IssueParameter) PassWord

func (p IssueParameter) PassWord() string

func (IssueParameter) SetAccessToken

func (p IssueParameter) SetAccessToken(v string) IssueParameter

func (IssueParameter) SetExpireTTL

func (p IssueParameter) SetExpireTTL(v int64) IssueParameter

func (IssueParameter) SetPassWord

func (p IssueParameter) SetPassWord(v string) IssueParameter

func (IssueParameter) SetUserName

func (p IssueParameter) SetUserName(v string) IssueParameter

func (IssueParameter) UserName

func (p IssueParameter) UserName() string

type IssueTokenRequest

type IssueTokenRequest struct {
	//认证方式
	Issuer string `json:"issuer"`
	//端类型
	Source SOURCE `json:"source"`
	// 参数
	Parameter IssueParameter `json:"parameter"`
}

用户会给我们用户的身份凭证,用于换取Token

func NewIssueTokenRequest

func NewIssueTokenRequest() *IssueTokenRequest

func (*IssueTokenRequest) IssueByPassword

func (i *IssueTokenRequest) IssueByPassword(username, password string)

type Issuer

type Issuer interface {
	IssueToken(context.Context, IssueParameter) (*Token, error)
}

func GetIssuer

func GetIssuer(name string) Issuer

type LOCK_TYPE

type LOCK_TYPE int
const (
	//用户退出登录
	LOCK_TYPE_REVOLK LOCK_TYPE = iota

	//刷新TOKEN过期,会话中断
	LOCK_TYPE_TOKEN_EXPIRED

	//异地登录
	LOCK_TYPE_OTHER_PLACE_LOGIN_IN

	//异常IP登录
	LOCK_TYPE_OTHER_IP_LOGGED_IN
)

type QueryTokenRequest

type QueryTokenRequest struct {
	*request.PageRequest
	//当前可用的没有过期的token
	Active *bool `json:"active"`

	//用户来源
	Source *SOURCE `json:"source"`

	//Uids
	UserIds []uint64 `json:"user_ids"`
}

func NewQueryTokenRequest

func NewQueryTokenRequest() *QueryTokenRequest

func (*QueryTokenRequest) AddUserId

func (r *QueryTokenRequest) AddUserId(uids ...uint64) *QueryTokenRequest

func (*QueryTokenRequest) SetActice

func (r *QueryTokenRequest) SetActice(v bool) *QueryTokenRequest

func (*QueryTokenRequest) SetSource

func (r *QueryTokenRequest) SetSource(v SOURCE) *QueryTokenRequest

type RevokeTokenRequest

type RevokeTokenRequest struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
}

func NewRevokeTokenRequest

func NewRevokeTokenRequest(at, rk string) *RevokeTokenRequest

type SOURCE

type SOURCE int
const (
	SOURCE_UNKNOWN SOURCE = iota
	SOURCE_WEB
	SOURCE_IOS
	SOURCE_ANDROID
	SOURCE_PC
	SOURCE_API SOURCE = 10
)

type Service

type Service interface {

	//颁发令牌Token:Login接口,用于用户登录时颁发令牌Token
	IssueToken(context.Context, *IssueTokenRequest) (*Token, error)

	//撤销令牌Token:Logout接口,此时令牌失效,用于用户退出。
	RevokeToken(context.Context, *RevokeTokenRequest) (*Token, error)

	//查询Token详情
	DescribeToken(context.Context, *DescribeTokenRequest) (*Token, error)

	// 查询已经颁发出去的Token
	QueryToken(context.Context, *QueryTokenRequest) (*types.Set[*Token], error)

	//校验Token是给内部中间层使用,身份校验层
	//验证令牌Token:VerifyToken接口,用于验证令牌是否合法。
	VeliateToken(context.Context, *VeliateTokenRequest) (*Token, error)
}

func GetService

func GetService() Service

type Status

type Status struct {
	//冻结时间
	LockAt *time.Time `json:"lock_at" bson:"lock_at" gorm:"column:lock_at;type:timestamp;index" description:"冻结时间"`
	//冻结类型 0:用户退出登录,1:刷新Token过期
	LockType LOCK_TYPE `` /* 203-byte string literal not displayed */
	//是否被冻结
	LockReason string `json:"lock_reason" bson:"lock_reason" gorm:"column:lock_reason;type:text" description:"冻结原因"`
}

func NewStatus

func NewStatus() *Status

func (*Status) SetLockAt

func (s *Status) SetLockAt(v time.Time)

func (*Status) ToMap

func (s *Status) ToMap() map[string]any

type Token

type Token struct {
	// 在添加数据需要, 主键
	Id uint64 `json:"id" gorm:"column:id;type:uint;primary_key;"`
	// 用户来源
	Source SOURCE `json:"source" gorm:"column:source;type:tinyint(1);index" description:"用户来源"`
	// 颁发器, 办法方式(user/pass )
	Issuer string `json:"issuer" gorm:"column:issuer;type:varchar(100);index" description:"颁发器"`
	// 该Token属于哪个用户
	UserId uint64 `json:"user_id" gorm:"column:user_id;index" description:"持有该Token的用户Id"`
	// 用户名
	UserName string `json:"user_name" gorm:"column:user_name;type:varchar(255);not null;index" description:"持有该Token的用户名称"`
	// 是不是管理员
	IsAdmin bool `json:"is_admin" gorm:"column:is_admin;type:tinyint(1)" description:"是不是管理员"`
	// 令牌生效空间Id
	NamespaceId uint64 `json:"namespace_id" gorm:"column:namespace_id;type:uint;index" description:"令牌所属空间Id"`
	// 令牌生效空间名称
	NamespaceName string `json:"namespace_name" gorm:"column:namespace_name;type:varchar(100);index" description:"令牌所属空间"`
	// 访问范围定义, 鉴权完成后补充
	Scope map[string]string `json:"scope" gorm:"column:scope;type:varchar(100);serializer:json" description:"令牌访问范围定义"`
	// 颁发给用户的访问令牌(用户需要携带Token来访问接口)
	AccessToken string `json:"access_token" gorm:"column:access_token;type:varchar(100);not null;uniqueIndex" description:"访问令牌"`
	// 访问令牌过期时间
	AccessTokenExpiredAt *time.Time `` /* 131-byte string literal not displayed */
	// 刷新Token
	RefreshToken string `json:"refresh_token" gorm:"column:refresh_token;type:varchar(100);not null;uniqueIndex" description:"刷新令牌"`
	// 刷新Token过期时间
	RefreshTokenExpiredAt *time.Time `` /* 133-byte string literal not displayed */
	// 创建时间
	IssueAt time.Time `` /* 127-byte string literal not displayed */
	// 更新时间
	RefreshAt *time.Time `json:"refresh_at" gorm:"column:refresh_at;type:timestamp" description:"令牌刷新时间"`
	// 令牌状态
	Status *Status `json:"status" gorm:"embedded" modelDescription:"令牌状态"`
	// 其他扩展信息
	Extras map[string]string `json:"extras" gorm:"column:extras;serializer:json;type:json" description:"其他扩展信息"`
}

func GetTokenFromCtx

func GetTokenFromCtx(ctx context.Context) *Token

func NewToken

func NewToken() *Token

func (*Token) AccessTokenExpiredTTL

func (t *Token) AccessTokenExpiredTTL() int

获取访问令牌token的存活时间

func (*Token) CheckRefreshToken

func (t *Token) CheckRefreshToken(refreshtoken string) error

func (*Token) IsAccessTokenExpired

func (t *Token) IsAccessTokenExpired() error

检查访问令牌是否过期,如果没有设置过期时间就代表永不过期

func (*Token) IsRefreshTokenExpired

func (t *Token) IsRefreshTokenExpired() error

检查刷新令牌是否过期

func (*Token) Lock

func (t *Token) Lock(l LOCK_TYPE, reason string)

func (*Token) SetAccessTokenExpiredAt

func (t *Token) SetAccessTokenExpiredAt(v time.Time)

func (*Token) SetExpiredAtByDuration

func (t *Token) SetExpiredAtByDuration(duration time.Duration, refreshMulti uint)

刷新Token的过期时间,是一个系统配置,刷新token的时间一定是要>访问token的时间

func (*Token) SetIssuer

func (t *Token) SetIssuer(issuer string) *Token

func (*Token) SetRefreshAt

func (t *Token) SetRefreshAt(v time.Time)

func (*Token) SetRefreshTokenExpiredAt

func (t *Token) SetRefreshTokenExpiredAt(v time.Time)

func (*Token) SetSource

func (t *Token) SetSource(source SOURCE) *Token

func (*Token) String

func (t *Token) String() string

func (*Token) TableName

func (t *Token) TableName() string

gorm映射表名

func (*Token) UserIdString

func (t *Token) UserIdString() string

type VeliateTokenRequest

type VeliateTokenRequest struct {
	AccessToken string `json:"access_token"`
}

func NewValiateTokenRequest

func NewValiateTokenRequest(accessToken string) *VeliateTokenRequest

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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