Django based app for Microsoft authentication of users.
Project description
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
- First create an App in https://portal.azure.com/#home. There one needs to create set up for authentication. The details can be found here:
- Add the following microsoft app authentication configuration to settings.py file. (e.g. below, please replace redirect and logout_uri with correct domain)
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
# True: creates new Django User after valid microsoft authentication.
# False: it will only allow those users which are already created in Django User model and
# will validate the email using Microsoft.
MICROSOFT_CREATE_NEW_DJANGO_USER = True # Optional, default value is True
MICROSOFT_NEW_DJANGO_USER_IS_STAFF = True # Optional, default value is True
- 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'))
]
- In templates
Use "{% url 'microsoft_authentication:microsoft_authentication_login' %}" as login url
Use "{% url 'microsoft_authentication:microsoft_authentication_logout' %}" as logout url
How it works?
- It authenticates the user using their microsoft email and microsoft authentication.
- It also verifies if the domain of the microsoft authenticated email is also in MICROSOFT["valid_email_domains"]
- 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.
- 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.
Project details
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
File details
Details for the file django-microsoft-authentication-0.1.6.tar.gz
.
File metadata
- Download URL: django-microsoft-authentication-0.1.6.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed0d941c4fa1afb1312242654ce3b5ebafe3977b0d4f884dcf16bb5ac95c1076 |
|
MD5 | 2bf1172b78f822dc9d465ea1912b2887 |
|
BLAKE2b-256 | c4dc1da64da7c8263babe36f42183166e77677f4d0c629f5ac892f64bb23be1e |
File details
Details for the file django_microsoft_authentication-0.1.6-py3-none-any.whl
.
File metadata
- Download URL: django_microsoft_authentication-0.1.6-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 974ecca76ccaef12a380a8d50c3e3121b34a92390dbc31f4f56c83cb15c0fb1d |
|
MD5 | 374b94f0c82a3cf48923851af95fbebb |
|
BLAKE2b-256 | c5d636e678b07720486a645aa98a879cabc81bc1ccc5f6fa45abfcbc07dc082e |