Ejemplo n.º 1
0
    async def authenticate(
            self, request: HTTPConnection
    ) -> Optional[Tuple[AuthCredentials, AuthUser]]:
        if JWT_AUTH_HEADER not in request.headers:
            return AuthCredentials(scopes=[]), AuthUser(user_id=None)

        auth = request.headers[JWT_AUTH_HEADER]
        try:
            scheme, token = auth.split()
            payload = await _Authenticate.verify(token)
        except Exception as exc:
            return AuthCredentials(
                scopes=[], error_message=str(exc)), AuthUser(user_id=None)

        scopes = User.get_permission(user_id=payload.user_id)
        return AuthCredentials(
            scopes=scopes, logged_in=True), AuthUser(user_id=payload.user_id)