Package: django-microsoft-authentication
Django microsoft authenticationIt 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).InstallationStandard pip install:pipinstalldjango-microsoft-authenticationConfigurationFirst create an App inhttps://portal.azure.com/#home. There one needs to create set up for authentication. The details can be found here:https://docs.microsoft.com/en-us/azure/api-management/api-management-howto-protect-backend-with-aadhttps://docs.microsoft.com/en-us/azure/app-service/configure-authentication-provider-microsoftAdd 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 TrueMICROSOFT_NEW_DJANGO_USER_IS_STAFF=True# Optional, default value is TrueAdd 'microsoft_authentication' to INSTALLED_APPSAdd the following to the project/urls.pyfromdjango.contribimportadminfromdjango.urlsimportpath,includeurlpatterns=[path('admin/',admin.site.urls),# Add the following linepath('microsoft_authentication/',include('microsoft_authentication.urls'))]In templatesUse "{% url 'microsoft_authentication:microsoft_authentication_login' %}" as login urlUse "{% url 'microsoft_authentication:microsoft_authentication_logout' %}" as logout urlHow 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.QuickstartThis app provides a decorator which can be used as follows:fromdjango.httpimportHttpResponsefrommicrosoft_authentication.auth.auth_decoratorsimportmicrosoft_login_required@microsoft_login_required()defhome(request):returnHttpResponse("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 namesdefspecific_group_access(request):returnHttpResponse("You are accessing page which is accessible only to users belonging to SpecificGroup1 or SpecificGroup2")Troubleshooting during developmentUsehttp://localhost:8000instead ofhttp://127.0.0.1:8000because session cookies
are set differently for these urls.