Skip to content

Commit

Permalink
Fix MAC address generation (#84)
Browse files Browse the repository at this point in the history
* Fix MAC address generation

* Generate 16 chars android id

* Generate in lowercase
  • Loading branch information
KapJI authored Apr 15, 2021
1 parent 0a5b03d commit 07b3404
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
18 changes: 9 additions & 9 deletions glocaltokens/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from datetime import datetime
import json
import logging
import random
from typing import List, Optional, TypedDict
from uuid import uuid4

from gpsoauth import perform_master_login, perform_oauth
import grpc
Expand Down Expand Up @@ -212,20 +212,20 @@ def __init__(
return

@staticmethod
def _generate_mac_string() -> str:
"""Generate random 14 char long string"""
LOGGER.debug("Generating mac string...")
random_uuid = uuid4()
random_string = str(random_uuid).replace("-", "")[:ANDROID_ID_LENGTH]
mac_string = random_string.upper()
LOGGER.debug("Generated mac string: %s", mac_string)
def _generate_android_id() -> str:
"""Generate random 16 char long string"""
LOGGER.debug("Generating android id...")
mac_string = "".join(
[f"{random.randrange(16):x}" for _ in range(ANDROID_ID_LENGTH)]
)
LOGGER.debug("Generated android id: %s", mac_string)
return mac_string

def get_android_id(self) -> str:
"""Return existing or generate android id"""
if not self.android_id:
LOGGER.debug("There is no stored android_id, generating a new one")
self.android_id = self._generate_mac_string()
self.android_id = self._generate_android_id()
return self.android_id

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion glocaltokens/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
ACCESS_TOKEN_DURATION: Final[int] = 60 * 60
ACCESS_TOKEN_SERVICE: Final[str] = "oauth2:https://www.google.com/accounts/OAuthLogin"

ANDROID_ID_LENGTH: Final[int] = 14
ANDROID_ID_LENGTH: Final[int] = 16
MASTER_TOKEN_LENGTH: Final[int] = 216
ACCESS_TOKEN_LENGTH: Final[int] = 315
LOCAL_AUTH_TOKEN_LENGTH: Final[int] = 108
Expand Down
8 changes: 4 additions & 4 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ def test_get_android_id(self) -> None:
self.assertEqual(android_id, self.client.get_android_id())

# pylint: disable=protected-access
def test_generate_mac_string(self) -> None:
def test_generate_android_id(self) -> None:
"""Test generating mac string"""
mac_string = GLocalAuthenticationTokens._generate_mac_string()
self.assertTrue(len(mac_string) == ANDROID_ID_LENGTH)
android_id = GLocalAuthenticationTokens._generate_android_id()
self.assertTrue(len(android_id) == ANDROID_ID_LENGTH)

# Make sure we get different generated mac string
self.assertNotEqual(
mac_string, GLocalAuthenticationTokens._generate_mac_string()
android_id, GLocalAuthenticationTokens._generate_android_id()
)

def test_has_expired(self) -> None:
Expand Down

0 comments on commit 07b3404

Please sign in to comment.