redis_utils: Extend put_dict_in_redis to accept token as param.

This extends `put_dict_in_redis` to take token as an argument
and return that with the as a `key` with following key format.

Also, edit regex for token to include uppercase letters as
a token sent during apple authentication contains uppercase
letters.

Useful for Adding "Sign in with Apple" support.
This commit is contained in:
Dinesh
2020-04-27 02:30:54 +05:30
committed by Tim Abbott
parent c7a2c48bb7
commit 2711619376

View File

@@ -26,12 +26,14 @@ def get_redis_client() -> redis.StrictRedis:
def put_dict_in_redis(redis_client: redis.StrictRedis, key_format: str,
data_to_store: Dict[str, Any],
expiration_seconds: int,
token_length: int=64) -> str:
token_length: int=64,
token: Optional[str]=None) -> str:
key_length = len(key_format) - len('{token}') + token_length
if key_length > MAX_KEY_LENGTH:
error_msg = "Requested key too long in put_dict_in_redis. Key format: %s, token length: %s"
raise ZulipRedisKeyTooLongError(error_msg % (key_format, token_length))
token = generate_random_token(token_length)
if token is None:
token = generate_random_token(token_length)
key = key_format.format(token=token)
with redis_client.pipeline() as pipeline:
pipeline.set(key, ujson.dumps(data_to_store))
@@ -58,7 +60,7 @@ def get_dict_from_redis(redis_client: redis.StrictRedis, key_format: str, key: s
def validate_key_fits_format(key: str, key_format: str) -> None:
assert "{token}" in key_format
regex = key_format.format(token=r"[a-z0-9]+")
regex = key_format.format(token=r"[a-zA-Z0-9]+")
if not re.fullmatch(regex, key):
raise ZulipRedisKeyOfWrongFormatError("%s does not match format %s" % (key, key_format))