Documentation
¶
Index ¶
- Constants
- Variables
- func GetAccessTokenFromHTTP(r *http.Request) string
- func GetIssueParameterValue[T any](p IssueParameter, key string) T
- func GetRefreshTokenFromHTTP(r *http.Request) string
- func MakeBearer(length int) string
- func RegistryIssuer(name string, p Issuer)
- type DescribeBy
- type DescribeTokenRequest
- type IssueParameter
- func (p IssueParameter) AccessToken() string
- func (p IssueParameter) ExpireTTL() time.Duration
- func (p IssueParameter) Password() string
- func (p IssueParameter) SetAccessToken(v string) IssueParameter
- func (p IssueParameter) SetExpireTTL(v int64) IssueParameter
- func (p IssueParameter) SetPassword(v string) IssueParameter
- func (p IssueParameter) SetUsername(v string) IssueParameter
- func (p IssueParameter) Username() string
- type IssueTokenRequest
- type Issuer
- type LockType
- type QueryTokenRequest
- type RevokeTokenRequest
- type SOURCE
- type Service
- type Status
- type Token
- func (t *Token) AccessTokenExpiredTTL() int
- func (t *Token) CheckRefreshToken(refreshToken string) error
- func (t *Token) IsAccessTokenExpired() error
- func (t *Token) IsRefreshTokenExpired() error
- func (t *Token) Lock(l LockType, reason string)
- func (t *Token) SetAccessTokenExpiredAt(v time.Time)
- func (t *Token) SetExpiredAtByDuration(duration time.Duration, refreshMulti uint)
- func (t *Token) SetIssuer(issuer string) *Token
- func (t *Token) SetRefreshAt(v time.Time)
- func (t *Token) SetRefreshTokenExpiredAt(v time.Time)
- func (t *Token) SetSource(source SOURCE) *Token
- func (t *Token) String() string
- func (t *Token) TableName() string
- func (t *Token) UserIdString() string
- type ValidateTokenRequest
Constants ¶
View Source
const ( AccessTokenHeaderName = "Authorization" AccessTokenCookieName = "access_token" AccessTokenResponseHeaderName = "X-OAUTH-TOKEN" RefreshTokenHeaderName = "X-REFRESH-TOKEN" )
View Source
const ( IssuerLDAP = "ldap" IssuerFEISHU = "feishu" IssuerPassword = "password" IssuerPrivateToken = "private_token" )
颁发器的类型
View Source
const (
AppName = "token"
)
Variables ¶
View Source
var (
CookieNotFound = exception.NewUnauthorized("cookie %s not found", AccessTokenCookieName)
)
View Source
var (
CtxTokenKey = tokenContextKey{}
)
Functions ¶
func GetAccessTokenFromHTTP ¶
GetAccessTokenFromHTTP 从 http 请求头中获取 token 信息
func GetIssueParameterValue ¶
func GetIssueParameterValue[T any](p IssueParameter, key string) T
func GetRefreshTokenFromHTTP ¶
GetRefreshTokenFromHTTP 从 http 请求头中获取刷新 token
Types ¶
type DescribeTokenRequest ¶
type DescribeTokenRequest struct {
DescribeBy DescribeBy `json:"describe_by"`
DescribeValue string `json:"describe_value"`
}
func NewDescribeTokenRequest ¶
func NewDescribeTokenRequest(accessToken string) *DescribeTokenRequest
type IssueParameter ¶
func NewIssueParameter ¶
func NewIssueParameter() 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 {
// Source 端类型
Source SOURCE `json:"source"`
// Issuer 认证方式
Issuer string `json:"issuer"`
// Parameter 参数
Parameter IssueParameter `json:"parameter"`
}
IssueTokenRequest 用户的身份的凭证,用于换取token
func NewIssueTokenRequest ¶
func NewIssueTokenRequest() *IssueTokenRequest
func (*IssueTokenRequest) IssueByPassword ¶
func (i *IssueTokenRequest) IssueByPassword(username, password string)
type Issuer ¶
type Issuer interface {
// IssueToken 颁发 token 的接口
IssueToken(context.Context, IssueParameter) (*Token, error)
}
Issuer 颁发器必须实现的接口
type QueryTokenRequest ¶
type QueryTokenRequest struct {
*request.PageRequest
Active *bool `json:"active"` // 当前可用的没过期的 token
Source *SOURCE `json:"source"` // 用户来源
UserIds []uint64 `json:"user_ids"` // user_ids
}
func NewQueryTokenRequest ¶
func NewQueryTokenRequest() *QueryTokenRequest
func (*QueryTokenRequest) AddUserId ¶
func (r *QueryTokenRequest) AddUserId(userIds ...uint64) *QueryTokenRequest
func (*QueryTokenRequest) SetActive ¶
func (r *QueryTokenRequest) SetActive(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 string, rt string) *RevokeTokenRequest
type Service ¶
type Service interface {
// IssueToken 颁发令牌: Login
IssueToken(context.Context, *IssueTokenRequest) (*Token, error)
// RevokeToken 撤销令牌: Logout
RevokeToken(context.Context, *RevokeTokenRequest) (*Token, error)
// ValidateToken 验证令牌: 检查令牌的合法性,是否伪造
ValidateToken(context.Context, *ValidateTokenRequest) (*Token, error)
// QueryToken 查询已经颁发出去的 token
QueryToken(context.Context, *QueryTokenRequest) (*types.Set[*Token], error)
DescribeToken(context.Context, *DescribeTokenRequest) (*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:"冻结时间"`
// 冻结类型
LockType LockType `` /* 203-byte string literal not displayed */
// 冻结原因
LockReason string `json:"lock_reason" bson:"lock_reason" gorm:"column:lock_reason;type:text" description:"冻结原因"`
}
type Token ¶
type Token struct {
Id uint64 `json:"id" gorm:"column:id;type:uint;primaryKey"`
// 用户来源
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(100);not null;index" description:"持有该Token的用户名称"`
// 是不是管理员
IsAdmin bool `json:"is_admin" gorm:"column:is_admin;type:tinyint(1)" description:"是不是管理员"`
// 令牌生效范围
policy.ResourceScope
// 令牌生效空间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 ¶
GetTokenFromCtx 从 context 上下文中获取 token 信息
func (*Token) AccessTokenExpiredTTL ¶
func (*Token) CheckRefreshToken ¶
func (*Token) IsAccessTokenExpired ¶
IsAccessTokenExpired 判断访问令牌是否过期,没设置代表永不过期
func (*Token) IsRefreshTokenExpired ¶
IsRefreshTokenExpired 判断刷新 token 是否过期
func (*Token) SetAccessTokenExpiredAt ¶
func (*Token) SetExpiredAtByDuration ¶
func (*Token) SetRefreshAt ¶
func (*Token) SetRefreshTokenExpiredAt ¶
func (*Token) UserIdString ¶
type ValidateTokenRequest ¶
type ValidateTokenRequest struct {
AccessToken string `json:"access_token"`
}
func NewValidateTokenRequest ¶
func NewValidateTokenRequest(accessToken string) *ValidateTokenRequest
Click to show internal directories.
Click to hide internal directories.