gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:19:47 27 4
gpt4 key购买 nike

我正在开发一个 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 highdegree of trust between the resource owner and the client (e.g., theclient is part of the device operating system or a highly privilegedapplication), and when other authorization grant types are notavailable (such as an authorization code).

这是重复的later on :

The authorization server should take special care whenenabling this grant type and only allow it when other flows are notviable.

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

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

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

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

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

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

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com