代码之家  ›  专栏  ›  技术社区  ›  Steffen Zschaler

同时支持github。com和github enterprise在设计登录?

  •  0
  • Steffen Zschaler  · 技术社区  · 7 年前

    我正在构建一个Rails应用程序,通常在登录用户时根据本地Github企业安装进行身份验证。我希望允许登录用户将其 github.com 我在我的应用程序上注册了他们的帐户,但很难弄清楚如何做到这一点。

    具体来说,问题是两种身份验证方法都将使用 github 中的身份验证策略 omniauth 设置然而 OAUTH URL以及我的内部回调控制器在每种情况下都需要不同(在一种情况下,我实际上想登录,在另一种情况下,我只想向 current_user )。很遗憾,据我所知,我只能配置 github 策略一次,但我需要为两个不同的提供者设置两种不同的配置。

    我已经在Desive和Omniauth文档中搜索了一段时间,但没有成功。我是不是完全找错人了?如果没有,请有人给我指一下正确的方向好吗?

    非常感谢,

    斯特芬

    1 回复  |  直到 7 年前
        1
  •  0
  •   Steffen Zschaler    7 年前

    devise 消息来源,我找到了一个相当简单的解决方案。核心问题是如何 发明 使用相同的 omniauth 策略两次,但使用不同的API和回调URL。所以我们需要注册两个 omniauth-github 在两个不同的提供者ID下 名称。为了实现这一点,您需要在第二个实例化中显式地给出策略类名。所以,你会得到这样的结果:

    # Github Enterprise login through standard github provider
    config.omniauth :github,
                    Rails.configuration.ghe_oauth_id,
                    Rails.configuration.ghe_oauth_secret,
                    client_options: {
                      site: 'https://github-enterprise/api/v3',
                      authorize_url: 'https://github-enterprise/login/oauth/authorize',
                      token_url: 'https://github-enterprise/login/oauth/access_token'
                    },
                    scope: 'user repo'
    
    # Github.com support using the same strategy, but with a different provider ID and name
    config.omniauth :github_com,
                    Rails.configuration.github_com_oauth_id,
                    Rails.configuration.github_com_oauth_secret,
                    strategy_class: OmniAuth::Strategies::GitHub,
                    name: :github_com,
                    scope: 'user repo'
    

    布线设置与使用单个 omniauth公司 提供商。在我的回调控制器中 发明 回调,我现在有了一个方法 github 用于通过Github Enterprise和one处理身份验证 github_com 对于github。com身份验证。