From 15275315bb060755c1328c5405b8392fa0a23a02 Mon Sep 17 00:00:00 2001 From: Rossen Georgiev Date: Tue, 23 Feb 2016 10:25:43 +0000 Subject: [PATCH] added get_web_session feature Allows for interacting with steamcommunity and store --- docs/api/steam.client.features.rst | 8 ++++ steam/client/__init__.py | 1 + steam/client/features/__init__.py | 3 +- steam/client/features/web.py | 68 ++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 steam/client/features/web.py diff --git a/docs/api/steam.client.features.rst b/docs/api/steam.client.features.rst index 5ea2de8..43305f9 100644 --- a/docs/api/steam.client.features.rst +++ b/docs/api/steam.client.features.rst @@ -11,6 +11,14 @@ User :undoc-members: :show-inheritance: +Web +--- + +.. automodule:: steam.client.features.web + :members: + :undoc-members: + :show-inheritance: + Misc ---- diff --git a/steam/client/__init__.py b/steam/client/__init__.py index b24cb6f..ebbcfd7 100644 --- a/steam/client/__init__.py +++ b/steam/client/__init__.py @@ -23,6 +23,7 @@ class SteamClient(EventEmitter, FeatureBase): current_jobid = 0 credential_location = None #: location for sentry username = None #: username when logged on + _logger = logger def __init__(self): self.cm = CMClient() diff --git a/steam/client/features/__init__.py b/steam/client/features/__init__.py index f1a8580..855e8d8 100644 --- a/steam/client/features/__init__.py +++ b/steam/client/features/__init__.py @@ -3,8 +3,9 @@ All high level features of :class:`steam.client.SteamClient` are here in seperat """ from steam.client.features.misc import Misc from steam.client.features.user import User +from steam.client.features.web import Web -class FeatureBase(Misc, User): +class FeatureBase(Misc, User, Web): """ This object is used as base to implement all high level functionality. The features are seperated into submodules. diff --git a/steam/client/features/web.py b/steam/client/features/web.py new file mode 100644 index 0000000..7452548 --- /dev/null +++ b/steam/client/features/web.py @@ -0,0 +1,68 @@ +""" +Web related features +""" +from Crypto.Hash import SHA +from Crypto.Random import new as randombytes +from steam import WebAPI +from steam.core.crypto import generate_session_key, symmetric_encrypt +from steam.util.web import make_requests_session + + +class Web(object): + def __init__(self): + super(Web, self).__init__() + + def get_web_session_cookies(self): + """ + Get web authentication cookies via WebAPI's ``AuthenticateUser`` + + .. note:: + only valid during the current steam session + + :return: dict with authentication cookies + :rtype: :class:`dict`, :class:`None` + """ + if not self.logged_on: + return None + + skey, ekey = generate_session_key() + + data = { + 'steamid': self.steam_id, + 'sessionkey': ekey, + 'encrypted_loginkey': symmetric_encrypt(self.cm.webapi_authenticate_user_nonce, skey), + } + + try: + api = WebAPI(None) + resp = api.ISteamUserAuth.AuthenticateUser(**data) + except Exception as exp: + self._logger.debug("get_web_session_cookies error: %s" % str(exp)) + return None + + return { + 'sessionid': SHA.new(randombytes().read(32)).hexdigest(), + 'steamLogin': resp['authenticateuser']['token'], + 'steamLoginSecure': resp['authenticateuser']['tokensecure'], + } + + + + + def get_web_session(self): + """ + See :meth:`get_web_session_cookies` + + :return: authenticated session ready for use + :rtype: :class:`requests.Session`, :class:`None` + """ + cookies = self.get_web_session_cookies() + if cookies is None: + return None + + session = make_requests_session() + + for name, val in cookies.items(): + session.cookies.set(name, val) + + return session