4

I have setup authentication on my flask backed Dash-plotly app using Kong oauth2 plugin. I want to send the authentication header to my dash app on the /dash route. I tried sending the same via my flask backend in the resp header.

from flask import Blueprint, Flask, flash, render_template, request, jsonify, Response, redirect, make_response, url_for, session
from flask_cors import CORS, cross_origin
from .. import server
import requests, logging, json, ast

login_controller = Blueprint(
    'login_controller', __name__, template_folder='templates')


def check(username, password):
    # test case
    if(username == "admin" and password == "admin"):
        session['logged_In'] = True
        val = '1'
    else:
        flash("Invalid Username or/and password")
        val = None

    return val


@login_controller.route("/login", methods=['POST'])
def login():
    if(request.method == 'POST'):
        error = None
        val = check(request.form['username'], request.form['password'])
        if session.get("logged_In") and val:
            # kong access token will be available here
            Session[KONG_ACCESS_TOKEN]
            resp = make_response(redirect("/dash"))
            resp.headers['Authorization'] = "Bearer "+KONG_ACCESS_TOKEN
            resp.set_cookie("login_auth", val, 60*15)
            logging.info("Authentication successful")
            return resp
        else:
            resp = make_response(redirect("/"))
            logging.info("Authentication failed")
            return resp

I am correctly getting the header in the resp from the login route but it does not show up in the request header and I get unauthorized response on redirection to /dash

Here, is my app.py for the Dash app

import flask, os, dash, logging
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from ..server import server
from logging.handlers import RotatingFileHandler

external_stylesheets = [
    "some stylesheets"
]
df = pd.read_csv(open(os.path.abspath(
    "example.csv")), 'r', delimiter=',')
logging.info('Dash Application launched on Flask server')
app = dash.Dash(__name__, server=server,
                external_stylesheets=external_stylesheets, url_base_pathname="/dash")
app.config.suppress_callback_exceptions = True

data = df.copy()

def indicator(text, id_value, size):
    return html.Div(
        [
            html.Div(
                text,
                className="class1"
            ),
            html.Div(
                id_value,
                id=text,
                className="class2"
            ),
        ],
        className=size,
    )

Appreciate any help in this regards.

Hritik
  • 53
  • 5

0 Answers0