-
Notifications
You must be signed in to change notification settings - Fork 0
/
otp.go
56 lines (47 loc) · 1.72 KB
/
otp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"context"
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
cognito "github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
log "github.com/sirupsen/logrus"
)
type UserOTP struct {
OTP string `json:"otp"`
Email string `json:"email"`
PhoneNumber string `json:"phoneNumber"`
}
func OTP(ctx context.Context, request events.APIGatewayProxyRequest) events.APIGatewayProxyResponse {
var userOTP UserOTP
err := json.Unmarshal([]byte(request.Body), &userOTP)
if err != nil {
log.Error("User OTP: ", err)
return writeGatewayProxyResponse("", ErrBadRequest)
}
cognitoUsername := getCognitoUsername(userOTP.Email, userOTP.PhoneNumber)
input := &cognito.ConfirmSignUpInput{
ConfirmationCode: aws.String(userOTP.OTP),
Username: aws.String(cognitoUsername),
ClientId: aws.String(appClientID),
SecretHash: aws.String(cognitoSecretHash(cognitoUsername)),
}
_, err = cognitoClient.ConfirmSignUp(input)
if awsErr, ok := err.(awserr.Error); ok {
log.Error("User OTP: ", awsErr)
switch awsErr.Code() {
case cognito.ErrCodeUserNotFoundException:
return writeGatewayProxyResponse("User not found", ErrBadRequest)
case cognito.ErrCodeCodeMismatchException:
return writeGatewayProxyResponse("Invalid OTP", ErrBadRequest)
case cognito.ErrCodeLimitExceededException:
return writeGatewayProxyResponse("Maximum OTP limit reached", ErrBadRequest)
case cognito.ErrCodeExpiredCodeException:
return writeGatewayProxyResponse("OTP expired", ErrBadRequest)
default:
return writeGatewayProxyResponse("", ErrInternalServerError)
}
}
return writeGatewayProxyResponse("OTP successful", nil)
}