package tokenservicelogic import ( "ark-permission/gen_result/pb/permission" "ark-permission/internal/svc" ers "code.30cm.net/wanderland/library-go/errors" "context" "github.com/zeromicro/go-zero/core/logx" ) type ValidationTokenLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewValidationTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ValidationTokenLogic { return &ValidationTokenLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } type refreshTokenReq struct { Token string `json:"token" validate:"required"` } // ValidationToken 驗證這個 Token 有沒有效 func (l *ValidationTokenLogic) ValidationToken(in *permission.ValidationTokenReq) (*permission.ValidationTokenResp, error) { // 驗證所需 if err := l.svcCtx.Validate.ValidateAll(&refreshTokenReq{ Token: in.GetToken(), }); err != nil { return nil, ers.InvalidFormat(err.Error()) } claims, err := parseClaims(in.GetToken(), l.svcCtx.Config.Token.Secret, true) if err != nil { logx.WithCallerSkip(1).WithFields( logx.Field("func", "parseClaims"), ).Info(err.Error()) return nil, err } token, err := l.svcCtx.TokenRedisRepo.GetAccessTokenByID(l.ctx, claims.ID()) if err != nil { logx.WithCallerSkip(1).WithFields( logx.Field("func", "TokenRedisRepo.GetByAccess"), logx.Field("claims", claims), ).Error(err.Error()) return nil, err } return &permission.ValidationTokenResp{ Token: &permission.Token{ Id: token.ID, Uid: token.UID, DeviceId: token.DeviceID, AccessCreateAt: token.AccessCreateAt.Unix(), AccessToken: token.AccessToken, ExpiresIn: int32(token.ExpiresIn), RefreshToken: token.RefreshToken, RefreshExpiresIn: int32(token.RefreshExpiresIn), RefreshCreateAt: token.RefreshCreateAt.Unix(), }, Data: claims, }, nil }