ASTRON Django Authentication app
Project description
ASTRONauth
Django App for quickly adding authentication to any Django app.
astronauth is based on the allauth package and updates the templates for follow the Design in ASTRON style (DIAS).
Installation
The package is available on pypi:
pip install astronauth
Django Settings
The following settings are the minimal settings required (also check the settings.py file.):
SITE_IDis used byallauth, usually it can be set to1without any issue. See the Django documentation for more info.INSTALLED_APPS: make sureastronauthis listed beforeallauthsince it overrides templates
INSTALLED_APPS = [
# These are the default and should already be included
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"my_app", # if you override any of the templates, this should be here (e.g. custom navigation bar)
## These are required for ASTRONauth
'django.contrib.sites',
"astronauth", # it is important that astronauth is included before allauth
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.keycloak',
]
SOCIALACCOUNT_PROVIDERS: is a list of auth providers.astronauthmainly uses Keycloak:
SOCIALACCOUNT_PROVIDERS = {
'keycloak': {
'KEYCLOAK_URL': 'https://sdc-dev.astron.nl/auth', # replace by https://keycloak.astron.nl/auth for production
'KEYCLOAK_REALM': 'SDC', # change this depending on which realm to use
'SCOPE': ['openid', 'profile', 'email']
}
}
-
LOGIN_REDIRECT_URLis used to determine where users should be redirected after login. Usually the main index page is a good one (http://localhost:8000/is used in development) -
While
DEBUG=TrueDjango hosts the file. For production purposes, consider the Django recommendations form their documentation. -
Important: with this configured, you should migrate the database:
python manage.py migratebefore further configuration. Also create asuperuser:
$ DJANGO_SUPERUSER_PASSWORD=password python manage.py createsuperuser --username admin --email no-reply@example.com --noinput
Important: replace the username/password and email where necessary
Since astronauth is based on allauth, please take a look at their documentation for all available settings.
Rename session and CSRF Cookies
If multiple apps or services are hosted at the same machine/url, their session or csrf cookies might interfere.
Therefore it is necessary to rename the session and csrf cookies to include the name of the app.
Add the following to your settings.py in your Django project folder:
SESSION_COOKIE_NAME = 'my_service_name_session_id'
CSRF_COOKIE_NAME = 'my_service_csrftoken'
Adding a provider
Before adding the provider in your app, make sure you have a realm setup in Keycloak and configured a client in that Keycloak realm. Take note of the Client ID, since you will use it in astronauth.
Make sure that the following options are set:
Client Protocolisopenid-connect- For granting acces there are two options:
- Front-end applications:
Acces Typeispublic - Back-end applications:
Acces Typeisconfidential+Authorization EnabledisOn(it also setsService Account EnabledtoOn). Note that is provides aSecret keyin theCredentialstab that you need to configure later in your application.
- Front-end applications:
Valid Redirect URIscontains a list or URI for your application. Includehttp://localhost:8000/for development (Important: not recommended for production clients!)
Django Admin configuration
- Log into the Django admin interface (
http://localhost:8000/admin/in development) - Make sure the
Sitescontains an entry (should be done by themigratecommand) and update them if you need to - In the
Social Accountssection, clickaddonSocial applicationsand use the following settingsProvider=>KeycloakName=>Keycloak(could be anything)Client id=> The Client ID configured in your Keycloak Realm.Secret=> TheSecret keyprovided by the Keycloak Realm in theCredentialstabSites=> Make sure the site is added, otherwise you will run intoNo providers availableerrors.
Adding a custom Navbar
You probably should override the navigation bar, so that it uses the logo of your own application and redirects to the correct view if you click on the logo.
This can be done by creating a template file in one of your apps. Check integration/my_app/templates/astronauth/navbar.html.
Note: Make sure that your app is above astronauth in the INSTALLED_APPS setting for this to work.
Deployment
TODO
- example of hosting static files with nginx
- example of docker-compose with traefik as reverse proxy
Contributing
This repository contains a minimal Django project for local development usage.
# Install as an editable package so changes are reflected immediately
pip install -e .
# then from the integration directory
cd integration
# Run the migrations
python manage.py migrate
# Create a super user
DJANGO_SUPERUSER_PASSWORD=password python manage.py createsuperuser --username admin --email no-reply@example.com --noinput
# Load the setup data
python manage.py loaddata fixtures/test_data.json
# the app is then served via "http://localhost:8000/"
python manage.py runserver
# set the secret key from the KeyCloak realm in the administrator console for the Social Application Keycloak
# Follow the configuration steps mentioned above for adding a superuser and configuring a client.
Code Format
The provided tox.ini files has a command tox -e format to format your code for you.
Acknowledgements
TODO Determine which grants are applicable.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file astronauth-0.3.3.tar.gz.
File metadata
- Download URL: astronauth-0.3.3.tar.gz
- Upload date:
- Size: 2.0 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
625ba44258b8d97f292dbea53e213f492b01162488ffea9ecaad7236191299e0
|
|
| MD5 |
f1b1e4820d32752338e27a77fb57c8de
|
|
| BLAKE2b-256 |
423aa0fd3b318bc10307f64aab186caae5d1fe5d4139ca1d8afa79919c6f46e6
|
File details
Details for the file astronauth-0.3.3-py3-none-any.whl.
File metadata
- Download URL: astronauth-0.3.3-py3-none-any.whl
- Upload date:
- Size: 2.0 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab983bb38f392515d9495aa7481779d80376997e5ae05d75920d2747156d83ac
|
|
| MD5 |
2b349f81b13b5e4f1e78c7eba62665ac
|
|
| BLAKE2b-256 |
d59243d481d6746a85b573bda30441ec5fafe5a5a61aa613be46c86b7a500b47
|