-
Create virtual environment
python -m venv .venv -
activate it.
. .venv/bin/activate -
install dependancies
pip install -r requirements.txt -
Setup database
-
Run migration command
python manage.py migrate -
Up the server
python manage.py runserver
In order to start celery worker in a separate container
celery -A AiMapper worker --loglevel=info
This backend has been tested with a standard Keycloak installation, but you might need to specialize it and tune the parameters according to your configuration.
This setup specializes the OAuth2 backend, which primarily offers authorization rather than authentication capabilities. Although Keycloak provides a full OpenID Connect implementation, its configuration can be labor-intensive.
This backend is designed to retrieve an access token, assuming that the token contains the necessary user details for authentication. The integrity of the authentication process is ensured through public key verification for the access_token, along with OpenID Connect aud (audience) field checking.
- Navigate to the Clients section in the Keycloak Admin Console.
- Choose a
Client IDunder the General Settings pane. - In the Capability config pane, select:
Client authenticationAuthorization
- Add a valid redirect url http://localhost:8000/accounts/complete/keycloak/
- Settings: Copy the
Client IDand use it as theKEYvalue in your settings. - Credentials:
- Under Client Authenticator, select
Client Id and Secret. - Copy the
Client secretvalue and use it as theSECRETin your settings.
- Under Client Authenticator, select
To ensure tokens work with JWT, configure the following settings:
- Advanced > Fine-Grain OpenID Connect Configuration:
- User Info Signed Response Algorithm:
RS256 - Request Object Signature Algorithm:
RS256
- User Info Signed Response Algorithm:
To include the aud (audience) field in tokens:
- Navigate to Client Scopes.
- Select your Client ID-dedicated scope.
- Click Add Mapper > Audience.
- Name the mapper and select your Client ID under
Included Client Audience.
- Go to Realm Settings > Keys.
- Copy the
Public keyand use it asPUBLIC_KEYin your settings.
To ensure correct token fields are included:
- Navigate to Clients > Client ID > Mappers.
- Include at least the
ID_KEYvalue and any dictionary keys required in theget_user_detailsmethod.
Example configuration for Django:
SOCIAL_AUTH_KEYCLOAK_KEY = 'example'
SOCIAL_AUTH_KEYCLOAK_SECRET = '1234abcd-1234-abcd-1234-abcd1234adcd'
SOCIAL_AUTH_KEYCLOAK_PUBLIC_KEY = \
'pempublickeythatis2048bitsinbase64andhaseg392characters'
SOCIAL_AUTH_KEYCLOAK_AUTHORIZATION_URL = \
'https://sso.com/auth/realms/example/protocol/openid-connect/auth'
SOCIAL_AUTH_KEYCLOAK_ACCESS_TOKEN_URL = \
'https://sso.com/auth/realms/example/protocol/openid-connect/token'By default, users are associated via the username field. To change the key:
SOCIAL_AUTH_KEYCLOAK_ID_KEY = 'email'Important: Ensure that your Keycloak and Django user databases do not conflict, preventing user account hijacking due to incorrect associations.
Users can log in via the following endpoint:
http://localhost:8000/accounts/login/keycloak
To start the Celery worker for async tasks, run:
celery -A AiMapper worker --loglevel=infoThis completes the setup for integrating Keycloak authentication with Django.