补充Token
This commit is contained in:
parent
76fd1c6f3e
commit
cd640a3a9b
@ -1,21 +1,42 @@
|
||||
package token
|
||||
|
||||
import "context"
|
||||
|
||||
// 业务域
|
||||
type Service interface {
|
||||
Outer
|
||||
Innter
|
||||
UserService
|
||||
InnterService
|
||||
}
|
||||
|
||||
// 1. 外部
|
||||
type Outer interface {
|
||||
type UserService interface {
|
||||
// 颁发令牌 登录
|
||||
IssueToken()
|
||||
IssueToken(context.Context, *IssueTokenRequest) (*Token, error)
|
||||
// 撤销令牌 退出
|
||||
RevolkToken()
|
||||
RevolkToken(context.Context, *RevolkTokenRequest) (*Token, error)
|
||||
}
|
||||
|
||||
type RevolkTokenRequest struct {
|
||||
// 访问令牌
|
||||
AccessToken string `json:"access_token"`
|
||||
// 刷新令牌, 构成一对,避免AccessToken 泄露,用户可以直接 revolk
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
}
|
||||
|
||||
type IssueTokenRequest struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
// 记住我, Token可能1天过期, 过去时间调整为7天
|
||||
RememberMe bool `json:"remember_me"`
|
||||
}
|
||||
|
||||
// 内部
|
||||
type Innter interface {
|
||||
type InnterService interface {
|
||||
// 令牌校验
|
||||
ValidateToken()
|
||||
ValidateToken(context.Context, *ValidateTokenRequest) (*Token, error)
|
||||
}
|
||||
|
||||
type ValidateTokenRequest struct {
|
||||
// 访问令牌
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
||||
|
29
vblog/apps/token/model.go
Normal file
29
vblog/apps/token/model.go
Normal file
@ -0,0 +1,29 @@
|
||||
package token
|
||||
|
||||
import "time"
|
||||
|
||||
// 用户的身份令牌
|
||||
type Token struct {
|
||||
// TokenId
|
||||
Id uint `json:"id" gorm:"primaryKey;column:id"`
|
||||
// 用户Id
|
||||
RefUserId string `json:"ref_user_id" gorm:"column:ref_user_id"`
|
||||
// 访问令牌
|
||||
AccessToken string `json:"access_token" gorm:"column:access_token;unique;index"`
|
||||
// 访问Token过期时间
|
||||
AccessTokenExpireAt *time.Time `json:"access_token_expire_at" gorm:"column:access_token_expire_at"`
|
||||
// 令牌办法的时间
|
||||
IssueAt time.Time `json:"issue_at" gorm:"column:issue_at"`
|
||||
|
||||
// 刷新Token
|
||||
RefreshToken string `json:"refresh_token" gorm:"column:refresh_token;unique;index"`
|
||||
// 刷新Token过期时间
|
||||
RefreshTokenExpireAt *time.Time `json:"refresh_token_expire_at" gorm:"column:refresh_token_expire_at"`
|
||||
|
||||
// 关联查询 需要查询出来
|
||||
RefUserName string `json:"ref_user_name" gorm:"-"`
|
||||
}
|
||||
|
||||
func (t *Token) TableName() string {
|
||||
return "tokens"
|
||||
}
|
@ -3,6 +3,16 @@ package user
|
||||
import "context"
|
||||
|
||||
type Service interface {
|
||||
AdminService
|
||||
UserService
|
||||
}
|
||||
|
||||
type AdminService interface {
|
||||
// 更新用户状态
|
||||
UpdateUserStatus(context.Context, *UpdateUserStatusRequest) (*User, error)
|
||||
}
|
||||
|
||||
type UserService interface {
|
||||
// 注册
|
||||
Registry(context.Context, *RegistryRequest) (*User, error)
|
||||
|
||||
@ -18,6 +28,13 @@ type Service interface {
|
||||
UnRegistry(context.Context, *UnRegistryRequest)
|
||||
}
|
||||
|
||||
type UpdateUserStatusRequest struct {
|
||||
// 用户Id
|
||||
UserId string `json:"user_id"`
|
||||
//
|
||||
Status
|
||||
}
|
||||
|
||||
type UpdateProfileRequest struct {
|
||||
// 用户Id
|
||||
UserId string `json:"user_id"`
|
||||
|
@ -1,6 +1,10 @@
|
||||
package user
|
||||
|
||||
import "gitlab.com/go-course-project/go17/vblog/utils"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gitlab.com/go-course-project/go17/vblog/utils"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
// 存放到数据里的对象的远数据信息
|
||||
@ -16,6 +20,8 @@ type RegistryRequest struct {
|
||||
Password string `json:"password" gorm:"column:username;type:varchar(255)"`
|
||||
// Profile 信息
|
||||
Profile
|
||||
// 用户状态
|
||||
Status
|
||||
}
|
||||
|
||||
type Profile struct {
|
||||
@ -27,6 +33,17 @@ type Profile struct {
|
||||
Email string `json:"email" gorm:"column:email;type:varchar(100)"`
|
||||
}
|
||||
|
||||
type Status struct {
|
||||
// 冻结时间
|
||||
BlockAt *time.Time `json:"block_at" gorm:"column:block_at"`
|
||||
// 冻结原因
|
||||
BlockReason string `json:"block_reason" gorm:"column:block_reason;type:text"`
|
||||
}
|
||||
|
||||
func (s *Status) IsBlocked() bool {
|
||||
return s.BlockAt != nil
|
||||
}
|
||||
|
||||
func (u *User) TableName() string {
|
||||
return "users"
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
<mxfile host="65bd71144e">
|
||||
<diagram id="G4rBrXM05HFn1dIN60R_" name="第 1 页">
|
||||
<mxGraphModel dx="893" dy="570" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||
<mxGraphModel dx="893" dy="449" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||
<root>
|
||||
<mxCell id="0"/>
|
||||
<mxCell id="1" parent="0"/>
|
||||
|
46
vblog/docs/refresh_token.drawio
Normal file
46
vblog/docs/refresh_token.drawio
Normal file
@ -0,0 +1,46 @@
|
||||
<mxfile host="65bd71144e">
|
||||
<diagram id="eCBvwoA0ZZQTiMH4xRy2" name="第 1 页">
|
||||
<mxGraphModel dx="893" dy="449" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||
<root>
|
||||
<mxCell id="0"/>
|
||||
<mxCell id="1" parent="0"/>
|
||||
<mxCell id="2" value="1h" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="70" y="210" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="3" value="1h" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="190" y="210" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="4" value="1h" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="310" y="210" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="5" value="4h" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="70" y="320" width="480" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="6" value="1h" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="430" y="210" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="8" value="4h" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="560" y="320" width="250" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="9" value="1h" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="550" y="210" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="10" value="1h" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="670" y="210" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="11" value="回话保持" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="70" y="160" width="60" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="12" value="1h" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="70" y="440" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="13" value="4h" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="70" y="540" width="480" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="14" value="1h<br>重新登录" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="560" y="450" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
</root>
|
||||
</mxGraphModel>
|
||||
</diagram>
|
||||
</mxfile>
|
Loading…
x
Reference in New Issue
Block a user