Django SRAM
SURF Research Access Management integration for Django
Installation
Add django_sram and django-filter to requirements.txt
pip install django-sram django-filter
Integration in Django project
To use this in a django application (assuming usage of keycloack and oauth2-proxy):
settings.py:
Add the following apps to INSTALLED_APPS:
INSTALLED_APPS = [
...
"my_client_app",
"django_sram",
"rest_framework" # Not strictly required, but prevents TemplateDoesNotExist errors
]
Configure REST_FRAMEWORK, SIMPLE_JWT, CSRF, Session cookie name:
REST_FRAMEWORK = {
"DEFAULT_FILTER_BACKENDS": ["django_filters.rest_framework.DjangoFilterBackend"],
"DEFAULT_AUTHENTICATION_CLASSES": [
# "django_sram.authentication.UpsertJWTAuthentication", # <-- use this to allow creation and updating of Django Users
"rest_framework_simplejwt.authentication.JWTStatelessUserAuthentication",
# assumes header is of format `Bearer <JWT>`
"django_sram.authentication.OAUTH2ProxyStatelessAuthentication", # assumes header is of format `<JWT>`
],
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.IsAuthenticated",
],
}
SIMPLE_JWT = {
"ALGORITHM": "RS256",
# using `eduperson_unique_id` from SRAM insdead of `sub` from Keycloak
"USER_ID_CLAIM": "eduperson_unique_id",
"TOKEN_TYPE_CLAIM": "typ", # Keycloak specific token type claim
# Specific header set by oauth2 proxy
# "AUTH_HEADER_NAME": "HTTP_X_FORWARDED_ACCESS_TOKEN", # Use this to get the access/bearer token
"AUTH_HEADER_NAME": "HTTP_AUTHORIZATION", # default; Oauth2 Proxy uses ID token here
# Custom token and TokenUser classes to parse claims
"AUTH_TOKEN_CLASSES": [
"django_sram.token.IDToken",
"django_sram.token.BearerToken",
],
"TOKEN_USER_CLASS": "django_sram.user.TokenUser",
"JWK_URL": os.environ.get("OAUTH2_JWK_URL", ""),
}
SESSION_COOKIE_NAME = "my_client_app-sessionid"
CSRF_COOKIE_NAME = "my_client_app-csrftoken"
CSRF_TRUSTED_ORIGINS = [
("http://" if ("localhost" in host) else "https://") + host
for host in ALLOWED_HOSTS
]
Expose userinfo endpoint in urls.py:
from django_sram.views.userinfo_viewset import UserInfo
urlpatterns = [
path('userinfo', # Add this route to skip_auth_routes of oauth2proxy config: "GET=^/userinfo"
UserInfo.as_view({'get': 'list'}),
name='userinfo'
),
]
Development
Development environment
To setup and activte the develop environment run source ./setup.sh from within the source directory.
If PyCharm is used, this only needs to be done once. Afterward the Python virtual env can be setup within PyCharm.
Contributing
To contribute, please create a feature branch and a "Draft" merge request. Upon completion, the merge request should be marked as ready and a reviewer should be assigned.
Verify your changes locally and be sure to add tests. Verifying local
changes is done through tox.
pip install tox
With tox the same jobs as run on the CI/CD pipeline can be ran. These include unit tests and linting.
tox
To automatically apply most suggested linting changes execute:
tox -e format
License
This project is licensed under the Apache License Version 2.0
Metadata
Release files for django-sram 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_sram-0.1.2.tar.gz | 20.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_sram-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 32.4 kB
Release files / django_sram-0.1.2.tar.gz
| Download URL | django_sram-0.1.2.tar.gz |
|---|---|
| Size | 20.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6b10afd49904a8309a0f27745be2257207dd4aaeda95a849d7b9288542b64996
|
|
BLAKE2b-256 checksum How to use checksums |
846b6e43cb779ba263009bd98f60fc864ff4e011420ab610c677e0cf5bce28a6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|
Release files / django_sram-0.1.2-py3-none-any.whl
| Download URL | django_sram-0.1.2-py3-none-any.whl |
|---|---|
| Size | 12.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
aa97f093ae00a12c13a604a6c28d49f42030cd781530b3bf33208bc83bd671e0
|
|
BLAKE2b-256 checksum How to use checksums |
9138da5700c99fb3db46dd1886eb4b95da19aa45f682d0c5a8015814d1fc58d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|