Work on demo

This commit is contained in:
Andrew Svetlov 2015-11-26 20:09:00 +02:00
parent f4a9232eef
commit 77ed3154e3
3 changed files with 52 additions and 3 deletions

View File

@ -1,10 +1,13 @@
from .abc import AbstractIdentityPolicy, AbstractAuthorizationPolicy from .abc import AbstractIdentityPolicy, AbstractAuthorizationPolicy
from .api import remember, forget, setup, authorized_userid, permits from .api import remember, forget, setup, authorized_userid, permits
from .cookies_identity import CookiesIdentityPolicy
from .session_identity import SessionIdentityPolicy
__version__ = '0.1.0' __version__ = '0.1.0'
__all__ = ('AbstractIdentityPolicy', 'AbstractAuthorizationPolicy', __all__ = ('AbstractIdentityPolicy', 'AbstractAuthorizationPolicy',
'CookiesIdentityPolicy', 'SessionIdentityPolicy',
'remember', 'forget', 'authorized_userid', 'remember', 'forget', 'authorized_userid',
'permits', 'setup') 'permits', 'setup')

View File

@ -4,8 +4,8 @@ from aiohttp_security.authorization import AbstractAuthorizationPolicy
class DBAuthorizationPolicy(AbstractAuthorizationPolicy): class DBAuthorizationPolicy(AbstractAuthorizationPolicy):
def __init__(self, db_pool): def __init__(self, dbengine):
self.db_pool = db_pool self.dbengine = dbengine
@asyncio.coroutine @asyncio.coroutine
def permits(self, identity, permission, context=None): def permits(self, identity, permission, context=None):
@ -18,6 +18,6 @@ class DBAuthorizationPolicy(AbstractAuthorizationPolicy):
@asyncio.coroutine @asyncio.coroutine
def authorized_user_id(self, identity): def authorized_user_id(self, identity):
with (yield from self.db_pool) as conn: with (yield from self.dbengine) as conn:
conn conn
return identity if identity in self.data else None return identity if identity in self.data else None

46
demo/main.py Normal file
View File

@ -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)))