【问题标题】:How to pass user's claim to upstream layer using mod_auth_openidc?如何使用 mod_auth_openidc 将用户声明传递给上游层?
【发布时间】:2018-10-03 03:38:27
【问题描述】:

我已经使用 Auth0 和 Google App Oauth 成功地使用 mod_auth_openidc 配置了 apache Web 服务器来保护我们的内部仪表板,如本文档中所述: - https://github.com/zmartzone/mod_auth_openidc#openid-connect-sso-with-google-sign-in - https://auth0.com/docs/quickstart/webapp/apache/01-login(不使用 auth0 规则管道)

我的问题是如何将用户的声明作为 http 标头传递给上游层(我们的内部工具/仪表板)?有可能吗?

问候, 阿贡

更新

我已经尝试过here 的建议, 这是我的 /etc/apache2/sites-available/000-default.conf 的 sn-p

<VirtualHost *:443>
ServerName my-host-name
UseCanonicalName on
ProxyPreserveHost on
DocumentRoot /var/www/html


# Pass the user's claim as http headers
OIDCPassClaimsAs "headers"
OIDCPassUserInfoAs "claims"
OIDCPassRefreshToken "On"
<Location />
  AuthType openid-connect

  <RequireAll>
    Require claim email~^(.*)@domain.com$
    Require claim email_verified:true
  </RequireAll>


  ProxyPass http://echo-server.default.svc.cluster.local:8080/
  ProxyPassReverse http://echo-server.default.svc.cluster.local:8080/
  LogLevel debug

</Location>
</VirtualHost>

我使用 echoserver (gcr.io/google_containers/echoserver:1.0) 作为 http://echo-server.default.svc.cluster.local:8080 的后端,它不会将任何用户的声明打印为 http 标头。我是否有任何错误配置?如何调试这个问题?

【问题讨论】:

    标签: apache oauth-2.0 mod-auth-openidc


    【解决方案1】:

    我正在使用 docker 映像 httpd:2.4-buster,从 Debian 软件包 repo 安装 libapache2-mod-auth-openidc 版本为 2.3.10,并代理对子位置(不是 /)的请求。我通过将LogLevel auth_openidc:debug 添加到httpd.conf 来打开调试,然后我看到了set-header 和set-env 调用声明的痕迹。但是使用上面的设置,我无法观察到任何 OIDC 设置的标头到达代理的 Python Flask WSGI 应用程序。

    我的解决方法是将环境变量转换为标题。我在 REST API 路径的 &lt;Location&gt; 块内添加了以下配置行,位于 Apache HTTPD 配置的 &lt;VirtualHost&gt; 块内:

        # module sets REMOTE_USER as env var
        RequestHeader set X-Forwarded-User-Email %{REMOTE_USER}s
    
        # module sets OIDC_CLAIM_sub as env var
        PassEnv OIDC_CLAIM_sub
        RequestHeader set X-Forwarded-User %{OIDC_CLAIM_sub}e
    

    然后应用获取用户名和完全限定的 user@host.domain 作为请求标头。

    更新:通过@https://github.com/zmartzone/mod_auth_openidc/discussions/705@hans-z 的讨论获得了进一步的帮助

    那次讨论告诉我,WSGI http 服务器会默默地丢弃所有名称中包含下划线的标头!另一种解决方案(而不是将环境变量转换为标头)是在 httpd.conf 文件中更改 mod-auth-openidc 前缀字符串,如下所示:

    OIDCClaimPrefix Oidc-Claim-
    

    然后所有由 mod-auth-openidc 设置的标头在进入代理的 Flask + WSGI 应用程序的过程中仍然存在。

    【讨论】:

      【解决方案2】:

      这就是模块默认执行的操作:它将在环境变量和标头中传递用户的声明,可以使用OIDCPassClaimsAs 进行配置,如下所述: https://github.com/zmartzone/mod_auth_openidc/blob/v2.3.8/auth_openidc.conf#L668

      # Define the way in which the claims and tokens are passed to the application environment:
      # "none": no claims/tokens are passed
      # "environment": claims/tokens are passed as environment variables
      # "headers": claims/tokens are passed in headers (also useful in reverse proxy scenario's)
      # "both": claims/tokens are passed as both headers as well as environment variables (default)
      # When not defined the default is "both"
      # The access token is passed in OIDC_access_token; the access token expiry is passed in OIDC_access_token_expires.
      # The refresh token is only passed in OIDC_refresh_token if enabled for that specific directory/location (see: OIDCPassRefreshToken)
      #OIDCPassClaimsAs [none|headers|environment|both]
      

      请注意,这些标头已添加到传播到应用程序的后端 HTTP 请求中,因此您不会在浏览器中看到它们。

      【讨论】:

      • 我已经尝试了您的建议,但未能将任何用户的声明打印为 http 标头。我已根据您的建议更新了我的问题
      • 问题似乎与回显服务器有关;将LogLevel 设置为auth_openidc:debug,您应该会看到正在设置的标题
      • 是的,你是对的,问题出在gcr.io/google_containers/echoserver:1.0。我使用另一个 echoserver 变体,它可以正确打印 http 标头。谢谢:)
      猜你喜欢
      • 2020-12-23
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-26
      相关资源
      最近更新 更多