Skip to main content

Django microsoft authentication

It is a very simple app which uses Microsoft authentication for user login and admin panel login. It uses the Microsoft authentication library for Python (msal).

Installation

Standard pip install:

pip install django-microsoft-authentication

Configuration

MICROSOFT = {
    "app_id": "YOUR_APP_ID_HERE",
    "app_secret": "YOUR_APP_SECRET_HERE",
    "redirect": "http://localhost:8000/microsoft_authentication/callback",
    "scopes": ["user.read"],
    "authority": "https://login.microsoftonline.com/common",  # or using tenant "https://login.microsoftonline.com/{tenant}",
    "valid_email_domains": ["<list_of_valid_domains>"],
    "logout_uri": "http://localhost:8000/admin/logout"
}
  • Add the following line to settings.py to change the LOGIN_URL and LOGIN_REDIRECT_URL settings.
LOGIN_URL = "/microsoft_authentication/login"
LOGIN_REDIRECT_URL = "/admin"  # optional and can be changed to any other url
  • Add 'microsoft_authentication' to INSTALLED_APPS
  • Add the following to the project/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    # Add the following line
    path('microsoft_authentication/', include('microsoft_authentication.urls'))
]

How it works?

  1. It authenticates the user using their microsoft email and microsoft authentication.
  2. It also verifies if the domain of the microsoft authenticated email is also in MICROSOFT["valid_email_domains"]
  3. After the first two steps of authentication, if the user is not found, it creates a new user but with no access to any apps in admin panel.
  4. Superusers can assign User Groups to the users for Group based access to views.

Quickstart

This app provides a decorator which can be used as follows:

from django.http import HttpResponse
from microsoft_authentication.auth.auth_decorators import microsoft_login_required


@microsoft_login_required()
def home(request):
    return HttpResponse("Logged in")

# If pages need to be restricted to certain groups of users.
@microsoft_login_required(groups=("SpecificGroup1", "SpecificGroup2"))  # Add here the list of Group names
def specific_group_access(request):
    return HttpResponse("You are accessing page which is accessible only to users belonging to SpecificGroup1 or SpecificGroup2")

Troubleshooting during development

  • Use http://localhost:8000 instead of http://127.0.0.1:8000 because session cookies are set differently for these urls.

Metadata

Release files for django-microsoft-auth-sync 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for django-microsoft-auth-sync 0.1.8
File Interpreter ABI Platform Actions
django_microsoft_auth_sync-0.1.8-py3-none-any.whl Python 3 none any Details for django_microsoft_auth_sync-0.1.8-py3-none-any.whl

Release files / django_microsoft_auth_sync-0.1.8-py3-none-any.whl

Download URL django_microsoft_auth_sync-0.1.8-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e6b988f5e248f8f0d8f18b9f54547e38e405a161d13184cfda95ada10df5459a
BLAKE2b-256 checksum
How to use checksums
2df74c2e82ec9f5ccf03de4be5484bf10108554be9ed2b103c0cc8d5e8517b80
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.10

Security for django-microsoft-auth-sync 0.1.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page