async def authenticate( self, conn: HTTPConnection) -> Tuple[bool, Optional[User]]: """ Main function that AuthenticationMiddleware uses from this backend. Should return whether request is authenticated based on credentials and if it was, return also user instance. :param conn: HTTPConnection of the current request-response cycle :return: 2-tuple: is authenticated & user instance if exists """ authorization: str = conn.headers.get("Authorization") if not authorization: return False, None scheme, credentials = get_authorization_scheme_param(authorization) if not (authorization and scheme and credentials): raise AuthenticationError("Not authenticated") if scheme.lower() != "token": raise AuthenticationError("Invalid authentication credentials") token = await Token.get( key=credentials, is_active=True, expires={"$not": { "$lt": get_now() }}, ) if token is None: return False, None conn.scope["token"] = token user = await User.get(id=token.user_id) if user is None: return False, None return True, user
def set_created_now(cls, v: datetime) -> datetime: """ If created is supplied (ex. from DB) -> use it, otherwise generate new. """ if v: return v return get_now()
def test_custom_get_now(): _now = get_now() assert custom_get_now() >= _now
def test_get_now(): _now = get_now() # import pdb;pdb.set_trace() assert datetime.now(tz=pytz.UTC) >= _now