Documentation ¶
Index ¶
Constants ¶
View Source
const ( TOKEN_HEADER_KEY = "Authorization" TOKEN_COOKIE_KEY = "token" TOKEN_MIDDLEWARE_KEY = "token" )
View Source
const ( DEFAULT_EXPIRED_AT = 2 * 60 * 60 WEEK_EXPIRED_AT = 7 * 24 * 60 * 60 )
View Source
const (
// 模块名称
AppName = "tokens"
)
Variables ¶
View Source
var ( ErrAccessTokenExpired = exception.NewAPIException(5000, "access token expired") ErrRefreshTokenExpired = exception.NewAPIException(5001, "refresh token expired") )
定义token模块的业务异常 token expired %f minitues 约定俗成: ErrXXXXX 来进行自定义异常定义, 方便快速在包里搜索
Functions ¶
func GetAccessTokenFromHttp ¶
Types ¶
type IssueTokenRequest ¶
type IssueTokenRequest struct { Username string `json:"username"` Password string `json:"password"` // 延长Token的有效性为1周 RemindMe bool `json:"remind_me"` }
颁发令牌的请求
func NewIssueTokenRequest ¶
func NewIssueTokenRequest(username, password string) *IssueTokenRequest
IssueTokenRequest的构造函数
type RevokeTokenRequest ¶
type RevokeTokenRequest struct { // 颁发给用户的访问令牌(用户需要携带Token来访问接口) AccessToken string // 他们配对的, 撤销时 需要验证他们是不是一对 RefreshToken string }
撤销令牌的请求
func NewRevokeTokenRequest ¶
func NewRevokeTokenRequest(accessToken, refreshToken string) *RevokeTokenRequest
RevokeTokenRequest构造函数
type Service ¶
type Service interface { // 登录: 颁发令牌 IssueToken(context.Context, *IssueTokenRequest) (*Token, error) // 退出: 撤销令牌 RevokeToken(context.Context, *RevokeTokenRequest) (*Token, error) // 校验令牌 ValidateToken(context.Context, *ValidateTokenRequest) (*Token, error) }
Token Service接口定义
type Token ¶
type Token struct { // 该Token是颁发 UserId string `json:"user_id" gorm:"column:user_id"` // 人的名称, user_name UserName string `json:"username" gorm:"column:username"` // 办法给用户的访问令牌(用户需要携带Token来访问接口) AccessToken string `json:"access_token" gorm:"column:access_token"` // 过期时间(2h), 单位是秒 AccessTokenExpiredAt int `json:"access_token_expired_at" gorm:"column:access_token_expired_at"` // 刷新Token RefreshToken string `json:"refresh_token" gorm:"column:refresh_token"` // 刷新Token过期时间(7d) RefreshTokenExpiredAt int `json:"refresh_token_expired_at" gorm:"column:refresh_token_expired_at"` // 创建时间 CreatedAt int64 `json:"created_at" gorm:"column:created_at"` // 更新实现 UpdatedAt int64 `json:"updated_at" gorm:"column:updated_at"` // 额外补充信息, gorm忽略处理 Role user.Role `json:"role" gorm:"-"` }
func (*Token) CheckRefreshToken ¶
核对refresh token
func (*Token) ValidateExpried ¶
验证token是否过期 1. access token过期 refresh token过期
type ValidateTokenRequest ¶
type ValidateTokenRequest struct {
AccessToken string
}
func NewValidateTokenRequest ¶
func NewValidateTokenRequest(accessToken string) *ValidateTokenRequest
ValidateTokenRequest构造函数
Click to show internal directories.
Click to hide internal directories.