MushroomMaula / fastapi_login

FastAPI-Login tries to provide similar functionality as Flask-Login does.
https://pypi.org/project/fastapi-login
MIT License
639 stars 58 forks source link

Feature request: Custom Exception when OAuth2 scopes requirements are not met #119

Closed LTsCreed closed 8 months ago

LTsCreed commented 9 months ago

Implement a custom exception for cases where OAuth2 scope requirements are not satisfied. This feature enables the display of distinct error messages for authenticated users, enhancing the overall understanding of the issue.

kigawas commented 9 months ago

Can you provide a more specific example?

LTsCreed commented 9 months ago

When the token does not contain all required scopes, the self.not_authenticated_exception is raised. I wish to have the ability to raise a different exception, so the user would know that they don't have the necessary permissions.

  ` token = await self._get_token(request)

    if token is None:
        # No token is present in the request and no Exception has been raised (auto_error=False)
        raise self.not_authenticated_exception

    # when the manager was invoked using fastapi.Security(manager, scopes=[...])
    # we have to check if all required scopes are contained in the token
    if security_scopes is not None and security_scopes.scopes:
        if not self.has_scopes(token, security_scopes):
            raise self.not_authenticated_exception

    return await self.get_current_user(token)`
kigawas commented 9 months ago

So you'd like to have something like not_have_scopes_exception?

LTsCreed commented 9 months ago

Yes, something like that

kigawas commented 8 months ago

Almost done. I'll release it in several days