Skip to content

Commit

Permalink
Escape plus sign in the username (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
KapJI authored Jul 11, 2022
1 parent c2154fb commit 1435b1c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
11 changes: 9 additions & 2 deletions glocaltokens/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ def _has_expired(creation_dt: datetime, duration: int) -> bool:
"""Checks if an specified token/object has expired"""
return datetime.now().timestamp() - creation_dt.timestamp() > duration

@staticmethod
def _escape_username(username: str) -> str:
"""Escape plus sign for some exotic accounts"""
return username.replace("+", "%2B")

def get_master_token(self) -> str | None:
"""Get google master token from username and password"""
if self.username is None or self.password is None:
Expand All @@ -220,7 +225,9 @@ def get_master_token(self) -> str | None:
res = {}
try:
res = perform_master_login(
self.username, self.password, self.get_android_id()
self._escape_username(self.username),
self.password,
self.get_android_id(),
)
except ValueError:
LOGGER.error(
Expand Down Expand Up @@ -254,7 +261,7 @@ def get_access_token(self) -> str | None:
LOGGER.error("Username is not set.")
return None
res = perform_oauth(
self.username,
self._escape_username(self.username),
master_token,
self.get_android_id(),
app=ACCESS_TOKEN_APP_NAME,
Expand Down
13 changes: 13 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,19 @@ def test_get_google_devices_json(
received_device[JSON_KEY_NETWORK_DEVICE][JSON_KEY_IP], ip_address
)

def test_username_escaping(self) -> None:
"""Test that plus sign is escaped."""
self.assertEqual(
self.client._escape_username("[email protected]"), "[email protected]"
)
self.assertEqual(
self.client._escape_username("[email protected]"),
"login%[email protected]",
)
self.assertEqual(self.client._escape_username("login"), "login")
# Such account should be impossible to create.
self.assertEqual(self.client._escape_username("login+tag"), "login%2Btag")


class DeviceClientTests(TypeAssertions, TestCase):
"""Device specific unittests"""
Expand Down

0 comments on commit 1435b1c

Please sign in to comment.