如何验证用户从 Web 应用程序到 API 的身份? [英] how can I authenticate a user from a web app to an API?

查看:36
本文介绍了如何验证用户从 Web 应用程序到 API 的身份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个被广泛提出的问题,在阅读了大量关于该主题的文档后,我仍然不确定是否正确理解了所有内容(我认为愚蠢是一个可能的答案;)).

It seems to be a widely asked questions and after having read tons of documentations on the subject, I'm still not sure to have understood everything correctly (I assume that being dumb is a possible answer ;)).

我正在尝试构建一个可以为用户提供服务的 API.用户将通过 Facebook 或任何 OpenId 提供商进行连接(我将 Facebook 分开,因为他们实施了自己的连接系统).

I'm trying to build an API that will provide a service to users. The users will be connected through Facebook or any OpenId provider (I separate Facebook since their implement their own connecting system).

(我认为这是一个好方法,因为我不会存储用户的密码,最终在类似 Gawker 问题的情况下问题更少.)

当客户端(网络应用、移动应用等)向 API 发出请求时,必须随请求一起发送一个指示符,以识别哪个用户正在使用该应用.这通常通过在身份验证期间定义的令牌使用.

When a request is made from the client (web app, mobile app, whatever) to the API, an indicator must be sent with the request in order to identify which user is using the app. This is generally used via a token, defined during the Authentication.

但关于身份验证,我找不到任何有价值的示例、教程、关于如何正确实施它的解释.

But regarding the Authentication, I can't find any valuable example, tutorial, explanations about how to implement it correctly.

我会(尝试)解释:

在我的(快乐的小熊的奇妙世界)中,我将我的项目分为多个部分:

In my (wonderful world of happy care bears), I structured my project in various parts :

  • RESTful API
  • 将使用该 API 的网络应用.理想情况下,我正在考虑制作一个完整的 html/css/js 项目,没有任何服务器端工作(php/python/java 或其他)
  • 一款移动应用
  • 一个 windows/mac/linux 应用程序

据我所知,每次有人问如何实现 RESTful API 身份验证时,都会弹出三个主要答案:

As far as I saw, every time someone ask how to implement a RESTful API authentication, three major answers pops out :

  • HTTP 基本(+ 最好是 SSL)/摘要方式
  • OAuth
  • OpenId

因为我不会存储用户的密码,第一个是我的,但其他两个让我很困惑.

Since I will not store the user's password, the first one is out for me, but the two other leave me perplex.

但是 OAuth 和 OpenId 相同,一个 (OpenId) 代表 身份验证(问题的基础),其中第二个 (OAuth) 代表授权

But OAuth and OpenId are not the sames, one (OpenId) stand for the Authentication (that the base of the questions) where the second (OAuth) stand for the Authorization!

当 Twitter 为其 API 实施 OAuth 时,他们并未实施身份验证系统,而是设置了一种方法来指示其用户应用程序 X 想要访问用户帐户(在不同级别的访问权限).如果用户当前未登录 Twitter,他将首先对自己进行身份验证,然后授权当前应用程序访问他的数据.

When Twitter implements OAuth for their API, they are not implementing an Authentication system, there are setting up a way to indicate their users that the application X want to have access to the user account (in various level of access). If the user is not currently logged in Twitter, he will first have to authenticate himself, and then authorize the current application to access his data.

所以,澄清一下,OAuth 不是一种身份验证机制,它是 :

So, just to clear things up, OAuth is NOT an authentication mechanism, it's a :

允许安全 API 的开放协议授权(来源:http://oauth.net/)

An open protocol to allow secure API authorization (source: http://oauth.net/)

然后,验证用户身份的唯一方法是使用 OpenId.然后,地狱成真了.

Then, the only way to authenticate a user would be using OpenId. And then, the hell comes true.

如果我以一个完全由 html/css/js 组成的 Web 应用程序为例,没有服务器端组件,则与 API 进行通信.

If I take as an example a web application that is exclusively made of html/css/js, with no server side components, communicate with an API.

Web 应用程序必须向 API 表明当前使用 API 的用户是 X 先生.

The web app must indicate to the API that the user currently using the API is mister X.

为此,Web 应用程序会显示一个包含 OpenId 提供程序列表的弹出窗口,要求用户对自己进行身份验证.用户点击其中一个,被重定向(或打开一个新的弹出窗口)到 OpenId 提供者,表明他的登录/通行证,通过 OpenId 提供者进行身份验证,用令牌返回成功(我简化了通信).

To do so, the web app show a popup containing a list of OpenId providers, asking the user to authenticate himself. The user click on one of them, get redirected (or a new popup open up) to the OpenId provider, indicate his login/pass, get authenticated by the OpenId provider, that return the success with a token (I simplified the communication).

太好了,Web 应用程序现在知道用户确实是 X 先生.但 API 仍然有任何线索!

That's great, the web app know now that the user is really mister X. But the API still have any clue !

最后,我的问题很简单:我如何通过 Web 应用程序通过 OpenId 向 API 验证 x 先生的身份,然后,Web 应用程序和 API 如何保留当前正在使用的 X 先生的信息网络应用程序,当然还有 API.

Finally, my question is quite simple : how can I authenticate mister x through the web app to the API via OpenId and after that, how can the web app and the api keep the information that this is mister X that is currently using the web app and of course, the API.

非常感谢您的帮助!

-编辑格式

推荐答案

(如果你不想阅读,下面的列表总结了整个想法)

一个可能的解决方案(如果我错了请告诉我)是在消费者(网络应用、移动应用等)中显示登录表单,用户点击它的提供者(myopenid、谷歌等),它会打开一个弹出窗口来进行登录.棘手的部分是 return_to 参数将设置为 API,而不是网站

A possible solution (tell me if I'm wrong) would be to display the login form in the consumer (web apps, mobile apps, etc), the user click on it's provider (myopenid, google, etc) that opens a popup to do the login. The tricky part is that the return_to parameter would be set to the API, not the website

然后 API 将重新发送 check_authentication 并获取 is_valid:true(或不是).在此步骤中,应用程序将查询 api 以返回身份验证状态(处理、失败、成功)的特定 url.在处理过程中,会向用户显示一个指示符(正在加载 gif),如果成功/失败,则会向用户显示结果.

The API will then resend the check_authentication and get the is_valid:true (or not). During this step, the app would query the api to a specific url that return the state of the authentication (processing, failed, success). While it's procesing, an indicator is displayed to the user (loading gif), and if it's success/fail the result is displayed to the user.

如果 api 收到 is_valid:true,那么它会向 openid 服务器询问有关用户的信息,如电子邮件、名字、姓氏,并将它们与用户的数据库进行比较.如果匹配,api 在它自己和应用之间创建一个会话,如果用户是新用户,它创建一个新条目,然后创建会话.

If the api receive a is_valid:true, then it will ask informations about the user to the openid server, like email, firstname, lastname, and compare them with it's user's database. If there is a match, the api create a session between itself and the app, if the user is new, it create a new entry and then the session.

会话将是具有特定持续时间的唯一令牌(可能等于 openid 服务器 assoc_handle 持续时间?)

The session would be a unique token with a specific duration (maybe equal to the openid server assoc_handle duration ?)

这似乎是可能的,但我不是安全专家.

It seems to be something possible, but I'm not an expert in security.

为了更简单地解释事情,这里有一个小地图":

In order to explain things simplier, here is a little "map" :

注意:Provider 是 OpenId 服务器(提供认证信息)

Note: Provider is the OpenId server (that provide the informations about the authentication)

  • 用户访问网络应用程序并点击其提供商(例如 Google)的登录图标
  • Web 应用程序打开一个包含提供程序登录页面和访问页面的弹出窗口,并指定 return_to 到 Api
  • 提供者向 Api 发送信息
  • API 通过 check_authentication 验证这些信息
  • 如果无效,API 会向 Web 应用程序(每 x 秒询问一次 API)指示失败
  • 如果有效,Api 会向提供商询问有关用户的信息,例如电子邮件、显示名称等
  • 如果用户存在,则创建一个会话
  • 如果用户是新用户,则会将其添加到数据库并创建会话
  • Api 使用令牌会话返回身份验证的状态(在本例中为成功),Web 应用程序将使用该会话进行进一步请求.

这篇关于如何验证用户从 Web 应用程序到 API 的身份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆