-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord_token_validator.py
41 lines (30 loc) · 1.12 KB
/
discord_token_validator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Discord token validator
Author: @remiliacn
"""
from base64 import b64decode
from re import match
from sys import argv
import httpx
from loguru import logger
from constants.regex import DISCORD_TOKEN_MATCHER
def validate(token: str, is_bot: bool) -> bool:
if not token:
return False
if not match(DISCORD_TOKEN_MATCHER, token):
logger.warning('Invalid discord token')
return False
headers = {
"Authorization": token.strip() if not is_bot else f'Bot {token.strip()}'
}
response = httpx.get("https://discord.com/api/v10/users/@me", headers=headers)
user_id = token.split(".")[0]
logger.info(
f'Token user id: {b64decode(user_id + '=' * (-len(user_id) % 4)).decode("utf-8")},'
f' status code: {response.status_code}')
if response.status_code != 200 and response.status_code != 401:
logger.warning(f'Received unknown status code {response.status_code}')
return response.status_code == 200
if __name__ == '__main__':
if len(argv) == 2:
logger.info(f'Is token valid: {validate(argv[1], True)}')