I'm developing a Django (2.2.3) application with Django Microsoft Auth installed to handle SSO with Azure AD. I've been able to follow the quickstart documentation to allow me to log into the Django Admin panel by either using my Microsoft identity, or a standard username and password I've added to the Django user table. This all works out of the box and is fine.
My question put (really) simply is "What do I do next?". From a user's perspective, I'd like them to:
- Navigate to my application (example.com/ or example.com/content) - Django will realise they aren't authenticated, and either
- automatically redirect them to the SSO portal in the same window, or
- redirect them to example.com/login, which requires them to click a button that will open the SSO portal in a window (which is what happens in the default admin case)
- Allow them to sign in and use MFA with their Microsoft Account
- Once successful redirect them to my
@login_required
pages (example.com/content)
Currently, at the root of my navigation (example.com/), I have this:
def index(request):
if request.user.is_authenticated:
return redirect("/content")
else:
return redirect("/login")
My original idea was to simply change the redirect("/login")
to redirect(authorization_url)
- and this is where my problems start..
As far as I can tell, there isn't any way to get the current instance(?) of the context processor or backend of the microsoft_auth
plugin to call the authorization_url()
function and redirect the user from views.py
.
Ok... Then I thought I'd just instantiate the MicrosoftClient
class that generates the auth URL. This didn't work - not 100% sure why, but it think it may have something to do with the fact that some state variable used by the actual MicrosoftClient
instance on the backend/context processor is inconsistent with my instance.
Finally, I tried to mimic what the automatic /admin
page does - present an SSO button for the user to click, and open the Azure portal in a separate window. After digging around a bit, I realise that I fundamentally have the same problem - the auth URL is passed into the admin login page template as inline JS, which is later used to create the Azure window asynchronously on the client side.
As a sanity check, I tried to manually navigate to the auth URL as it is presented in the admin login page, and that did work (though the redirect to /content
didn't).
At this point, given how difficult I think I'm making it for myself, I'm feel like I'm going about this whole thing completely the wrong way. Sadly, I can't find any documentation on how to complete this part of the process.
So, what am I doing wrong?!
Login with Microsoft
button on the admin login page to work on your app's main page? That is what I have been struggling with for days. I have the link on the page, but the login does not work. I getAn invalid state variable was provided
. If I look at the URL generated on the admin page and compare it to mine the state is indeed different. But I use the same code which gets the URL from {{ microsoft_authorization_url }}. I feel like I am missing one small thing. – Pluton