Return back to passing response parameter into remember and forget methods

This commit is contained in:
Andrew Svetlov 2015-07-29 23:41:16 +03:00
parent bc6efd67a5
commit 29695bd24f
3 changed files with 19 additions and 29 deletions

View File

@ -15,10 +15,10 @@ class AbstractIdentityPolicy(metaclass=abc.ABCMeta):
@asyncio.coroutine @asyncio.coroutine
@abc.abstractmethod @abc.abstractmethod
def remember(self, request, identity, **kwargs): def remember(self, request, response, identity, **kwargs):
"""Remember identity. """Remember identity.
Return MultiDict with headers on this request's response. Modify response object by filling it's headers with remembered user.
An individual identity policy and its consumers can decide on An individual identity policy and its consumers can decide on
the composition and meaning of **kw. the composition and meaning of **kw.
@ -27,8 +27,8 @@ class AbstractIdentityPolicy(metaclass=abc.ABCMeta):
@asyncio.coroutine @asyncio.coroutine
@abc.abstractmethod @abc.abstractmethod
def forget(self, request): def forget(self, request, response):
""" Modify request.response which can be used to 'forget' the """ Modify response which can be used to 'forget' the
current identity on subsequent requests.""" current identity on subsequent requests."""
pass pass

View File

@ -7,17 +7,15 @@ AUTZ_KEY = 'aiohttp_security_autz_policy'
@asyncio.coroutine @asyncio.coroutine
def remember(request, identity, **kwargs): def remember(request, response, identity, **kwargs):
identity_policy = request.app[IDENTITY_KEY] identity_policy = request.app[IDENTITY_KEY]
headers = yield from identity_policy.remember(request, identity, **kwargs) yield from identity_policy.remember(request, response, identity, **kwargs)
return headers
@asyncio.coroutine @asyncio.coroutine
def forget(request): def forget(request, response):
identity_policy = request.app[IDENTITY_KEY] identity_policy = request.app[IDENTITY_KEY]
headers = yield from identity_policy.forget(request) yield from identity_policy.forget(request, response)
return headers
@asyncio.coroutine @asyncio.coroutine

View File

@ -6,6 +6,9 @@ from aiohttp import hdrs, CIMultiDict
from .abc import AbstractIdentityPolicy from .abc import AbstractIdentityPolicy
sentinel = object()
class CookiesIdentityPolicy(AbstractIdentityPolicy): class CookiesIdentityPolicy(AbstractIdentityPolicy):
def __init__(self): def __init__(self):
@ -18,23 +21,12 @@ class CookiesIdentityPolicy(AbstractIdentityPolicy):
return identity return identity
@asyncio.coroutine @asyncio.coroutine
def remember(self, request, identity, **kwargs): def remember(self, request, response, identity, max_age=sentinel,
cookies = http.cookies.SimpleCookie() **kwargs):
max_age = kwargs.pop('max_age', self._max_age) if max_age is sentinel:
cookies[self._cookie_name] = identity max_age = self._max_age
cookie = cookies[self._cookie_name] response.set_cookie(self._cookie_name, max_age=max_age, **kwargs)
cookie['max-age'] = max_age
cookie.update(kwargs)
value = cookie.output(header='')[1:]
result = CIMultiDict({hdrs.SET_COOKIE: value})
return result
@asyncio.coroutine @asyncio.coroutine
def forget(self, request): def forget(self, request, response):
cookies = http.cookies.SimpleCookie() response.del_cookie(self._cookie_name)
cookies[self._cookie_name] = ''
cookie = cookies[self._cookie_name]
value = cookie.output(header='')[1:]
result = CIMultiDict({hdrs.SET_COOKIE: value})
return result