backend/internal/logic/auth/refresh_token_logic.go

52 lines
1.2 KiB
Go
Raw Normal View History

package auth
import (
2025-10-06 13:14:58 +00:00
"backend/internal/domain"
"backend/pkg/permission/domain/entity"
"context"
2025-10-06 13:14:58 +00:00
"time"
"backend/internal/svc"
"backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type RefreshTokenLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
2025-10-06 13:14:58 +00:00
// NewRefreshTokenLogic 刷新 Access Token
func NewRefreshTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RefreshTokenLogic {
return &RefreshTokenLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *RefreshTokenLogic) RefreshToken(req *types.RefreshTokenReq) (resp *types.RefreshTokenResp, err error) {
2025-10-06 13:14:58 +00:00
data, err := l.svcCtx.TokenUC.ReadTokenBasicData(l.ctx, req.AccessToken)
if err != nil {
return nil, err
}
tk, err := l.svcCtx.TokenUC.RefreshToken(l.ctx, entity.RefreshTokenReq{
Token: req.RefreshToken,
Scope: domain.DefaultScope,
Expires: time.Now().UTC().Add(l.svcCtx.Config.Token.RefreshTokenExpiry).Unix(),
DeviceID: data["uid"],
})
if err != nil {
return nil, err
}
2025-10-06 13:14:58 +00:00
return &types.RefreshTokenResp{
AccessToken: tk.Token,
RefreshToken: tk.OneTimeToken,
TokenType: tk.TokenType,
}, nil
}