(20250220)

This commit is contained in:
2025-02-20 16:26:43 +05:30
parent 2435760ea3
commit d16fa61ec3
7 changed files with 475 additions and 195 deletions
+15 -53
View File
@@ -146,15 +146,18 @@ class TimedOTPController:
self,
redis_cache: AsyncRedisCache,
inbound_data: GenerateTimedOTP
) -> str | None:
) -> FunctionCallResponse:
"""
To generate one OTP and store it in cache.
:param redis_cache: The instance of the cache client to use to hold the OTP.
:param inbound_data: The
:return:
:param inbound_data: The data from which the OTP will be generated.
:return: A structured response to describe what happened in the process.
"""
# Start by assuming failure:
response = FunctionCallResponse()
# Generate the OTP:
otp_key = self.KEY_PREFIX + redis_cache.make_key(str(inbound_data.id))
otp_client = HashedOTP(secret = HashedOTP.generate_secret())
@@ -172,53 +175,12 @@ class TimedOTPController:
expiry = inbound_data.seconds
)
# Done here:
return otp if otp_stored else None
async def generate_and_send_sms(
self,
mongo_data_conn: AsyncMongo,
redis_cache: AsyncRedisCache,
auth_token: CoreAuthTokenModel,
sms_client: SMSController,
sms_message: NimbusSMSIndiaMessage | SavvyBulkSMSKenyaMessage,
inbound_data: GenerateTimedOTP,
tags: List[str] = None
) -> FunctionCallResponse:
"""
Generate an OTP and send it via SMS.
:param mongo_data_conn: The client to use to connect to the database.
:param redis_cache: The client to use to connect to the cache.
:param auth_token: The credentials
:param sms_client:
:param sms_message:
:param inbound_data:
:param tags:
:return:
"""
# Start by assuming failure:
response = FunctionCallResponse()
# Generate the OTP:
otp = await self.generate(redis_cache, inbound_data)
if not otp:
response.message = "Failed to generate OTP."
return response
# Send the SMS:
sms_response = await sms_client.send_many_sms(
mongo_data_conn = mongo_data_conn,
auth_token = auth_token,
messages = [sms_message],
tags = tags
)
# Analyze the actions:
success = True if sms_response.successCount == 1 else False
response.success = success
response.message = "OTP sent successfully." if success else "OTP could NOT be sent."
# Analyze the response:
if otp_stored:
response.success = True
response.message = "OTP generated successfully."
response.data = otp
else: response.message = "Failed to generate an OTP."
# Done here:
return response
@@ -235,9 +197,9 @@ class TimedOTPController:
"""
To verify if an OTP is valid, or not.
:param redis_cache:
:param inbound_data:
:return:
:param redis_cache: The instance of the cache client to use to hold the OTP.
:param inbound_data: The data from which the OTP will be verified.
:return: A structured response to describe what happened in the process.
"""
# Start by assuming failure: