silvermobi/servers/grpcapi/server/onboarding_validation.go

286 lines
6.8 KiB
Go
Raw Permalink Normal View History

2023-08-08 10:28:43 +00:00
package grpcserver
import (
"context"
2023-09-19 09:06:20 +00:00
"encoding/base64"
"encoding/json"
2024-10-31 19:32:54 +00:00
"strings"
"time"
2023-08-08 10:28:43 +00:00
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
2023-12-08 06:35:04 +00:00
"github.com/rs/zerolog/log"
2023-09-19 09:06:20 +00:00
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
2023-08-08 10:28:43 +00:00
)
2024-10-31 19:32:54 +00:00
func (s SilvermobiGRPCService) SetPhoneNumber(ctx context.Context,
req *grpcproto.SetPhoneNumberRequest) (res *grpcproto.SetPhoneNumberResponse, err error) {
var (
md metadata.MD
authHeader []string
ok bool
)
if md, ok = metadata.FromIncomingContext(ctx); !ok {
2023-09-19 09:06:20 +00:00
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
}
2024-10-31 19:32:54 +00:00
authHeader, ok = md["authorization"]
2023-09-19 09:06:20 +00:00
if !ok || len(authHeader) == 0 {
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
}
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
id := ExtractIdFromToken(tokenString)
2024-10-31 19:32:54 +00:00
2023-12-08 06:35:04 +00:00
log.Info().
Str("User ID", id).
Msg("SetPhoneNumber")
2023-08-08 10:28:43 +00:00
if err = s.Handler.UpdatePhoneNumber(
context.Background(),
2023-09-19 09:06:20 +00:00
id,
2023-08-08 10:28:43 +00:00
req.PhoneNumber,
); err != nil {
return nil, err
}
return &grpcproto.SetPhoneNumberResponse{Ok: true}, nil
}
2024-10-31 19:32:54 +00:00
func (s SilvermobiGRPCService) VerifyPhoneNumber(ctx context.Context,
req *grpcproto.VerifyPhoneNumberRequest) (res *grpcproto.VerifyPhoneNumberResponse, err error) {
var (
md metadata.MD
authHeader []string
ok bool
)
if md, ok = metadata.FromIncomingContext(ctx); !ok {
2023-09-19 09:06:20 +00:00
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
}
2024-10-31 19:32:54 +00:00
authHeader, ok = md["authorization"]
2023-09-19 09:06:20 +00:00
if !ok || len(authHeader) == 0 {
2024-10-31 19:32:54 +00:00
return nil, status.Errorf(codes.Unauthenticated,
"Missing authorization header")
2023-09-19 09:06:20 +00:00
}
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
id := ExtractIdFromToken(tokenString)
2023-12-08 06:35:04 +00:00
log.Info().
Str("User ID", id).
Msg("VerifyPhoneNumber")
2024-10-31 19:32:54 +00:00
2023-08-08 10:28:43 +00:00
if err = s.Handler.VerifyPhoneNumber(
context.Background(),
2023-09-19 09:06:20 +00:00
id,
2023-08-08 10:28:43 +00:00
req.PhoneNumber,
req.VerificationCode,
); err != nil {
return nil, err
}
return &grpcproto.VerifyPhoneNumberResponse{Ok: true}, nil
}
2024-10-31 19:32:54 +00:00
func (s SilvermobiGRPCService) SetBirthDate(ctx context.Context,
req *grpcproto.BirthDateRequest) (res *grpcproto.BirthDateResponse, err error) {
var (
md metadata.MD
authHeader []string
ok bool
)
if md, ok = metadata.FromIncomingContext(ctx); !ok {
2023-09-19 09:06:20 +00:00
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
}
2024-10-31 19:32:54 +00:00
authHeader, ok = md["authorization"]
2023-09-19 09:06:20 +00:00
if !ok || len(authHeader) == 0 {
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
}
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
id := ExtractIdFromToken(tokenString)
2024-10-31 19:32:54 +00:00
2023-12-08 06:35:04 +00:00
log.Info().
Str("User ID", id).
Msg("SetBirthDate")
2024-10-31 19:32:54 +00:00
2023-08-08 10:28:43 +00:00
birthdate := time.Unix(req.Birthdate.Seconds, int64(req.Birthdate.Nanos)).UTC()
birthdateString := birthdate.Format("2006-01-02T15:04:05Z")
2023-09-19 09:06:20 +00:00
if err = s.Handler.UpdateBirthDate(ctx, id, birthdateString); err != nil {
2023-08-08 10:28:43 +00:00
return nil, err
}
2023-09-19 09:06:20 +00:00
2023-08-08 10:28:43 +00:00
return &grpcproto.BirthDateResponse{
Ok: true,
}, nil
}
2024-10-31 19:32:54 +00:00
func (s SilvermobiGRPCService) SetAccountType(ctx context.Context,
req *grpcproto.AccountTypeRequest) (res *grpcproto.AccountTypeResponse, err error) {
var (
md metadata.MD
authHeader []string
ok bool
)
if md, ok = metadata.FromIncomingContext(ctx); !ok {
2023-09-19 09:06:20 +00:00
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
}
2024-10-31 19:32:54 +00:00
authHeader, ok = md["authorization"]
2023-09-19 09:06:20 +00:00
if !ok || len(authHeader) == 0 {
2024-10-31 19:32:54 +00:00
return nil, status.Errorf(codes.Unauthenticated,
"Missing authorization header")
2023-09-19 09:06:20 +00:00
}
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
id := ExtractIdFromToken(tokenString)
2024-10-31 19:32:54 +00:00
2023-12-08 06:35:04 +00:00
log.Info().
Str("User ID", id).
Msg("SetAccountType")
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
if req.GetType() != grpcproto.AccountTypeRequest_PASSENGER && req.GetType() != grpcproto.AccountTypeRequest_DRIVER {
return nil, status.Errorf(codes.InvalidArgument, "Type should be PASSENGER or DRIVER")
}
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
if err = s.Handler.SetAccountType(ctx, id, req.GetType().String()); err != nil {
return nil, err
}
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
return &grpcproto.AccountTypeResponse{
Ok: true,
}, nil
}
2024-10-31 19:32:54 +00:00
func (s SilvermobiGRPCService) GetAccountType(ctx context.Context,
req *grpcproto.AccountTypeRequest) (res *grpcproto.AccountTypeResponse, err error) {
var (
md metadata.MD
authHeader []string
ok bool
accountType string
responseType grpcproto.AccountTypeResponse_AccountType
)
if md, ok = metadata.FromIncomingContext(ctx); !ok {
2023-09-19 09:06:20 +00:00
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
}
2024-10-31 19:32:54 +00:00
authHeader, ok = md["authorization"]
2023-09-19 09:06:20 +00:00
if !ok || len(authHeader) == 0 {
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
}
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
id := ExtractIdFromToken(tokenString)
2024-10-31 19:32:54 +00:00
2023-12-08 06:35:04 +00:00
log.Info().
Str("User ID", id).
Msg("GetAccountType")
2024-10-31 19:32:54 +00:00
2024-08-02 13:11:30 +00:00
if req.Request == false || !req.Request {
2023-09-19 09:06:20 +00:00
return nil, status.Errorf(codes.InvalidArgument, "request arg should be true")
}
2024-10-31 19:32:54 +00:00
if accountType, err = s.Handler.GetAccountType(ctx, id); err != nil {
2023-09-19 09:06:20 +00:00
return nil, err
}
2024-10-31 19:32:54 +00:00
switch accountType {
2023-09-19 09:06:20 +00:00
case "PASSENGER":
responseType = grpcproto.AccountTypeResponse_PASSENGER
case "DRIVER":
responseType = grpcproto.AccountTypeResponse_DRIVER
default:
return nil, status.Errorf(codes.InvalidArgument, "Invalid account type")
}
response := &grpcproto.AccountTypeResponse{
2024-08-02 13:11:30 +00:00
Type: responseType,
2023-09-19 09:06:20 +00:00
}
return response, nil
}
2024-10-31 19:32:54 +00:00
func (s SilvermobiGRPCService) GetValidation(ctx context.Context,
req *grpcproto.ValidationRequest) (res *grpcproto.ValidationResponse, err error) {
var (
md metadata.MD
authHeader []string
ok, phoneValidation, birthValidation, typeValidation bool
)
if md, ok = metadata.FromIncomingContext(ctx); ok {
2023-09-19 09:06:20 +00:00
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
}
2024-10-31 19:32:54 +00:00
authHeader, ok = md["authorization"]
2023-09-19 09:06:20 +00:00
if !ok || len(authHeader) == 0 {
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
}
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
id := ExtractIdFromToken(tokenString)
2024-10-31 19:32:54 +00:00
if phoneValidation, birthValidation, typeValidation, err = s.Handler.CheckValidation(ctx, id); err != nil {
2023-08-08 10:28:43 +00:00
return nil, err
}
2024-10-31 19:32:54 +00:00
2023-08-08 10:28:43 +00:00
return &grpcproto.ValidationResponse{
2024-10-31 19:32:54 +00:00
Phone: phoneValidation,
Birthdate: birthValidation,
Type: typeValidation,
2023-08-08 10:28:43 +00:00
}, nil
}
2023-09-19 09:06:20 +00:00
func ExtractIdFromToken(tokenString string) string {
2024-10-31 19:32:54 +00:00
var (
err error
payloadBytes []byte
payloadMap map[string]interface{}
id string
ok bool
)
2023-09-19 09:06:20 +00:00
parts := strings.Split(tokenString, ".")
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
if len(parts) != 3 {
return ""
}
2024-10-31 19:32:54 +00:00
if payloadBytes, err = base64.RawStdEncoding.DecodeString(parts[1]); err != nil {
2023-09-19 09:06:20 +00:00
return ""
}
2024-10-31 19:32:54 +00:00
if err = json.Unmarshal(payloadBytes, &payloadMap); err != nil {
2023-09-19 09:06:20 +00:00
return ""
}
2024-10-31 19:32:54 +00:00
if id, ok = payloadMap["sub"].(string); !ok {
2023-09-19 09:06:20 +00:00
return ""
}
2024-10-31 19:32:54 +00:00
2023-09-19 09:06:20 +00:00
return id
}