Azure-Samples / ms-identity-python-webapp

A Python web application calling Microsoft graph that is secured using the Microsoft identity platform
MIT License
284 stars 135 forks source link

Integration in Flask with blueprints and application factory pattern for a large application #69

Closed s-AlirezaSadeghi closed 3 months ago

s-AlirezaSadeghi commented 2 years ago

I've been looking to integrate ms-identity with an existing (flask) project. I've looked around and so far haven't found a structure that allows for integration of (flask) using app factory and Blueprints with ms-identity.

I know in Application factory pattern you use current_app ( which is a proxy ) to access the app in blueprints, but doing the same is not possible as ms-identity (ContextAdaptor) requires flask_app instance and not a proxy as shown below.

image

Is there a best practice/way on structuring Flask app that allows for such integration of MS-identity and Blueprints ?

mregni commented 2 years ago

I know this is a very old thread but did you ever found a solution for this? @s-AlirezaSadeghi

george-kuanli-peng commented 2 years ago

This repo has blueprint integration

https://github.com/Azure-Samples/ms-identity-python-samples-common

s-AlirezaSadeghi commented 2 years ago

@mregni I haven't found it mentioned it , but I used flask-dance with azure. it was as easy as registering a blueprint! ( I have no affiliation with this Flask extension but I found it easy to use) @george-kuanli-peng I did not find this, but upon looking flask_blueprint folder is empty. (update: source code is in init. so technically it is there but i haven't tried it)

benkey0 commented 1 year ago

Did anyone get any further with this?

I am unable to import the decorator and use it in blueprints. Flask Dance seems to work with only B2C and not Internal only which doesnt work for my requirements.

pamelafox commented 1 year ago

This sample now uses the identity package, a higher-level package that wraps msal operations. I have modified a sample Blueprint app to use the identity package, and it seems to work well. Here's a blog post with details on how I achieved the integration: https://blog.pamelafox.org/2023/03/adding-microsoft-graph-authentication.html Hopefully that helps those of you looking to integrate with your Blueprint apps. Please let me know if you have issues adapting my integration or if you have your own approaches to share.

rayluo commented 3 months ago

With the upcoming #136 , I believe there is no need to wrap this sample into blueprint.