security - HTTP 基本身份验证 + 访问 token ?

标签 security authentication oauth oauth-2.0 basic-authentication

我正在开发一个 REST API,计划将其与 Web 和 IOS 应用程序一起使用。我打算让这个 API 在一段时间内保持私有(private)(私有(private)意味着我只希望我的 Web 应用程序和 ios 应用程序访问该 API)。

我已经了解了许多不同的身份验证方法,但我仍然很困惑为我的 API 选择适当的身份验证方法。

据我了解,oAuth2是为了允许用户使用其他服务提供商登录您的APP,以便您可以访问相应服务提供商的数据。我正在自己的 API 中访问数据,所以我相信这不适用于我?

所以,这就是我的想法:

  • 1) 使用 HTTP 基本身份验证将用户/密码发送到服务器。

  • 2) 服务器验证登录后,返回将在 x 小时后过期的访问 token 。这将允许我简单地存储 token 而不是用户/通行证凭据。

我在 Google 上搜索了这种技术,但没有真正找到有关此方法的任何信息,这让我相信这不是一个好方法,因为我可能会尝试重新发明某些东西?

我应该做什么?我正在寻找两条腿的 oAuth 吗?

最佳答案

OAuth 2.0 已成为保护 Web API 的首选协议(protocol)。它需要用户授权应用程序访问您的 Web API。

您希望您的应用程序是唯一可以访问某些 API 的应用程序。 OAuth 2.0 允许这样做。

在您的授权服务器中,实现 Authorization Code Grant需要客户端凭据(不是可选的)。确保只有您的应用程序(或配置的第一方应用程序列表)才能获取进行这些 API 调用所需的范围。只要您确实对客户端 secret 保密,您的应用程序将是唯一能够获得具有所需范围的访问 token 的应用程序。在 Web API 中,确保将范围授予用于调用 API 的访问 token 。

良好的授权服务器,例如The Identity Hub ,将允许您做到这一点。

请勿使用Resource Owner Password Credentials Grant 。正如规范所述:

The credentials should only be used when there is a high degree of trust between the resource owner and the client (e.g., the client is part of the device operating system or a highly privileged application), and when other authorization grant types are not available (such as an authorization code).

这是重复的later on :

The authorization server should take special care when enabling this grant type and only allow it when other flows are not viable.

如果密码凭据授予可用,任何应用程序都可以通过询问用户的用户 ID 和密码来获取 token 。这正是您不想要的。

规范is very clear关于使用密码固有的问题:

In the traditional client-server authentication model, the client requests an access-restricted resource (protected resource) on the server by authenticating with the server using the resource owner's credentials. In order to provide third-party applications access to restricted resources, the resource owner shares its credentials with the third party. This creates several problems and limitations

OAuth 2.0 专门设计用于克服使用密码的一些问题:

OAuth addresses these issues by introducing an authorization layer and separating the role of the client from that of the resource owner. In OAuth, the client requests access to resources controlled by the resource owner and hosted by the resource server, and is issued a different set of credentials than those of the resource owner.

此外,如果您的 API 想要了解用户(除了了解客户端应用程序之外),则不可能滥用资源所有者密码凭证授予来验证客户端(即应用程序)而不是资源所有者(即用户) ),按照 Florent Morselli 的建议。

关于security - HTTP 基本身份验证 + 访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26502495/

相关文章:

python - 托管应用程序的 Google 联合登录 (OpenID+Oauth) - 更改端点?

flash - 将 HTTPS 与 App Engine 结合使用以确保来自 Flash swf 的安全轮询结果

facebook - 应用程序配置不允许给定 URL Facebook 应用程序错误

php - 创建登录表单

php - Symfony2 异常捕捉困惑

javascript - LinkedIn:Javascript token 并在服务器中验证用户

.net - MVVM/ViewModels 和处理授权

security - 编写自己的加密是否违法?

java - 自动为 JWS 应用程序安装证书

android - 使用 Android 帐户管理器验证在我的服务器上使用 FB 登录的用户