Skip to content

Instantly share code, notes, and snippets.

@murilobsd
Created December 5, 2016 19:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murilobsd/40c52c5a82b0cf779d5e8e4abedf916a to your computer and use it in GitHub Desktop.
Save murilobsd/40c52c5a82b0cf779d5e8e4abedf916a to your computer and use it in GitHub Desktop.
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