Initial commit
This commit is contained in:
40
aiohttp_security/cookies_identity.py
Normal file
40
aiohttp_security/cookies_identity.py
Normal 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
|
||||
Reference in New Issue
Block a user