Documentation ¶
Index ¶
- Constants
- Variables
- func GetAccessTokenFromHTTP(r *http.Request) string
- func GetRefreshTokenFromHTTP(r *http.Request) string
- func NewAuthFailed(format string, a ...any) *exception.APIException
- func NewPermissionDeny(format string, a ...any) *exception.APIException
- func NewTokenExpired(format string, a ...any) *exception.APIException
- type LoginRequest
- type LogoutRequest
- type Service
- type Token
- type ValiateToken
Constants ¶
View Source
const ( ACCESS_TOKEN_HEADER_NAME = "Authorization" ACCESS_TOKEN_COOKIE_NAME = "access_token" ACCESS_TOKEN_GIN_KEY_NAME = "access_token" ACCESS_TOKEN_RESPONSE_HEADER_NAME = "X-OAUTH-TOKEN" REFRESH_TOKEN_HEADER_NAME = "X-REFRUSH-TOKEN" )
View Source
const (
AppName = "tokens"
)
Variables ¶
View Source
var (
CookieNotFound = NewAuthFailed("cookie %s not found", ACCESS_TOKEN_COOKIE_NAME)
)
Functions ¶
func GetAccessTokenFromHTTP ¶
func GetRefreshTokenFromHTTP ¶
func NewAuthFailed ¶
func NewAuthFailed(format string, a ...any) *exception.APIException
func NewPermissionDeny ¶
func NewPermissionDeny(format string, a ...any) *exception.APIException
func NewTokenExpired ¶
func NewTokenExpired(format string, a ...any) *exception.APIException
Types ¶
type LoginRequest ¶
func NewLoginRequest ¶
func NewLoginRequest() *LoginRequest
type LogoutRequest ¶
type LogoutRequest struct { AccessToken string `json:"access_token"` RefreshToken string `json:"refresh_token"` }
万一的Token泄露, 不知道refresh_token,也没法推出
func NewLogoutRequest ¶
func NewLogoutRequest(at, rk string) *LogoutRequest
type Service ¶
type Service interface { // 登录接口(颁发Token) Login(context.Context, *LoginRequest) (*Token, error) // 退出接口(销毁Token) Logout(context.Context, *LogoutRequest) (*Token, error) // 校验Token 是给内部中间层使用 身份校验层 // 校验完后返回Token, 通过Token获取 用户信息 ValiateToken(context.Context, *ValiateToken) (*Token, error) }
type Token ¶
type Token struct { // 该Token是颁发 UserId string `json:"user_id"` // 人的名称, user_name UserName string `json:"username" gorm:"column:username"` // 办法给用户的访问令牌(用户需要携带Token来访问接口) AccessToken string `json:"access_token"` // 过期时间(2h), 单位是秒 AccessTokenExpiredAt int `json:"access_token_expired_at"` // 刷新Token RefreshToken string `json:"refresh_token"` // 刷新Token过期时间(7d) RefreshTokenExpiredAt int `json:"refresh_token_expired_at"` // 创建时间 CreatedAt int64 `json:"created_at"` // 更新实现 UpdatedAt int64 `json:"updated_at"` // 额外补充信息, gorm忽略处理 Role user.Role `json:"role" gorm:"-"` }
type ValiateToken ¶
type ValiateToken struct {
AccessToken string `json:"access_token"`
}
func NewValiateToken ¶
func NewValiateToken(at string) *ValiateToken
Click to show internal directories.
Click to hide internal directories.