如何获取Keycloak用户的密码过期日期

huangapple go评论101阅读模式
英文:

How to get the password expired date of a user in Keycloak

问题

我现在使用Keycloak 18.0.1。

当用户通过Keycloak登录我的系统时,我想要确切地知道密码何时会过期。
供您参考,我正在使用keycloak-angular和keycloak-js。

我已在https://www.keycloak.org/docs-api/15.0/rest-api/index.html#_passwordpolicytyperepresentation上搜索过,但它没有适合我需求的API。

英文:

I now using Keycloak 18.0.1.

When the user log in my system by Keycloak, I want to know exactly when the password will be expired.
For your information, I'm using keycloak-angular and keycloak-js

I have searched on https://www.keycloak.org/docs-api/15.0/rest-api/index.html#_passwordpolicytyperepresentation but it doesn't have an API that suit my need

答案1

得分: 0

这个API可以获取策略的详细信息。

  1. GET {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}/authz/resource-server/policy/{policy-uuid}

示例

  1. http://localhost:8080/auth/admin/realms/test/clients/246d7abb-da85-420b-92b4-65b1b3d287c1/authz/resource-server/policy/04dbe6e9-a1d3-449a-b001-42eab0eb51e6

结果

  1. {
  2. "id": "04dbe6e9-a1d3-449a-b001-42eab0eb51e6",
  3. "name": "policy1",
  4. "description": "short term license",
  5. "type": "time",
  6. "logic": "POSITIVE",
  7. "decisionStrategy": "UNANIMOUS",
  8. "config": {
  9. "noa": "2023-03-02 12:35:45",
  10. "nbf": "2022-01-02 01:02:06"
  11. }
  12. }

这个结果可以获取UI信息。

示例:基于时间的策略可以获取时间段。

概述

在左侧,通过UI设置顺序。

在右侧,通过API调用获取信息。
我认为您对蓝色圆圈 #10 感兴趣。

我将演示用户可以按时间基础策略访问资源。
例如,按时间段控制许可证。

如何获取Keycloak用户的密码过期日期

UI设置

如何获取Keycloak用户的密码过期日期

API

我正在使用Keycloak v18.0.1(如果使用v19/v20,只需在API端点中删除auth)

获取用户

  1. GET {keycloak_url}/auth/admin/realms/{realm}/users/{user-uuid}

获取客户端

  1. GET {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}

获取资源列表

  1. GET {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}/authz/resource-server/resource/

获取资源

  1. GET {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}/authz/resource-server/resource/{resource-uuid}

获取权限和策略列表

  1. GET {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}/authz/resource-server/policy

Postman示例 -
您可以查看时间段信息 - 红框

如何获取Keycloak用户的密码过期日期

获取特定权限的策略

  1. GET {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}/authz/resource-server/policy/{permission-uuid}/associatedPolicies

如何获取Keycloak用户的密码过期日期

用户映射到策略

  1. GET {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}/authz/resource-server/policy/{policy-uuid}

用户列表将在响应正文中返回

  1. {
  2. "id": {policy-uuid},
  3. "name": {policy-name},
  4. "config": {
  5. "users": "[array of {user-uuid}]"
  6. }
  7. }

如何获取Keycloak用户的密码过期日期

通过UI评估

如何获取Keycloak用户的密码过期日期

结果 -
*注意权限的决策策略 -

#1 Unanimous (AND - policy1 和 policy 2) 然后许可

#2 Affirmative (OR - policy1 或 policy 2) 然后许可

如何获取Keycloak用户的密码过期日期

如果使策略过期

如何获取Keycloak用户的密码过期日期

,将被拒绝 - 由于AND条件

如何获取Keycloak用户的密码过期日期

通过API评估

  1. POST {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}/authz/resource-server/policy/evaluate

在请求正文中

  1. {
  2. "resources": [
  3. {
  4. "name": <resource-name>,
  5. "owner": {
  6. "id": <client-uuid>,
  7. "name": <client-name>
  8. },
  9. "ownerManagedAccess": false,
  10. "_id": <resource-uuid>,
  11. "uris": [],
  12. "scopes": []
  13. }
  14. ],
  15. "context": { "attributes": {} },
  16. "roleIds": [],
  17. "clientId": <client-uuid>,
  18. "userId": <user-uuid>,
  19. "entitlements": false
  20. }

通过Postman评估
如何获取Keycloak用户的密码过期日期

结果

  1. {
  2. "results": [
  3. {
  4. "resource": {
  5. "name": "resource1",
  6. "_id": "3cb04615-ed9f-42a6-ab77-4254bf470891"
  7. },
  8. "scopes": [],
  9. "policies": [
  10. {
  11. "policy": {
  12. "id": "8597a6b3-ba5f-4849-9987-9a57b2f3db90",
  13. "name": "permissions1",
  14. "type": "resource",
  15. "resources": [
  16. "resource1"
  17. ],
  18. "scopes": [],
  19. "logic": "POSITIVE",
  20. "decisionStrategy": "UNANIMOUS",
  21. "config": {}
  22. },
  23. "status": "DENY",
  24. "associatedPolicies": [
  25. {
  26. "policy": {
  27. "id": "6b2a4cce-f6ba-48eb-a8d4-ee3aad88c677",
  28. "name": "policy-user",
  29. "type": "user",
  30. "resources": [],
  31. "scopes": [],
  32. "logic": "POSITIVE",
  33. "decisionStrategy": "UNANIMOUS",
  34. "config": {}
  35. },
  36. "status": "PERMIT",
  37. "associatedPolicies": [],
  38. "scopes": []
  39. },
  40. {
  41. "policy": {
  42. "id": "04dbe6e9-a1d3-449a-b001-42eab0eb51e6",
  43. "name": "policy1",
  44. "description": "short term license",
  45. "type": "time",
  46. "resources": [],
  47. "scopes": [],
  48. "logic": "POSITIVE",
  49. "decisionStrategy": "UNANIMOUS",
  50. "config": {}
  51. },
  52. "status": "DENY",
  53. "associatedPolicies": [],
  54. "scopes": []
  55. }
  56. ],
  57. "scopes": []
  58. }
  59. ],
  60. <details>
  61. <summary>英文:</summary>
  62. This API can get the detail of Policy.

GET {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}/authz/resource-server/policy/{policy-uuid}

  1. Example

http://localhost:8080/auth/admin/realms/test/clients/246d7abb-da85-420b-92b4-65b1b3d287c1/authz/resource-server/policy/04dbe6e9-a1d3-449a-b001-42eab0eb51e6

  1. Result

{
"id": "04dbe6e9-a1d3-449a-b001-42eab0eb51e6",
"name": "policy1",
"description": "short term license",
"type": "time",
"logic": "POSITIVE",
"decisionStrategy": "UNANIMOUS",
"config": {
"noa": "2023-03-02 12:35:45",
"nbf": "2022-01-02 01:02:06"
}
}

  1. This result get this UI information
  2. Example : time based policy can get the time period.
  3. ### Overview
  4. In left side, setup order by UI
  5. In Right side, get information by API call
  6. I think you interest blue circle #10.
  7. I will demo user can access the resource by time base policy.
  8. Example, the license control by time period.
  9. [![Overview][1]][1]
  10. ### UI setup
  11. [![UI setup][2]][2]
  12. ### API
  13. I am using Keycloak v18.0.1 (if use v19/v20, just remove auth in API endpoint)
  14. Get User

GET {keycloak_url}/auth/admin/realms/{realm}/users/{user-uuid}

  1. Get Client

GET {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}

  1. Get Resource list

GET {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}/authz/resource-server/resource/

  1. Get Resource

GET {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}/authz/resource-server/resource/{resource-uuid}

  1. Get Permissions &amp; Policy list

GET {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}/authz/resource-server/policy

  1. Example by Postman -
  2. You can see the time period information - red box
  3. [![enter image description here][3]][3]
  4. Get policy of specific permission

GET {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}/authz/resource-server/policy/{permission-uuid}/associatedPolicies

  1. [![enter image description here][4]][4]
  2. User mapped into policy

GET {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}/authz/resource-server/policy/{policy-uuid}

  1. The user list will return in body of response

{
"id": {policy-uuid},
"name": {policy-name},
"config": {
"users": "[array of {user-uuid}]"
}
}

  1. [![enter image description here][5]][5]
  2. ### Evaluate by UI
  3. [![enter image description here][6]][6]
  4. Result -
  5. *note permission&#39;s Decision Strategy -
  6. #1 Unanimous (AND - policy1 and policy 2) then Permit
  7. #2 Affirmative(OR - policy1 or policy 2) then Permit
  8. [![enter image description here][7]][7]
  9. If make expired policy
  10. [![enter image description here][8]][8]
  11. , will be deny - due to AND condition
  12. [![enter image description here][9]][9]
  13. ### Evaluate by API

POST {keycloak_url}/auth/admin/realms/{realm}/clients/{client-uuid}/authz/resource-server/policy/evaluate

  1. In body

{
"resources": [
{
"name": <resource-name>,
"owner": {
"id": <client-uuid>,
"name": <client-name>
},
"ownerManagedAccess": false,
"_id": <resource-uuid>,
"uris": [],
"scopes": []
}
],
"context": { "attributes": {} },
"roleIds": [],
"clientId": <client-uuid>,
"userId": <user-uuid>,
"entitlements": false
}

  1. Evaluate by Postman
  2. [![enter image description here][10]][10]
  3. Result
  4. ```json
  5. {
  6. &quot;results&quot;: [
  7. {
  8. &quot;resource&quot;: {
  9. &quot;name&quot;: &quot;resource1&quot;,
  10. &quot;_id&quot;: &quot;3cb04615-ed9f-42a6-ab77-4254bf470891&quot;
  11. },
  12. &quot;scopes&quot;: [],
  13. &quot;policies&quot;: [
  14. {
  15. &quot;policy&quot;: {
  16. &quot;id&quot;: &quot;8597a6b3-ba5f-4849-9987-9a57b2f3db90&quot;,
  17. &quot;name&quot;: &quot;permissions1&quot;,
  18. &quot;type&quot;: &quot;resource&quot;,
  19. &quot;resources&quot;: [
  20. &quot;resource1&quot;
  21. ],
  22. &quot;scopes&quot;: [],
  23. &quot;logic&quot;: &quot;POSITIVE&quot;,
  24. &quot;decisionStrategy&quot;: &quot;UNANIMOUS&quot;,
  25. &quot;config&quot;: {}
  26. },
  27. &quot;status&quot;: &quot;DENY&quot;,
  28. &quot;associatedPolicies&quot;: [
  29. {
  30. &quot;policy&quot;: {
  31. &quot;id&quot;: &quot;6b2a4cce-f6ba-48eb-a8d4-ee3aad88c677&quot;,
  32. &quot;name&quot;: &quot;policy-user&quot;,
  33. &quot;type&quot;: &quot;user&quot;,
  34. &quot;resources&quot;: [],
  35. &quot;scopes&quot;: [],
  36. &quot;logic&quot;: &quot;POSITIVE&quot;,
  37. &quot;decisionStrategy&quot;: &quot;UNANIMOUS&quot;,
  38. &quot;config&quot;: {}
  39. },
  40. &quot;status&quot;: &quot;PERMIT&quot;,
  41. &quot;associatedPolicies&quot;: [],
  42. &quot;scopes&quot;: []
  43. },
  44. {
  45. &quot;policy&quot;: {
  46. &quot;id&quot;: &quot;04dbe6e9-a1d3-449a-b001-42eab0eb51e6&quot;,
  47. &quot;name&quot;: &quot;policy1&quot;,
  48. &quot;description&quot;: &quot;short term license&quot;,
  49. &quot;type&quot;: &quot;time&quot;,
  50. &quot;resources&quot;: [],
  51. &quot;scopes&quot;: [],
  52. &quot;logic&quot;: &quot;POSITIVE&quot;,
  53. &quot;decisionStrategy&quot;: &quot;UNANIMOUS&quot;,
  54. &quot;config&quot;: {}
  55. },
  56. &quot;status&quot;: &quot;DENY&quot;,
  57. &quot;associatedPolicies&quot;: [],
  58. &quot;scopes&quot;: []
  59. }
  60. ],
  61. &quot;scopes&quot;: []
  62. }
  63. ],
  64. &quot;status&quot;: &quot;DENY&quot;,
  65. &quot;allowedScopes&quot;: []
  66. }
  67. ],
  68. &quot;entitlements&quot;: false,
  69. &quot;status&quot;: &quot;DENY&quot;,
  70. &quot;rpt&quot;: {
  71. &quot;exp&quot;: 1677207180,
  72. &quot;iat&quot;: 1677206880,
  73. &quot;jti&quot;: &quot;c0f813e4-eff1-4c4a-9c65-4cc31fcc54a8&quot;,
  74. &quot;aud&quot;: &quot;my-test&quot;,
  75. &quot;sub&quot;: &quot;fd3d621a-565c-4dfb-b476-b605faadd798&quot;,
  76. &quot;typ&quot;: &quot;Bearer&quot;,
  77. &quot;azp&quot;: &quot;my-test&quot;,
  78. &quot;session_state&quot;: &quot;45f5a765-e95f-48cb-95ea-36e4a6ca22a0&quot;,
  79. &quot;acr&quot;: &quot;1&quot;,
  80. &quot;allowed-origins&quot;: [
  81. &quot;http://localhost:3000&quot;
  82. ],
  83. &quot;realm_access&quot;: {
  84. &quot;roles&quot;: [
  85. &quot;default-roles-test&quot;,
  86. &quot;offline_access&quot;,
  87. &quot;uma_authorization&quot;
  88. ]
  89. },
  90. &quot;resource_access&quot;: {
  91. &quot;account&quot;: {
  92. &quot;roles&quot;: [
  93. &quot;manage-account&quot;,
  94. &quot;manage-account-links&quot;,
  95. &quot;view-profile&quot;
  96. ]
  97. }
  98. },
  99. &quot;authorization&quot;: {
  100. &quot;permissions&quot;: []
  101. },
  102. &quot;scope&quot;: &quot;email profile&quot;,
  103. &quot;sid&quot;: &quot;45f5a765-e95f-48cb-95ea-36e4a6ca22a0&quot;,
  104. &quot;email_verified&quot;: true,
  105. &quot;preferred_username&quot;: &quot;user1&quot;,
  106. &quot;email&quot;: &quot;user1@test.com&quot;
  107. }
  108. }

References

how to get all keycloak users who can access to a specific resource

logic when evaluating permissions for a shared resource in keycloak

答案2

得分: 0

没有直接提供该信息的 API 端点,但可以通过使用密码策略和密码创建日期来计算该信息。

参考链接:

https://medium.com/@lejdiprifti/expiration-date-of-users-password-in-keycloak-389566d5d78c

英文:

There is not an API endpoint that gives that information directly, but it can be calculated by using the password policy and the password creation date.

Reference

https://medium.com/@lejdiprifti/expiration-date-of-users-password-in-keycloak-389566d5d78c

huangapple
  • 本文由 发表于 2023年2月16日 14:54:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定