Skip to content

Commit

Permalink
v0.4.1. Fix re expression in steam session auth function Function to …
Browse files Browse the repository at this point in the history
…extract open id payload.
  • Loading branch information
somespecialone committed Apr 6, 2024
1 parent 63e8a38 commit 22af4c1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
38 changes: 28 additions & 10 deletions aiosteampy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
"gen_two_factor_code",
"generate_confirmation_key",
"generate_device_id",
"extract_openid_payload",
"do_session_steam_auth",
"get_cookie_value_from_session",
"async_throttle",
"create_ident_code",
"account_id_to_steam_id",
"steam_id_to_account_id",
"id64_to_id32",
"id32_to_id64",
"to_int_boolean",
"restore_from_cookies",
"get_jsonable_cookies",
Expand Down Expand Up @@ -76,11 +79,28 @@ def generate_device_id(steam_id: int) -> str:
)


# TODO make enum with service urls (loot.farm, ...)
def extract_openid_payload(page_text: str) -> dict[str, str]:
"""
Extract steam openid payload (specs) from page html raw text.
Use it if 3rd party websites have extra or non-cookie auth (JWT via service API call, for ex.).
:param page_text:
:return: dict with payload data
"""

# not so beautiful as with bs4 but dependency free
return {
"action": re_search(r"id=\"actionInput\"[\w=\"\s]+value=\"(?P<action>\w+)\"", page_text)["action"],
"openid.mode": re_search(r"name=\"openid\.mode\"[\w=\"\s]+value=\"(?P<mode>\w+)\"", page_text)["mode"],
"openidparams": re_search(r"name=\"openidparams\"[\w=\"\s]+value=\"(?P<params>[\w=/]+)\"", page_text)["params"],
"nonce": re_search(r"name=\"nonce\"[\w=\"\s]+value=\"(?P<nonce>\w+)\"", page_text)["nonce"],
}


async def do_session_steam_auth(session: ClientSession, auth_url: str | URL):
"""
Request auth page, find specs of steam openid and log in through steam with passed session.
Use it when you need to log in 3rd party site trough Steam.
Use it when you need to log in 3rd party site trough Steam using only cookies.
.. seealso:: https://aiosteampy.somespecial.one/examples/auth_3rd_party_site/
Expand All @@ -91,15 +111,9 @@ async def do_session_steam_auth(session: ClientSession, auth_url: str | URL):
r = await session.get(auth_url)
rt = await r.text()

# not so beautiful as with bs4 but dependency free
login_data = {
"action": re_search(r"id=\"actionInput\".+value=\"(?P<action>\w+)\"", rt)["action"],
"openid.mode": re_search(r"name=\"openid\.mode\".+value=\"(?P<mode>\w+)\"", rt)["mode"],
"openidparams": re_search(r"name=\"openidparams\".+value=\"(?P<params>[\w=]+)\"", rt)["params"],
"nonce": re_search(r"name=\"nonce\".+value=\"(?P<nonce>\w+)\"", rt)["nonce"],
}
data = extract_openid_payload(rt)

await session.post("https://steamcommunity.com/openid/login", data=login_data)
await session.post("https://steamcommunity.com/openid/login", data=data, allow_redirects=True)


def get_cookie_value_from_session(session: ClientSession, url: URL, field: str) -> str | None:
Expand Down Expand Up @@ -210,6 +224,10 @@ def account_id_to_steam_id(account_id: int) -> int:
return 1 << 56 | 1 << 52 | 1 << 32 | account_id


id64_to_id32 = steam_id_to_account_id
id32_to_id64 = account_id_to_steam_id


def to_int_boolean(s):
"""Convert something to 1, 0."""

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "aiosteampy"
version = "0.4.0"
version = "0.4.1"
description = "Simple library to trade and interact with steam market, webapi, guard"
license = "MIT"
authors = ["Dmytro Tkachenko <itsme@somespecial.one>"]
Expand Down

0 comments on commit 22af4c1

Please sign in to comment.