mingyang-admin-iot-app/rpc/internal/logic/auth/auth_token_logic.go

69 lines
1.6 KiB
Go

package auth
import (
"context"
"google.golang.org/protobuf/types/known/timestamppb"
"mingyang-admin-app-rpc/internal/jwt_manager"
"mingyang-admin-app-rpc/internal/svc"
"mingyang-admin-app-rpc/types/app"
"github.com/zeromicro/go-zero/core/logx"
)
type TokenLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
jwtManager *jwt_manager.JWTManager
}
func NewAuthTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TokenLogic {
return &TokenLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
jwtManager: jwt_manager.NewJWTManager(&svcCtx.Config.JWTConf),
}
}
// AuthToken 校验token
func (l *TokenLogic) AuthToken(in *app.AuthReq) (*app.AuthInfoResp, error) {
token, err := l.jwtManager.VerifyAccessToken(in.GetToken())
if err != nil {
logx.Errorf("verify access token failed: %v", err)
return nil, err
}
return &app.AuthInfoResp{
UserId: token.UserID,
Type: token.Type,
Claims: &app.RegisteredClaims{
IssuedAt: &app.NumericDate{
Timestamp: &timestamppb.Timestamp{
Seconds: token.IssuedAt.Unix(), // 转换为 Unix 时间戳
Nanos: 0,
},
},
ExpiresAt: &app.NumericDate{
Timestamp: &timestamppb.Timestamp{
Seconds: token.ExpiresAt.Unix(), // 转换为 Unix 时间戳
Nanos: 0,
},
},
Issuer: token.Issuer,
Subject: token.Subject,
Audience: &app.ClaimStrings{
Values: token.Audience,
},
NotBefore: &app.NumericDate{
Timestamp: &timestamppb.Timestamp{
Seconds: token.NotBefore.Unix(), // 转换为 Unix 时间戳
Nanos: 0,
},
},
Id: token.ID,
},
}, nil
}