659168fe32
- Implemented authz-adapter deployment and service for Envoy gRPC authorization. - Created PowerShell script to generate JWK for JWT authentication. - Documented the integration of ext_authz with user-rpc.ValidateToken in ENVOY_EXT_AUTHZ_ADAPTER.md. - Added comprehensive Envoy Gateway configuration guide with JWT authentication and access control in ENVOY_GATEWAY_GUIDE.md.
87 lines
2.2 KiB
Go
87 lines
2.2 KiB
Go
// Code scaffolded by goctl. Safe to edit.
|
|
// goctl 1.9.2
|
|
|
|
package user
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"juwan-backend/app/users/api/internal/contextx"
|
|
"regexp"
|
|
|
|
"juwan-backend/app/users/api/internal/svc"
|
|
"juwan-backend/app/users/api/internal/types"
|
|
"juwan-backend/app/users/rpc/pb"
|
|
"juwan-backend/app/users/rpc/usercenter"
|
|
"juwan-backend/common/utils"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type RegisterLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// 用户注册接口
|
|
func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
|
|
return &RegisterLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
var usernameRegex = regexp.MustCompile("^[a-zA-Z0-9_]+$")
|
|
|
|
func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterResp, err error) {
|
|
existingUser, err := l.svcCtx.UserRpc.GetUserByUsername(l.ctx, &pb.GetUserByUsernameReq{
|
|
Username: req.Username,
|
|
})
|
|
if len(req.Username) < 3 {
|
|
return nil, errors.New("username must be at least 3 characters long")
|
|
}
|
|
if len(req.Username) > 20 {
|
|
return nil, errors.New("username must be at most 20 characters long")
|
|
}
|
|
if !usernameRegex.MatchString(req.Username) {
|
|
return nil, errors.New("username can only contain letters, numbers, and underscores")
|
|
}
|
|
if err == nil && existingUser != nil {
|
|
return nil, errors.New("user already exists")
|
|
}
|
|
|
|
hashedPassword, err := utils.HashPassword(req.Password)
|
|
if err != nil {
|
|
return nil, errors.New("hash password failed")
|
|
}
|
|
|
|
requestId, err := contextx.RequestIdFrom(l.ctx)
|
|
if err != nil {
|
|
logx.Errorf("contextx.RequestIdFrom failed: %v", err)
|
|
return nil, errors.New("contextx.RequestIdFrom failed")
|
|
}
|
|
|
|
_, err = l.svcCtx.UserRpc.Register(l.ctx, &usercenter.RegisterReq{
|
|
Username: req.Username,
|
|
Passwd: hashedPassword,
|
|
Phone: req.Username,
|
|
Vcode: req.Vcode,
|
|
Email: req.Email,
|
|
RequestId: requestId,
|
|
})
|
|
if err != nil {
|
|
logx.Error("failed to register user: ", err)
|
|
return nil, errors.New("failed to register user")
|
|
}
|
|
|
|
// 返回响应
|
|
return &types.RegisterResp{
|
|
UserId: 0,
|
|
Username: req.Username,
|
|
Email: req.Email,
|
|
Message: "register success",
|
|
}, nil
|
|
}
|