86 Commits

Author SHA1 Message Date
Andrew Svetlov
aff9a6a915 Bump to 0.2.0 2017-11-17 17:54:46 +02:00
Andrew Lytvyn
92e6fec6f5 is_anonymous, login_required, has_permission helpers (#114)
* add is_anonymous helper function and login_required, has_permission decorators

* add docs for `is_anonymous`, `login_required` and `has_permission` functions

* permission can be `enum.Enum` object; cover with tests

* isort fix
2017-11-17 17:51:40 +02:00
pyup.io bot
810312b508 Scheduled weekly dependency update for week 46 (#113)
* Update coverage from 4.4.1 to 4.4.2

* Update aiohttp-session from 1.0.1 to 1.2.0

* Update aioredis from 0.3.4 to 0.3.5

* Update aiohttp from 2.3.1 to 2.3.2
2017-11-13 16:46:55 +02:00
pyup.io bot
18a04b879e Scheduled weekly dependency update for week 44 (#111)
* Update flake8 from 3.4.1 to 3.5.0

* Update sphinx from 1.6.4 to 1.6.5

* Update aioredis from 0.3.3 to 0.3.4

* Update aiohttp from 2.2.5 to 2.3.1
2017-10-30 16:41:06 +02:00
Andrew Svetlov
cd833edb6c Update trove classifiers 2017-10-17 12:41:11 +03:00
Andrew Svetlov
0694e6e084 Add autodeployment on travis 2017-10-17 12:39:18 +03:00
Andrew Svetlov
63870d992e Bump to 0.1.2 2017-10-17 12:37:09 +03:00
Alex Kuzmenko
aef24fae3d aiohttp-session is not required (#107)
* aiohttp_session is not required

* fix code coverage

* has_aiohttp_session -> HAS_AIOHTTP_SESSION
2017-10-17 12:32:53 +03:00
Nikolay Novik
747ec05cfb Merge pull request #109 from alxpy/alxpy-patch-7
Added new python versions to travis-ci
2017-10-10 16:51:15 +03:00
Alex Kuzmenko
66391b9f25 added new python versions to travis-ci 2017-10-10 16:44:17 +03:00
pyup.io bot
a46ca6ec68 Scheduled weekly dependency update for week 41 (#106)
* Update pytest from 3.2.2 to 3.2.3

* Update sphinx from 1.6.3 to 1.6.4
2017-10-10 14:39:07 +03:00
Devin Fee
1a9ab6424e added simplistic dictionary_auth example (#105) 2017-09-19 11:54:37 +03:00
pyup.io bot
b0895806af Scheduled weekly dependency update for week 38 (#104)
* Update pytest-cov from 2.4.0 to 2.5.1

* Update coverage from 4.3.4 to 4.4.1

* Update pytest-aiohttp from 0.1.2 to 0.1.3
2017-09-18 18:27:15 +03:00
Nikolay Novik
dbac083096 Merge pull request #102 from aio-libs/pyup-update-aiohttp-session-1.0.0-to-1.0.1
Update aiohttp-session to 1.0.1
2017-09-14 21:11:53 +03:00
pyup.io bot
5811ab90c8 Update pytest from 3.2.0 to 3.2.2 (#99) 2017-09-14 13:20:20 +03:00
Andrew Svetlov
4a0a078976 Create .pyup.yml 2017-09-14 13:20:00 +03:00
pyup-bot
22c51b5fd0 Update aiohttp-session from 1.0.0 to 1.0.1 2017-09-14 13:19:18 +03:00
pyup.io bot
921506c3c4 Update aiopg from 0.13.0 to 0.13.1 (#100) 2017-09-14 13:18:49 +03:00
pyup.io bot
d30a158345 Update flake8 from 3.3.0 to 3.4.1 (#97) 2017-08-04 12:59:21 +03:00
pyup.io bot
df198ae8d1 Update aiohttp from 2.2.3 to 2.2.5 (#96) 2017-08-04 12:26:39 +03:00
pyup.io bot
05a4cc03d2 Update aiohttp-session from 0.8.0 to 1.0.0 (#91) 2017-08-04 12:25:48 +03:00
pyup.io bot
bbb41dd3ad Update pytest from 3.1.3 to 3.2.0 (#94) 2017-08-02 09:29:27 +03:00
pyup.io bot
f716fb353b Update pytest from 3.1.2 to 3.1.3 (#89) 2017-07-24 09:59:09 +03:00
Thijs Triemstra
e99ce6ba23 update passlib url (#90) 2017-07-24 09:58:54 +03:00
pyup.io bot
6ebf9355fc Update aiohttp from 2.2.2 to 2.2.3 (#88) 2017-07-04 19:26:50 +03:00
pyup.io bot
d473f57e8e Update aioredis from 0.3.2 to 0.3.3 (#84) 2017-07-03 13:04:48 +03:00
pyup.io bot
7ab68c9f6d Update sphinx from 1.6.2 to 1.6.3 (#85) 2017-07-03 13:04:30 +03:00
pyup.io bot
b7dbe3ae16 Update aiohttp from 2.2.0 to 2.2.2 (#87) 2017-07-03 13:04:10 +03:00
Nikolay Novik
1b52d4b76b Merge pull request #82 from aio-libs/pyup-update-aiohttp-2.1.0-to-2.2.0
Update aiohttp to 2.2.0
2017-06-29 00:31:08 +03:00
Nikolay Novik
5d89f5feed Merge pull request #83 from aio-libs/pyup-update-aioredis-0.3.1-to-0.3.2
Update aioredis to 0.3.2
2017-06-29 00:30:54 +03:00
pyup-bot
640ae0fe72 Update aioredis from 0.3.1 to 0.3.2 2017-06-21 11:29:34 +03:00
pyup.io bot
352d1f4526 Update pytest from 3.1.1 to 3.1.2 (#81) 2017-06-20 16:10:45 +03:00
pyup-bot
aab14aedae Update aiohttp from 2.1.0 to 2.2.0 2017-06-20 14:51:33 +03:00
Nikolay Novik
6e0864d088 Merge pull request #80 from aio-libs/pyup-update-pytest-3.1.0-to-3.1.1
Update pytest to 3.1.1
2017-06-08 22:23:59 +03:00
pyup-bot
4de9d8f5f9 Update pytest from 3.1.0 to 3.1.1 2017-05-31 05:14:57 -07:00
Thanos Lefteris
1009a29a63 Add asyncio trove classifier (#69) 2017-05-28 14:08:53 -07:00
pyup.io bot
867c8d8e07 Update pytest from 3.0.7 to 3.1.0 (#77) 2017-05-28 14:08:13 -07:00
pyup.io bot
118f1f473a Update sphinx from 1.5.5 to 1.6.2 (#79) 2017-05-28 14:07:47 -07:00
pyup.io bot
d276618899 Update aioredis from 0.3.0 to 0.3.1 (#71) 2017-05-26 09:37:32 -07:00
pyup.io bot
86a7733815 Update aiohttp from 2.0.7 to 2.1.0 (#78) 2017-05-26 09:37:13 -07:00
pyup.io bot
8432f680e4 Update passlib from 1.7.0 to 1.7.1 (#67) 2017-04-13 20:45:28 +03:00
pyup.io bot
0d2d2a1c96 Update aiohttp from 2.0.6 to 2.0.7 (#66) 2017-04-13 19:47:20 +03:00
Nikolay Novik
d714f82319 Merge pull request #38 from aio-libs/pyup-update-aioredis-0.2.9-to-0.3.0
Update aioredis to 0.3.0
2017-04-09 10:36:28 +03:00
Nikolay Novik
14bd43fe4f Merge pull request #58 from aio-libs/pyup-update-flake8-3.2.1-to-3.3.0
Update flake8 to 3.3.0
2017-04-09 10:35:50 +03:00
Nikolay Novik
401ebf130a Merge pull request #62 from aio-libs/pyup-update-sphinx-1.5.3-to-1.5.5
Update sphinx to 1.5.5
2017-04-09 10:35:38 +03:00
Nikolay Novik
c84a82d3dc Merge pull request #63 from aio-libs/pyup-update-aiohttp-2.0.3-to-2.0.6
Update aiohttp to 2.0.6
2017-04-09 10:35:27 +03:00
pyup-bot
08bdad3d85 Update aiohttp from 2.0.3 to 2.0.6 2017-04-06 18:26:21 +03:00
pyup-bot
d7b6a911b9 Update sphinx from 1.5.3 to 1.5.5 2017-04-03 18:48:41 +03:00
pyup-bot
65cebd3ff0 Update flake8 from 3.2.1 to 3.3.0 2017-03-26 00:59:55 +02:00
Nikolay Novik
c13d9b4c70 Merge pull request #49 from aio-libs/pyup-update-sphinx-1.5.2-to-1.5.3
Update sphinx to 1.5.3
2017-03-26 00:59:35 +02:00
Nikolay Novik
d591ad3caa Merge pull request #50 from aio-libs/pyup-update-pytest-3.0.6-to-3.0.7
Update pytest to 3.0.7
2017-03-26 00:59:25 +02:00
Nikolay Novik
73e55280b7 Merge pull request #57 from aio-libs/pyup-update-aiohttp-1.2.0-to-2.0.3
Update aiohttp to 2.0.3
2017-03-25 17:10:03 +02:00
pyup-bot
367d388cff Update aiohttp from 1.2.0 to 2.0.3 2017-03-24 23:50:06 +02:00
Nickolai Novik
d652f29df5 make compatible tests 2017-03-22 00:10:14 +02:00
pyup-bot
face5ddaa2 Update pytest from 3.0.6 to 3.0.7 2017-03-14 23:25:40 +02:00
pyup-bot
c523b4fa49 Update sphinx from 1.5.2 to 1.5.3 2017-02-25 22:30:33 -09:00
Nikolay Novik
6e4355ce3c Merge pull request #40 from alxpy/alxpy-patch-2
Fix code coverage
2017-01-25 16:16:15 +02:00
Nikolay Novik
4c92553761 Merge pull request #41 from alxpy/alxpy-patch-3
Add some labels
2017-01-25 16:15:52 +02:00
Alex Kuzmenko
d76ecf59fc Add pypi label 2017-01-25 13:29:57 +02:00
Alex Kuzmenko
a990a657c4 Add some labels 2017-01-25 00:51:02 +02:00
Alex Kuzmenko
ede5beeb41 Fix code coverage 2017-01-25 00:39:39 +02:00
Nikolay Novik
d22d3a1d1a Merge pull request #36 from aio-libs/pyup-update-sphinx-1.5.1-to-1.5.2
Update sphinx to 1.5.2
2017-01-24 22:40:35 +02:00
pyup-bot
962e022090 Update aioredis from 0.2.9 to 0.3.0 2017-01-24 22:30:10 +02:00
pyup-bot
4b1740a8e3 Update sphinx from 1.5.1 to 1.5.2 2017-01-24 22:29:42 +02:00
Nikolay Novik
a2bf4c9c00 Merge pull request #34 from aio-libs/pyup-update-pytest-3.0.5-to-3.0.6
Update pytest to 3.0.6
2017-01-24 22:29:18 +02:00
Nikolay Novik
c4f3c06476 Merge pull request #35 from aio-libs/pyup-update-coverage-4.3-to-4.3.4
Update coverage to 4.3.4
2017-01-24 22:29:02 +02:00
pyup-bot
929468b684 Update coverage from 4.3 to 4.3.4 2017-01-24 22:11:22 +02:00
pyup-bot
7e68c44de6 Update pytest from 3.0.5 to 3.0.6 2017-01-24 22:11:15 +02:00
Nikolay Novik
7d4a4802f3 Merge pull request #33 from aio-libs/jettify-patch-1
Update .travis.yml
2017-01-24 22:10:49 +02:00
Nikolay Novik
7bd5c37c8a Update .travis.yml 2017-01-24 22:06:19 +02:00
Nikolay Novik
87942913c9 Merge pull request #30 from alxpy/update_readme
Update readme
2017-01-24 14:50:49 +02:00
Nikolay Novik
69743859a1 Merge pull request #32 from alxpy/travis_ci_labels
Add travis-ci labels
2017-01-24 14:49:09 +02:00
Alex Kuzmenko
1d596c4a3d Add travis-ci labels 2017-01-24 14:36:34 +02:00
Nikolay Novik
88b8ffbf5c Merge pull request #20 from alanc10n/DocOptionsFix
Remove unknown option github_style
2017-01-24 12:59:59 +02:00
Alex Kuzmenko
076c7f1d5f Update readme 2017-01-24 02:31:06 +02:00
Alan Christianson
474209d945 Remove unknown option github_style 2017-01-09 13:46:48 -07:00
pyup.io bot
131ee39bf6 Update coverage from 4.2 to 4.3 (#17) 2016-12-28 05:53:49 +02:00
pyup.io bot
8163d72b2d Update aiohttp from 1.1.6 to 1.2.0 (#15) 2016-12-28 05:53:14 +02:00
Nikolay Novik
679db05664 Merge pull request #14 from aio-libs/pyup-update-sphinx-1.5-to-1.5.1
Update sphinx to 1.5.1
2016-12-13 18:17:03 +02:00
pyup-bot
1ace967956 Update sphinx from 1.5 to 1.5.1 2016-12-13 18:17:14 +03:00
pyup.io bot
67d5590439 Update pytest from 3.0.4 to 3.0.5 (#13) 2016-12-05 18:37:59 +02:00
pyup.io bot
deae9e74e1 Update sphinx from 1.4.9 to 1.5 (#12) 2016-12-05 14:03:16 +02:00
pyup.io bot
76a6ecf86a Update aiohttp-session from 0.7.1 to 0.8.0 (#11) 2016-12-05 14:03:01 +02:00
pyup.io bot
2fc73acfb7 Update aiopg from 0.12.0 to 0.13.0 (#10) 2016-12-02 16:09:24 +02:00
pyup.io bot
2202eb3faa Update aiohttp from 1.1.5 to 1.1.6 (#9) 2016-12-02 15:37:00 +02:00
pyup.io bot
9cd055b36e Initial Update (#8)
* Pin flake8 to latest version 3.2.1

* Update passlib from 1.6.5 to 1.7.0

* Pin aiohttp to latest version 1.1.5

* Pin aiohttp-session to latest version 0.7.1

* Update aioredis from 0.2.8 to 0.2.9

* Pin pep257 to latest version 0.7.0

* Pin pytest-aiohttp to latest version 0.1.2

* Pin aiopg to latest version 0.12.0

* Pin pytest to latest version 3.0.4

* Pin sphinx to latest version 1.4.9

* Pin coverage to latest version 4.2

* Pin pytest-cov to latest version 2.4.0
2016-11-24 13:13:11 +02:00
25 changed files with 545 additions and 39 deletions

4
.pyup.yml Normal file
View File

@@ -0,0 +1,4 @@
# Label PRs with `deps-update` label
label_prs: deps-update
schedule: every week

View File

@@ -1,7 +1,9 @@
language: python
python:
- 3.4
- 3.4.3
- 3.5
- 3.6
- 3.7-dev
- nightly
install:
@@ -10,8 +12,7 @@ install:
- pip install codecov
script:
- flake8 aiohttp_security tests
- coverage run --source=aiohttp_security setup.py test
- make coverage
after_success:
- codecov
@@ -20,3 +21,14 @@ env:
matrix:
- PYTHONASYNCIODEBUG=x
- PYTHONASYNCIODEBUG=
deploy:
provider: pypi
user: andrew.svetlov
password:
secure: "JdBvuOBA/198ognVDOY/qZpIKGXfCx47725kyJo/SpQ3nP+x0GLZb3PMQkR0jfSWWkx6Sisk3vOCYsoWclPyPzp+o4ZpfM8yAjHNFmtbr+k+XJdUEApEiWb6/Y3g7DCyY2Qa/L8IYlyABPWrrJI/nld2sKm5kmhFpR/z3HfeFtINP6Ivp34dUOkeRP6kOvCi9d6GyWnvTRnhlybAnk/Ngrroh8XrbKHdDv0zkQkshF8+pmxVzwao4C6S5ld5cFXIYZHLBA9lNC3zgvOMuFeGPUEN9vab3q77MvaiMIuTC9QjcgIhfw3gabH2u7knqfFzqqzXMaVptx5z8o1JtsxMyYt5NVBqS4NPIljpZjaoS/CASHJlRxniJiYfjvjOtFEcfGMNtZj8ZYsGR0nuP2jwzgpEHHWIs4qL0Y8h9t7pGirxCuQcnY10sr+Y+JKaZNJsugNLgbqE2aaZUye5gjDcEj9WY8kKNZXucLP7c0McJuwPqplDEO4CQouMttcKSYkA0QoETmpAFqaXCaMs3p/glOoU2ZyHSH9mXWir69yo84ymb2NlGPMTAstXlv/g/oLmLMSq7lbl6cSUnO1/wxBGlyfv5AAq/75YUaqsgYofzN5CjUgA3m6NedvbWxLUJaxVQ7nduYGEQKDvGEBmzCNv6CdVRCjQ9J1xX3XzkVheQGc="
distributions: "sdist bdist_wheel"
on:
tags: true
all_branches: true
python: 3.6

View File

@@ -1,2 +1,12 @@
Changes
=======
0.2.0 (2017-11-17)
------------------
- Add `is_anonymous`, `login_required`, `has_permission` helpers (#114)
0.1.2 (2017-10-17)
------------------
- Make aiohttp-session optional dependency (#107)

View File

@@ -1,5 +1,13 @@
aiohttp_security
================
.. image:: https://travis-ci.org/aio-libs/aiohttp-security.svg?branch=master
:target: https://travis-ci.org/aio-libs/aiohttp-security
.. image:: https://codecov.io/github/aio-libs/aiohttp-security/coverage.svg?branch=master
:target: https://codecov.io/github/aio-libs/aiohttp-security
.. image:: https://readthedocs.org/projects/aiohttp-security/badge/?version=latest
:target: https://aiohttp-security.readthedocs.io/
.. image:: https://img.shields.io/pypi/v/aiohttp-security.svg
:target: https://pypi.python.org/pypi/aiohttp-security
The library provides identity and autorization for `aiohttp.web`__.
@@ -13,6 +21,11 @@ To install type ``pip install aiohttp_security``.
Launch ``make doc`` and see examples or look under **demo** directory for a
sample project.
Documentation
-------------
https://aiohttp-security.readthedocs.io/
Develop
-------

View File

@@ -1,13 +1,14 @@
from .abc import AbstractIdentityPolicy, AbstractAuthorizationPolicy
from .api import remember, forget, setup, authorized_userid, permits
from .abc import AbstractAuthorizationPolicy, AbstractIdentityPolicy
from .api import (authorized_userid, forget, has_permission, is_anonymous,
login_required, permits, remember, setup)
from .cookies_identity import CookiesIdentityPolicy
from .session_identity import SessionIdentityPolicy
__version__ = '0.1.1'
__version__ = '0.2.0'
__all__ = ('AbstractIdentityPolicy', 'AbstractAuthorizationPolicy',
'CookiesIdentityPolicy', 'SessionIdentityPolicy',
'remember', 'forget', 'authorized_userid',
'permits', 'setup')
'permits', 'setup', 'is_anonymous',
'login_required', 'has_permission')

View File

@@ -1,7 +1,9 @@
import asyncio
import enum
from aiohttp import web
from aiohttp_security.abc import (AbstractIdentityPolicy,
AbstractAuthorizationPolicy)
from functools import wraps
IDENTITY_KEY = 'aiohttp_security_identity_policy'
AUTZ_KEY = 'aiohttp_security_autz_policy'
@@ -62,7 +64,7 @@ def authorized_userid(request):
@asyncio.coroutine
def permits(request, permission, context=None):
assert isinstance(permission, str), permission
assert isinstance(permission, (str, enum.Enum)), permission
assert permission
identity_policy = request.app.get(IDENTITY_KEY)
autz_policy = request.app.get(AUTZ_KEY)
@@ -74,6 +76,85 @@ def permits(request, permission, context=None):
return access
@asyncio.coroutine
def is_anonymous(request):
"""Check if user is anonymous.
User is considered anonymous if there is not identity
in request.
"""
identity_policy = request.app.get(IDENTITY_KEY)
if identity_policy is None:
return True
identity = yield from identity_policy.identify(request)
if identity is None:
return True
return False
def login_required(fn):
"""Decorator that restrict access only for authorized users.
User is considered authorized if authorized_userid
returns some value.
"""
@asyncio.coroutine
@wraps(fn)
def wrapped(*args, **kwargs):
request = args[-1]
if not isinstance(request, web.BaseRequest):
msg = ("Incorrect decorator usage. "
"Expecting `def handler(request)` "
"or `def handler(self, request)`.")
raise RuntimeError(msg)
userid = yield from authorized_userid(request)
if userid is None:
raise web.HTTPUnauthorized
ret = yield from fn(*args, **kwargs)
return ret
return wrapped
def has_permission(
permission,
context=None,
):
"""Decorator that restrict access only for authorized users
with correct permissions.
If user is not authorized - raises HTTPUnauthorized,
if user is authorized and does not have permission -
raises HTTPForbidden.
"""
def wrapper(fn):
@asyncio.coroutine
@wraps(fn)
def wrapped(*args, **kwargs):
request = args[-1]
if not isinstance(request, web.BaseRequest):
msg = ("Incorrect decorator usage. "
"Expecting `def handler(request)` "
"or `def handler(self, request)`.")
raise RuntimeError(msg)
userid = yield from authorized_userid(request)
if userid is None:
raise web.HTTPUnauthorized
allowed = yield from permits(request, permission, context)
if not allowed:
raise web.HTTPForbidden
ret = yield from fn(*args, **kwargs)
return ret
return wrapped
return wrapper
def setup(app, identity_policy, autz_policy):
assert isinstance(identity_policy, AbstractIdentityPolicy), identity_policy
assert isinstance(autz_policy, AbstractAuthorizationPolicy), autz_policy

View File

@@ -6,7 +6,11 @@ to configure aiohttp_session properly.
import asyncio
from aiohttp_session import get_session
try:
from aiohttp_session import get_session
HAS_AIOHTTP_SESSION = True
except ImportError: # pragma: no cover
HAS_AIOHTTP_SESSION = False
from .abc import AbstractIdentityPolicy
@@ -16,6 +20,10 @@ class SessionIdentityPolicy(AbstractIdentityPolicy):
def __init__(self, session_key='AIOHTTP_SECURITY'):
self._session_key = session_key
if not HAS_AIOHTTP_SESSION: # pragma: no cover
raise ImportError(
'SessionIdentityPolicy requires `aiohttp_session`')
@asyncio.coroutine
def identify(self, request):
session = yield from get_session(request)

View File

@@ -0,0 +1,34 @@
from aiohttp_security.abc import AbstractAuthorizationPolicy
class DictionaryAuthorizationPolicy(AbstractAuthorizationPolicy):
def __init__(self, user_map):
super().__init__()
self.user_map = user_map
async def authorized_userid(self, identity):
"""Retrieve authorized user id.
Return the user_id of the user identified by the identity
or 'None' if no user exists related to the identity.
"""
if identity in self.user_map:
return identity
async def permits(self, identity, permission, context=None):
"""Check user permissions.
Return True if the identity is allowed the permission in the
current context, else return False.
"""
# pylint: disable=unused-argument
user = self.user_map.get(identity)
if not user:
return False
return permission in user.permissions
async def check_credentials(user_map, username, password):
user = user_map.get(username)
if not user:
return False
return user.password == password

View File

@@ -0,0 +1,107 @@
import asyncio
import functools
from textwrap import dedent
from aiohttp import web
from aiohttp_security import remember, forget, authorized_userid, permits
from .authz import check_credentials
def require(permission):
def wrapper(f):
@asyncio.coroutine
@functools.wraps(f)
def wrapped(request):
has_perm = yield from permits(request, permission)
if not has_perm:
message = 'User has no permission {}'.format(permission)
raise web.HTTPForbidden(body=message.encode())
return (yield from f(request))
return wrapped
return wrapper
index_template = dedent("""
<!doctype html>
<head>
</head>
<body>
<p>{message}</p>
<form action="/login" method="post">
Login:
<input type="text" name="username">
Password:
<input type="password" name="password">
<input type="submit" value="Login">
</form>
<a href="/logout">Logout</a>
</body>
""")
async def index(request):
username = await authorized_userid(request)
if username:
template = index_template.format(
message='Hello, {username}!'.format(username=username))
else:
template = index_template.format(message='You need to login')
return web.Response(
text=template,
content_type='text/html',
)
async def login(request):
response = web.HTTPFound('/')
form = await request.post()
username = form.get('username')
password = form.get('password')
verified = await check_credentials(request.app.user_map, username, password)
if verified:
await remember(request, response, username)
return response
return web.HTTPUnauthorized(body='Invalid username / password combination')
@require('public')
async def logout(request):
response = web.Response(
text='You have been logged out',
content_type='text/html',
)
await forget(request, response)
return response
@require('public')
async def internal_page(request):
# pylint: disable=unused-argument
response = web.Response(
text='This page is visible for all registered users',
content_type='text/html',
)
return response
@require('protected')
async def protected_page(request):
# pylint: disable=unused-argument
response = web.Response(
text='You are on protected page',
content_type='text/html',
)
return response
def configure_handlers(app):
router = app.router
router.add_get('/', index, name='index')
router.add_post('/login', login, name='login')
router.add_get('/logout', logout, name='logout')
router.add_get('/public', internal_page, name='public')
router.add_get('/protected', protected_page, name='protected')

View File

@@ -0,0 +1,33 @@
import base64
from cryptography import fernet
from aiohttp import web
from aiohttp_session import setup as setup_session
from aiohttp_session.cookie_storage import EncryptedCookieStorage
from aiohttp_security import setup as setup_security
from aiohttp_security import SessionIdentityPolicy
from .authz import DictionaryAuthorizationPolicy
from .handlers import configure_handlers
from .users import user_map
def make_app():
app = web.Application()
app.user_map = user_map
configure_handlers(app)
# secret_key must be 32 url-safe base64-encoded bytes
fernet_key = fernet.Fernet.generate_key()
secret_key = base64.urlsafe_b64decode(fernet_key)
storage = EncryptedCookieStorage(secret_key, cookie_name='API_SESSION')
setup_session(app, storage)
policy = SessionIdentityPolicy()
setup_security(app, policy, DictionaryAuthorizationPolicy(user_map))
return app
if __name__ == '__main__':
web.run_app(make_app(), port=9000)

View File

@@ -0,0 +1,10 @@
from collections import namedtuple
User = namedtuple('User', ['username', 'password', 'permissions'])
user_map = {
user.username: user for user in [
User('devin', 'password', ('public',)),
User('jack', 'password', ('public', 'protected',)),
]
}

View File

@@ -142,9 +142,9 @@ html_theme_options = {
'logo': 'aiohttp-icon-128x128.png',
'description': 'Authorization and identity for aoihttp',
'github_user': 'aio-libs',
'github_repo': 'aiohttp_security',
'github_repo': 'aiohttp-security',
'github_button': True,
'github_style': 'star',
'github_type': 'star',
'github_banner': True,
'travis_button': True,
'codecov_button': True,

View File

@@ -12,7 +12,7 @@ the `demo source`_.
https://github.com/aio-libs/aiohttp_security/tree/master/demo
.. _passlib:
https://pythonhosted.org/passlib/
https://passlib.readthedocs.io
Database
--------

View File

@@ -78,7 +78,7 @@ Public API functions
:param request: :class:`aiohttp.web.Request` object.
:param str permission: requested :term:`permission`.
:param permission: Requested :term:`permission`. :class:`str` or :class:`enum.Enum` object.
:param context: additional object may be passed into
:meth:`AbstractAuthorizationPolicy.permission`
@@ -88,6 +88,33 @@ Public API functions
``False`` otherwise.
.. coroutinefunction:: is_anonymous(request)
Checks if user is anonymous user.
Return ``True`` if user is not remembered in request, otherwise returns ``False``.
:param request: :class:`aiohttp.web.Request` object.
.. decorator:: login_required
Decorator for handlers that checks if user is authorized.
Raises :class:`aiohttp.web.HTTPUnauthorized` if user is not authorized.
.. decorator:: has_permission(permission)
Decorator for handlers that checks if user is authorized
and has correct permission.
Raises :class:`aiohttp.web.HTTPUnauthorized` if user is not authorized.
Raises :class:`aiohttp.web.HTTPForbidden` if user is authorized but has no access rights.
:param str permission: requested :term:`permission`.
.. function:: setup(app, identity_policy, autz_policy)
Setup :mod:`aiohttp` application with security policies.

View File

@@ -1,14 +1,14 @@
-e .
flake8
pytest
pytest-cov
coverage
sphinx
pep257
aiohttp_session
aiopg[sa]
aioredis==0.2.8
flake8==3.5.0
pytest==3.2.3
pytest-cov==2.5.1
coverage==4.4.2
sphinx==1.6.5
pep257==0.7.0
aiohttp-session==1.2.0
aiopg[sa]==0.13.1
aioredis==0.3.5
hiredis==0.2.0
passlib==1.6.5
aiohttp
pytest-aiohttp
passlib==1.7.1
aiohttp==2.3.2
pytest-aiohttp==0.1.3

View File

@@ -27,10 +27,12 @@ with codecs.open(os.path.join(os.path.abspath(os.path.dirname(
def read(f):
return open(os.path.join(os.path.dirname(__file__), f)).read().strip()
install_requires = ['aiohttp>=0.18']
tests_require = install_requires + ['pytest']
extras_require = {'session': 'aiohttp-session'}
setup(name='aiohttp-security',
version=version,
description=("security for aiohttp.web"),
@@ -40,9 +42,12 @@ setup(name='aiohttp-security',
'Intended Audience :: Developers',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Topic :: Internet :: WWW/HTTP'],
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Topic :: Internet :: WWW/HTTP',
'Framework :: AsyncIO',
],
author='Andrew Svetlov',
author_email='andrew.svetlov@gmail.com',
url='https://github.com/aio-libs/aiohttp_security/',

View File

@@ -94,14 +94,14 @@ def test_forget(loop, test_client):
client = yield from test_client(app)
resp = yield from client.post('/login')
assert 200 == resp.status
assert resp.url.endswith('/')
assert str(resp.url).endswith('/')
cookies = client.session.cookie_jar.filter_cookies(
client.make_url('/'))
assert 'Andrew' == cookies['AIOHTTP_SECURITY'].value
yield from resp.release()
resp = yield from client.post('/logout')
assert 200 == resp.status
assert resp.url.endswith('/')
assert str(resp.url).endswith('/')
cookies = client.session.cookie_jar.filter_cookies(
client.make_url('/'))
assert 'AIOHTTP_SECURITY' not in cookies

View File

@@ -1,10 +1,11 @@
import asyncio
import enum
from aiohttp import web
from aiohttp_security import (remember,
authorized_userid, permits,
AbstractAuthorizationPolicy)
from aiohttp_security import setup as _setup
from aiohttp_security import (AbstractAuthorizationPolicy, authorized_userid,
forget, has_permission, is_anonymous,
login_required, permits, remember)
from aiohttp_security.cookies_identity import CookiesIdentityPolicy
@@ -73,7 +74,27 @@ def test_authorized_userid_not_authorized(loop, test_client):
@asyncio.coroutine
def test_permits(loop, test_client):
def test_permits_enum_permission(loop, test_client):
class Permission(enum.Enum):
READ = '101'
WRITE = '102'
UNKNOWN = '103'
class Autz(AbstractAuthorizationPolicy):
@asyncio.coroutine
def permits(self, identity, permission, context=None):
if identity == 'UserID':
return permission in {Permission.READ, Permission.WRITE}
else:
return False
@asyncio.coroutine
def authorized_userid(self, identity):
if identity == 'UserID':
return 'Andrew'
else:
return None
@asyncio.coroutine
def login(request):
@@ -83,11 +104,11 @@ def test_permits(loop, test_client):
@asyncio.coroutine
def check(request):
ret = yield from permits(request, 'read')
ret = yield from permits(request, Permission.READ)
assert ret
ret = yield from permits(request, 'write')
ret = yield from permits(request, Permission.WRITE)
assert ret
ret = yield from permits(request, 'unknown')
ret = yield from permits(request, Permission.UNKNOWN)
assert not ret
return web.Response()
@@ -121,3 +142,143 @@ def test_permits_unauthorized(loop, test_client):
resp = yield from client.get('/')
assert 200 == resp.status
yield from resp.release()
@asyncio.coroutine
def test_is_anonymous(loop, test_client):
@asyncio.coroutine
def index(request):
is_anon = yield from is_anonymous(request)
if is_anon:
return web.HTTPUnauthorized()
return web.HTTPOk()
@asyncio.coroutine
def login(request):
response = web.HTTPFound(location='/')
yield from remember(request, response, 'UserID')
return response
@asyncio.coroutine
def logout(request):
response = web.HTTPFound(location='/')
yield from forget(request, response)
return response
app = web.Application(loop=loop)
_setup(app, CookiesIdentityPolicy(), Autz())
app.router.add_route('GET', '/', index)
app.router.add_route('POST', '/login', login)
app.router.add_route('POST', '/logout', logout)
client = yield from test_client(app)
resp = yield from client.get('/')
assert web.HTTPUnauthorized.status_code == resp.status
yield from client.post('/login')
resp = yield from client.get('/')
assert web.HTTPOk.status_code == resp.status
yield from client.post('/logout')
resp = yield from client.get('/')
assert web.HTTPUnauthorized.status_code == resp.status
@asyncio.coroutine
def test_login_required(loop, test_client):
@login_required
@asyncio.coroutine
def index(request):
return web.HTTPOk()
@asyncio.coroutine
def login(request):
response = web.HTTPFound(location='/')
yield from remember(request, response, 'UserID')
return response
@asyncio.coroutine
def logout(request):
response = web.HTTPFound(location='/')
yield from forget(request, response)
return response
app = web.Application(loop=loop)
_setup(app, CookiesIdentityPolicy(), Autz())
app.router.add_route('GET', '/', index)
app.router.add_route('POST', '/login', login)
app.router.add_route('POST', '/logout', logout)
client = yield from test_client(app)
resp = yield from client.get('/')
assert web.HTTPUnauthorized.status_code == resp.status
yield from client.post('/login')
resp = yield from client.get('/')
assert web.HTTPOk.status_code == resp.status
yield from client.post('/logout')
resp = yield from client.get('/')
assert web.HTTPUnauthorized.status_code == resp.status
@asyncio.coroutine
def test_has_permission(loop, test_client):
@has_permission('read')
@asyncio.coroutine
def index_read(request):
return web.HTTPOk()
@has_permission('write')
@asyncio.coroutine
def index_write(request):
return web.HTTPOk()
@has_permission('forbid')
@asyncio.coroutine
def index_forbid(request):
return web.HTTPOk()
@asyncio.coroutine
def login(request):
response = web.HTTPFound(location='/')
yield from remember(request, response, 'UserID')
return response
@asyncio.coroutine
def logout(request):
response = web.HTTPFound(location='/')
yield from forget(request, response)
return response
app = web.Application(loop=loop)
_setup(app, CookiesIdentityPolicy(), Autz())
app.router.add_route('GET', '/permission/read', index_read)
app.router.add_route('GET', '/permission/write', index_write)
app.router.add_route('GET', '/permission/forbid', index_forbid)
app.router.add_route('POST', '/login', login)
app.router.add_route('POST', '/logout', logout)
client = yield from test_client(app)
resp = yield from client.get('/permission/read')
assert web.HTTPUnauthorized.status_code == resp.status
resp = yield from client.get('/permission/write')
assert web.HTTPUnauthorized.status_code == resp.status
resp = yield from client.get('/permission/forbid')
assert web.HTTPUnauthorized.status_code == resp.status
yield from client.post('/login')
resp = yield from client.get('/permission/read')
assert web.HTTPOk.status_code == resp.status
resp = yield from client.get('/permission/write')
assert web.HTTPOk.status_code == resp.status
resp = yield from client.get('/permission/forbid')
assert web.HTTPForbidden.status_code == resp.status
yield from client.post('/logout')
resp = yield from client.get('/permission/read')
assert web.HTTPUnauthorized.status_code == resp.status
resp = yield from client.get('/permission/write')
assert web.HTTPUnauthorized.status_code == resp.status
resp = yield from client.get('/permission/forbid')
assert web.HTTPUnauthorized.status_code == resp.status

View File

@@ -116,14 +116,14 @@ def test_forget(make_app, test_client):
resp = yield from client.post('/login')
assert 200 == resp.status
assert resp.url.endswith('/')
assert str(resp.url).endswith('/')
txt = yield from resp.text()
assert 'Andrew' == txt
yield from resp.release()
resp = yield from client.post('/logout')
assert 200 == resp.status
assert resp.url.endswith('/')
assert str(resp.url).endswith('/')
txt = yield from resp.text()
assert '' == txt
yield from resp.release()