From 77ed3154e34369ac06a6ea2b409d9370f14f4637 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Thu, 26 Nov 2015 20:09:00 +0200 Subject: [PATCH] Work on demo --- aiohttp_security/__init__.py | 3 +++ demo/db_auth.py | 6 ++--- demo/main.py | 46 ++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 demo/main.py diff --git a/aiohttp_security/__init__.py b/aiohttp_security/__init__.py index 5e1acd1..27b7367 100644 --- a/aiohttp_security/__init__.py +++ b/aiohttp_security/__init__.py @@ -1,10 +1,13 @@ from .abc import AbstractIdentityPolicy, AbstractAuthorizationPolicy from .api import remember, forget, setup, authorized_userid, permits +from .cookies_identity import CookiesIdentityPolicy +from .session_identity import SessionIdentityPolicy __version__ = '0.1.0' __all__ = ('AbstractIdentityPolicy', 'AbstractAuthorizationPolicy', + 'CookiesIdentityPolicy', 'SessionIdentityPolicy', 'remember', 'forget', 'authorized_userid', 'permits', 'setup') diff --git a/demo/db_auth.py b/demo/db_auth.py index 14b18e1..52f1572 100644 --- a/demo/db_auth.py +++ b/demo/db_auth.py @@ -4,8 +4,8 @@ from aiohttp_security.authorization import AbstractAuthorizationPolicy class DBAuthorizationPolicy(AbstractAuthorizationPolicy): - def __init__(self, db_pool): - self.db_pool = db_pool + def __init__(self, dbengine): + self.dbengine = dbengine @asyncio.coroutine def permits(self, identity, permission, context=None): @@ -18,6 +18,6 @@ class DBAuthorizationPolicy(AbstractAuthorizationPolicy): @asyncio.coroutine def authorized_user_id(self, identity): - with (yield from self.db_pool) as conn: + with (yield from self.dbengine) as conn: conn return identity if identity in self.data else None diff --git a/demo/main.py b/demo/main.py new file mode 100644 index 0000000..eb7ba96 --- /dev/null +++ b/demo/main.py @@ -0,0 +1,46 @@ +import asyncio + +from aiohttp import web +from aiohttp_session import setup as setup_session +from aiohttp_session.redis_storage import RedisStorage +from aiohttp_security import setup as setup_security +from aiohttp_security import SessionIdentityPolicy +from aiopg.sa import create_engine +from aioredis import create_pool + + +from demo.db_auth import DBAuthorizationPolicy + + +@asyncio.coroutine +def init(loop): + redis_pool = yield from create_pool(('localhost', 6379)) + dbengine = yield from create_engine(user='aiohttp_security', + password='aiohttp_security', + database='aiohttp_security', + host='127.0.0.1') + app = web.Application(loop=loop) + setup_session(app, RedisStorage(redis_pool)) + setup_security(app, + SessionIdentityPolicy(), + DBAuthorizationPolicy(dbengine)) + + app.add_route() + + handler = app.make_handler() + srv = yield from loop.create_server(handler, '127.0.0.1', 8080) + print("Server started at http://127.0.0.1:8080") + return srv, handler + + +@asyncio.coroutine +def finalize(loop, srv, handler): + pass + + +loop = asyncio.get_event_loop() +srv, handler = loop.run_until_complete(init(loop)) +try: + loop.run_forever() +except KeyboardInterrupt: + loop.run_until_complete((finalize(loop)))