fixed compile errors and tested decorator working

This commit is contained in:
Sick Yoon 2016-05-12 20:40:48 +09:00
parent 7043b24377
commit c951e3486f
2 changed files with 11 additions and 10 deletions

View File

@ -1,5 +1,5 @@
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, authorize, permits
from .cookies_identity import CookiesIdentityPolicy from .cookies_identity import CookiesIdentityPolicy
from .session_identity import SessionIdentityPolicy from .session_identity import SessionIdentityPolicy
@ -9,5 +9,4 @@ __version__ = '0.1.0'
__all__ = ('AbstractIdentityPolicy', 'AbstractAuthorizationPolicy', __all__ = ('AbstractIdentityPolicy', 'AbstractAuthorizationPolicy',
'CookiesIdentityPolicy', 'SessionIdentityPolicy', 'CookiesIdentityPolicy', 'SessionIdentityPolicy',
'remember', 'forget', 'authorized_userid', 'remember', 'forget', 'authorize', 'permits', 'setup')
'permits', 'setup')

View File

@ -8,20 +8,23 @@ IDENTITY_KEY = 'aiohttp_security_identity_policy'
AUTZ_KEY = 'aiohttp_security_autz_policy' AUTZ_KEY = 'aiohttp_security_autz_policy'
def authorize(required=True): def authorize(required=True, redirect_url=None, permission=None):
def wrapper(f): def wrapper(f):
@asyncio.coroutine @asyncio.coroutine
@functools.wraps(f) @functools.wraps(f)
def wrapped(**kwargs): def wrapped(*args, **kwargs):
assert 'request' in kwargs # assuming first argument is request
assert isinstance(args[0], web.Request)
request = args[0]
if asyncio.iscoroutinefunction(f): if asyncio.iscoroutinefunction(f):
coro = f coro = f
else: else:
coro = asyncio.coroutine(f) coro = asyncio.coroutine(f)
identity = yield from get_user_identity(kwargs['request']) identity = yield from get_user_identity(request)
if not identity and not required: if not identity and required:
raise web.HTTPForbidden() raise web.HTTPForbidden()
return (yield from coro(identity=identity, **kwargs)) kwargs['identity'] = identity
return (yield from coro(*args, **kwargs))
return wrapped return wrapped
return wrapper return wrapper
@ -74,7 +77,6 @@ def get_user_identity(request):
return None return None
identity = yield from identity_policy.identify(request) identity = yield from identity_policy.identify(request)
return identity return identity
authorized_userid method in AbstractAuthorizationPolicy required?
''' '''
@asyncio.coroutine @asyncio.coroutine