def is_valid_timestamp(b64_content: str) -> bool: """ Return True if `b64_content` decodes to a valid timestamp. If the timestamp is greater than the Discord epoch, it's probably valid. See: https://i.imgur.com/7WdehGn.png """ b64_content = utils.pad_base64(b64_content) try: decoded_bytes = base64.urlsafe_b64decode(b64_content) timestamp = int.from_bytes(decoded_bytes, byteorder="big") except (binascii.Error, ValueError) as e: log.debug(f"Failed to decode token timestamp '{b64_content}': {e}") return False # Seems like newer tokens don't need the epoch added, but add anyway since an upper bound # is not checked. if timestamp + TOKEN_EPOCH >= DISCORD_EPOCH: return True else: log.debug( f"Invalid token timestamp '{b64_content}': smaller than Discord epoch" ) return False
def extract_user_id(b64_content: str) -> t.Optional[int]: """Return a user ID integer from part of a potential token, or None if it couldn't be decoded.""" b64_content = utils.pad_base64(b64_content) try: decoded_bytes = base64.urlsafe_b64decode(b64_content) string = decoded_bytes.decode('utf-8') if not (string.isascii() and string.isdigit()): # This case triggers if there are fancy unicode digits in the base64 encoding, # that means it's not a valid user id. return None return int(string) except (binascii.Error, ValueError): return None
def is_valid_user_id(b64_content: str) -> bool: """ Check potential token to see if it contains a valid Discord user ID. See: https://discordapp.com/developers/docs/reference#snowflakes """ b64_content = utils.pad_base64(b64_content) try: decoded_bytes = base64.urlsafe_b64decode(b64_content) string = decoded_bytes.decode('utf-8') # isdigit on its own would match a lot of other Unicode characters, hence the isascii. return string.isascii() and string.isdigit() except (binascii.Error, ValueError): return False