Initial commit

This commit is contained in:
Andrew Svetlov
2015-07-08 20:30:24 +03:00
commit 7c702e5df9
26 changed files with 1802 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
import asyncio
import http.cookies
from aiohttp import hdrs, CIMultiDict
from .abc import AbstractIdentityPolicy
class CookiesIdentityPolicy(AbstractIdentityPolicy):
def __init__(self):
self._cookie_name = 'AIOHTTP_SECURITY'
self._max_age = 30 * 24 * 3600
@asyncio.coroutine
def identify(self, request):
identity = request.cookies.get(self._cookie_name)
return identity
@asyncio.coroutine
def remember(self, request, identity, **kwargs):
cookies = http.cookies.SimpleCookie()
max_age = kwargs.pop('max_age', self._max_age)
cookies[self._cookie_name] = identity
cookie = cookies[self._cookie_name]
cookie['max-age'] = max_age
cookie.update(kwargs)
value = cookie.output(header='')[1:]
result = CIMultiDict({hdrs.SET_COOKIE: value})
return result
@asyncio.coroutine
def forget(self, request):
cookies = http.cookies.SimpleCookie()
cookies[self._cookie_name] = ''
cookie = cookies[self._cookie_name]
value = cookie.output(header='')[1:]
result = CIMultiDict({hdrs.SET_COOKIE: value})
return result