简体   繁体   English

如何允许用户使用我的REST API进行身份验证?

[英]How do I allow users to authenticate with my REST API?

I am trying to create a REST API for my next project. 我正在尝试为我的下一个项目创建REST API。 I think I understand most of the concepts, but am a little unsure about security. 我想我了解大多数概念,但是对安全性有些不确定。 Obviously security is the one thing you don't want to get wrong even from the first release of production code. 显然,即使从生产代码的第一个发行版起,安全性就不会出错。

I understand that REST is stateless, so instead of having a user log in and their session be stored on the server and restarted whenever they make a request, they send the server their unique API key and the server authenticates them on every request. 我知道REST是无状态的,因此,与其让用户登录并将会话存储在服务器上并在每次发出请求时重新启动,不如让他们登录,而是向服务器发送其唯一的API密钥,然后服务器对每个请求进行身份验证。

So how does a user logging into a system look "under the hood"? 那么,用户登录系统的外观如何? Is it something like: 是这样的吗?

  1. The user enters their username and password 用户输入用户名和密码
  2. These are sent via POST (or PUT) to an API endpoint 这些通过POST(或PUT)发送到API端点
  3. If the credentials are valid, a unique API key is generated and returned to the client 如果凭据有效,则生成唯一的API密钥并将其返回给客户端
  4. If the credentials are not valid, an error is returned to the client 如果凭据无效,则将错误返回给客户端

It is then the client's responsibility to store the API key and submit it with each request. 然后,客户有责任存储API密钥并随每个请求一起提交。 This key is stored on the server in a database and used to identify the user and their permissions etc. on each request. 该密钥存储在数据库中的服务器上,用于标识每个请求的用户及其权限等。

This sounds reasonable, but also breaks the true statelessness of the application because most requests require the initial "make me an API key" request to have been sent. 这听起来很合理,但也打破了应用程序的真正无状态性,因为大多数请求都需要发送初始的“使我成为API密钥”请求。

Thanks in advance for helping me understand! 预先感谢您帮助我理解!

GroupMe 's API for example uses a token-based approach to authenticating users of their API (this allows users of their API to create scripts to call the API on their behalf). 例如, GroupMe的API使用基于令牌的方法来验证其API的用户(这允许其API的用户创建代表其调用API的脚本)。

In order to successfully make API calls, I (as the user of the API) had to sign in and create an application, which provided me with an Access Token (see image). 为了成功进行API调用,我(作为API的用户)必须登录并创建一个应用程序,该应用程序为我提供了访问令牌(见图)。

This access token (in the case of groupme) is what I had to include with all requests in order for the request to be successful. 这个访问令牌(在groupme的情况下)是我必须包含在所有请求中才能使请求成功的原因。 The format may differ depending on who's API you are using but in the case of groupme, it looks like this: 格式可能会有所不同,具体取决于您使用的是谁的API,但是对于groupme,它看起来像这样:

https://api.groupme.com/v3/PAGE_TO_CALL?token=YOUR_ACCESS_TOKEN

This would allow the GroupMe API to identify me and perform whatever action I had specified. 这将使GroupMe API可以识别我并执行我指定的任何操作。 This access token is like my username and password, whoever has it will be able to make API calls as me, including any scripts I create... 此访问令牌就像我的用户名和密码一样,无论谁拥有它,都可以像我一样进行API调用,包括我创建的所有脚本...

If you are interested in creating your own REST API, I would look into this article, specifically the last section on creating a token-based authentication method that other users could use to authenticate with your API. 如果您有兴趣创建自己的REST API,我会去了解一下这个文章,特别是最后一节上创建其他用户可以使用您的API来验证基于令牌的认证方法。

Here is the most relevant excerpt: 这是最相关的摘录:

API authentication API认证

In normal web applications, handling authentication is usually handled by accepting a username and password, and saving the user ID in the session. 在普通的Web应用程序中,通常通过接受用户名和密码并在会话中保存用户ID来处理身份验证。 The user's browser saves a cookie with ID of the session. 用户的浏览器保存一个带有会话ID的cookie。 When the user visits a page on the site that requires authentication, the browser sends the cookie, the app looks up the session by the ID (if it hasn't expired), and since the user ID was saved in the session, the user is allowed to view the page. 当用户访问需要身份验证的站点上的页面时,浏览器发送cookie,应用程序通过ID(如果尚未过期)查找会话,并且由于用户ID已保存在会话中,允许查看页面。

With an API, using sessions to keep track of users is not necessarily the best approach. 使用API​​,使用会话来跟踪用户不一定是最好的方法。 Sometimes, your users may want to access the API directly, other times the user may way to authorize another application to access the API on their behalf. 有时,您的用户可能希望直接访问API,而有时用户可能会授权其他应用程序代表他们访问API。

The solution to this is to use token based authentication. 解决方案是使用基于令牌的身份验证。 The user logs in with their username and password and the application responds with a unique token that the user can use for future requests. 用户使用其用户名和密码登录,应用程序以唯一令牌响应,该令牌可用于将来的请求。 This token can be passed onto the application so that the user can revoke that token later if they choose to deny that application further access. 该令牌可以传递到应用程序上,以便用户在以后选择拒绝该应用程序进一步访问时可以撤消该令牌。

There is a standard way of doing this that has become very popular. 有一种标准的方法已经很流行。 It's called OAuth. 称为OAuth。 Specifically, version 2 of the OAuth standard. 具体来说,是OAuth标准的版本2。 There are a lot of great resources online for implementing OAuth so I would say that is outside the scope of this tutorial. 在线上有很多很棒的资源可用于实现OAuth,所以我认为这超出了本教程的范围。 If you are using Ruby, there are some great libraries that handle most of the work for you, like OmniAuth. 如果您使用的是Ruby,则有一些出色的库可以为您处理大部分工作,例如OmniAuth。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM