2015-07-08 17:30:24 +00:00
|
|
|
from aiohttp import web
|
2015-11-19 12:36:15 +00:00
|
|
|
from aiohttp_security import (remember, forget,
|
2015-07-08 17:30:24 +00:00
|
|
|
AbstractAuthorizationPolicy)
|
2015-11-19 12:36:15 +00:00
|
|
|
from aiohttp_security import setup as _setup
|
2015-07-08 17:30:24 +00:00
|
|
|
from aiohttp_security.cookies_identity import CookiesIdentityPolicy
|
|
|
|
from aiohttp_security.api import IDENTITY_KEY
|
|
|
|
|
|
|
|
|
|
|
|
class Autz(AbstractAuthorizationPolicy):
|
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async def permits(self, identity, permission, context=None):
|
2015-07-08 17:30:24 +00:00
|
|
|
pass
|
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async def authorized_userid(self, identity):
|
2015-07-08 17:30:24 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-09-06 10:06:55 +00:00
|
|
|
async def test_remember(loop, aiohttp_client):
|
2015-07-08 17:30:24 +00:00
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async def handler(request):
|
2015-11-19 12:36:15 +00:00
|
|
|
response = web.Response()
|
2017-12-13 14:51:46 +00:00
|
|
|
await remember(request, response, 'Andrew')
|
2015-11-19 12:36:15 +00:00
|
|
|
return response
|
|
|
|
|
2018-09-06 10:06:55 +00:00
|
|
|
app = web.Application()
|
2015-11-19 12:36:15 +00:00
|
|
|
_setup(app, CookiesIdentityPolicy(), Autz())
|
|
|
|
app.router.add_route('GET', '/', handler)
|
2018-09-06 10:06:55 +00:00
|
|
|
client = await aiohttp_client(app)
|
2017-12-13 14:51:46 +00:00
|
|
|
resp = await client.get('/')
|
2015-11-19 12:36:15 +00:00
|
|
|
assert 200 == resp.status
|
2016-08-30 17:38:59 +00:00
|
|
|
assert 'Andrew' == resp.cookies['AIOHTTP_SECURITY'].value
|
2015-11-19 12:36:15 +00:00
|
|
|
|
|
|
|
|
2018-09-06 10:06:55 +00:00
|
|
|
async def test_identify(loop, aiohttp_client):
|
2015-11-19 12:36:15 +00:00
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async def create(request):
|
2015-11-19 12:36:15 +00:00
|
|
|
response = web.Response()
|
2017-12-13 14:51:46 +00:00
|
|
|
await remember(request, response, 'Andrew')
|
2015-11-19 12:36:15 +00:00
|
|
|
return response
|
2015-07-08 17:30:24 +00:00
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async def check(request):
|
2015-11-19 12:36:15 +00:00
|
|
|
policy = request.app[IDENTITY_KEY]
|
2017-12-13 14:51:46 +00:00
|
|
|
user_id = await policy.identify(request)
|
2015-11-19 12:36:15 +00:00
|
|
|
assert 'Andrew' == user_id
|
|
|
|
return web.Response()
|
|
|
|
|
2018-09-06 10:06:55 +00:00
|
|
|
app = web.Application()
|
2015-11-19 12:36:15 +00:00
|
|
|
_setup(app, CookiesIdentityPolicy(), Autz())
|
|
|
|
app.router.add_route('GET', '/', check)
|
|
|
|
app.router.add_route('POST', '/', create)
|
2018-09-06 10:06:55 +00:00
|
|
|
client = await aiohttp_client(app)
|
2017-12-13 14:51:46 +00:00
|
|
|
resp = await client.post('/')
|
2015-11-19 12:36:15 +00:00
|
|
|
assert 200 == resp.status
|
2017-12-13 14:51:46 +00:00
|
|
|
await resp.release()
|
|
|
|
resp = await client.get('/')
|
2015-11-19 12:36:15 +00:00
|
|
|
assert 200 == resp.status
|
|
|
|
|
|
|
|
|
2018-09-06 10:06:55 +00:00
|
|
|
async def test_forget(loop, aiohttp_client):
|
2015-07-08 17:30:24 +00:00
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async def index(request):
|
2015-11-19 12:36:15 +00:00
|
|
|
return web.Response()
|
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async def login(request):
|
2015-11-19 12:36:15 +00:00
|
|
|
response = web.HTTPFound(location='/')
|
2017-12-13 14:51:46 +00:00
|
|
|
await remember(request, response, 'Andrew')
|
2018-09-06 10:06:55 +00:00
|
|
|
raise response
|
2015-07-08 17:30:24 +00:00
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async def logout(request):
|
2015-11-19 12:36:15 +00:00
|
|
|
response = web.HTTPFound(location='/')
|
2017-12-13 14:51:46 +00:00
|
|
|
await forget(request, response)
|
2018-09-06 10:06:55 +00:00
|
|
|
raise response
|
2015-11-19 12:36:15 +00:00
|
|
|
|
2018-09-06 10:06:55 +00:00
|
|
|
app = web.Application()
|
2015-11-19 12:36:15 +00:00
|
|
|
_setup(app, CookiesIdentityPolicy(), Autz())
|
|
|
|
app.router.add_route('GET', '/', index)
|
|
|
|
app.router.add_route('POST', '/login', login)
|
|
|
|
app.router.add_route('POST', '/logout', logout)
|
2018-09-06 10:06:55 +00:00
|
|
|
client = await aiohttp_client(app)
|
2017-12-13 14:51:46 +00:00
|
|
|
resp = await client.post('/login')
|
2015-11-19 12:36:15 +00:00
|
|
|
assert 200 == resp.status
|
2017-03-21 22:10:14 +00:00
|
|
|
assert str(resp.url).endswith('/')
|
2016-09-27 14:57:11 +00:00
|
|
|
cookies = client.session.cookie_jar.filter_cookies(
|
|
|
|
client.make_url('/'))
|
|
|
|
assert 'Andrew' == cookies['AIOHTTP_SECURITY'].value
|
2017-12-13 14:51:46 +00:00
|
|
|
|
|
|
|
resp = await client.post('/logout')
|
2015-11-19 12:36:15 +00:00
|
|
|
assert 200 == resp.status
|
2017-03-21 22:10:14 +00:00
|
|
|
assert str(resp.url).endswith('/')
|
2016-09-27 14:57:11 +00:00
|
|
|
cookies = client.session.cookie_jar.filter_cookies(
|
|
|
|
client.make_url('/'))
|
|
|
|
assert 'AIOHTTP_SECURITY' not in cookies
|