我应该在REST api调用中的哪里放置API密钥? [英] Where should I place API keys in REST api calls?

查看:218
本文介绍了我应该在REST api调用中的哪里放置API密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

共有三个选项:

  1. 标题
  2. URL
  3. 身体

我没有对authentication做过多的工作,但是正在构建需要它的API.我之前所做的是parse请求正文中的parse api key (e.g sha-256).

I haven't done too much work with authentication but am building an API that will need it. What I have done previously is parse the api key (e.g sha-256) in the body of a post request.

但是我刚刚意识到您不能通过GET 请求执行此操作,对吗?因此,我需要找到对每个API请求进行身份验证的最安全方法.

But I have just realised that you can't do this with GET requests, right? So I need to find the most secure way of authenticating each API request.

我当时在想,在每个请求中传递users authenticationkey(存储在用户数据库表中)和oAuth密钥是否更安全?这样,任何黑客都需要两个密钥才能进入系统?

I was thinking, would it be more secure to pass the users authenticationkey (which is store in the users DB table) aswell as an oAuth key with each request? This way any hacker would need two keys to get into the system?

有人对实现此目标的最佳方法有什么见识吗?在线上有什么不错的tutorials可以告诉我如何解析标头中的值吗?

Does anyone have any insight to what the best way to go about this is? Are there any good tutorials online that can show me how to parse values in a header?

要验证每个请求,我是否可以让router检查每个请求中的api key?

To authenticate every request, would I have the router check each request for an api key?

这样的排序:

app.all('/api/v1/*', [require('./middlewares/validateRequestAPIKEY')]); 

推荐答案

我为移动应用程序设计了一些API,以下是我为客户端实现的内容:

I had design few API for the mobile applications and following is what I have implemented for my clients:

The Keys will be passed in header

在服务器端,我将在每个API调用中检查密钥以进行验证,在某些API中,您可能需要跳过验证(登录是一个示例)

On server side I will check for keys on each API call for validation, in some API you might need to skip the validation (login is one example)

这是我的实现

1)令牌/密钥不需要过期:

1) Token/keys expiration is not required:

  • 用户注册到系统
  • 服务器将为用户存储到系统中生成唯一的密钥(您可以将公共密钥提供给客户端进行加密,并将私钥存储在服务器中进行解密)
  • 客户端将在随后的每个请求中发送此密钥(您可以使用其他算法对密钥进行加密,可以在服务器上对其进行解密以提高安全性)

2)当令牌需要到期时:

2) When token requires expiration:

  • 创建一个API,该API将在有限的时间内生成密钥,客户端将调用此API来获取密钥,该API将检查客户端是否为有效用户,并在特定时间段内返回密钥.
  • 在下一个API请求中,客户端将在标头中发送密钥,服务器将检查令牌/密钥是否有效/已过期,如果有效则执行该操作,否则将响应发送给客户端以再次生成密钥.
  • 您可以再次使用其他算法对密钥进行加密,以提高安全性.

希望有帮助

这篇关于我应该在REST api调用中的哪里放置API密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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