microsoft-authentication-library-for-python
microsoft-authentication-library-for-python copied to clipboard
Update to quickstart SSO w/ python & MSAL for anyone using FastAPI instead of Flask
New way to use another framework to Add sign-in with Microsoft to a Python web app.
Note: This is a very simple demo - to keep it easy to understand for anyone getting started.
Reason i posted this is that a good friend was having some issues following the getting started with Azure SSO & Flask since the example is in flask only. Thus, I borrowed the flask demo referenced from the quick start guide here. The repo handles redirect to SSO, azure auth, and redirect back to the callback url which eventually gets the JWT(s), and allows you to make calls to the graph api. There are some subtle nuances and conversion is not as easy as one would think, so i wanted to post this in case anyone is looking or a super simple reference repository for doing the same thing from the original guide, but with a really awesome concurrent python library. You get checkout FastAPI here.
The repository demo can be found at https://github.com/AndrewJHart/Azure-FastAPI-Auth-Example
The link to the original guide is below. You will need to follow the guide to get started and create your web app. The only difference in the setup steps is that the repo i'm sharing runs on port 9000 and the cb url is
/oauthinstead of port 5000 and cb beinggetAToken(all of which is configurable). The project layouts are almost identical as well.
This will work in a multi-tenant situation as well. I hope this is helpful. Thanks!
Perhaps it would behoove Azure to use this or a similar example to demonstrate how to do something similar without requiring Flask or Django.
Thanks very much,
Thank you for sharing, @AndrewJHart ! Some devs in the issue tracker for https://github.com/Azure-Samples/ms-identity-python-webapp/ were also looking for FastAPI samples. By the way, there's now an identity package that is more higher-level than msal. The ms-identity-python-webapp sample was recently updated to use it. You could try that out in your sample to see if it simplifies the code (or if it's even compatible with FastAPI).
Excellent! I have a much better implementation in a real env, that I obviously can't share, however I'll check out the new wrapper around your SSO functionality and go from there.. possibly build a v2 of the example app with FastAPI.
Thanks! 👍