From cd640a3a9b9ebedb030d9a6c29da6eb8a9c9bf56 Mon Sep 17 00:00:00 2001 From: yumaojun03 <719118794@qq.com> Date: Sun, 1 Dec 2024 14:54:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85Token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vblog/apps/token/interface.go | 35 ++++++++++++++++++++----- vblog/apps/token/model.go | 29 +++++++++++++++++++++ vblog/apps/user/interface.go | 17 ++++++++++++ vblog/apps/user/model.go | 19 +++++++++++++- vblog/docs/arch.drawio | 2 +- vblog/docs/refresh_token.drawio | 46 +++++++++++++++++++++++++++++++++ 6 files changed, 139 insertions(+), 9 deletions(-) create mode 100644 vblog/apps/token/model.go create mode 100644 vblog/docs/refresh_token.drawio diff --git a/vblog/apps/token/interface.go b/vblog/apps/token/interface.go index 0f50e7b..e7fa75b 100644 --- a/vblog/apps/token/interface.go +++ b/vblog/apps/token/interface.go @@ -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"` } diff --git a/vblog/apps/token/model.go b/vblog/apps/token/model.go new file mode 100644 index 0000000..bdc49c2 --- /dev/null +++ b/vblog/apps/token/model.go @@ -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" +} diff --git a/vblog/apps/user/interface.go b/vblog/apps/user/interface.go index 8c32d6e..e57b824 100644 --- a/vblog/apps/user/interface.go +++ b/vblog/apps/user/interface.go @@ -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"` diff --git a/vblog/apps/user/model.go b/vblog/apps/user/model.go index cda18d6..4197f12 100644 --- a/vblog/apps/user/model.go +++ b/vblog/apps/user/model.go @@ -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" } diff --git a/vblog/docs/arch.drawio b/vblog/docs/arch.drawio index 1027152..8e25d38 100644 --- a/vblog/docs/arch.drawio +++ b/vblog/docs/arch.drawio @@ -1,6 +1,6 @@ - + diff --git a/vblog/docs/refresh_token.drawio b/vblog/docs/refresh_token.drawio new file mode 100644 index 0000000..cf4cf70 --- /dev/null +++ b/vblog/docs/refresh_token.drawio @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file