User API Referenceο
Django settingsο
Note
We provide a set of predefined settings for some identity providers. As such, you can avoid having to configure by yourself this library and itβs views. Take a look at the tutorial !
Providers settingsο
All those settings must be defined in settings.py under the variable name DJANGO_PYOIDC.
You should define them as a nested dictionary. The key to this dictionary is called your provider name (or op_name in some places). All your settings configuration are local to this provider. This allows multi-provider configurations.
DJANGO_PYOIDC = {
'my_provider_name' : {
'setting_1' : 'value',
'setting_2' : 'value'
}
}
POST_LOGIN_URI_FAILUREο
This setting configures where a user is redirected on login failure, defaults to Django base url.
POST_LOGIN_URI_SUCCESSο
This setting configures the default redirection URI on login success, defaults to Django base url.
POST_LOGOUT_REDIRECT_URIο
This setting configures where a user is redirected after successful SSO logout, defaults to Django base url.
URI_PROVIDERο
This setting configures your provider root URI. TODO : rename to PROVIDER_HOST or something like that.
URI_CONFIGο
This settings configures the path to your OIDC configuration. TODO : example.
OIDC_CALLBACK_PATHο
This setting is used to reference the callback view that should be provided as the redirect_uri parameter of the Authorization Code Flow.
LOGIN_REDIRECTION_REQUIRES_HTTPSο
This setting configures if dynamic login redirection URI must have the https scheme.
LOGIN_URIS_REDIRECT_ALLOWED_HOSTSο
This setting configures the list of allowed host in dynamic URI redirections.
OIDC_CLIENT_SECRETο
This setting configures the client secret used to authentify your application with an identity provider.
OIDC_CLIENT_IDο
This setting configures the client id used to authentify your application with an identity provider.
CACHE_DJANGO_BACKENDο
This setting configures the cache backend that is used to store sessions details. You can read more about Cache Management here.
Hook settingsο
Hook settings are path to a python function that should be called in specific context. We use a custom syntax to reference a function of a module.
The syntax is : <module path>:<function name>.
So for example, if you were to have a module named oidc.py next to your project settings with a function called logout_callback you should use the string <your application root module>.oidc:logout_callback in your settings.
Note
Hook settings work on a provider by provider basis, you can have different hook functions for each of your identity providers
Note
All those settings are optional
HOOK_USER_LOGOUTο
Calls the provided function on user logout. The function is called if the logout is successful, but before redirecting the user.
This function takes two arguments :
a request instance
django.http.HttpRequestTODO FIXME RLE
If the user was logged in, you can get the user using request.user.
HOOK_USER_LOGINο
Calls the provided function on user login. The functions is called if the login is successful.
This function takes two arguments :
a request instance
django.http.HttpRequesta user instance
django.contrib.auth.models.User
Since the user wasnβt logged in, it is not yet attached to the request instance at this stage. As such trying to access request.user will return an unauthenticated user.
HOOK_GET_USERο
Calls the provided function on user login. It takes two arguments :
the user info token (a dictionary) from the identity provider
the id token
It is expected to return a django.contrib.auth.models.User instance.
Viewsο
Note
When instantiating a view from this library (ie through djangoβs βas_view()β) you must set the named argument op_name to point to a valid DJANGO_PYOIDC settings entry.
If you use Providers then this behaviour is automatically implemented.
Here is an example :
from .oidc_providers import my_project_provider
urlpatterns = [
path("auth/callback", OIDCCallbackView.as_view(op_name="keycloak"),),
]
Providersο
Providers classes allows the final user to configure their project without having to understand how to map their Identity Provider configuration settings to this library settings.
Each provider implements the configuration logic and provides mostly two methods :
One to generate a configuration dict to be inserted in the
DJANGO_PYOIDCvalue of your django settings :get_config()One to generate urls to be
includedin your url configuration :get_urlpatterns()
- class django_pyoidc.providers.KeycloakProvider(keycloak_base_uri: str, keycloak_realm: str, *args, **kwargs)ο
Provide django settings/urlconf based on keycloak behaviour (latest version).
For older Keycloak versions please check the other Keycloak_* providers.
- class django_pyoidc.providers.base.Provider(op_name: str, provider_discovery_uri: str, logout_redirect: str, failure_redirect: str, success_redirect: str, redirect_requires_https: bool, client_secret: str, client_id: str)ο
This is the base Provider class that is used to implement common provider configuration patterns. You should not use this class directly. Instead, you should but subclass it to implement the configuration logic.
- __init__(op_name: str, provider_discovery_uri: str, logout_redirect: str, failure_redirect: str, success_redirect: str, redirect_requires_https: bool, client_secret: str, client_id: str)ο
- Parameters:
op_name (str) β the name of the sso provider that you are using
logout_redirect (str) β the URI where a user should be redirected to on logout success
failure_redirect (str) β the URI where a user should be redirected to on login failure
success_redirect (str) β the URI a user should be redirected to on login success if no redirection url where provided
redirect_requires_https (bool) β set to True to disallow redirecting user to non-https uri on login success
client_secret (str) β the OIDC client secret
client_id (str) β the OIDC client ID
- property callback_uri_nameο
The callback viewname (use in
django.urls.reverse()django directive for example) of this configuration
- get_config(allowed_hosts, cache_backend: str = 'default') Dict[str, Dict[str, Any]]ο
- Parameters:
allowed_hosts (
list) β A list of allowed domains that can be redirected to. A good idea is to this toALLOWED_HOSTS. See Redirect the user after login for more details.cache_backend (
str, optional) β Defaults to βdefaultβ. The cache backend that should be used to store this provider sessions. Take a look at Cache Management
- Returns:
A dictionary with all the settings that django-pyoidc expects to work properly
- Return type:
- get_urlpatterns() List[Any]ο
- Returns:
A list of urllpatterns to be included using
django.urls.include()in your urllconfiguration
- property login_uri_nameο
The login viewname (use in
django.urls.reverse()django directive for example) of this configuration
- property logout_uri_nameο
The logout viewname (use in
django.urls.reverse()django directive for example) of this configuration