Custom Auth provider¶
You can implement custom auth provider by inheriting from class below and implementing necessary methods.
AuthProvider
¶
Basic class for all Auth providers.
Constructor is called by FastAPI, and can use Dependency injection mechanism. See setup for more details.
get_current_user(access_token, uow)
¶
This method should return currently logged in user.
Parameters:
-
(access_token¶str) –JWT token got from
Authorization: Bearer <token>header.
Returns:
-
User–Current user object
get_token(uow, grant_type=None, login=None, password=None, scopes=None, client_id=None, client_secret=None)
¶
This method should perform authentication and return JWT token.
See OAuth2 token endpoint documentation:
- https://auth0.com/docs/get-started/authentication-and-authorization-flow/call-your-api-using-resource-owner-password-flow
- https://connect2id.com/products/server/docs/api/token
Returns:
-
dict[str, Any]–Access token response.
Examples:
{
"access_token": "some.jwt.token",
"token_type": "bearer",
"expires_in": 3600,
}
setup(app)
¶
This method is called by application_factory.
Here you should configure your auth provider, set app.state.auth_provider
and return new app object.
Examples:
from fastapi import FastAPI
from my_awesome_auth_provider.settings import MyAwesomeAuthProviderSettings
from horizon.backend.dependencies import Stub
class MyAwesomeAuthProvider(AuthProvider):
def setup(app):
settings_dict = app.state.settings.auth.model_dump(exclude={"provider}, warnings=False)
settings = MyAwesomeAuthProviderSettings.model_validate(settings_dict)
app.state.auth_provider = MyAwesomeAuthProviderSettings(settings)
return app
def __init__(
self,
settings: MyAwesomeAuthProviderSettings,
):
self.settings = settings