我应该如何使用 Keycloak 为 "role-less" (ACL, ABAC) REST API 设置授权?

How should I set up authorization for a "role-less" (ACL, ABAC) REST API with Keycloak?

我的推理基于这样一个前提,即在涉及安全性时,不应重新发明轮子,最好研究哪种可用解决方案最适合。

假设您有一个应用程序,允许用户创建 事物 并允许他们的朋友以某种方式更改它们。 我想在这种情况下使用 role-based 方法不会很好地工作,并且每件事的访问控制列表之类的东西更接近理想。

按照我在第一段中提出的前提,我研究了 Keycloak 的文档以寻找类似于 ACL 的东西,因为我还需要一种身份验证机制。我无法想出一个没有感觉的解决方案 "hacky",至少可以说...

我想问的是:

与其将我的想法写成一个详尽的清单,不如问:

谢谢!

编辑 (14/05/20)

为了进一步讨论这个话题,我将提供一个更具体(虽然简单)的例子。我还将分享 Keycloak 的主要贡献者的一些建议。

假设我们正在开发一个简单的 Medium 克隆,允许用户共同编辑文章。 Lance 是一个已经注册并经过身份验证的用户,他想开始写一篇关于自行车的文章,因此他导航到我们 SPA 中的 "editing screen"。 Lance 然后给它一个标题,写几个字并点击一个 "Create" 按钮,向资源服务器(已经受 Keycloak 保护)发送一个 POST 请求。资源服务器(服务)然后将: 1. 将文章持久化到数据库; 2. 通过 Keycloak 的保护 API 创建一个资源,其所有者属性设置为 Lances 的用户 ID 和一个空的 "editors" 属性。

后来 Lance 决定也听取他的朋友 Chris 的意见会很好,因此将他添加到编辑列表中。 这也可以通过 Keycloak 的保护 API 来完成。

此 set-up 将允许我们设置 JavaScript-Based 策略来保护进一步的编辑。

你的推理是正确的,因为它实际上符合 "resource" 的定义。作为你自己的应用程序资源服务器,Keycloak提供访问与否的授权,取决于你的考虑。

我想你的具体案例已包含在 User Managed Access:

As an example, consider a user Alice (resource owner) using an Internet Banking Service (resource server) to manage his Bank Account (resource). One day, Alice decides to open her bank account to Bob (requesting party), a accounting professional. However, Bob should only have access to view (scope) Alice’s account.

As a resource server, the Internet Banking Service must be able to protect Alice’s Bank Account. For that, it relies on Keycloak Resource Registration Endpoint to create a resource in the server representing Alice’s Bank Account.

At this moment, if Bob tries to access Alice’s Bank Account, access will be denied. The Internet Banking Service defines a few default policies for banking accounts. One of them is that only the owner, in this case Alice, is allowed to access her bank account.

However, Internet Banking Service in respect to Alice’s privacy also allows her to change specific policies for the banking account. One of these policies that she can change is to define which people are allowed to view her bank account. For that, Internet Banking Service relies on Keycloak to provide to Alice a space where she can select individuals and the operations (or data) they are allowed to access. At any time, Alice can revoke access or grant additional permissions to Bob.

对于您的情况,资源将是 thing 并且您的应用程序需要在资源注册端点中注册它。然后,您可以使用策略来定义 访问资源以及 如何.

根据我在问题中提到的贡献者 (post-edit) 告诉我的,这个推理是正确的,这是可以完成的一种方式。另外,正如@Xtreme Biker 所说,我们还可以利用 Keycloak 提供的 UMA 功能让用户管理他们的资源。

贡献者还表示,应该权衡此解决方案的开销和重复,可能会添加并考虑使细粒度授权更接近 application/resource server/service。