Chris Black
Chris Black

Reputation: 11

How to integrate Azure AD auth with Flask Blueprints

I would like to check for authentication against Azure AD in a separate file other than the one that declares the flask app. To do this I would like to make a Blueprint and within it I would like to keep track of the authentication. The reason I am trying to use Blueprint is because I am also using Celery.

So far I have tried mixing in the app.py from the Microsoft Github with This Flask Celery Project

When doing this I am unable by way of: project/views.py

from flask import Blueprint, render_template, request
from celery.result import AsyncResult

from .tasks import mytask

main = Blueprint("main", __name__)

auth = identity.web.Auth(
    session=session,
    authority=current_app.config["AUTHORITY"],
    client_id=current_app.config["CLIENT_ID"],
    client_credential=current_app.config["CLIENT_SECRET"],
)

@main.route("/login")
def login():
    return render_template("login.html", version=__version__, **auth.log_in(
        scopes=app_config.SCOPE, # Have user consent to scopes during log-in
        redirect_uri=url_for("auth_response", _external=True), # Optional. If present, this absolute URL must match your app's redirect_uri registered in Azure Portal
        prompt="select_account",
        ))

@main.route("/")
def index():
    return render_template("index.html")

@main.route("/task/start", methods=["POST"])
def task_start():
    task = mytask.delay(10)
    return {"task_id": task.id}

@main.route("/task/progress", methods=["POST"])
def task_progress():
    data = request.get_json()
    task = AsyncResult(data["task_id"])
    return {"state": task.state, "progress": task.info.get("progress", 0)}

I have tried both making a new auth.py file but that just results in circular imports.

Upvotes: 1

Views: 32

Answers (0)

Related Questions