Skip to main content

Custom Microsoft Entra ID Authentication Backend for Django.

Project description

django-epfl-entra-id

Test Status Coverage Status PyPI version

Custom Microsoft Entra ID Authentication Backend for Django.

Requirements

  • Python 3.6 or later
  • Django 1.11, 2.2, 3.2, 4.2 or 5.2

Installation

pip install django-epfl-entra-id

Documentation

Settings

Add mozilla_django_oidc to INSTALLED_APPS:

INSTALLED_APPS = [
  ...
  "django.contrib.auth",
  "mozilla_django_oidc",  # Load after auth
  ...
]

Add django_epfl_entra_id authentication backend:

AUTHENTICATION_BACKENDS = ("django_epfl_entra_id.auth.EPFLOIDCAB",)

Register your application in the App Portal and add the OIDC configuration:

TENANT_ID = os.environ["TENANT_ID"]

OIDC_RP_CLIENT_ID = os.environ["OIDC_RP_CLIENT_ID"]
OIDC_RP_CLIENT_SECRET = os.environ["OIDC_RP_CLIENT_SECRET"]

AUTH_DOMAIN = f"https://login.microsoftonline.com/{TENANT_ID}"
OIDC_OP_AUTHORIZATION_ENDPOINT = f"{AUTH_DOMAIN}/oauth2/v2.0/authorize"
OIDC_OP_TOKEN_ENDPOINT = f"{AUTH_DOMAIN}/oauth2/v2.0/token"
OIDC_OP_JWKS_ENDPOINT = f"{AUTH_DOMAIN}/discovery/v2.0/keys"
OIDC_OP_USER_ENDPOINT = "https://graph.microsoft.com/oidc/userinfo"
OIDC_RP_SIGN_ALGO = "RS256"

LOGIN_URL = "/auth/authenticate"
LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/"

Routing

Edit your urls.py and add the following:

urlpatterns = [
  ...
  path("", include("django_epfl_entra_id.urls")),
  path("auth/", include("mozilla_django_oidc.urls")),
  ...
]

Example template:

{% if user.is_authenticated %}
  <p>Current user: {{ user.username }}</p>
  <form action="{% url 'oidc_logout' %}" method="post">
    {% csrf_token %}
    <input type="submit" value="logout">
  </form>
{% else %}
  <a href="{% url 'oidc_authentication_init' %}?next={{ request.path }}">
    Login
  </a>
{% endif %}

Optional configuration

AUTH_PROFILE_MODULE = "userprofile.UserProfile"

Logging

Enable these loggers in settings to see logging messages to help you debug:

LOGGING = {
  ...
  "loggers": {
      "mozilla_django_oidc": {
        "handlers": ["console"], 
        "level": "DEBUG"
      },
      "django_epfl_entra_id": {
        "handlers": ["console"],
        "level": "DEBUG",
      },
  ...
}

Make sure to use the appropriate handler for your app.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_epfl_entra_id-0.0.7.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_epfl_entra_id-0.0.7-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file django_epfl_entra_id-0.0.7.tar.gz.

File metadata

  • Download URL: django_epfl_entra_id-0.0.7.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_epfl_entra_id-0.0.7.tar.gz
Algorithm Hash digest
SHA256 011e1a205abe027d81858031999f8150a7bd2b9a42ee37bcf18505c4d5c7e2c5
MD5 4c1d8f3f6dbdebc07d9b807fc4ee329b
BLAKE2b-256 5fc61afb8c6bde1cda006cd98653786176f6cb85e47ddd4e3a888bee61bff8eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_epfl_entra_id-0.0.7.tar.gz:

Publisher: release.yml on epfl-si/django-epfl-entra-id

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_epfl_entra_id-0.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for django_epfl_entra_id-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 51d8d1a7e3a8fa6fd4994e4406a3bd5e40f88689915c5a0a6b6af7c3782ae913
MD5 e52c7f8da571eb7a663f539b3e3473ea
BLAKE2b-256 d48e03e2e160535bc50466ee3ea3ba3660009dfb049bb0751688bd2644c5c74c

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_epfl_entra_id-0.0.7-py3-none-any.whl:

Publisher: release.yml on epfl-si/django-epfl-entra-id

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page