2024-12-01 18:04:02 +08:00
|
|
|
package impl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-12-08 11:59:36 +08:00
|
|
|
"fmt"
|
2024-12-01 18:04:02 +08:00
|
|
|
|
2024-12-08 11:59:36 +08:00
|
|
|
"github.com/infraboard/mcube/v2/exception"
|
|
|
|
"github.com/infraboard/mcube/v2/ioc/config/datasource"
|
2024-12-01 18:04:02 +08:00
|
|
|
"gitlab.com/go-course-project/go17/vblog/apps/token"
|
2024-12-08 11:59:36 +08:00
|
|
|
"gitlab.com/go-course-project/go17/vblog/apps/user"
|
|
|
|
"gitlab.com/go-course-project/go17/vblog/apps/user/impl"
|
2024-12-01 18:04:02 +08:00
|
|
|
)
|
|
|
|
|
2024-12-08 11:59:36 +08:00
|
|
|
var TokenService token.Service = &TokenServiceImpl{
|
|
|
|
user: impl.UserService,
|
|
|
|
}
|
2024-12-01 18:04:02 +08:00
|
|
|
|
2024-12-15 10:13:35 +08:00
|
|
|
func NewTokenService(user user.AdminService) token.Service {
|
|
|
|
return &TokenServiceImpl{
|
|
|
|
user: impl.UserService,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-01 18:04:02 +08:00
|
|
|
// 定义一个struct, 用于实现 UserService就是刚才定义的接口
|
|
|
|
// 怎么才能判断这个结构体没有实现这个接口
|
|
|
|
type TokenServiceImpl struct {
|
2024-12-08 11:59:36 +08:00
|
|
|
// user service
|
|
|
|
user user.AdminService
|
2024-12-01 18:04:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IssueToken implements token.Service.
|
2024-12-08 11:59:36 +08:00
|
|
|
func (t *TokenServiceImpl) IssueToken(ctx context.Context, in *token.IssueTokenRequest) (*token.Token, error) {
|
|
|
|
if err := in.Validate(); err != nil {
|
|
|
|
return nil, exception.NewBadRequest("参数校验异常: %s", err)
|
|
|
|
}
|
|
|
|
// 1. 查询用户
|
|
|
|
u, err := t.user.DescribeUser(ctx, &user.DescribeUserRequest{
|
|
|
|
DescribeBy: user.DESCRIBE_BY_USERNAME,
|
|
|
|
Value: in.Username,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. 比对密码
|
|
|
|
if err := u.CheckPassword(in.Password); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. 颁发Token
|
|
|
|
tk := token.NewToken(fmt.Sprintf("%d", u.Id)).SetRefUserName(u.Username)
|
|
|
|
|
|
|
|
if err := datasource.DBFromCtx(ctx).Create(tk).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tk, nil
|
2024-12-01 18:04:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RevolkToken implements token.Service.
|
|
|
|
func (t *TokenServiceImpl) RevolkToken(context.Context, *token.RevolkTokenRequest) (*token.Token, error) {
|
|
|
|
panic("unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateToken implements token.Service.
|
2024-12-08 11:59:36 +08:00
|
|
|
// 1. 这个令牌是不是我们发
|
|
|
|
// 2. 这个令牌有没有过期
|
|
|
|
func (t *TokenServiceImpl) ValidateToken(ctx context.Context, in *token.ValidateTokenRequest) (*token.Token, error) {
|
|
|
|
tk := &token.Token{}
|
|
|
|
if err := datasource.DBFromCtx(ctx).Where("access_token = ?", in.AccessToken).Take(tk).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tk.IsAccessTokenExpired(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-12-08 18:10:55 +08:00
|
|
|
// 1. 补充用户信息
|
|
|
|
u, err := t.user.DescribeUser(ctx, &user.DescribeUserRequest{
|
|
|
|
DescribeBy: user.DESCRIBE_BY_ID,
|
|
|
|
Value: tk.RefUserId,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tk.RefUserName = u.Username
|
2024-12-08 11:59:36 +08:00
|
|
|
return tk, nil
|
2024-12-01 18:04:02 +08:00
|
|
|
}
|