tldr; - There is a microsoft github django project that I am trying to understand. I am trying to figure out where there /redirect endpoint's code is or leads to for there first project called 1-authentication. Here is the link: https://github.com/Azure-Samples/ms-identity-python-django-tutorial
I am very new to django and I am porting over a flask webapp I made to django. The specific endpoint I am trying to find is the /redirect endpoint's code.
The specific problem I am trying to find out where Microsoft/Azure places you once you completed the sign-in process.
I want to find out where this endpoint's code is so I can force it to redirect to an endpoint called /closeauth. /closeauth is an html file I made that will do a try block to see if you are using outlook, if you are not using outlook, but are using the web, it will kick you out to the normal / home endpoint. However, if you are using outlook, it will send a message back to the parent webpage to let outlook know that authenciation is succesful, then the parent webpage will try to go to /.
To understand what I mean, here is an example in flask with the app.config file and the redirect endpoint which is the exact code I am looking for django:
app_config.py:
import os
REDIRECT_PATH = "/getAToken"
app.py for flask example
@app.route(app_config.REDIRECT_PATH)
def authorized():
try:
cache = _load_cache()
result = _build_msal_app(cache=cache).acquire_token_by_auth_code_flow(
session.get("flow", {}), request.args)
if "error" in result:
return render_template("auth_error.html", result=result)
session["user"] = result.get("id_token_claims")
_save_cache(cache)
except ValueError: # Usually caused by CSRF
pass # Simply ignore them
return redirect(url_for("closeauth"))
However I can't find where this /redirect is in django.
to test the code I am talking about use this microsoft github link https://github.com/Azure-Samples/ms-identity-python-django-tutorial
I am using the first project 1-authentication and changed the code for that. But I still can't figure out where the redirect code is to route the redirect somewhere else AFTER it was authenticated with Microsoft.
It comes with a read me which says:
- In the app's
Sample/urls.pymodule, the necessary MSAL endpoints are hooked up:
```python
from ms_identity_web.django.msal_views_and_urls import MsalViews
...
msal_urls = MsalViews(settings.MS_IDENTITY_WEB).url_patterns()
...
path(f'{settings.AAD_CONFIG.django.auth_endpoints.prefix}/', include(msal_urls)),
```
- The above code sets up middlwares and hooks up all necessary endpoints for the authentication process into your Django app under a route prefix (
/authby default). For example, the redirect endpoint is found at/auth/redirect.- When a user navigates to
/auth/sign_inand completes a sign-in attempt, the resulting identity data is put into the session, which can be accessed through the request object atrequest.identity_context_data.
but I can't find out where the /auth/redirect code is to change the ending point from / to /closeauth
I also checked the file of msal_views_and_urls.py and didn't see where the /auth/redirect goes to.
try:
from django.urls import path
from django.shortcuts import redirect
from django.urls import reverse
except:
pass
import logging
class MsalViews:
def __init__(self, ms_identity_web):
self.logger = logging.getLogger('MsalViewsLogger')
self.ms_identity_web = ms_identity_web
self.prefix = self.ms_identity_web.aad_config.django.auth_endpoints.prefix + "/"
self.endpoints = self.ms_identity_web.aad_config.django.auth_endpoints
def url_patterns(self):
return [
path(self.endpoints.sign_in, self.sign_in, name=self.endpoints.sign_in),
path(self.endpoints.edit_profile, self.edit_profile, name=self.endpoints.edit_profile),
path(self.endpoints.redirect, self.aad_redirect, name=self.endpoints.redirect),
path(self.endpoints.sign_out, self.sign_out, name=self.endpoints.sign_out),
path(self.endpoints.post_sign_out, self.post_sign_out, name=self.endpoints.post_sign_out),
]
def sign_in(self, request):
self.logger.debug(f"{self.prefix}{self.endpoints.sign_in}: request received. will redirect browser to login")
auth_url = self.ms_identity_web.get_auth_url(redirect_uri=request.build_absolute_uri(reverse(self.endpoints.redirect)))
return redirect(auth_url)
def edit_profile(self, request):
self.logger.debug(f"{self.prefix}{self.endpoints.edit_profile}: request received. will redirect browser to edit profile")
auth_url = self.ms_identity_web.get_auth_url(
redirect_uri=request.build_absolute_uri(reverse(self.endpoints.redirect)),
b2c_policy=self.ms_identity_web.aad_config.b2c.profile)
return redirect(auth_url)
def aad_redirect(self, request):
self.logger.debug(f"{self.prefix}{self.endpoints.redirect}: request received. will process params")
return self.ms_identity_web.process_auth_redirect(
redirect_uri=request.build_absolute_uri(reverse(self.endpoints.redirect)),
afterwards_go_to_url=reverse('index'))
def sign_out(self, request):
self.logger.debug(f"{self.prefix}{self.endpoints.sign_out}: signing out username: {request.identity_context_data.username}")
return self.ms_identity_web.sign_out(request.build_absolute_uri(reverse(self.endpoints.post_sign_out))) # send the user to Azure AD logout endpoint
def post_sign_out(self, request):
self.logger.debug(f"{self.prefix}{self.endpoints.post_sign_out}: clearing session for username: {request.identity_context_data.username}")
self.ms_identity_web.remove_user(request.identity_context_data.username) # remove user auth from session on successful logout
return redirect(reverse('index')) # take us back to the home page
Note: I have a very weird feeling no one knows what I am talking about. If that is the case, I will get rid of the MS authentication that came with the django test app from here https://github.com/Azure-Samples/ms-identity-python-django-tutorial and instead use the msal library that was used in flask.