mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	push_notifications: Declare token of type str, not bytes.
Declaring a CharField of type bytes made no sense. Signed-off-by: Anders Kaseorg <andersk@zulipchat.com>
This commit is contained in:
		@@ -46,11 +46,11 @@ else:  # nocoverage  -- Not convenient to add test for this.
 | 
				
			|||||||
DeviceToken = Union[PushDeviceToken, RemotePushDeviceToken]
 | 
					DeviceToken = Union[PushDeviceToken, RemotePushDeviceToken]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# We store the token as b64, but apns-client wants hex strings
 | 
					# We store the token as b64, but apns-client wants hex strings
 | 
				
			||||||
def b64_to_hex(data: bytes) -> str:
 | 
					def b64_to_hex(data: str) -> str:
 | 
				
			||||||
    return binascii.hexlify(base64.b64decode(data)).decode('utf-8')
 | 
					    return binascii.hexlify(base64.b64decode(data)).decode('utf-8')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def hex_to_b64(data: str) -> bytes:
 | 
					def hex_to_b64(data: str) -> str:
 | 
				
			||||||
    return base64.b64encode(binascii.unhexlify(data.encode('utf-8')))
 | 
					    return base64.b64encode(binascii.unhexlify(data)).decode()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# Sending to APNs, for iOS
 | 
					# Sending to APNs, for iOS
 | 
				
			||||||
@@ -362,7 +362,7 @@ def num_push_devices_for_user(user_profile: UserProfile, kind: Optional[int]=Non
 | 
				
			|||||||
        return PushDeviceToken.objects.filter(user=user_profile, kind=kind).count()
 | 
					        return PushDeviceToken.objects.filter(user=user_profile, kind=kind).count()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def add_push_device_token(user_profile: UserProfile,
 | 
					def add_push_device_token(user_profile: UserProfile,
 | 
				
			||||||
                          token_str: bytes,
 | 
					                          token_str: str,
 | 
				
			||||||
                          kind: int,
 | 
					                          kind: int,
 | 
				
			||||||
                          ios_app_id: Optional[str]=None) -> None:
 | 
					                          ios_app_id: Optional[str]=None) -> None:
 | 
				
			||||||
    logger.info("Registering push device: %d %r %d %r",
 | 
					    logger.info("Registering push device: %d %r %d %r",
 | 
				
			||||||
@@ -398,7 +398,7 @@ def add_push_device_token(user_profile: UserProfile,
 | 
				
			|||||||
    except IntegrityError:
 | 
					    except IntegrityError:
 | 
				
			||||||
        pass
 | 
					        pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def remove_push_device_token(user_profile: UserProfile, token_str: bytes, kind: int) -> None:
 | 
					def remove_push_device_token(user_profile: UserProfile, token_str: str, kind: int) -> None:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # If we're sending things to the push notification bouncer
 | 
					    # If we're sending things to the push notification bouncer
 | 
				
			||||||
    # unregister this user with them here
 | 
					    # unregister this user with them here
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1251,7 +1251,7 @@ class AbstractPushDeviceToken(models.Model):
 | 
				
			|||||||
    # sent to us from each device:
 | 
					    # sent to us from each device:
 | 
				
			||||||
    #   - APNS token if kind == APNS
 | 
					    #   - APNS token if kind == APNS
 | 
				
			||||||
    #   - GCM registration id if kind == GCM
 | 
					    #   - GCM registration id if kind == GCM
 | 
				
			||||||
    token = models.CharField(max_length=4096, db_index=True)  # type: bytes
 | 
					    token = models.CharField(max_length=4096, db_index=True)  # type: str
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # TODO: last_updated should be renamed date_created, since it is
 | 
					    # TODO: last_updated should be renamed date_created, since it is
 | 
				
			||||||
    # no longer maintained as a last_updated value.
 | 
					    # no longer maintained as a last_updated value.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,7 +9,7 @@ from zerver.lib.request import has_request_variables, REQ, JsonableError
 | 
				
			|||||||
from zerver.lib.response import json_success
 | 
					from zerver.lib.response import json_success
 | 
				
			||||||
from zerver.models import PushDeviceToken, UserProfile
 | 
					from zerver.models import PushDeviceToken, UserProfile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def validate_token(token_str: bytes, kind: int) -> None:
 | 
					def validate_token(token_str: str, kind: int) -> None:
 | 
				
			||||||
    if token_str == '' or len(token_str) > 4096:
 | 
					    if token_str == '' or len(token_str) > 4096:
 | 
				
			||||||
        raise JsonableError(_('Empty or invalid length token'))
 | 
					        raise JsonableError(_('Empty or invalid length token'))
 | 
				
			||||||
    if kind == PushDeviceToken.APNS:
 | 
					    if kind == PushDeviceToken.APNS:
 | 
				
			||||||
@@ -22,7 +22,7 @@ def validate_token(token_str: bytes, kind: int) -> None:
 | 
				
			|||||||
@human_users_only
 | 
					@human_users_only
 | 
				
			||||||
@has_request_variables
 | 
					@has_request_variables
 | 
				
			||||||
def add_apns_device_token(request: HttpRequest, user_profile: UserProfile,
 | 
					def add_apns_device_token(request: HttpRequest, user_profile: UserProfile,
 | 
				
			||||||
                          token: bytes=REQ(),
 | 
					                          token: str=REQ(),
 | 
				
			||||||
                          appid: str=REQ(default=settings.ZULIP_IOS_APP_ID)
 | 
					                          appid: str=REQ(default=settings.ZULIP_IOS_APP_ID)
 | 
				
			||||||
                          ) -> HttpResponse:
 | 
					                          ) -> HttpResponse:
 | 
				
			||||||
    validate_token(token, PushDeviceToken.APNS)
 | 
					    validate_token(token, PushDeviceToken.APNS)
 | 
				
			||||||
@@ -32,7 +32,7 @@ def add_apns_device_token(request: HttpRequest, user_profile: UserProfile,
 | 
				
			|||||||
@human_users_only
 | 
					@human_users_only
 | 
				
			||||||
@has_request_variables
 | 
					@has_request_variables
 | 
				
			||||||
def add_android_reg_id(request: HttpRequest, user_profile: UserProfile,
 | 
					def add_android_reg_id(request: HttpRequest, user_profile: UserProfile,
 | 
				
			||||||
                       token: bytes=REQ()) -> HttpResponse:
 | 
					                       token: str=REQ()) -> HttpResponse:
 | 
				
			||||||
    validate_token(token, PushDeviceToken.GCM)
 | 
					    validate_token(token, PushDeviceToken.GCM)
 | 
				
			||||||
    add_push_device_token(user_profile, token, PushDeviceToken.GCM)
 | 
					    add_push_device_token(user_profile, token, PushDeviceToken.GCM)
 | 
				
			||||||
    return json_success()
 | 
					    return json_success()
 | 
				
			||||||
@@ -40,7 +40,7 @@ def add_android_reg_id(request: HttpRequest, user_profile: UserProfile,
 | 
				
			|||||||
@human_users_only
 | 
					@human_users_only
 | 
				
			||||||
@has_request_variables
 | 
					@has_request_variables
 | 
				
			||||||
def remove_apns_device_token(request: HttpRequest, user_profile: UserProfile,
 | 
					def remove_apns_device_token(request: HttpRequest, user_profile: UserProfile,
 | 
				
			||||||
                             token: bytes=REQ()) -> HttpResponse:
 | 
					                             token: str=REQ()) -> HttpResponse:
 | 
				
			||||||
    validate_token(token, PushDeviceToken.APNS)
 | 
					    validate_token(token, PushDeviceToken.APNS)
 | 
				
			||||||
    remove_push_device_token(user_profile, token, PushDeviceToken.APNS)
 | 
					    remove_push_device_token(user_profile, token, PushDeviceToken.APNS)
 | 
				
			||||||
    return json_success()
 | 
					    return json_success()
 | 
				
			||||||
@@ -48,7 +48,7 @@ def remove_apns_device_token(request: HttpRequest, user_profile: UserProfile,
 | 
				
			|||||||
@human_users_only
 | 
					@human_users_only
 | 
				
			||||||
@has_request_variables
 | 
					@has_request_variables
 | 
				
			||||||
def remove_android_reg_id(request: HttpRequest, user_profile: UserProfile,
 | 
					def remove_android_reg_id(request: HttpRequest, user_profile: UserProfile,
 | 
				
			||||||
                          token: bytes=REQ()) -> HttpResponse:
 | 
					                          token: str=REQ()) -> HttpResponse:
 | 
				
			||||||
    validate_token(token, PushDeviceToken.GCM)
 | 
					    validate_token(token, PushDeviceToken.GCM)
 | 
				
			||||||
    remove_push_device_token(user_profile, token, PushDeviceToken.GCM)
 | 
					    remove_push_device_token(user_profile, token, PushDeviceToken.GCM)
 | 
				
			||||||
    return json_success()
 | 
					    return json_success()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -32,7 +32,7 @@ def validate_entity(entity: Union[UserProfile, RemoteZulipServer]) -> RemoteZuli
 | 
				
			|||||||
    return entity
 | 
					    return entity
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def validate_bouncer_token_request(entity: Union[UserProfile, RemoteZulipServer],
 | 
					def validate_bouncer_token_request(entity: Union[UserProfile, RemoteZulipServer],
 | 
				
			||||||
                                   token: bytes, kind: int) -> RemoteZulipServer:
 | 
					                                   token: str, kind: int) -> RemoteZulipServer:
 | 
				
			||||||
    if kind not in [RemotePushDeviceToken.APNS, RemotePushDeviceToken.GCM]:
 | 
					    if kind not in [RemotePushDeviceToken.APNS, RemotePushDeviceToken.GCM]:
 | 
				
			||||||
        raise JsonableError(err_("Invalid token type"))
 | 
					        raise JsonableError(err_("Invalid token type"))
 | 
				
			||||||
    server = validate_entity(entity)
 | 
					    server = validate_entity(entity)
 | 
				
			||||||
@@ -84,7 +84,7 @@ def register_remote_server(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
@has_request_variables
 | 
					@has_request_variables
 | 
				
			||||||
def register_remote_push_device(request: HttpRequest, entity: Union[UserProfile, RemoteZulipServer],
 | 
					def register_remote_push_device(request: HttpRequest, entity: Union[UserProfile, RemoteZulipServer],
 | 
				
			||||||
                                user_id: int=REQ(), token: bytes=REQ(),
 | 
					                                user_id: int=REQ(), token: str=REQ(),
 | 
				
			||||||
                                token_kind: int=REQ(validator=check_int),
 | 
					                                token_kind: int=REQ(validator=check_int),
 | 
				
			||||||
                                ios_app_id: Optional[str]=None) -> HttpResponse:
 | 
					                                ios_app_id: Optional[str]=None) -> HttpResponse:
 | 
				
			||||||
    server = validate_bouncer_token_request(entity, token, token_kind)
 | 
					    server = validate_bouncer_token_request(entity, token, token_kind)
 | 
				
			||||||
@@ -106,7 +106,7 @@ def register_remote_push_device(request: HttpRequest, entity: Union[UserProfile,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
@has_request_variables
 | 
					@has_request_variables
 | 
				
			||||||
def unregister_remote_push_device(request: HttpRequest, entity: Union[UserProfile, RemoteZulipServer],
 | 
					def unregister_remote_push_device(request: HttpRequest, entity: Union[UserProfile, RemoteZulipServer],
 | 
				
			||||||
                                  token: bytes=REQ(),
 | 
					                                  token: str=REQ(),
 | 
				
			||||||
                                  token_kind: int=REQ(validator=check_int),
 | 
					                                  token_kind: int=REQ(validator=check_int),
 | 
				
			||||||
                                  user_id: int=REQ(),
 | 
					                                  user_id: int=REQ(),
 | 
				
			||||||
                                  ios_app_id: Optional[str]=None) -> HttpResponse:
 | 
					                                  ios_app_id: Optional[str]=None) -> HttpResponse:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user