rest - 我应该将我的 SPA 中的 id token 发送到我的休息后端吗?

标签 rest security authentication oauth-2.0 openid-connect

我有一个由 REST API 服务器支持的 SPA 应用程序。

我使用 Auth0 使用隐式授权流程进行身份验证和授权。
我阅读的所有示例都说明我应该将收到的访问 token 发送到 api 以进行授权。例如 :
https://auth0.com/blog/why-should-use-accesstokens-to-secure-an-api

另一方面,我读到访问 token 不能用作身份验证的证明:
http://www.thread-safe.com/2012/01/problem-with-oauth-for-authentication.html
https://oauth.net/articles/authentication/

这意味着,我不能相信我的访问 token 上的 sub 声明来确保这确实是用户而不是另一个发送其访问 token 的客户端。
意思是,如果我将 facebook 用作 IDP,另一个 Web 应用程序可以将颁发给用户使用的访问 token 发送到我的服务器,并且由于访问 token 没有 aud 声明,我的服务器会认为用户已通过身份验证在我的网络应用程序中。
而且,我看到google登录确实引导spa向服务器发送id token :https://developers.google.com/identity/sign-in/web/backend-auth

所以:我应该将 id token (用于身份验证)和访问 token (用于授权)发送到我的服务器吗?

最佳答案

我经历了 Authenticate with a backend server 正如你所指出的。正如它所建议的那样,可以使用 id token 对后端服务器进行身份验证。这不仅是谷歌推荐的,还有其他一些实体的推荐。但是 id token 用于依赖方(客户端)来验证和验证最终用户。访问 token 是应该用于访问资源的 token 。

您可以考虑的一种替代方法是使用 用户信息端点 由 OpenID Connect 规范定义。

User info endpoint

The UserInfo Endpoint is an OAuth 2.0 Protected Resource that returns Claims about the authenticated End-User. To obtain the requested Claims about the End-User, the Client makes a request to the UserInfo Endpoint using an Access Token obtained through OpenID Connect Authentication. These Claims are normally represented by a JSON object that contains a collection of name and value pairs for the Claims.



Google 确实提供用户信息端点。他们的 documentation's 获取用户资料信息 部分解释了端点、如何调用它和响应细节。

To obtain additional profile information about the user, you can use the access token (which your application receives during the authentication flow) and the OpenID Connect standard:



成功的详细信息将显示最终用户信息,其格式在 中进行了解释。 People: getOpenIdConnect 格式。

这样您就可以避免将 id token 暴露给其他方。您的后端可以使用访问 token 来访问这些信息,以检测最终用户并基于此进行身份验证。

不管这些替代方法如何,id token 都旨在用于身份验证。因此,只要您保护 id token ,就可以将其传递给服务器并使用声明来识别最终用户并在 token 有效性上进行身份验证。

关于rest - 我应该将我的 SPA 中的 id token 发送到我的休息后端吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48153269/

相关文章:

authentication - 什么是直径协议(protocol)?

java - Restful : How to dynamically extend path via interface

java - 需要有关剩余 PUT 方法的解释

web-services - 最佳实践 - REST API 版本控制 : Where and How to physically store source code

php - 我的网站中的一篇帖子浏览量正在迅速增加 - 有人正在玩我的网站

python - 在 Windows 上运行良好时在 Linux 上运行 Mechanize 脚本时遇到问题

带有密码确认的 Javascript 表单验证

c# - 如何使用 ServiceStack 测试 WebServiceException 处理?

c# - 在 ASP.NET MVC 应用程序中处理记录/实体级别的安全性

java - 用于在 Java 中生成 key 的 SHA3-512