2025-10-02 06:43:57 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2025-10-06 13:14:58 +00:00
|
|
|
"backend/internal/domain"
|
|
|
|
"backend/pkg/permission/domain/entity"
|
2025-10-02 06:43:57 +00:00
|
|
|
"context"
|
2025-10-06 13:14:58 +00:00
|
|
|
"time"
|
2025-10-02 06:43:57 +00:00
|
|
|
|
|
|
|
"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
|
2025-10-02 06:43:57 +00:00
|
|
|
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-02 06:43:57 +00:00
|
|
|
|
2025-10-06 13:14:58 +00:00
|
|
|
return &types.RefreshTokenResp{
|
|
|
|
AccessToken: tk.Token,
|
|
|
|
RefreshToken: tk.OneTimeToken,
|
|
|
|
TokenType: tk.TokenType,
|
|
|
|
}, nil
|
2025-10-02 06:43:57 +00:00
|
|
|
}
|