token

package
v0.0.0-...-54787ef Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

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 GetAccessTokenFromHTTP(r *http.Request) string

func GetRefreshTokenFromHTTP

func GetRefreshTokenFromHTTP(r *http.Request) string

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

type LoginRequest struct {
	Username string
	Password string
}

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:"-"`
}

func NewToken

func NewToken() *Token

func (*Token) ExpiredTime

func (t *Token) ExpiredTime() time.Time

计算Token的过期时间

func (*Token) IsExpired

func (t *Token) IsExpired() error

func (*Token) String

func (u *Token) String() string

func (*Token) TableName

func (t *Token) TableName() string

type ValiateToken

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

func NewValiateToken

func NewValiateToken(at string) *ValiateToken

Directories

Path Synopsis
api
gin
impl

Jump to

Keyboard shortcuts

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