我的 EKS Web 应用程序如何通过 OKTA OIDC 检索由 AWS ALB 验证的当前用户

How can my EKS web application retrieve the current user authenticated by AWS ALB through OKTA OIDC

我正在使用部署在 Amazon Elastic Kubernetes Service (EKS) 上的 Web 应用程序。用户身份验证是通过 Okta 完成的。一切正常,但如果我已登录,我希望 Web 应用程序显示我的名字,即 当前用户的名字

有一个 blog post 详细描述了我拥有的设置类型,这也是通过 Terraform 完成的。到目前为止,我对此的在线搜索让我找到了关于如何设置身份验证(已经完成)的答案,而不是关于如何获取当前用户的答案。

我的基本理解是:原始请求转到应用程序负载均衡器 (ALB),并使用 Okta 处理身份验证。最终结果是一个会话 cookie,应用程序通过浏览器发送未来的请求,以便它们最终到达后端可以响应的目标 IP。这是来自 AWS documentation:

的身份验证流程的相关文本

"After the load balancer validates the ID token, it exchanges the access token with the IdP user info endpoint to get the user claims.

The load balancer creates the authentication session cookie and sends it to the client so that the client's user agent can send the cookie to the load balancer when making requests."

我可以从负载均衡器侦听器规则中看到有一个用户信息端点:https://myorg.okta.com/oauth2/v1/userinfo 但是客户端应用程序可以使用它来检索用户详细信息,如果是这样,怎么办?我尝试使用会话 cookie curl 它,但出现 400 错误。

还有一个 API 记录在 Okta site 上 "Fetches the current user linked to API token or session cookie":

curl -v -X GET \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: SSWS ${api_token}" \
"https://${yourOktaDomain}/api/v1/users/me"

我没有 API 令牌,所以我尝试使用 AWSELBAuthSessionCookie,但得到:

{"errorCode":"E0000011","errorSummary":"Invalid token provided","errorLink":"E0000011","errorId":"oaeRNui7fSPQIq_LWuGro5EbV","errorCauses":[]}

任何带有步骤摘要和相关文档链接的答案都很好。谢谢。

用户信息始终由 ALB 在 header 中针对来自客户端的每个请求发送到应用程序(目标),因此 back-end 将需要处理它并以某种方式使它可供 front-end 使用。可能最简单的方法是将其包含在响应中。

答案实际上在关于 user claims:

的文档中更进一步

After your load balancer authenticates a user successfully, it sends the user claims received from the IdP to the target. The load balancer signs the user claim so that applications can verify the signature and verify that the claims were sent by the load balancer.

The load balancer adds the following HTTP headers:

x-amzn-oidc-accesstoken The access token from the token endpoint, in plain text.

x-amzn-oidc-identity The subject field (sub) from the user info endpoint, in plain text.

x-amzn-oidc-data The user claims, in JSON web tokens (JWT) format.

最后的header正是我需要的。它具有所有用户信息,Base64 编码。值得注意的是,我已经为 x-amzn-oidc-data 搜索了日志,但一无所获。但是在通过将请求日志传输到 grep -i x-amzn-oidc-data 进行不区分大小写的搜索后,我发现了很多带有 X-Amzn-Oidc-Data header.

的条目

顺便说一下,我使用 mitmproxy 来记录对应用程序的每个请求,包括 headers,结果对找到这个解决方案非常有用!