Skip to content

Commit

Permalink
feat(api): Add endpoint to allow user to resend confirmation email
Browse files Browse the repository at this point in the history
A new API endpoint has been added to allow a user to resend a registration
confirmation to a user with the provided email.

Limitations:
For the MVP, this commit causes the API to rely on the Cognito service to rate
limit the requests rather than keep track of the requests itself.

closes #542
  • Loading branch information
paulespinosa committed Jul 20, 2023
1 parent 5a50d03 commit 121dcd5
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
31 changes: 31 additions & 0 deletions api/openapi_server/_spec/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,37 @@ paths:
tags:
- auth
x-openapi-router-controller: openapi_server.controllers.auth_controller
/auth/resend_confirmation_code:
post:
description: Resends the registration confirmation code to the specified user (identified by email).
operationId: resend_confirmation_code
requestBody:
content:
application/json:
schema:
type: object
properties:
email:
type: string
required:
- email
responses:
'200':
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/ApiResponse'
'400':
description: The email parameter was not sent or the user with the given email was not found.
'429':
description: Too many requests to resend the registration confirmation code were made to this user.
tags:
- auth
x-openapi-router-controller: openapi_server.controllers.auth_controller
security:
- jwt:
- secret
/auth/confirm:
get:
description: Confirm or deny verification of users email
Expand Down
38 changes: 38 additions & 0 deletions api/openapi_server/controllers/auth_controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import connexion
import boto3
import botocore
import hmac
import base64
import requests
Expand Down Expand Up @@ -214,6 +215,43 @@ def signin():
}


def resend_confirmation_code():
'''
Resends the registration confirmation code to the specified user (identified by email).
'''

if connexion.request.is_json:
body = connexion.request.get_json()

if "email" not in body:
raise AuthError({"message": "email invalid"}, 400)

secret_hash = get_secret_hash(body['email'])

try:
email = body['email']
response = userClient.resend_confirmation_code(
ClientId=COGNITO_CLIENT_ID,
SecretHash=secret_hash,
Username=email,
)
return response
except botocore.exceptions.ClientError as error:
match error.response['Error']['Code']:
case 'UserNotFoundException':
msg = "User not found. Confirmation not sent."
raise AuthError({"message": msg}, 400)
case 'TooManyRequestsException':
msg = "Too many attempts to resend confirmation in a short amount of time."
raise AuthError({"message": msg}, 429)
case _:
msg = error.response['Error']['Message']
raise AuthError({"message": msg}, 500)
except botocore.exceptions.ParamValidationError as error:
msg = f"The parameters you provided are incorrect: {error}"
raise AuthError({"message": msg}, 500)


def confirm():
# Validate request data
if connexion.request.is_json:
Expand Down
6 changes: 4 additions & 2 deletions api/openapi_server/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ paths:
/host:
$ref: "./paths/host.yaml"
/auth/signup/host:
$ref: "./paths/auth/authSignupHost.yaml"
$ref: "./paths/auth/authSignUpHost.yaml"
/auth/signup/coordinator:
$ref: "./paths/auth/authSignupCoordinator.yaml"
$ref: "./paths/auth/authSignUpCoordinator.yaml"
/auth/signin:
$ref: "./paths/auth/authSignin.yaml"
/auth/resend_confirmation_code:
$ref: "./paths/auth/authResendConfirmationCode.yaml"
/auth/confirm:
$ref: "./paths/auth/authConfirm.yaml"
/auth/signout:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
post:
description: Resends the registration confirmation code to the specified user (identified by email).
operationId: resend_confirmation_code
requestBody:
content:
application/json:
schema:
type: object
properties:
email:
type: string
required:
- email
responses:
'200':
description: successful operation
content:
application/json:
schema:
$ref: '../../openapi.yaml#/components/schemas/ApiResponse'
'400':
description: The email parameter was not sent or the user with the given email was not found.
'429':
description: Too many requests to resend the registration confirmation code were made to this user.
tags:
- auth
x-openapi-router-controller: openapi_server.controllers.auth_controller
security:
- jwt: ["secret"]

0 comments on commit 121dcd5

Please sign in to comment.