silvermobi/servers/grpcapi/server/onboarding_validation.go

186 lines
5.8 KiB
Go

package grpcserver
import (
"context"
"encoding/base64"
"encoding/json"
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"strings"
"time"
)
func (s SilvermobiGRPCService) SetPhoneNumber(ctx context.Context, req *grpcproto.SetPhoneNumberRequest) (res *grpcproto.SetPhoneNumberResponse, err error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
}
authHeader, ok := md["authorization"]
if !ok || len(authHeader) == 0 {
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
}
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
id := ExtractIdFromToken(tokenString)
if err = s.Handler.UpdatePhoneNumber(
context.Background(),
id,
req.PhoneNumber,
); err != nil {
return nil, err
}
return &grpcproto.SetPhoneNumberResponse{Ok: true}, nil
}
func (s SilvermobiGRPCService) VerifyPhoneNumber(ctx context.Context, req *grpcproto.VerifyPhoneNumberRequest) (res *grpcproto.VerifyPhoneNumberResponse, err error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
}
authHeader, ok := md["authorization"]
if !ok || len(authHeader) == 0 {
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
}
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
id := ExtractIdFromToken(tokenString)
if err = s.Handler.VerifyPhoneNumber(
context.Background(),
id,
req.PhoneNumber,
req.VerificationCode,
); err != nil {
return nil, err
}
return &grpcproto.VerifyPhoneNumberResponse{Ok: true}, nil
}
func (s SilvermobiGRPCService) SetBirthDate(ctx context.Context, req *grpcproto.BirthDateRequest) (res *grpcproto.BirthDateResponse, err error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
}
authHeader, ok := md["authorization"]
if !ok || len(authHeader) == 0 {
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
}
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
id := ExtractIdFromToken(tokenString)
birthdate := time.Unix(req.Birthdate.Seconds, int64(req.Birthdate.Nanos)).UTC()
birthdateString := birthdate.Format("2006-01-02T15:04:05Z")
if err = s.Handler.UpdateBirthDate(ctx, id, birthdateString); err != nil {
return nil, err
}
return &grpcproto.BirthDateResponse{
Ok: true,
}, nil
}
func (s SilvermobiGRPCService) SetAccountType(ctx context.Context, req *grpcproto.AccountTypeRequest) (res *grpcproto.AccountTypeResponse, err error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
}
authHeader, ok := md["authorization"]
if !ok || len(authHeader) == 0 {
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
}
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
id := ExtractIdFromToken(tokenString)
if req.GetType() != grpcproto.AccountTypeRequest_PASSENGER && req.GetType() != grpcproto.AccountTypeRequest_DRIVER {
return nil, status.Errorf(codes.InvalidArgument, "Type should be PASSENGER or DRIVER")
}
if err = s.Handler.SetAccountType(ctx, id, req.GetType().String()); err != nil {
return nil, err
}
return &grpcproto.AccountTypeResponse{
Ok: true,
}, nil
}
func (s SilvermobiGRPCService) GetAccountType(ctx context.Context, req *grpcproto.AccountTypeRequest) (res *grpcproto.AccountTypeResponse, err error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
}
authHeader, ok := md["authorization"]
if !ok || len(authHeader) == 0 {
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
}
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
id := ExtractIdFromToken(tokenString)
if req.Request == nil || !*req.Request {
return nil, status.Errorf(codes.InvalidArgument, "request arg should be true")
}
account_type, err := s.Handler.GetAccountType(ctx, id)
if err != nil {
return nil, err
}
var responseType grpcproto.AccountTypeResponse_AccountType
switch account_type {
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{
Type: &responseType,
}
return response, nil
}
func (s SilvermobiGRPCService) GetValidation(ctx context.Context, req *grpcproto.ValidationRequest) (res *grpcproto.ValidationResponse, err error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
}
authHeader, ok := md["authorization"]
if !ok || len(authHeader) == 0 {
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
}
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
id := ExtractIdFromToken(tokenString)
phone_validation, birth_validation, type_validation, err := s.Handler.CheckValidation(ctx, id)
if err != nil {
return nil, err
}
return &grpcproto.ValidationResponse{
Phone: phone_validation,
Birthdate: birth_validation,
Type: type_validation,
}, nil
}
func ExtractIdFromToken(tokenString string) string {
parts := strings.Split(tokenString, ".")
if len(parts) != 3 {
return ""
}
payloadBytes, err := base64.RawStdEncoding.DecodeString(parts[1])
if err != nil {
return ""
}
var payloadMap map[string]interface{}
if err := json.Unmarshal(payloadBytes, &payloadMap); err != nil {
return ""
}
id, ok := payloadMap["sub"].(string)
if !ok {
return ""
}
return id
}