153 lines
6.1 KiB
Go
153 lines
6.1 KiB
Go
package grpc
|
|
|
|
import (
|
|
"context"
|
|
|
|
userv1 "hyapp/api/proto/user/v1"
|
|
"hyapp/pkg/xerr"
|
|
authservice "hyapp/services/user-service/internal/service/auth"
|
|
userservice "hyapp/services/user-service/internal/service/user"
|
|
)
|
|
|
|
// Server 把 user-service 用例层适配为 protobuf gRPC 接口。
|
|
type Server struct {
|
|
userv1.UnimplementedAuthServiceServer
|
|
userv1.UnimplementedUserServiceServer
|
|
userv1.UnimplementedUserIdentityServiceServer
|
|
|
|
authSvc *authservice.Service
|
|
userSvc *userservice.Service
|
|
}
|
|
|
|
// NewServer 创建 user-service gRPC server。
|
|
func NewServer(authSvc *authservice.Service, userSvc *userservice.Service) *Server {
|
|
return &Server{
|
|
authSvc: authSvc,
|
|
userSvc: userSvc,
|
|
}
|
|
}
|
|
|
|
// LoginPassword 统一把短号不存在、未设置密码和密码错误留给 service 层映射为 AUTH_FAILED。
|
|
func (s *Server) LoginPassword(ctx context.Context, req *userv1.LoginPasswordRequest) (*userv1.AuthResponse, error) {
|
|
token, err := s.authSvc.LoginPassword(ctx, req.GetDisplayUserId(), req.GetPassword(), req.GetMeta().GetDeviceId(), authMeta(req.GetMeta()))
|
|
if err != nil {
|
|
return nil, xerr.ToGRPCError(err)
|
|
}
|
|
|
|
return &userv1.AuthResponse{Token: toProtoToken(token)}, nil
|
|
}
|
|
|
|
// LoginThirdParty 只接受 provider 凭证,不在 gateway 泄漏 provider secret。
|
|
func (s *Server) LoginThirdParty(ctx context.Context, req *userv1.LoginThirdPartyRequest) (*userv1.AuthResponse, error) {
|
|
token, isNewUser, err := s.authSvc.LoginThirdParty(ctx, req.GetProvider(), req.GetCredential(), req.GetMeta().GetDeviceId(), authMeta(req.GetMeta()))
|
|
if err != nil {
|
|
return nil, xerr.ToGRPCError(err)
|
|
}
|
|
|
|
return &userv1.AuthResponse{Token: toProtoToken(token), IsNewUser: isNewUser}, nil
|
|
}
|
|
|
|
// SetPassword 为已登录用户首次写入密码身份。
|
|
func (s *Server) SetPassword(ctx context.Context, req *userv1.SetPasswordRequest) (*userv1.SetPasswordResponse, error) {
|
|
if err := s.authSvc.SetPassword(ctx, req.GetUserId(), req.GetPassword(), authMeta(req.GetMeta())); err != nil {
|
|
return nil, xerr.ToGRPCError(err)
|
|
}
|
|
|
|
return &userv1.SetPasswordResponse{PasswordSet: true}, nil
|
|
}
|
|
|
|
// RefreshToken 执行 refresh token 轮换。
|
|
func (s *Server) RefreshToken(ctx context.Context, req *userv1.RefreshTokenRequest) (*userv1.RefreshTokenResponse, error) {
|
|
token, err := s.authSvc.RefreshToken(ctx, req.GetRefreshToken(), req.GetMeta().GetDeviceId(), authMeta(req.GetMeta()))
|
|
if err != nil {
|
|
return nil, xerr.ToGRPCError(err)
|
|
}
|
|
|
|
return &userv1.RefreshTokenResponse{Token: toProtoToken(token)}, nil
|
|
}
|
|
|
|
// Logout 失效指定会话或 refresh token。
|
|
func (s *Server) Logout(ctx context.Context, req *userv1.LogoutRequest) (*userv1.LogoutResponse, error) {
|
|
revoked, err := s.authSvc.Logout(ctx, req.GetSessionId(), req.GetRefreshToken(), authMeta(req.GetMeta()))
|
|
if err != nil {
|
|
return nil, xerr.ToGRPCError(err)
|
|
}
|
|
|
|
return &userv1.LogoutResponse{Revoked: revoked}, nil
|
|
}
|
|
|
|
// GetUser 返回用户主状态。
|
|
func (s *Server) GetUser(ctx context.Context, req *userv1.GetUserRequest) (*userv1.GetUserResponse, error) {
|
|
user, err := s.userSvc.GetUser(ctx, req.GetUserId())
|
|
if err != nil {
|
|
return nil, xerr.ToGRPCError(err)
|
|
}
|
|
|
|
return &userv1.GetUserResponse{User: toProtoUser(user)}, nil
|
|
}
|
|
|
|
// BatchGetUsers 批量返回用户主状态,缺失用户不填充占位对象。
|
|
func (s *Server) BatchGetUsers(ctx context.Context, req *userv1.BatchGetUsersRequest) (*userv1.BatchGetUsersResponse, error) {
|
|
users, err := s.userSvc.BatchGetUsers(ctx, req.GetUserIds())
|
|
if err != nil {
|
|
return nil, xerr.ToGRPCError(err)
|
|
}
|
|
|
|
result := make(map[int64]*userv1.User, len(users))
|
|
for userID, user := range users {
|
|
result[userID] = toProtoUser(user)
|
|
}
|
|
|
|
return &userv1.BatchGetUsersResponse{Users: result}, nil
|
|
}
|
|
|
|
// GetUserIdentity 按系统 user_id 查询当前 active display_user_id。
|
|
func (s *Server) GetUserIdentity(ctx context.Context, req *userv1.GetUserIdentityRequest) (*userv1.GetUserIdentityResponse, error) {
|
|
identity, err := s.userSvc.GetUserIdentity(ctx, req.GetUserId())
|
|
if err != nil {
|
|
return nil, xerr.ToGRPCError(err)
|
|
}
|
|
|
|
return &userv1.GetUserIdentityResponse{Identity: toProtoIdentity(identity)}, nil
|
|
}
|
|
|
|
// ResolveDisplayUserID 把展示短号解析为系统 user_id。
|
|
func (s *Server) ResolveDisplayUserID(ctx context.Context, req *userv1.ResolveDisplayUserIDRequest) (*userv1.ResolveDisplayUserIDResponse, error) {
|
|
identity, err := s.userSvc.ResolveDisplayUserID(ctx, req.GetDisplayUserId())
|
|
if err != nil {
|
|
return nil, xerr.ToGRPCError(err)
|
|
}
|
|
|
|
return &userv1.ResolveDisplayUserIDResponse{Identity: toProtoIdentity(identity)}, nil
|
|
}
|
|
|
|
// ChangeDisplayUserID 修改当前 active 短号并返回新绑定。
|
|
func (s *Server) ChangeDisplayUserID(ctx context.Context, req *userv1.ChangeDisplayUserIDRequest) (*userv1.ChangeDisplayUserIDResponse, error) {
|
|
identity, err := s.userSvc.ChangeDisplayUserID(ctx, req.GetUserId(), req.GetNewDisplayUserId(), req.GetReason(), req.GetOperatorUserId(), req.GetMeta().GetRequestId())
|
|
if err != nil {
|
|
return nil, xerr.ToGRPCError(err)
|
|
}
|
|
|
|
return &userv1.ChangeDisplayUserIDResponse{Identity: toProtoIdentity(identity)}, nil
|
|
}
|
|
|
|
// ApplyPrettyDisplayUserID 申请临时靓号并覆盖当前展示号。
|
|
func (s *Server) ApplyPrettyDisplayUserID(ctx context.Context, req *userv1.ApplyPrettyDisplayUserIDRequest) (*userv1.ApplyPrettyDisplayUserIDResponse, error) {
|
|
identity, leaseID, err := s.userSvc.ApplyPrettyDisplayUserID(ctx, req.GetUserId(), req.GetPrettyDisplayUserId(), req.GetLeaseDurationMs(), req.GetPaymentReceiptId(), req.GetMeta().GetRequestId())
|
|
if err != nil {
|
|
return nil, xerr.ToGRPCError(err)
|
|
}
|
|
|
|
return &userv1.ApplyPrettyDisplayUserIDResponse{Identity: toProtoIdentity(identity), LeaseId: leaseID}, nil
|
|
}
|
|
|
|
// ExpirePrettyDisplayUserID 主动触发靓号过期恢复。
|
|
func (s *Server) ExpirePrettyDisplayUserID(ctx context.Context, req *userv1.ExpirePrettyDisplayUserIDRequest) (*userv1.ExpirePrettyDisplayUserIDResponse, error) {
|
|
identity, err := s.userSvc.ExpirePrettyDisplayUserID(ctx, req.GetUserId(), req.GetLeaseId(), req.GetMeta().GetRequestId())
|
|
if err != nil {
|
|
return nil, xerr.ToGRPCError(err)
|
|
}
|
|
|
|
return &userv1.ExpirePrettyDisplayUserIDResponse{Identity: toProtoIdentity(identity)}, nil
|
|
}
|