OAuth failing for flask-dance with Azure AD

192 Views Asked by At

Im trying to implement OAuth authentication for an example dash app running on flask. App is registered in Azure AD, but when trying to authenticate with the flask-dance library,im getting this error:

Error AADSTS70001 - Application with Identifier None was not found in the directory

from dash import Dash, html
from werkzeug.middleware.proxy_fix import ProxyFix
from flask import Flask, redirect, url_for
from flask_dance.contrib.azure import azure, make_azure_blueprint
import os


CLIENT_ID = os.environ.get("CLIENT_ID")
CLIENT_SECRET = os.environ.get("CLIENT_SECRET")


def login_required(func):
    """Require a login for the given view function."""

    def check_authorization(*args, **kwargs):
        if not azure.authorized or azure.token.get("expires_in") < 0:
            return redirect(url_for("azure.login"))
        else:
            return func(*args, **kwargs)

    return check_authorization

blueprint = make_azure_blueprint(
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
    tenant="MyTenant", # Hidden for this example
    scope=["user.read"],
)


app = Flask(__name__)
app.config["SECRET_KEY"] = "secretkey123"
app.register_blueprint(blueprint, url_prefix="/login")

# dash_app = create_app(server=app)
dash_app = Dash(__name__, server=app)

# use this in your production environment since you will otherwise run into problems
# https://flask-dance.readthedocs.io/en/v0.9.0/proxies.html#proxies-and-https
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)

for view_func in app.view_functions:
    if not view_func.startswith("azure"):
        app.view_functions[view_func] = login_required(app.view_functions[view_func])

dash_app.layout = html.Div(children=[
  html.H1(children='Hello Dash'),
  html.Div(children="You are logged in!")
])

if __name__ == '__main__':
    dash_app.run_server(debug=True, port=5010, host="localhost")
1

There are 1 best solutions below

0
Rob On

The problem was the scope. it was not necessary and when it was removed, the rest worked just fine