2016-08-30 17:38:59 +00:00
|
|
|
.. _aiohttp-security-example-db-auth:
|
|
|
|
|
|
|
|
===========================================
|
|
|
|
Permissions with PostgreSQL-based storage
|
|
|
|
===========================================
|
|
|
|
|
|
|
|
Make sure that you have PostgreSQL and Redis servers up and running.
|
|
|
|
If you want the full source code in advance or for comparison, check out
|
|
|
|
the `demo source`_.
|
|
|
|
|
|
|
|
.. _demo source:
|
|
|
|
https://github.com/aio-libs/aiohttp_security/tree/master/demo
|
|
|
|
|
|
|
|
.. _passlib:
|
2017-07-24 06:58:54 +00:00
|
|
|
https://passlib.readthedocs.io
|
2016-08-30 17:38:59 +00:00
|
|
|
|
|
|
|
Database
|
|
|
|
--------
|
|
|
|
|
|
|
|
Launch these sql scripts to init database and fill it with sample data:
|
|
|
|
|
|
|
|
``psql template1 < demo/sql/init_db.sql``
|
|
|
|
|
2018-01-24 10:29:20 +00:00
|
|
|
and
|
2016-08-30 17:38:59 +00:00
|
|
|
|
|
|
|
``psql template1 < demo/sql/sample_data.sql``
|
|
|
|
|
|
|
|
|
2018-01-24 10:29:20 +00:00
|
|
|
Now you have two tables:
|
|
|
|
|
|
|
|
- for storing users
|
2016-08-30 17:38:59 +00:00
|
|
|
|
|
|
|
+--------------+
|
|
|
|
| users |
|
|
|
|
+==============+
|
|
|
|
| id |
|
|
|
|
+--------------+
|
|
|
|
| login |
|
|
|
|
+--------------+
|
|
|
|
| passwd |
|
|
|
|
+--------------+
|
|
|
|
| is_superuser |
|
|
|
|
+--------------+
|
|
|
|
| disabled |
|
|
|
|
+--------------+
|
|
|
|
|
2018-01-24 10:29:20 +00:00
|
|
|
- for storing their permissions
|
2016-08-30 17:38:59 +00:00
|
|
|
|
|
|
|
+-----------------+
|
|
|
|
| permissions |
|
|
|
|
+=================+
|
|
|
|
| id |
|
|
|
|
+-----------------+
|
|
|
|
| user_id |
|
|
|
|
+-----------------+
|
|
|
|
| permission_name |
|
|
|
|
+-----------------+
|
|
|
|
|
|
|
|
|
|
|
|
Writing policies
|
|
|
|
----------------
|
|
|
|
|
|
|
|
You need to implement two entities: *IdentityPolicy* and *AuthorizationPolicy*.
|
|
|
|
First one should have these methods: *identify*, *remember* and *forget*.
|
|
|
|
For second one: *authorized_userid* and *permits*. We will use built-in
|
|
|
|
*SessionIdentityPolicy* and write our own database-based authorization policy.
|
|
|
|
|
2018-01-24 10:29:20 +00:00
|
|
|
In our example we will lookup database by user login and if presents then return
|
2016-08-30 17:38:59 +00:00
|
|
|
this identity::
|
|
|
|
|
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async def authorized_userid(self, identity):
|
2018-01-24 10:29:20 +00:00
|
|
|
async with self.dbengine as conn:
|
2016-08-30 17:38:59 +00:00
|
|
|
where = sa.and_(db.users.c.login == identity,
|
|
|
|
sa.not_(db.users.c.disabled))
|
|
|
|
query = db.users.count().where(where)
|
2017-12-13 14:51:46 +00:00
|
|
|
ret = await conn.scalar(query)
|
2016-08-30 17:38:59 +00:00
|
|
|
if ret:
|
|
|
|
return identity
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2018-01-24 10:29:20 +00:00
|
|
|
For permission checking we will fetch the user first, check if he is superuser
|
2016-08-30 17:38:59 +00:00
|
|
|
(all permissions are allowed), otherwise check if permission is explicitly set
|
|
|
|
for that user::
|
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async def permits(self, identity, permission, context=None):
|
2016-08-30 17:38:59 +00:00
|
|
|
if identity is None:
|
|
|
|
return False
|
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async with self.dbengine as conn:
|
2016-08-30 17:38:59 +00:00
|
|
|
where = sa.and_(db.users.c.login == identity,
|
|
|
|
sa.not_(db.users.c.disabled))
|
|
|
|
query = db.users.select().where(where)
|
2017-12-13 14:51:46 +00:00
|
|
|
ret = await conn.execute(query)
|
|
|
|
user = await ret.fetchone()
|
2016-08-30 17:38:59 +00:00
|
|
|
if user is not None:
|
|
|
|
user_id = user[0]
|
2018-01-24 10:29:20 +00:00
|
|
|
is_superuser = user[3]
|
2016-08-30 17:38:59 +00:00
|
|
|
if is_superuser:
|
|
|
|
return True
|
|
|
|
|
|
|
|
where = db.permissions.c.user_id == user_id
|
|
|
|
query = db.permissions.select().where(where)
|
2017-12-13 14:51:46 +00:00
|
|
|
ret = await conn.execute(query)
|
|
|
|
result = await ret.fetchall()
|
2016-08-30 17:38:59 +00:00
|
|
|
if ret is not None:
|
|
|
|
for record in result:
|
|
|
|
if record.perm_name == permission:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
Setup
|
|
|
|
-----
|
|
|
|
|
|
|
|
Once we have all the code in place we can install it for our application::
|
|
|
|
|
|
|
|
from aiohttp_session.redis_storage import RedisStorage
|
|
|
|
from aiohttp_security import setup as setup_security
|
|
|
|
from aiohttp_security import SessionIdentityPolicy
|
|
|
|
from aiopg.sa import create_engine
|
|
|
|
from aioredis import create_pool
|
|
|
|
|
|
|
|
from .db_auth import DBAuthorizationPolicy
|
|
|
|
|
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async def init(loop):
|
|
|
|
redis_pool = await create_pool(('localhost', 6379))
|
|
|
|
dbengine = await create_engine(user='aiohttp_security',
|
|
|
|
password='aiohttp_security',
|
|
|
|
database='aiohttp_security',
|
|
|
|
host='127.0.0.1')
|
2016-08-30 17:38:59 +00:00
|
|
|
app = web.Application(loop=loop)
|
|
|
|
setup_session(app, RedisStorage(redis_pool))
|
|
|
|
setup_security(app,
|
|
|
|
SessionIdentityPolicy(),
|
|
|
|
DBAuthorizationPolicy(dbengine))
|
|
|
|
return app
|
|
|
|
|
|
|
|
|
|
|
|
Now we have authorization and can decorate every other view with access rights
|
2018-01-24 10:29:20 +00:00
|
|
|
based on permissions. There are already implemented two decorators::
|
2016-08-30 17:38:59 +00:00
|
|
|
|
2018-01-24 10:29:20 +00:00
|
|
|
from aiohttp_security import has_permission, login_required
|
2016-08-30 17:38:59 +00:00
|
|
|
|
2018-01-24 10:29:20 +00:00
|
|
|
For each view you need to protect - just apply the decorator on it::
|
2016-08-30 17:38:59 +00:00
|
|
|
|
|
|
|
class Web:
|
2018-01-24 10:29:20 +00:00
|
|
|
@has_permission('protected')
|
2017-12-13 14:51:46 +00:00
|
|
|
async def protected_page(self, request):
|
2016-08-30 17:38:59 +00:00
|
|
|
response = web.Response(body=b'You are on protected page')
|
|
|
|
return response
|
|
|
|
|
2018-01-24 10:29:20 +00:00
|
|
|
or::
|
|
|
|
|
|
|
|
class Web:
|
|
|
|
@login_required
|
|
|
|
async def logout(self, request):
|
|
|
|
response = web.Response(body=b'You have been logged out')
|
|
|
|
await forget(request, response)
|
|
|
|
return response
|
2016-08-30 17:38:59 +00:00
|
|
|
|
2018-01-24 10:29:20 +00:00
|
|
|
If someone try to access that protected page he will see::
|
2016-08-30 17:38:59 +00:00
|
|
|
|
2018-01-24 10:29:20 +00:00
|
|
|
403: Forbidden
|
2016-08-30 17:38:59 +00:00
|
|
|
|
|
|
|
|
2018-01-24 10:29:20 +00:00
|
|
|
The best part of it - you can implement any logic you want until it
|
2016-08-30 17:38:59 +00:00
|
|
|
follows the API conventions.
|
|
|
|
|
|
|
|
Launch application
|
|
|
|
------------------
|
|
|
|
|
|
|
|
For working with passwords there is a good library passlib_. Once you've
|
|
|
|
created some users you want to check their credentials on login. Similar
|
2018-01-24 10:29:20 +00:00
|
|
|
function may do what you are trying to accomplish::
|
2016-08-30 17:38:59 +00:00
|
|
|
|
|
|
|
from passlib.hash import sha256_crypt
|
|
|
|
|
2017-12-13 14:51:46 +00:00
|
|
|
async def check_credentials(db_engine, username, password):
|
|
|
|
async with db_engine as conn:
|
2016-08-30 17:38:59 +00:00
|
|
|
where = sa.and_(db.users.c.login == username,
|
|
|
|
sa.not_(db.users.c.disabled))
|
|
|
|
query = db.users.select().where(where)
|
2017-12-13 14:51:46 +00:00
|
|
|
ret = await conn.execute(query)
|
|
|
|
user = await ret.fetchone()
|
2016-08-30 17:38:59 +00:00
|
|
|
if user is not None:
|
|
|
|
hash = user[2]
|
|
|
|
return sha256_crypt.verify(password, hash)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
Final step is to launch your application::
|
|
|
|
|
2018-01-24 10:29:20 +00:00
|
|
|
python demo/database_auth/main.py
|
2016-08-30 17:38:59 +00:00
|
|
|
|
|
|
|
|
2018-01-24 10:29:20 +00:00
|
|
|
Try to login with admin/moderator/user accounts (with **password** password)
|
2016-08-30 17:38:59 +00:00
|
|
|
and access **/public** or **/protected** endpoints.
|