简体   繁体   English

如何从Web应用程序向API验证用户身份?

[英]how can I authenticate a user from a web app to an 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 ;)). 这似乎是一个广泛问的问题,在阅读了大量关于这个主题的文档后,我仍然不确定是否正确理解了所有内容(我认为愚蠢是一个可能的答案;))。

I'm trying to build an API that will provide a service to users. 我正在尝试构建一个为用户提供服务的API。 The users will be connected through Facebook or any OpenId provider (I separate Facebook since their implement their own connecting system). 用户将通过Facebook或任何OpenId提供商连接(我将Facebook分开,因为他们实现了自己的连接系统)。

(I think it's a good way because I will not store the user's password and finally will have less problem in case of a similar Gawker issue.) (我认为这是一个很好的方法,因为我不会存储用户的密码,并且在类似的Gawker问题的情况下最终会有更少的问题。)

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. 当客户端(Web应用程序,移动应用程序等)向API发出请求时,必须与请求一起发送指示符 ,以便确定哪个用户正在使用该应用程序。 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. 而对于认证,我无法找到任何有价值的例子,教程,了解如何正确地实现它的解释。

I'll (try to) explain : 我(试着)解释一下:

In my (wonderful world of happy care bears), I structured my project in various parts : 在我(幸福护理熊的精彩世界)中,我将项目分为不同部分:

  • A RESTful API RESTful API
  • A web apps that will use the api. 一个将使用api的网络应用程序。 Ideally, I was thinking about making a complete html/css/js project, without any server side work (php/python/java or whatever) 理想情况下,我正在考虑制作一个完整的html / css / js项目,没有任何服务器端工作(php / python / java或其他)
  • A mobile application 移动应用程序
  • An windows/mac/linux application 一个windows / mac / linux应用程序

As far as I saw, every time someone ask how to implement a RESTful API authentication, three major answers pops out : 据我所知,每当有人询问如何实现RESTful API身份验证时,会弹出三个主要答案:

  • The HTTP basic( + preferably SSL)/digest way HTTP基本(最好是SSL)/摘要方式
  • OAuth OAuth的
  • OpenId 的OpenID

Since I will not store the user's password, the first one is out for me, but the two other leave me perplex. 因为我不会存储用户的密码,所以第一个用于我,但另外两个让我感到困惑。

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 ! 但OAuth和OpenId 并不相同 ,一个(OpenId)代表身份验证 (问题的基础),其中第二个(OAuth)代表授权

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). 当Twitter为其API实现OAuth时,他们没有实现身份验证系统,有一种方法可以指示用户应用程序X希望访问用户帐户(在各种访问级别)。 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. 如果用户当前未登录Twitter,则他首先必须对自己进行身份验证, 然后授权当前应用程序访问其数据。

So, just to clear things up, OAuth is NOT an authentication mechanism , it's a : 所以,为了清理起来, OAuth不是一种身份验证机制 ,它是:

An open protocol to allow secure API authorization (source: http://oauth.net/ ) 允许安全API授权的开放协议(来源: http//oauth.net/

Then, the only way to authenticate a user would be using OpenId. 然后,验证用户的唯一方法是使用OpenId。 And then, the hell comes true. 然后,地狱成真了。

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. 如果我以一个专门由html / css / js组成的Web应用程序为例,没有服务器端组件,则与API通信。

The web app must indicate to the API that the user currently using the API is mister X. Web应用程序必须向API指示当前使用API​​的用户是X先生。

To do so, the web app show a popup containing a list of OpenId providers, asking the user to authenticate himself. 为此,Web应用程序会显示一个包含OpenId提供程序列表的弹出窗口,要求用户对自己进行身份验证。 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). 用户单击其中一个,重定向(或打开一个新的弹出窗口)到OpenId提供程序,指示他的登录/通过,由OpenId提供程序进行身份验证,使用令牌返回成功(我简化了通信)。

That's great, the web app know now that the user is really mister X. But the API still have any clue ! 这很棒,网络应用现在知道用户真的是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. 最后,我的问题非常简单:我如何通过Web应用程序通过OpenId对x先生进行身份验证,然后,如何通过Web应用程序和api保存当前正在使用Web应用程序的先生X的信息当然还有API。

Thank you very much for your help ! 非常感谢您的帮助 !

-edited format - 编辑格式

(If you don't want to read, the list bellow sum up the whole idea) (如果你不想阅读,下面的列表总结了整个想法)

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. 一个可能的解决方案(告诉我,如果我错了)将在消费者(网络应用程序,移动应用程序等)中显示登录表单,用户点击它的提供者(myopenid,谷歌等)打开一个弹出窗口登录。 The tricky part is that the return_to parameter would be set to the API, not the website 棘手的部分是return_to参数将设置为API,而不是网站

The API will then resend the check_authentication and get the is_valid:true (or not). 然后,API将重新发送check_authentication并获取is_valid:true(或不是)。 During this step, the app would query the api to a specific url that return the state of the authentication (processing, failed, success). 在此步骤中,应用程序将查询api以返回返回身份验证状态(处理,失败,成功)的特定URL。 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. 在处理过程中,会向用户显示一个指示符(加载gif),如果成功/失败,则会向用户显示结果。

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. 如果api收到一个is_valid:true,那么它会向openid服务器询问有关用户的信息,比如email,firstname,lastname,并将它们与用户的数据库进行比较。 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. 如果匹配,则api在其自身与应用之间创建会话,如果用户是新用户,则创建新条目然后创建会话。

The session would be a unique token with a specific duration (maybe equal to the openid server assoc_handle duration ?) 会话将是具有特定持续时间的唯一令牌(可能等于openid server assoc_handle持续时间?)

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" : 为了解释简单的事情,这里有一点“地图”:

Note: Provider is the OpenId server (that provide the informations about the authentication) 注意:Provider是OpenId服务器(提供有关身份验证的信息)

  • The User go the webapp and click on the login icon of his provider (Google for ex) 用户访问webapp并单击其提供商的登录图标(Google for ex)
  • The webapp opens a popup containing the provider login page and access page, and specify a return_to to the Api webapp打开一个包含提供者登录页面和访问页面的弹出窗口,并指定返回到Api的return_to
  • The provider sends informations to the Api 提供者向Api发送信息
  • The Api validate these informations via the check_authentication Api通过check_authentication验证这些信息
  • If not valid, the API indicate to the webapp (that ask the api every x seconds) the failure 如果无效,API会向webapp指示(每隔x秒询问api)失败
  • If valid, the Api asks informations about the user to the provider, like email, display name, etc 如果有效,Api会向提供者询问有关用户的信息,例如电子邮件,显示名称等
  • If the user exists, a session is created 如果用户存在,则创建会话
  • If the user is new, he's added to the database and the session is created 如果用户是新用户,则将其添加到数据库并创建会话
  • The Api returns the state of the auth (in this case, success) with a token session that will be used by the web app for further requests. Api使用令牌会话返回auth的状态(在本例中为成功),该会话将由Web应用程序用于进一步请求。

You don't really want to login to the API using OpenID. 您真的不想使用OpenID登录API。 As you said, OpenID is for Authentication , ie Who, while OAuth is for Authorization , ie am I allowed? 如你所说,OpenID用于身份验证 ,即Who,而OAuth用于授权 ,即我是否允许? But your structure suggest you'll be using an API as a backend and a web app as a front-end. 但是您的结构建议您将API用作后端,将Web应用程序用作前端。

The best way then is to use OpenID on the web-app to authenticate the user, and then the web-app connects to the API and stores the OpenID credentials. 最好的方法是在Web应用程序上使用OpenID对用户进行身份验证,然后Web应用程序连接到API并存储OpenID凭据。 The web-app then knows who the user is, and can provide the service. 然后,web-app知道用户是谁,并且可以提供服务。 The API has nothing to do with the user, except that it stores its data. API与用户无关,只是它存储了数据。

The fundamental difference between OpenID and OAuth is its use. OpenID和OAuth之间的根本区别在于它的使用。 In your situation, you could have something like that: 在你的情况下,你可以有类似的东西:

--------          ---------            -------
| User | <------> |  App  | <--------> | API |
--------  OpenID  ---------   (OAuth)  -------

The User never interacts directly with the API: who would want to manually send HTTP request? 用户永远不会直接与API交互:谁想要手动发送HTTP请求? (lol) Instead, the service is provided through the app, which can optionally be authorized using OAuth. (lol)相反,该服务是通过应用程序提供的,可以选择使用OAuth授权。 However, in the case of a single app accessing the API, you can make the app <=> API connection internal and never expose it. 但是,如果单个应用程序访问API,您可以将应用程序<=> API连接内部并且永远不会公开它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用web api验证CRM用户? - how to authenticate CRM user using web api? 如何验证试图访问Azure AD中托管的Web Api的用户名/密码? - How do I authenticate a user's name/password trying to access Web Api hosted in Azure AD? 如何验证我的API的用户 - How Can I Authenticate Users of my API 如何在不要求用户凭证的情况下认证从Unity到Node的Web请求? - How do I authenticate a web request from Unity to Node without requiring user credentials? 如何在我的 API 上针对 Steam 验证用户? - How do I authenticate a User on my API against Steam? 如何根据 Laravel 中 rest API 的响应对用户进行身份验证? - How to authenticate user from the respons of rest API in Laravel? 如何使用Spotify网络API获取用户的播放列表列表? - How can I get a list of playlists by user with the spotify web api? 如何从商店的主题中验证Shopify API? - How can I authenticate Shopify API from within my store's theme? 如何在同一个域中托管我的API和Web应用程序? - How can I host my API and web app on the same domain? 如何将 web 应用程序与 fablo API 集成? - how can I integrate web app with fablo API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM