69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
// Code scaffolded by goctl. Safe to edit.
|
|
// goctl 1.10.1
|
|
|
|
package auth
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"sundynix-micro-go/app/user/api/internal/svc"
|
|
"sundynix-micro-go/app/user/api/internal/types"
|
|
"sundynix-micro-go/app/user/rpc/user"
|
|
jwtUtil "sundynix-micro-go/common/utils/jwt"
|
|
|
|
jwtv5 "github.com/golang-jwt/jwt/v5"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type LoginLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
|
|
return &LoginLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err error) {
|
|
// 通过 user-rpc 查询用户 (account登录暂用GetUserByOpenId的方式,后续需补充account查询RPC)
|
|
// 这里简化处理:直接在api层查询并验证密码
|
|
// TODO: 后续应该在user-rpc中增加LoginByAccount方法
|
|
|
|
_ = req
|
|
_ = l
|
|
|
|
return nil, status.Error(codes.Unimplemented, "账号密码登录功能开发中")
|
|
}
|
|
|
|
// generateToken 生成JWT Token的辅助方法
|
|
func generateToken(config svc.ServiceContext, userInfo *user.UserInfo) (string, error) {
|
|
j := jwtUtil.NewJWT(config.Config.Auth.AccessSecret)
|
|
claims := jwtUtil.CustomClaims{
|
|
BaseClaims: jwtUtil.BaseClaims{
|
|
ID: userInfo.Id,
|
|
Account: userInfo.Account,
|
|
},
|
|
BufferTime: 3600,
|
|
RegisteredClaims: jwtv5.RegisteredClaims{
|
|
Audience: jwtv5.ClaimStrings{"sundynix"},
|
|
NotBefore: jwtv5.NewNumericDate(time.Now().Add(-1000)),
|
|
ExpiresAt: jwtv5.NewNumericDate(time.Now().Add(time.Duration(config.Config.Auth.AccessExpire) * time.Second)),
|
|
Issuer: "sundynix",
|
|
},
|
|
}
|
|
token, err := j.CreateToken(claims)
|
|
if err != nil {
|
|
return "", fmt.Errorf("生成Token失败: %w", err)
|
|
}
|
|
return token, nil
|
|
}
|