-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor verification service and fix type issues
- Loading branch information
Showing
4 changed files
with
36 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,52 @@ | ||
from datetime import timedelta | ||
from random import randint | ||
from typing import Any | ||
|
||
from redis.typing import KeyT, ResponseT | ||
|
||
from src.cache import redis, separate | ||
from src.config import settings | ||
|
||
__CACHE_KEY_PREFIX = "codes" | ||
|
||
|
||
def set_code(email: str) -> int: | ||
code = generate_code() | ||
name = generate_code_key(email) | ||
redis.set(name, code) | ||
redis.expire(name, timedelta(minutes=settings.otp.expire_minutes)) | ||
def set_code(subject: KeyT) -> int: | ||
name = generate_code_cache_name(subject) | ||
code = generate_random_code() | ||
expires_at = timedelta(minutes=settings.otp.expire_minutes) | ||
|
||
redis.set(name, code, expires_at) | ||
|
||
return code | ||
|
||
|
||
def get_code(email: str) -> Any: # TODO: Use proper type conversion. | ||
return redis.get(generate_code_key(email)) | ||
def get_code(subject: KeyT) -> ResponseT: | ||
name = generate_code_cache_name(subject) | ||
code = redis.get(name) | ||
|
||
return code | ||
|
||
|
||
def expire_code_if_valid(email: str, code: int) -> bool: | ||
is_valid = is_valid_code(email, code) | ||
if is_valid: | ||
expire_code(email) | ||
return is_valid | ||
def expire_code(subject: KeyT) -> ResponseT: | ||
name = generate_code_cache_name(subject) | ||
response = redis.delete(name) | ||
|
||
return response | ||
|
||
def is_valid_code(email: str, code: int) -> bool: | ||
cached_code = get_code(email) | ||
if cached_code: | ||
# TODO: Use proper type conversion. | ||
return int(cached_code) == code # type: ignore | ||
return False | ||
|
||
def expire_code_if_correct(subject: KeyT, code: int) -> bool: | ||
is_correct = is_code_correct(subject, code) | ||
if is_correct: | ||
expire_code(subject) | ||
return is_correct | ||
|
||
def generate_code() -> int: | ||
return randint(settings.otp.min, settings.otp.max) | ||
|
||
def is_code_correct(subject: KeyT, code: int) -> bool: | ||
return get_code(subject) == str(code).encode() | ||
|
||
def generate_code_key(email: str) -> str: | ||
return separate(__CACHE_KEY_PREFIX, email) | ||
|
||
def generate_random_code() -> int: | ||
return randint(settings.otp.min, settings.otp.max) | ||
|
||
|
||
def expire_code(email: str) -> None: | ||
redis.delete(generate_code_key(email)) | ||
def generate_code_cache_name(subject: KeyT) -> KeyT: | ||
return separate(__CACHE_KEY_PREFIX, subject) |