OpenID 身份验证和 API 访问

问题描述:

OpenID 身份验证本质上是基于浏览器的.如果我想允许 OpenID 用户针对用于替代客户端的 API 进行身份验证,是否有公认的最佳实践?

OpenID authentication is inherently browser based. If I wanted to allow an OpenID user to authenticate against an API for use in alternative clients, is there an accepted best practice for that?

例如,如果用户尝试使用其 OpenID 登录 iPhone 应用程序,这将如何运作?我唯一能想到的就是为他们生成某种 API 令牌并让用户在某处手动输入它.这种方法对用户不友好.

So if a user tried to log in with their OpenID into an iPhone app, for instance, how would that work? The only thing I can think of generating an API token of some sort for them and get the user to manually enter it somewhere. This approach is not user friendly.

这是像 Basecamp 这样的网站的工作方式,但对我来说它仍然很笨拙.

This is the way sites like Basecamp work, but it still seems clunky to me.

您看到的问题并非 OpenID 独有.任何无密码身份验证方案都可能存在此问题.OAuth (http://oauth.net/) 是一种开放标准的解决方案,在很多网站.它完全独立于用户的身份验证方式,因此他们的 OpenID 提供者不需要支持甚至不需要意识到您的站点(OAuth 术语中的服务提供者")正在使用 OAuth.您的 API 客户端可以是基于 Web 的,甚至可以是本地应用程序!

The problem you're seeing is not unique to OpenID. Any password-less authentication scheme can have this problem. OAuth (http://oauth.net/) is a solution that is an open standard that is quickly gaining traction on a lot of web sites. It is totally independent of how the user authenticates, so their OpenID Provider does not need to support or even be aware that your site (the "service provider" in OAuth terms) is using OAuth. Your API client can be web based or even a local application!

流程大概是这样的:

角色:

  • 用户:拥有您网站帐户的人.
  • 服务提供商:您的网站,它具有需要一些凭据才能访问的编程 API.
  • 消费者:需要访问服务提供商 API 的客户端,无论是 Web 应用程序还是本地应用程序.

流程:

  1. 用户是消费者.他表示他想访问服务提供商处的数据.
  2. 用户被重定向(如果消费者是网站)或浏览器弹出(如果消费者是本地应用)并且用户看到服务提供商网站.
  3. 用户要么已经通过持久性 cookie 登录到服务提供商,要么用户必须先登录到服务提供商,但已完成(在您的情况下为 OpenID).
  4. 服务提供商随后会询问用户:消费者(某些消费者)想要访问您的数据(或我们的 API 或其他任何内容).您想对此进行授权吗?(是/否)
  5. 用户确认,浏览器窗口关闭或重定向回消费者网站.
  6. 通过一些 OAuth 协议魔法,消费者现在拥有一个秘密凭证,可用于访问您的 API 并访问您刚刚授权的任何用户隐私信息.

显然我不能在这里包含整个 OAuth 规范,但是您可以从上面看到希望这应该可以解决您的问题.OAuth 库的存在是为了轻松添加对它的支持.

Obviously I can't include the whole OAuth spec here, but you can see hopefully from the above that this should solve your problem. OAuth libraries exist to make adding support for it easy.

如果您碰巧使用 ASP.NET,我建议您使用 http://dotnetopenid.googlecode.com/ 因为它最近添加了 OAuth 支持(v3.0 beta 1).

If you happen to be using ASP.NET, I suggest http://dotnetopenid.googlecode.com/ as it recently added OAuth support (v3.0 beta 1).