2024-08-24 07:14:58 +00:00
|
|
|
package tokenservicelogic
|
|
|
|
|
|
|
|
import (
|
2024-08-24 14:40:09 +00:00
|
|
|
"app-cloudep-permission-server/internal/domain"
|
|
|
|
ers "code.30cm.net/digimon/library-go/errors"
|
2024-08-24 07:14:58 +00:00
|
|
|
"context"
|
|
|
|
|
|
|
|
"app-cloudep-permission-server/gen_result/pb/permission"
|
|
|
|
"app-cloudep-permission-server/internal/svc"
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GetUserTokensByUidLogic struct {
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
logx.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGetUserTokensByUidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserTokensByUidLogic {
|
|
|
|
return &GetUserTokensByUidLogic{
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-24 14:40:09 +00:00
|
|
|
type getUserTokensByUidReq struct {
|
|
|
|
UID string `json:"uid" validate:"required"`
|
|
|
|
}
|
|
|
|
|
2024-08-24 07:14:58 +00:00
|
|
|
// GetUserTokensByUid 取得目前所對應的 UID 所存在的 Tokens
|
|
|
|
func (l *GetUserTokensByUidLogic) GetUserTokensByUid(in *permission.QueryTokenByUIDReq) (*permission.Tokens, error) {
|
2024-08-24 14:40:09 +00:00
|
|
|
if err := l.svcCtx.Validate.ValidateAll(&getUserTokensByUidReq{
|
|
|
|
UID: in.GetUid(),
|
|
|
|
}); err != nil {
|
|
|
|
return nil, ers.InvalidFormat(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
uidTokens, err := l.svcCtx.TokenRedisRepo.GetAccessTokensByUID(l.ctx, in.GetUid())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens := make([]*permission.TokenResp, 0, len(uidTokens))
|
|
|
|
for _, v := range uidTokens {
|
|
|
|
tokens = append(tokens, &permission.TokenResp{
|
|
|
|
AccessToken: v.AccessToken,
|
|
|
|
TokenType: domain.TokenTypeBearer,
|
|
|
|
ExpiresIn: int32(v.ExpiresIn),
|
|
|
|
RefreshToken: v.RefreshToken,
|
|
|
|
})
|
|
|
|
}
|
2024-08-24 07:14:58 +00:00
|
|
|
|
2024-08-24 14:40:09 +00:00
|
|
|
return &permission.Tokens{
|
|
|
|
Token: tokens,
|
|
|
|
}, nil
|
2024-08-24 07:14:58 +00:00
|
|
|
}
|