Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

auth: Avoid unchecked casts.

Anders Kaseorgf6c73ed45ae1
diff --git a/zerver/lib/redis_utils.py b/zerver/lib/redis_utils.py
index af150680bb5..7636b4e9c6a 100644
--- a/zerver/lib/redis_utils.py
+++ b/zerver/lib/redis_utils.py
@@ -1,5 +1,5 @@
import re
-from typing import Any, Dict, Optional
+from typing import Any, Dict, Mapping, Optional
import redis
import ujson
@@ -25,7 +25,7 @@ def get_redis_client() -> redis.StrictRedis:
password=settings.REDIS_PASSWORD, db=0)
def put_dict_in_redis(redis_client: redis.StrictRedis, key_format: str,
- data_to_store: Dict[str, Any],
+ data_to_store: Mapping[str, Any],
expiration_seconds: int,
token_length: int=64,
token: Optional[str]=None) -> str:
diff --git a/zerver/views/auth.py b/zerver/views/auth.py
index 4ee1b3dc987..238f8326d60 100644
--- a/zerver/views/auth.py
+++ b/zerver/views/auth.py
@@ -2,7 +2,7 @@ import logging
import os
import urllib
from functools import wraps
-from typing import Any, Dict, List, Mapping, Optional, cast
+from typing import Any, Dict, List, Mapping, Optional
import jwt
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
@@ -224,7 +224,7 @@ def register_remote_user(request: HttpRequest, result: ExternalAuthResult) -> Ht
# We have verified the user controls an email address, but
# there's no associated Zulip user account. Consider sending
# the request to registration.
- kwargs = cast(Dict[str, Any], result.copy_data_dict())
+ kwargs: Dict[str, Any] = dict(result.data_dict)
# maybe_send_to_registration doesn't take these arguments, so delete them.
kwargs.pop('subdomain', None)
kwargs.pop('redirect_to', None)
diff --git a/zproject/backends.py b/zproject/backends.py
index 44443009002..21c0b89a05a 100644
--- a/zproject/backends.py
+++ b/zproject/backends.py
@@ -985,7 +985,7 @@ class ExternalAuthResult:
"with login_token is disallowed.")
self.instantiate_with_token(login_token, delete_stored_data)
else:
- self.data_dict = cast(ExternalAuthDataDict, {**data_dict})
+ self.data_dict = data_dict.copy()
self.user_profile = user_profile
if self.user_profile is not None:
@@ -1005,11 +1005,8 @@ class ExternalAuthResult:
if not self.user_profile.is_mirror_dummy:
self.data_dict['is_signup'] = False
- def copy_data_dict(self) -> ExternalAuthDataDict:
- return self.data_dict.copy()
-
def store_data(self) -> str:
- key = put_dict_in_redis(redis_client, self.LOGIN_KEY_FORMAT, cast(Dict[str, Any], self.data_dict),
+ key = put_dict_in_redis(redis_client, self.LOGIN_KEY_FORMAT, self.data_dict,
expiration_seconds=self.LOGIN_KEY_EXPIRATION_SECONDS,
token_length=self.LOGIN_TOKEN_LENGTH)
token = key.split(self.LOGIN_KEY_PREFIX, 1)[1] # remove the prefix