在 API 中处理部分授权的最佳实践是什么

What is best practice for handling partial authorization in APIs

在 API 年代阅读了一些关于最佳实践的帖子后,我仍然有一个悬而未决的问题:

API 是否应该回应:

  1. A(已编辑,更正,不是 401:)403 Forbidden 错误代码,拒绝整个请求

  1. ID 为 [1,2,3] 的对象的数据并拒绝对 [4,5]
  2. 的读取访问

After reading a few posts about best practices in APIs, I still have an unanswered question :

  • Assuming I have a user requesting to read objects with IDs [1,2,3,4,5]
  • Assuming this user is authorized to read [1,2,3] but not [4,5]

Should the API respond with :

  1. A 401 Unauthorized error code, denying the whole request

没有。 HTTP 401 是关于无法进行身份验证(是的,尽管名称如此)。你会 return (如果有的话)HTTP 403。但在这种情况下,假设你确实对端点进行了身份验证并且你确实被允许查看某些元素,我不会 return 那个。

or

  1. The datas of objects with IDs [1,2,3] and deny read access to [4,5]

是的。我会 return 用户可以看到什么。更具体地说,我认为你有一个 API 沿着

/api/items/{itemid}

/api/items 的 HTTP GET 将 return 用户可以查看的整个项目列表,即项目 1、2 和 3。

/api/items/1 的 HTTP GET 将 return 该项目。对 /api/items/4 的 HTTP GET 可能是 return HTTP 403(您无权查看该项目)或 HTTP 404(在您甚至不想透露其存在的情况下未找到该项目)文件)。

如何确定授权?它只是一个ACL吗?红十字会?算术?如果是后者,请将 XACML 视为一种编写授权策略以控制您的 APIs 的方法。