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

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

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可以获取策略的详细信息。

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

示例

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

结果

{
    "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"
    }
}

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

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

概述

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

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

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

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

UI设置

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

API

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

获取用户

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

获取客户端

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

获取资源列表

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

获取资源

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

获取权限和策略列表

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

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

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

获取特定权限的策略

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

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

用户映射到策略

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

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

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

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

通过UI评估

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

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

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

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

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

如果使策略过期

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

,将被拒绝 - 由于AND条件

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

通过API评估

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

在请求正文中

{
  "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
}

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

结果

{
    "results": [
        {
            "resource": {
                "name": "resource1",
                "_id": "3cb04615-ed9f-42a6-ab77-4254bf470891"
            },
            "scopes": [],
            "policies": [
                {
                    "policy": {
                        "id": "8597a6b3-ba5f-4849-9987-9a57b2f3db90",
                        "name": "permissions1",
                        "type": "resource",
                        "resources": [
                            "resource1"
                        ],
                        "scopes": [],
                        "logic": "POSITIVE",
                        "decisionStrategy": "UNANIMOUS",
                        "config": {}
                    },
                    "status": "DENY",
                    "associatedPolicies": [
                        {
                            "policy": {
                                "id": "6b2a4cce-f6ba-48eb-a8d4-ee3aad88c677",
                                "name": "policy-user",
                                "type": "user",
                                "resources": [],
                                "scopes": [],
                                "logic": "POSITIVE",
                                "decisionStrategy": "UNANIMOUS",
                                "config": {}
                            },
                            "status": "PERMIT",
                            "associatedPolicies": [],
                            "scopes": []
                        },
                        {
                            "policy": {
                                "id": "04dbe6e9-a1d3-449a-b001-42eab0eb51e6",
                                "name": "policy1",
                                "description": "short term license",
                                "type": "time",
                                "resources": [],
                                "scopes": [],
                                "logic": "POSITIVE",
                                "decisionStrategy": "UNANIMOUS",
                                "config": {}
                            },
                            "status": "DENY",
                            "associatedPolicies": [],
                            "scopes": []
                        }
                    ],
                    "scopes": []
                }
            ],


<details>
<summary>英文:</summary>

This API can get the detail of Policy.

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


Example

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


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"
}
}

This result get this UI information


Example : time based policy can get the time period.

### Overview
In left side, setup order by UI

In Right side, get information by API call
I think you interest blue circle #10.

I will demo user can access the resource by time base policy.
Example, the license control by time period.

[![Overview][1]][1]

### UI setup

[![UI setup][2]][2]

### API

I am using Keycloak v18.0.1 (if use v19/v20, just remove auth in API endpoint)

Get User

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



Get Client

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


Get Resource list

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


Get Resource

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


Get Permissions &amp; Policy list

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


Example by Postman - 
You can see the time period information - red box

[![enter image description here][3]][3]

Get policy of specific permission

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

[![enter image description here][4]][4]

User mapped into policy

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

The user list will return in body of response

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

[![enter image description here][5]][5]


### Evaluate by UI
[![enter image description here][6]][6]

Result - 
*note permission&#39;s Decision Strategy - 

#1 Unanimous (AND - policy1 and policy 2) then Permit

#2 Affirmative(OR - policy1 or policy 2) then Permit

[![enter image description here][7]][7]

If make expired policy

[![enter image description here][8]][8]

, will be deny - due to AND condition

[![enter image description here][9]][9]

### Evaluate by API

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


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
}

Evaluate by Postman
[![enter image description here][10]][10]

Result
```json
{
    &quot;results&quot;: [
        {
            &quot;resource&quot;: {
                &quot;name&quot;: &quot;resource1&quot;,
                &quot;_id&quot;: &quot;3cb04615-ed9f-42a6-ab77-4254bf470891&quot;
            },
            &quot;scopes&quot;: [],
            &quot;policies&quot;: [
                {
                    &quot;policy&quot;: {
                        &quot;id&quot;: &quot;8597a6b3-ba5f-4849-9987-9a57b2f3db90&quot;,
                        &quot;name&quot;: &quot;permissions1&quot;,
                        &quot;type&quot;: &quot;resource&quot;,
                        &quot;resources&quot;: [
                            &quot;resource1&quot;
                        ],
                        &quot;scopes&quot;: [],
                        &quot;logic&quot;: &quot;POSITIVE&quot;,
                        &quot;decisionStrategy&quot;: &quot;UNANIMOUS&quot;,
                        &quot;config&quot;: {}
                    },
                    &quot;status&quot;: &quot;DENY&quot;,
                    &quot;associatedPolicies&quot;: [
                        {
                            &quot;policy&quot;: {
                                &quot;id&quot;: &quot;6b2a4cce-f6ba-48eb-a8d4-ee3aad88c677&quot;,
                                &quot;name&quot;: &quot;policy-user&quot;,
                                &quot;type&quot;: &quot;user&quot;,
                                &quot;resources&quot;: [],
                                &quot;scopes&quot;: [],
                                &quot;logic&quot;: &quot;POSITIVE&quot;,
                                &quot;decisionStrategy&quot;: &quot;UNANIMOUS&quot;,
                                &quot;config&quot;: {}
                            },
                            &quot;status&quot;: &quot;PERMIT&quot;,
                            &quot;associatedPolicies&quot;: [],
                            &quot;scopes&quot;: []
                        },
                        {
                            &quot;policy&quot;: {
                                &quot;id&quot;: &quot;04dbe6e9-a1d3-449a-b001-42eab0eb51e6&quot;,
                                &quot;name&quot;: &quot;policy1&quot;,
                                &quot;description&quot;: &quot;short term license&quot;,
                                &quot;type&quot;: &quot;time&quot;,
                                &quot;resources&quot;: [],
                                &quot;scopes&quot;: [],
                                &quot;logic&quot;: &quot;POSITIVE&quot;,
                                &quot;decisionStrategy&quot;: &quot;UNANIMOUS&quot;,
                                &quot;config&quot;: {}
                            },
                            &quot;status&quot;: &quot;DENY&quot;,
                            &quot;associatedPolicies&quot;: [],
                            &quot;scopes&quot;: []
                        }
                    ],
                    &quot;scopes&quot;: []
                }
            ],
            &quot;status&quot;: &quot;DENY&quot;,
            &quot;allowedScopes&quot;: []
        }
    ],
    &quot;entitlements&quot;: false,
    &quot;status&quot;: &quot;DENY&quot;,
    &quot;rpt&quot;: {
        &quot;exp&quot;: 1677207180,
        &quot;iat&quot;: 1677206880,
        &quot;jti&quot;: &quot;c0f813e4-eff1-4c4a-9c65-4cc31fcc54a8&quot;,
        &quot;aud&quot;: &quot;my-test&quot;,
        &quot;sub&quot;: &quot;fd3d621a-565c-4dfb-b476-b605faadd798&quot;,
        &quot;typ&quot;: &quot;Bearer&quot;,
        &quot;azp&quot;: &quot;my-test&quot;,
        &quot;session_state&quot;: &quot;45f5a765-e95f-48cb-95ea-36e4a6ca22a0&quot;,
        &quot;acr&quot;: &quot;1&quot;,
        &quot;allowed-origins&quot;: [
            &quot;http://localhost:3000&quot;
        ],
        &quot;realm_access&quot;: {
            &quot;roles&quot;: [
                &quot;default-roles-test&quot;,
                &quot;offline_access&quot;,
                &quot;uma_authorization&quot;
            ]
        },
        &quot;resource_access&quot;: {
            &quot;account&quot;: {
                &quot;roles&quot;: [
                    &quot;manage-account&quot;,
                    &quot;manage-account-links&quot;,
                    &quot;view-profile&quot;
                ]
            }
        },
        &quot;authorization&quot;: {
            &quot;permissions&quot;: []
        },
        &quot;scope&quot;: &quot;email profile&quot;,
        &quot;sid&quot;: &quot;45f5a765-e95f-48cb-95ea-36e4a6ca22a0&quot;,
        &quot;email_verified&quot;: true,
        &quot;preferred_username&quot;: &quot;user1&quot;,
        &quot;email&quot;: &quot;user1@test.com&quot;
    }
}

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:

确定