mirror of
https://github.com/zulip/zulip.git
synced 2025-11-07 15:33:30 +00:00
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:
@@ -26,12 +26,14 @@ def get_redis_client() -> redis.StrictRedis:
|
|||||||
def put_dict_in_redis(redis_client: redis.StrictRedis, key_format: str,
|
def put_dict_in_redis(redis_client: redis.StrictRedis, key_format: str,
|
||||||
data_to_store: Dict[str, Any],
|
data_to_store: Dict[str, Any],
|
||||||
expiration_seconds: int,
|
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
|
key_length = len(key_format) - len('{token}') + token_length
|
||||||
if key_length > MAX_KEY_LENGTH:
|
if key_length > MAX_KEY_LENGTH:
|
||||||
error_msg = "Requested key too long in put_dict_in_redis. Key format: %s, token length: %s"
|
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))
|
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)
|
key = key_format.format(token=token)
|
||||||
with redis_client.pipeline() as pipeline:
|
with redis_client.pipeline() as pipeline:
|
||||||
pipeline.set(key, ujson.dumps(data_to_store))
|
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:
|
def validate_key_fits_format(key: str, key_format: str) -> None:
|
||||||
assert "{token}" in key_format
|
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):
|
if not re.fullmatch(regex, key):
|
||||||
raise ZulipRedisKeyOfWrongFormatError("%s does not match format %s" % (key, key_format))
|
raise ZulipRedisKeyOfWrongFormatError("%s does not match format %s" % (key, key_format))
|
||||||
|
|||||||
Reference in New Issue
Block a user