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.

validate_token: Move `validate_token` to avoid dependency cycle.

Prakhar Pratyushe6bdf84363bf
diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.py
index 4032a88b3a3..11f9a153a79 100644
--- a/zerver/lib/push_notifications.py
+++ b/zerver/lib/push_notifications.py
@@ -85,6 +85,17 @@ def hex_to_b64(data: str) -> str:
return base64.b64encode(bytes.fromhex(data)).decode()
+def validate_token(token_str: str, kind: int) -> None:
+ if token_str == "" or len(token_str) > 4096:
+ raise JsonableError(_("Empty or invalid length token"))
+ if kind == PushDeviceToken.APNS:
+ # Validate that we can actually decode the token.
+ try:
+ b64_to_hex(token_str)
+ except Exception:
+ raise JsonableError(_("Invalid APNS token"))
+
+
def get_message_stream_name_from_database(message: Message) -> str:
"""
Never use this function outside of the push-notifications
diff --git a/zerver/views/push_notifications.py b/zerver/views/push_notifications.py
index 20025cb7808..e34503dbe3e 100644
--- a/zerver/views/push_notifications.py
+++ b/zerver/views/push_notifications.py
@@ -18,10 +18,10 @@ from zerver.lib.exceptions import (
from zerver.lib.push_notifications import (
InvalidPushDeviceTokenError,
add_push_device_token,
- b64_to_hex,
remove_push_device_token,
send_test_push_notification,
uses_notification_bouncer,
+ validate_token,
)
from zerver.lib.remote_server import (
SELF_HOSTING_REGISTRATION_TAKEOVER_CHALLENGE_TOKEN_REDIS_KEY,
@@ -38,17 +38,6 @@ from zerver.views.errors import config_error
redis_client = redis_utils.get_redis_client()
-def validate_token(token_str: str, kind: int) -> None:
- if token_str == "" or len(token_str) > 4096:
- raise JsonableError(_("Empty or invalid length token"))
- if kind == PushDeviceToken.APNS:
- # Validate that we can actually decode the token.
- try:
- b64_to_hex(token_str)
- except Exception:
- raise JsonableError(_("Invalid APNS token"))
-
-
@human_users_only
@typed_endpoint
def add_apns_device_token(
diff --git a/zilencer/views.py b/zilencer/views.py
index 2a619709040..73309a70555 100644
--- a/zilencer/views.py
+++ b/zilencer/views.py
@@ -51,6 +51,7 @@ from zerver.lib.push_notifications import (
send_android_push_notification,
send_apple_push_notification,
send_test_push_notification_directly_to_devices,
+ validate_token,
)
from zerver.lib.queue import queue_event_on_commit
from zerver.lib.rate_limiter import rate_limit_endpoint_absolute
@@ -75,7 +76,6 @@ from zerver.lib.typed_endpoint_validators import check_string_fixed_length
from zerver.lib.types import RemoteRealmDictValue
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.realms import DisposableEmailError
-from zerver.views.push_notifications import validate_token
from zilencer.auth import (
InvalidZulipServerKeyError,
generate_registration_transfer_verification_secret,