问题描述
我正在尝试将 Okta OIDC 身份验证和 Dash 集成到 Flask 应用程序中,以便用户只能在使用我们组织的 Okta SSO 登录后才能查看 Dash 应用程序
I'm trying to integrate Okta OIDC authentication and Dash into a Flask app, so that a user would only be able to view the Dash app once they signed in using our org's Okta SSO
尝试:
from flask import Blueprint, Flask, jsonify, redirect, render_template_string, url_for
from flask_oidc import OpenIDConnect
from okta import UsersClient
import dash
oidc = OpenIDConnect()
okta_client = UsersClient("https://dev-XXXXXX.okta.com",
"somesecret")
app = Flask(__name__)
app.config.from_mapping(
SECRET_KEY="testkey",
OIDC_CLIENT_SECRETS="/app/appname/client_secrets.json",
OIDC_COOKIE_SECURE= False,
OIDC_CALLBACK_ROUTE="/oidc/callback",
OIDC_SCOPES=["openid", "email", "profile"],
OIDC_ID_TOKEN_COOKIE_NAME="oidc_token",
DEBUG_TB_INTERCEPT_REDIRECTS = False)
oidc.init_app(app)
dash_app.layout = layout.layout
callbacks.register_dash_callbacks(dash_app)
dash_app.run_server(debug=True)
@app.route("/" , methods=["GET"])
@oidc.require_login
def redirect_to_app():
return redirect(url_for(dash_app.config.requests_pathname_prefix))
给我以下内容:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
该应用程序运行良好,并能很好地集成到 Flask 应用程序中.我试过研究,但找不到任何有用的东西.
The app works just fine and integrates well into the Flask app. I've tried researching but wasn't able to find anything useful.
我也可以通过将其拆分为蓝图来使其工作,如下所示:
I could also get it to work by separating it out into a blueprint like so:
home_blueprint = Blueprint("home", __name__, url_prefix="/")
@home_blueprint.route("/" , methods=["GET"])
@oidc.require_login
def index():
return redirect(url_for('/dash/'))
但是,在未经授权的情况下,仍然可以通过 localhost:5000/dash/
直接访问应用程序.
But then accessing the app directly via localhost:5000/dash/
was still possible without authorization.
预先感谢您的帮助.
这里是完整的回溯:
Traceback (most recent call last):
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/ddtrace/vendor/wrapt/wrappers.py", line 602, in __call__
return self._self_wrapper(self.__wrapped__, self._self_instance,
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/ddtrace/contrib/flask/helpers.py", line 20, in wrapper
return func(pin, wrapped, instance, args, kwargs)
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/ddtrace/contrib/flask/patch.py", line 331, in traced_wsgi_app
return wrapped(environ, start_response)
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/ddtrace/vendor/wrapt/wrappers.py", line 602, in __call__
return self._self_wrapper(self.__wrapped__, self._self_instance,
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/ddtrace/contrib/flask/helpers.py", line 20, in wrapper
return func(pin, wrapped, instance, args, kwargs)
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/ddtrace/contrib/flask/helpers.py", line 29, in wrapper
return wrapped(*args, **kwargs)
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/flask/app.py", line 1816, in full_dispatch_request
return self.finalize_request(rv)
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/flask/app.py", line 1833, in finalize_request
response = self.process_response(response)
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/ddtrace/vendor/wrapt/wrappers.py", line 602, in __call__
return self._self_wrapper(self.__wrapped__, self._self_instance,
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/ddtrace/contrib/flask/helpers.py", line 20, in wrapper
return func(pin, wrapped, instance, args, kwargs)
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/ddtrace/contrib/flask/helpers.py", line 29, in wrapper
return wrapped(*args, **kwargs)
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/flask/app.py", line 2112, in process_response
response = handler(response)
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/ddtrace/vendor/wrapt/wrappers.py", line 554, in __call__
return self._self_wrapper(self.__wrapped__, instance,
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/ddtrace/contrib/flask/wrappers.py", line 23, in trace_func
return wrapped(*args, **kwargs)
File "/pyenv/versions/3.8.0/envs/customer-feedback-hub/lib/python3.8/site-packages/flask_debugtoolbar/__init__.py", line 202, in process_response
response_html = response.data.decode(response.charset)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
推荐答案
好的,所以我找到了 this 帖子,whih最终解决了我的问题,只需将 login_required
替换为 oidc.require_login
.原来无效的起始字节与我遇到的另一个错误有关.
OK, so I found this post, whih ended up solving my problem simply by replacing login_required
with oidc.require_login
. Turns out the invalid start byte was related to another bug I had.
app = Flask(__name__)
app.config.from_mapping(
SECRET_KEY="testkey",
OIDC_CLIENT_SECRETS="XXX",
OIDC_COOKIE_SECURE= False,
OIDC_CALLBACK_ROUTE="/oidc/callback",
OIDC_SCOPES=["openid", "email", "profile"],
OIDC_ID_TOKEN_COOKIE_NAME="oidc_token",
DEBUG_TB_INTERCEPT_REDIRECTS = False
)
oidc.init_app(app)
dash_app = dash.Dash(__name__,
server = app,
url_base_pathname='/dash/'
)
for view_func in app.view_functions:
if view_func.startswith(dash_app.config['url_base_pathname']):
app.view_functions[view_func] = oidc.require_login(app.view_functions[view_func])
dash_app.layout = html.Div([intro_div, navbar, exec_dashboard, body])
register_dash_callbacks(dash_app)
这篇关于将 Okta OIDC 与 Dash 应用程序集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!