Skip to content

Commit

Permalink
simple auth; store user credentials in galaxy db
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleGoogle committed Jul 17, 2019
1 parent 6023886 commit d50db1d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 58 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# galaxy-integration-humblebundle
# galaxy-integration-humblebundle

COMING: WHEN IT'S READY
2 changes: 1 addition & 1 deletion galaxy-integrations-python-api
4 changes: 2 additions & 2 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pip-tools==3.8.0
psutil==5.6.3
psutil==5.6.3
pylint==2.3.1
34 changes: 22 additions & 12 deletions src/backend.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from galaxy.http import create_client_session
from http.cookies import SimpleCookie
import aiohttp
import json
import base64
import logging

from galaxy.http import create_client_session

PROCESS_LOGIN = "https://www.humblebundle.com/processlogin"
ORDER_LIST_URL = "https://www.humblebundle.com/api/v1/user/order"
ORDER_URL = "https://www.humblebundle.com/api/v1/order/{order_id}"



class Backend:
_default_params = {"ajax": "true"}
_default_headers = {
Expand All @@ -23,27 +24,36 @@ def __init__(self):
self._simpleauth_sess = None
self._session = create_client_session(headers=self._default_headers)

async def authenticate(self, cookies):
return ('mock', 'mockname')

async def authenticate(self, auth_cookie: dict):
cookie = SimpleCookie()
cookie[auth_cookie['name']] = auth_cookie['value']
self._session.cookie_jar.update_cookies(cookie)
user_id = self.decode_user_id(auth_cookie['value'])
return (user_id, user_id)

async def _request(*args, **kwargs):
if 'params' not in kwargs:
kwargs['params'] = self._default_params
return await self._session.request(*args, **kwargs)

def decode_user_id(self, _simpleauth_sess):
info = _simpleauth_sess.split('|')[0]
info_padded = info + '=='
j = json.loads(base64.b64decode(info_padded))
return j.user_id
decoded = json.loads(base64.b64decode(info_padded))
logging.debug(decoded)
return decoded['user_id']

async def get_orders_list(self):
res = self._session.request('get',
res = await self._session.request('get',
'https://www.humblebundle.com/api/v1/user/order?ajax=true')
return res.json()

async def get_order_details(self, gamekey):
res = self._session.request('get',
res = await self._session.request('get',
f'https://www.humblebundle.com/api/v1/order/{gamekey}?all_tpkds=true')
return res.json()






51 changes: 10 additions & 41 deletions src/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@
from version import __version__
from backend import Backend

import requests
import http.cookiejar
import http.cookies
import aiohttp.cookiejar
from http.cookies import Morsel, SimpleCookie

from galaxy.http import create_client_session

AUTH_PARAMS = {
"window_title": "Login to HumbleBundle",
Expand All @@ -39,49 +32,25 @@ async def authenticate(self, stored_credentials=None):
return NextStep("web_session", AUTH_PARAMS)

logging.info('stored credentials found')
res = await self.poc(stored_credentials)
# user_id, user_name = await self._backend.authenticate(stored_credentials)
# return Authentication(user_id, user_name)
user_id, user_name = await self._backend.authenticate(stored_credentials)
return Authentication(user_id, user_name)

async def pass_login_credentials(self, step, credentials, cookies):
logging.info(json.dumps(cookies, indent=4))
auth_cookie = next(filter(lambda c: c['name'] == '_simpleauth_sess', cookies))
logging.debug(f'===auth cookie, type {type(auth_cookie)}, val: {auth_cookie}')
self.store_credentials(auth_cookie)

with open(os.path.join(os.path.dirname(__file__), "cookies"), 'w') as f:
json.dump(cookies, f, indent=4)

for c in cookies:
if c['name'] == '_simpleauth_sess':
simple_auth = c['value']

self.store_credentials(simple_auth)
await self.poc(simple_auth)
user_id, user_name = await self._backend.authenticate(cookie_dict)
user_id, user_name = await self._backend.authenticate(auth_cookie)
return Authentication(user_id, user_name)

async def get_owned_games(self):
orders = await self._backend.get_orders_list()
log.info(orders)
return []

async def poc(self, auth_sess_cookie):
self.default_headers = {
"Accept": "application/json",
"Accept-Charset": "utf-8",
"Keep-Alive": "true",
"X-Requested-By": "hb_android_app",
"User-Agent": "Apache-HttpClient/UNAVAILABLE (java 1.4)"
}
self.default_params = {"ajax": "true"}

self.session = create_client_session(headers=self.default_headers)

cookie = SimpleCookie()
cookie['_simpleauth_sess'] = auth_sess_cookie['value']
self.session.cookie_jar.update_cookies(cookie)

ORDER_LIST_URL = "https://www.humblebundle.com/api/v1/user/order?ajax=true"
response = await self.session.request("GET", ORDER_LIST_URL)
j = await response.json()
print(j)

def shutdown(self):
self._backend._session.close()

def main():
create_and_run_plugin(HumbleBundlePlugin, sys.argv)
Expand Down
2 changes: 1 addition & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async def wakeup():
await asyncio.sleep(1)

async def start_test():
await asyncio.start_server(run_server_connection, "127.0.0.1", "7996")
await asyncio.start_server(run_server_connection, "127.0.0.1", "7994")

loop = asyncio.get_event_loop()
loop.create_task(start_test())
Expand Down

0 comments on commit d50db1d

Please sign in to comment.