Since it's the same site, I'm just directly validating the Drupal login cookie instead of using OAuth; ``` import hashlib from http.cookies import SimpleCookie from sqlalchemy.sql.expression import select, join, update from aiohttp.web import middleware, HTTPFound import time @middleware async def drupal_authenticator(request, handler): def redirect_login(): redirect_path = request.path if not redirect_path.startswith("/snakeside"): redirect_path = "/snakeside" + redirect_path return HTTPFound("https://{hostname}/user?destination={redirect_path}" .format( hostname=request.host, redirect_path=redirect_path)) db = request.app['db'] t = db.tables cookie = SimpleCookie() cookie.load(request.headers.get('cookie', "")) session_cookie_name = _get_cookie_name(request) session_cookie_value = cookie.get(session_cookie_name, None) if session_cookie_value is None: raise redirect_login() async with db.engine.connect() as conn: res = await conn.execute(select([t.Sessions.uid, t.Users.name, t.Users.status]). select_from(join(t.Sessions, t.Users, t.Sessions.uid == t.Users.uid)) .where(t.Sessions.sid.in_([session_cookie_value.value]))) user_sessions = await res.fetchall() if not len(user_sessions): return redirect_login() user_session = user_sessions[0] user_id, user_name, user_status = user_session if not user_status: raise redirect_login() request['user_id'] = user_id request['user_name'] = user_name epoch_seconds= int(time.time()) async with db.engine.connect() as conn: await conn.execute(update(t.Users).where(t.Users.uid == user_id), [dict(access=epoch_seconds)]) response = await handler(request) return response def _get_cookie_name(request): m = hashlib.sha256() m.update(request.host.encode('utf-8')) return "SESS" + m.hexdigest()[0:32] ```