Skip to content

Commit

Permalink
WebAuth: Throw HTTPError when we're supposed to
Browse files Browse the repository at this point in the history
  • Loading branch information
WinterPhoenix committed Feb 22, 2025
1 parent f6ff386 commit bc57b85
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions steam/webauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@


API_HEADERS = {
'origin': 'https://steamcommunity.com',
'referer': 'https://steamcommunity.com/',
'accept': 'application/json, text/plain, */*'
'Origin': 'https://steamcommunity.com',
'Referer': 'https://steamcommunity.com/',
'Accept': 'application/json, text/plain, */*'
}

API_URL = 'https://api.steampowered.com/{}Service/{}/v{}'
Expand Down Expand Up @@ -148,14 +148,18 @@ def send_api_request(data, steam_api_interface, steam_api_method,
steam_url = API_URL.format(steam_api_interface, steam_api_method,
steam_api_version)

if steam_api_method == "GetPasswordRSAPublicKey": # It's GET method
res = requests.get(steam_url, timeout=10, headers=API_HEADERS,
params=data)
else: # Every other API endpoints are POST.
res = requests.post(steam_url, timeout=10, headers=API_HEADERS,
data=data)
try:
if steam_api_method == "GetPasswordRSAPublicKey": # It's GET method
res = self.session.get(steam_url, timeout=10, headers=API_HEADERS,
params=data)
else: # Every other API endpoints are POST.
res = self.session.post(steam_url, timeout=10, headers=API_HEADERS,
data=data)

res.raise_for_status()
except requests.exceptions.RequestException as e:
raise HTTPError(str(e))

res.raise_for_status()
return res.json()

def _get_rsa_key(self):
Expand Down Expand Up @@ -301,18 +305,27 @@ def login(self, username: str = '', password: str = '', code: str = None,
self.password = password

self._startLoginSession()

if code:
self._update_login_token(code)

if email_required:
# We do another request, which force steam to send email code
# (otherwise code just not sent).

url = (f'https://login.steampowered.com/jwt/checkdevice/'
f'{self.steam_id}')
res = self.session.post(url, data={
'clientid': self.client_id,
'steamid': self.steam_id
}).json()

try:
res = self.session.post(url, data={
'clientid': self.client_id,
'steamid': self.steam_id
})
res.raise_for_status()
res = res.json()
except requests.exceptions.RequestException as e:
raise HTTPError(str(e))

if res.get('result') == 8:
# This usually mean code sent now.
def end_login(email_code: str):
Expand All @@ -327,6 +340,7 @@ def end_login(email_code: str):
# Actually this must will never be called, because
# Errors can be only like wrong cookies. (Theoretically)
raise WebAuthException("Something invalid went. Try again later.")

self._pollLoginStatus()
self._finalizeLogin()

Expand All @@ -350,10 +364,14 @@ def logout_everywhere(self):
"action": "deauthorize",
"sessionid": session_id
}
resp = self.session.post(
'https://store.steampowered.com/twofactor/manage_action',
data=data
)

try:
resp = self.session.post(
'https://store.steampowered.com/twofactor/manage_action',
data=data
)
except requests.exceptions.RequestException as e:
raise HTTPError(str(e))

return resp.status_code == 200

Expand Down

0 comments on commit bc57b85

Please sign in to comment.