Django Auth0 authentication backend Backend implemented against Auth0 Regular Python application
- Install django-auth0
$ pip install django-auth0
- Add
django_auth0
toINSTALLED_APPS
- Add
django_auth0.auth_backend.Auth0Backend
toAUTHENTICATION_BACKENDS
- Add
django_auth0.context_processors.auth0
toCONTEXT_PROCESSORS
so necessary template context will be provided - Include callback urls
Update AUTH0_CALLBACK_URL
in settings.py
to the following if want to use default authentication handler
- Add Auth0 client side JavaScript and initialize it
Options:
AUTH0_CLIENT_ID
- Auth0 client app id,AUTH0_SECRET
- Auth0 app secret,AUTH0_DOMAIN
- Auth0 subdomainYOU_APP.auth0.com
.AUTH0_CALLBACK_URL
- Auth0 callback url is full url to your callback view likehttps://YOUR_DOMAIN/CALLBACK
AUTH0_SUCCESS_URL
- Url to redirect once you login successfully
Overriding callback view
Default callback view looks like this so you can always write your own and
set AUTH0_CALLBACK_URL
to your custom view it should be url name.
Sample application is at https://github.com/imanhodjaev/auth0-sample
The @login_required decorator uses the next parameter so that you can have a dynamic login URL. If you modify the lock javascript so that the callback url includes the next parameter, you can take advantage of this Django feature using this library. If no next parameter is included, the callback will redirect to your success url.
Sample modal lock javascript:
var authredirect = '{{ AUTH0_CALLBACK_URL}}' + location.search; var lock = new Auth0Lock('{{ AUTH0_CLIENT_ID }}', '{{ AUTH0_DOMAIN }}', {
- auth: {
redirectUrl: authredirect, responseType: 'code', params: {
scope: 'openid profile'}
}, rememberLastLogin: true, theme: {
primaryColor: '#3884FF',} }); lock.show();
- Improve tests,
- Add Auth0 user profile model,
- Add support for settings from Auth0,
- Move string literals to configuration file
Does the code actually work?
source <YOURVIRTUALENV>/bin/activate (myenv) $ pip install -r requirements-test.txt (myenv) $ python runtests.py
Tools used in rendering this package: