validate_token: Move validate_token to avoid dependency cycle.

Prep commit to avoid depencency cycle.
This commit is contained in:
Prakhar Pratyush
2025-06-16 23:17:02 +05:30
committed by Tim Abbott
parent 8b3cef554b
commit e6bdf84363
3 changed files with 13 additions and 13 deletions

View File

@@ -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

View File

@@ -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(

View File

@@ -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,