-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.py
201 lines (177 loc) · 6.78 KB
/
auth.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import contextlib
from secrets import token_urlsafe
from httpx import Client
import requests
from dataclasses import dataclass
from time import time
import jwt
import capmonster_python
import json
def magic_decode(string: str):
with contextlib.suppress(json.JSONDecodeError):
return json.loads(string)
with contextlib.suppress(jwt.exceptions.DecodeError):
return jwt.decode(string, options={"verify_signature": False})
raise DecodeException
@dataclass
class User:
username: str = ''
password: str = ''
def __hash__(self):
return hash(self.username)
@dataclass
class Token():
access_token: str
id_token: str
expire: float
created = time()
class Version:
def __init__(self):
self.versions = requests.get("https://valorant-api.com/v1/version").json()["data"]
self.valorant = self.valorant()
self.riot = self.riot()
self.sdk = self.sdk()
def riot(self):
return self.versions["riotClientBuild"]
def sdk(self):
return sdk if (sdk := self.versions["riotClientVersion"].split(".")[1]) else "23.8.0.1382"
def valorant(self):
return self.versions["riotClientVersion"]
version = Version()
class CaptchaFlow:
def __init__(self, session, user):
self.ses = session
self.user = user
def get_captcha_token(self):
data = {
"clientId": "riot-client",
"language": "",
"platform": "windows",
"remember": False,
"riot_identity": {
"language": "it_IT",
"state": "auth",
},
"sdkVersion": version.sdk,
"type": "auth",
}
url = "https://authenticate.riotgames.com/api/v1/login"
response_data = self.ses.post(url, json=data).json()
return response_data
def get_login_token(self, code: str):
data = {
"riot_identity": {
"captcha": f"hcaptcha {code}",
"language": "en_GB",
"password": self.user.password,
"remember": False,
"username": self.user.username
},
"type": "auth"
}
url = "https://authenticate.riotgames.com/api/v1/login"
response_data = self.ses.put(url, json=data).json()
return response_data["success"]["login_token"]
def login_cookies(self, login_token: str):
data = {
"authentication_type": "RiotAuth",
"code_verifier": "",
"login_token": login_token,
"persist_login": False
}
url = "https://auth.riotgames.com/api/v1/login-token"
self.ses.post(url, json=data)
def solve_2captcha(self, data):
from twocaptcha import TwoCaptcha
sitekey = data["captcha"]["hcaptcha"]["key"]
rqdata = data["captcha"]["hcaptcha"]["data"]
solver = TwoCaptcha("<YOUR_API_TOKEN>")
try:
result = solver.hcaptcha(
sitekey=sitekey,
url='https://auth.riotgames.com/',
param1=rqdata
)
return json.loads(json.dumps(result))["code"]
except Exception as e:
print(e)
def solve_captcha(self, data):
sitekey = data["captcha"]["hcaptcha"]["key"]
rqdata = data["captcha"]["hcaptcha"]["data"]
capmonster = capmonster_python.HCaptchaTask("<YOUR_API_TOKEN>") # api key
capmonster.set_user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36")
task_id = capmonster.create_task(website_url="https://auth.riotgames.com", website_key=sitekey, custom_data=rqdata)
result = capmonster.join_task_result(task_id)
return result.get("gRecaptchaResponse")
def captcha_flow(self):
captcha_data = self.get_captcha_token()
captcha_token = self.solve_captcha(captcha_data)
login_token = self.get_login_token(captcha_token)
self.login_cookies(login_token)
class RiotAuth:
def __init__(self, user):
self.session = self.setup_session()
self.setup_auth(self.session)
CaptchaFlow(self.session, user).captcha_flow()
self.token, self.cookies = self.get_auth_data(self.session)
self.entitlements_token = self.get_entitlement(self.session, self.token)
def setup_session(self):
app = "rso-auth"
session = Client()
session.headers.update({
"User-Agent": f'RiotClient/{version.riot} {app} (Windows;10;;Professional, x64)',
"Cache-Control": "no-cache",
"Accept": "application/json",
"Content-Type": "application/json"
})
session.cookies.update({"tdid": "", "asid": "", "did": "", "clid": ""})
return session
def setup_auth(self, session):
data = {
"client_id": "riot-client",
"nonce": token_urlsafe(16),
"redirect_uri": "http://localhost/redirect",
"response_type": "token id_token",
"scope": "account openid",
}
url = "https://auth.riotgames.com/api/v1/authorization"
r = session.post(url, json=data)
return r
def get_auth_data(self, session):
r = self.setup_auth(session)
cookies = dict(r.cookies)
data = r.json()
if "error" in data: raise Exception(data["error"])
uri = data["response"]["parameters"]["uri"]
token = self.get_token(uri)
return token, cookies
def get_token(self, uri: str) -> Token:
access_token = uri.split("access_token=")[1].split("&scope")[0]
token_id = uri.split("id_token=")[1].split("&")[0]
expires_in = uri.split("expires_in=")[1].split("&")[0]
timestamp = time() + float(expires_in)
token = Token(access_token, token_id, timestamp)
return token
def get_entitlement(self, session, token: Token) -> str:
app = 'entitlements'
url = "https://entitlements.auth.riotgames.com/api/token/v1"
headers = {
"Accept-Encoding": "gzip, deflate, br",
"Authorization": f"Bearer {token.access_token}",
"User-Agent": f'RiotClient/{version.riot} {app} (Windows;10;;Professional, x64)',
}
r = session.post(url, headers=headers, json={})
data = magic_decode(r.text)
return data["entitlements_token"]
def get_user_info(session, token: Token) -> str:
headers = {
"Accept-Encoding": "gzip, deflate, br",
"Authorization": f"Bearer {token.access_token}",
}
r = session.post("https://auth.riotgames.com/userinfo", headers=headers, json={})
return magic_decode(r.text)
if __name__ == "__main__":
user = User("username", "password")
client = RiotAuth(user)
user = get_user_info(client.session, client.token)
print(user)