added simplistic dictionary_auth example (#105)
This commit is contained in:
committed by
Andrew Svetlov
parent
b0895806af
commit
1a9ab6424e
34
demo/dictionary_auth/authz.py
Normal file
34
demo/dictionary_auth/authz.py
Normal 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
|
||||
Reference in New Issue
Block a user