繁体   English   中英

如何在 Flask App Builder / Airflow 中跳过额外的“登录方式”页面

[英]How to skip extra 'sign in with' page in Flask App Builder / Airflow

我已经开始使用Apache Airflow (基于 FAB 构建)并通过 OAuth 启用身份验证(如FAB Security中所示)。 我只有一个 OAUTH_PROVIDER(天蓝色)。

仅供参考,Airflow 版本 2.1.4

当我启动我的 airflow 主页时,它曾经打开我的 OAUTH_PROVIDER 的直接登录页面。

Airflow 2.1.4 登录页面

现在,当我将 airflow 升级到 2.2.4 并配置相同的 OAUTH (Azure) 提供程序时,真正的问题开始了。

当我启动我的 airflow 主页时,出现这样的页面

Airflow 2.2.4 登录页面

点击“Sign In with azure”按钮后,进入用户登录页面。

与旧版 airflow 相比,最新版多了一个页面。

为什么对我来说很重要?

我们在 web 应用程序中渲染 airflow,这个额外的“登录方式”页面看起来不太好。

请提供一些有关跳过该额外交互的信息。

升级 Airflow、Flask 后,Appbuilder 版本可能也发生了变化,导致行为发生变化。 基本上:

如果您希望在部署中使用以前的行为,这很可能会起作用(尽管我没有对其进行测试):

  1. 准备自定义视图 class,我们称之为CustomAuth0AuthView - 它会恢复login方法的旧行为。 (我使用 v4.1.3 作为参考代码并稍作修改)。

webserver_config.py

from flask_appbuilder.security.views import AuthOAuthView


class CustomAuthOAuthView(AuthOAuthView):
    @expose("/login/")
    @expose("/login/<provider>")
    @expose("/login/<provider>/<register>")
    def login(self, provider: Optional[str] = None) -> WerkzeugResponse:
        log.debug("Provider: {0}".format(provider))
        if g.user is not None and g.user.is_authenticated:
            log.debug("Already authenticated {0}".format(g.user))
            return redirect(self.appbuilder.get_url_for_index)

        if provider is None:
            if len(self.appbuilder.sm.oauth_providers) > 1:
                return self.render_template(
                    self.login_template,
                    providers=self.appbuilder.sm.oauth_providers,
                    title=self.title,
                    appbuilder=self.appbuilder,
                )
            else:
                provider = self.appbuilder.sm.oauth_providers[0]["name"]

        log.debug("Going to call authorize for: {0}".format(provider))
        state = jwt.encode(
            request.args.to_dict(flat=False),
            self.appbuilder.app.config["SECRET_KEY"],
            algorithm="HS256",
        )
        try:
            if provider == "twitter":
                return self.appbuilder.sm.oauth_remotes[provider].authorize_redirect(
                    redirect_uri=url_for(
                        ".oauth_authorized",
                        provider=provider,
                        _external=True,
                        state=state,
                    )
                )
            else:
                return self.appbuilder.sm.oauth_remotes[provider].authorize_redirect(
                    redirect_uri=url_for(
                        ".oauth_authorized", provider=provider, _external=True
                    ),
                    state=state.decode("ascii") if isinstance(state, bytes) else state,
                )
        except Exception as e:
            log.error("Error on OAuth authorize: {0}".format(e))
            flash(as_unicode(self.invalid_login_message), "warning")
            return redirect(self.appbuilder.get_url_for_index)
  1. 准备自定义安全管理器。 我们称之为CustomSecurityManager 它将使用您的自定义视图来处理登录。

webserver_config.py

from airflow.www.security import AirflowSecurityManager


class CustomSecurityManager(AirflowSecurityManager):
    authoidview = CustomAuthOAuthView
  1. 配置 Airflow 以使用您的CustomSecurityManager

webserver_config.py

SECURITY_MANAGER_CLASS = CustomSecurityManager

更多关于这个:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM