Created
December 5, 2016 19:48
-
-
Save murilobsd/40c52c5a82b0cf779d5e8e4abedf916a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from bson import ObjectId | |
class MongoAuthPlugin(BaseAuthPlugin): | |
def __init__(self, context): | |
super().__init__(context) | |
self._db = db | |
@asyncio.coroutine | |
def authenticate(self, *args, **kwargs): | |
authenticated = super().authenticate(*args, **kwargs) | |
if authenticated: | |
session = kwargs.get('session', None) | |
if session.username: | |
user = yield from self._db.users.find_one({"_id": ObjectId(session.username)}) | |
if not user: | |
authenticated = False | |
self.context.logger.debug( | |
"No hash found for user '%s'" % session.username) | |
else: | |
authenticated = yield from self._db.users.find_one({"_id": ObjectId(session.username), "secret": session.password}) | |
else: | |
return None | |
return authenticated |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment