Keycloak-基于资源的角色和功能范围基础认证 [英] Keycloak - Resource based Role & scope base auth

查看:101
本文介绍了Keycloak-基于资源的角色和功能范围基础认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我想限制用户进入密钥斗篷

我有用户

用户可以访问多个帐户在多个帐户中,可以使用管理员或代理(阅读者)

 用户||| -------帐户1|||| -------管理员| -------帐户2|||| -------代理 

我们如何在Keycloak中使用策略,权限和角色对此进行映射?

任何参考文档的任何示例都很有帮助

也基于:

  • 关于 law ,我们只将他与 Reader bank-reader 关联.

将客户范围与客户相关联

通过以下方式创建客户范围:

  • 点击左侧>上的 Client Scopes 链接.点击 Create >在 Name 字段中输入 custom-client-scope ,然后点击Save.看起来应该像这样

  • 点击左侧>上的 Clients .选择 my-demo-client >点击顶部>上的 Client Scopes 标签.为了方便起见,我们将其移至 Assigned Default Client Scopes .

检查访问令牌

我们可以通过Keycloak轻松为我们的设置生成访问令牌,以查看其外观.为此:

  • 单击客户范围下的评估选项卡.

  • 选择 paul 作为用户

  • 点击蓝色的 Evaluate 按钮

  • 点击生成的访问令牌.检查令牌时,请查找:

    • resource_access 以查看与 paul
    • 相关联的客户端级别角色
    • realm_access 查看保罗的领域级别角色
    • scope 来查看我们创建的名为 custom-client-scope Client Scope
  • 如果为 law 生成令牌,则与 paul 相比,您会看到较少的角色.

政策评估后获得范围

继续我们的设置:

  • 我已经创建了一个 account/{id} 资源,该资源具有两个称为 account:read account:modify的 Authorization Scopes 像这样

  • 此外,我还创建了两个基于角色的策略,分别称为 Only Reader Role Policy Only Admin Role Policy ,其中前者需要 Reader 领域角色,而后者需要 Admin 领域角色.这是一个示例供参考.

  • 请注意,如果愿意,您可以在客户端级别进一步增强该策略,但我选择不这样做.

  • 此外,我已经创建了两个基于范围的权限,称为 Read Account Scope Permission Modify Account Scope权限.

  • 如果用户是 Admin管理员,则 Read Account Scope权限将授予 account:read Authorization Scope 一个阅读器.这里要注意的一件事是,必须将决策策略设置为 Affirmative ,以实现此行为.

    另一方面,
  • 修改帐户权限向具有 Admin 的用户授予 account:modify Authorization Scope 角色.

  • 现在,如果您选择针对 Account Resource的用户 paul (请记住他同时是 Admin Reader )进行评估,他将被同时授予 account:read account:modify Authorization Scopes .让我们看看这是否正确.这是我们的 Evaluate 屏幕,请注意,我没有将任何角色与 paul 相关联,因为这已经通过 Users >完成.角色映射标签

  • 这是预期的评估结果

  • 这是法律的评估结果.由于他不是 Admin 管理员,因此将拒绝他 account:modify 范围,但将为他授予 account:read 范围.

  • 最后,我们可以通过单击 Show Authorization Data (显示授权数据)进一步确认这一点,其中显示了 law
  • 的访问令牌内的权限

希望这可以帮助您查看难题的每个部分在您的体系结构中的位置.干杯!

i have a scenario where i want to restrict the user in keycloak

i have user

user can have access to multiple accounts in multiple accounts, use can be Admin or agent (reader)

user
|
|
|-------account-1
|            |
|            |-------admin
|-------account-2
|            |
|            |-------agent

How can we map this in Keycloak with Policy, Permission, and role?

any reference document any example really helpful

also based from : Resources, scopes, permissions and policies in keycloak

From the answer of Andy, i have created one resource Account and role admin & agent.

created same policies as in example.

i am looking forward to add scopes (auth scope) and roles to JWT token how to map that part so that API gateway or service can verify further.

解决方案

@changa, I've rewritten my answer based on our discussion. Hope this helps!

Let me first clarify some key areas before I answer. My main focus on the answer that you've linked was really on how to play around the Evaluate tool and I didn't really dive too deeply into some of the concepts - so let's do that :)

In Keycloak, you'll encounter Client and Authorization Scopes. For a formal definition of these terms please check out the Core Concepts and Terms in the Server Administration Guide, but simply put:

Client Scopes are scopes which are granted to clients when they are requested via the scope parameter (once the resource owner permits it). Note that there's also the concept of Default Client Scope but I've chosen to keep things simple. Furthermore, you can leverage protocol and role scope mappers to tailor what claims and assertions are present in the access token.

Authorization Scopes on the other hand are granted to clients after successful evaluation of the policies against a protected resource. These scopes are not granted to clients based on user consent.

The key differences between the two is really when and how a client obtains these scopes. To help you visualize all of this, here's a scenario:

  1. A renowned martial artist called Bob authenticates via Keycloak

  2. Bob get presented with a consent screen where he is asked to share his name, his fighting style and his age.

  3. Bob chooses to give access to his name and fighting style but he declines to share his age.

  4. When we inspect the token now, we would see the following (completely made up) entries for the scope attribute of the access token: name and fighting_style.

  5. Additionally, let's assume that we've set up a couple of protocol mappers (e.g. User Attribute Mapper Type - there are a ton) to display the values for full name and fighting style via the following token claims: fighter_name and martial_arts when the two Client Scopes above are present in the access token. In addition to two previously mentioned scopes, we would also see something like fighter_name: Robert Richards and martial_arts: Freestyle Karate when examining the access token.

    • Side Note: Given the length of this answer, I've decided to skip this topic but please check out this awesome video at around the 7 minute mark along with the associated GitHub Project for more information. The README is pretty good.
  6. Additionally, let's assume that Bob is mapped to a realm role called Contestant and a client role of Fighter and we did place any restrictions in Keycloak when it comes to sharing this info. So in addition to all the things mentioned above, we would see that information inside the token as well.

    • Needless to say, this is an oversimplification on my part as I'm simply setting up the stage for demo. purposes and there's much more information inside the access token.
  7. Bob doesn't like how the tournament bracket is laid out as he's eager to fight the world champ as soon as possible, so he attempts to change his placement by sending a request against tournament/tekken6/bracket/{id}. This resource is associated with the scope bracket:modify. Additionally, there is a permission which associates the resource in question with a role based policy named Referee Role Required. If Bob were a Referee then he would be granted the bracket:modify scope but since he isn't, then he is denied that scope.

    • I've barely touched the surface when it comes to the inner workings of the Authorization process in Keycloak. For more information, check out this practical guide. You can do some pretty cool stuff with UMA.

Ok, so that's enough theory. Let's set up our environment to demo all of this. I'm using the following:

  • A realm called demo
  • A client called my-demo-client
  • A client scope called client_roles
  • 2 users - paul and law
  • Two realms level roles - Admin and Reader
  • Two client level roles - demo-admin and demo-reader

Please note that I will using Keycloak 12.0.4 and I will skip almost all the basic setup instructions. I will only share the relevant bits. If you're not sure how to set this all up, please check out the Getting Started Guide or this answer. The answer contains steps for version 8 but the differences are very minor as far as I could tell.

Associating Users And Roles

In order to associate paul with the Admin, Reader, bank-admin and bank-reader roles, please do the following:

  • Click on Users > View all users > Click on the ID value for paul > Click on Role Mappings > Under Realm Roles move Admin and Reader under Assigned Roles > Select my-demo-client under the Client Roles select box and move demo-admin and demo-reader under Assigned Roles like so

  • As for law we'll just associate him with Reader and bank-reader.

Associating a client scope with a client

Create a Client Scope by:

  • Clicking on the Client Scopes link on the left > Click on Create > Enter custom-client-scope for the Name field and Hit Save. It should look like this

  • Click on Clients on the left > Select the my-demo-client > Click on the Client Scopes tab at the top > and let's just move it to Assigned Default Client Scopes for convenience.

Inspecting the Access Token

We can easily generate an access token for our setup via Keycloak to see what it looks like. In order to do so:

  • Click on Evaluate tab under Client Scopes.

  • Select paul as the user

  • Click on the blue Evaluate button

  • Click on Generated Access Token. While inspecting the token, look for:

    • resource_access to see client level roles associated with paul
    • realm_access to see paul's realm level roles
    • scope to see the Client Scope that we created called custom-client-scope
  • If you generate a token for law, you would see less roles when compared to paul.

Obtaining a Scope After Policy Evaluation

Continuing with our setup:

  • I've created an account/{id} resource with two Authorization Scopes called account:read and account:modify like so

  • Additionally, I've created two role based policies called Only Reader Role Policy and Only Admin Role Policy where the former requires the Reader realm role while the latter requires the Admin realm role. Here's an example for reference.

  • Note that you can further enhance that policy at the client level if you wish but to keep things simple, I chose not to do so.

  • Furthermore, I've created two scoped based permissions called Read Account Scope Permission and Modify Account Scope Permission.

  • The Read Account Scope Permission will grant the account:read Authorization Scope if the user is either an Admin or a Reader. One key thing to notice here is the the Decision Strategy has to be set to Affirmative in order to achieve this behavior.

  • Modify Account Permission on the other hand grants the account:modify Authorization Scope to users with the Admin role.

  • Now, if you choose the evaluate the user paul (remember he is both Admin and Reader) against the Account Resource, he will be granted both the account:read and account:modify Authorization Scopes. Let's see if this true. Here's our Evaluate screen and notice that I did not associate any roles with paul since this was already done via the Users > Role Mappings tab

  • And here are the results of that evaluation as predicted

  • Here is the evaluation result for law. Since he's not an Admin he'll be denied the account:modify scope but he'll be granted the account:read scope.

  • And finally, we can further confirm this by click on Show Authorization Data which shows the permissions inside the access token for law

Hopefully this helps you see where each piece of the puzzle fits in your architecture. Cheers!

这篇关于Keycloak-基于资源的角色和功能范围基础认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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