def get_user(self, request, user_id=None, token_id=None, username=None,
                 email=None, auth_key=None, **kw):
        odm = request.app.odm()
        now = datetime.utcnow()

        if token_id and user_id:
            with odm.begin() as session:
                query = session.query(odm.token)
                query = query.filter_by(user_id=user_id, id=token_id)
                query.update({'last_access': now},
                             synchronize_session=False)
                if not query.count():
                    return

        if auth_key:
            with odm.begin() as session:
                query = session.query(odm.registration)
                reg = query.get(auth_key)
                if reg and reg.expiry > now:
                    if not reg.confirmed:
                        user_id = reg.user_id
                else:
                    return

        with odm.begin() as session:
            query = session.query(odm.user)
            try:
                if user_id:
                    user = query.get(user_id)
                elif username:
                    user = query.filter_by(username=username).one()
                elif email:
                    user = query.filter_by(email=normalise_email(email)).one()
                else:
                    return
            except NoResultFound:
                return

        return user
Securely fetch a user by id, username, email or auth key
dataset/ETHPy150Open quantmind/lux/lux/extensions/auth/backends.py/AuthMixin.get_user