hide aberrant behind buckler

This commit is contained in:
iou1name 2019-09-28 04:05:45 -04:00
parent bfb968fc4f
commit 2a5ca2df16
5 changed files with 75 additions and 6 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ __pycache__/
*.swp
*.swo
sync.sh
config.py

View File

@ -12,8 +12,9 @@ from aiohttp_jinja2 import render_template
import config
import events
import rtorrent
import buckler_aiohttp
app = web.Application()
app = web.Application(middlewares=[buckler_aiohttp.buckler_session])
app.on_shutdown.append(rtorrent.stop_watch)
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('templates'))
rtorrent.init()

61
buckler_aiohttp.py Normal file
View File

@ -0,0 +1,61 @@
#!/usr/bin/env python3
"""
Session interface middlewares to integrate the aiohttp app with Buckler.
"""
import json
from datetime import datetime
import aiohttp
from aiohttp import web
import config
@web.middleware
async def buckler_session(request, handler):
"""
Verifies the user with the configured Buckler app and retrieves any
session data they may have. Redirects them to the login page otherwise.
"""
user_id = request.cookies.get('userid')
user_sid = request.cookies.get('session')
url = config.buckler['url'] + '/get_session'
params = {
'app_id': config.buckler['app_id'],
'app_key': config.buckler['app_key'],
'userid': user_id,
'session': user_sid
}
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params) as resp:
data = await resp.json()
if data.get('error'):
raise web.HTTPFound(location=config.buckler['login_url'])
request['session'] = data['session_data']
request['meta'] = data['meta']
resp = await handler(request)
if request['session'] != data['session_data']: # session data modified
url = config.buckler['url'] + '/set_session'
data = json.dumps(request['session'])
session.post(url, params=params, data=data) # TODO: error handle?
last_used = datetime.fromisoformat(request['meta']['last_used'])
now = datetime.now(last_used.tzinfo)
delta = now - last_used
if delta.seconds > 600:
resp.set_cookie(
'userid',
user_id,
mas_age=30*24*60*60,
secure=True,
http_only=True)
resp.set_cookie(
'session',
user_sid,
mas_age=30*24*60*60,
secure=True,
http_only=True)
return resp

View File

@ -1,5 +0,0 @@
#!/usr/bin/env python3
"""
Configuation settings for Aberrant.
"""
prefix = '/aberrant'

11
config.py.template Normal file
View File

@ -0,0 +1,11 @@
#!/usr/bin/env python3
"""
Configuation settings for Aberrant.
"""
prefix = '/aberrant'
buckler = {
'url': "http://192.168.1.100:5400/buckler",
'app_id': 1,
'app_key': """password""",
'login_url': "/buckler/login",
}