如何使用Keycloak的RESTful API获取用户的自定义属性?

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

How to get custom attributes for a user in Keycloak using the RESTful API?

问题

以下是已翻译的代码部分:

我认为这应该很简单,但我在文档中找不到它。

以下的curl命令:

    curl \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer $ACCESS_TOKEN" \
        'https://$URL/auth/realms/$REALM/protocol/openid-connect/userinfo';

的结果是:

    {
      "sub": "8182...415",
      "email_verified": true,
      "name": "n.a. n.a.",
      "groups": [],
      "preferred_username": "foo@example.com",
      "given_name": "n.a.",
      "family_name": "n.a.",
      "email": "foo@example.com"
    }

如何获取用户的自定义属性?
英文:

I would assume this to be straight forward but I can't find it in the docs.

The following curl command:

curl \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $ACCESS_TOKEN" \
    'https://$URL/auth/realms/$REALM/protocol/openid-connect/userinfo'

results in:

{
  "sub": "8182...415",
  "email_verified": true,
  "name": "n.a. n.a.",
  "groups": [],
  "preferred_username": "foo@example.com",
  "given_name": "n.a.",
  "family_name": "n.a.",
  "email": "foo@example.com"
}

How do I get the custom attributes for a user?

答案1

得分: 1

使用Keycloak Admin Rest API的get users端点,您可以获取用户属性:

GET /{realm}/users

使用查询参数,exact=trueusername

逐步说明:

您可以使用Keycloak Admin REST API获取此信息;要调用该API,您需要使用具有适当权限的用户的访问令牌。暂时,我将使用master领域的admin用户:

curl https://${KEYCLOAK_HOST}/auth/realms/master/protocol/openid-connect/token \
    -d "client_id=admin-cli" \
    -d "username=$ADMIN_NAME" \
    -d "password=$ADMIN_PASSWORD" \
    -d "grant_type=password"

您将获得管理员令牌的JSON响应。从该响应中提取属性access_token的值。让我们将其保存在变量$ACCESS_TOKEN中以备后用。

要从您的领域$REALM_NAME获取用户属性:

curl -X GET https://${KEYCLOAK_HOST}/auth/admin/realms/${REALM_NAME}/users/?username=${USERNAME}&exact=true \
     -H "Content-Type: application/json" \
     -H "Authorization: bearer $ACCESS_TOKEN"

从响应中提取用户attributes,例如如下所示:

jq -r .[].attributes

要通过userinfo端点检索自定义用户属性,您需要为用于认证用户的客户端创建协议映射器。

该映射器也可以使用Keycloak Admin Rest API创建。有关如何为用户属性创建协议映射器的更详细回答(包括针对旧和新的Keycloak API),请查看此SO答案

或者,您可以按照以下方式通过Keycloak Admin UI执行,进入Keycloak:

  • 选择您的realm
  • 转到clients
  • 选择适用于您用例的适当client

(对于旧版Keycloak UI)

  • 转到Mappers
  • 单击Create
  • 选择Mapper TypeUser Attribute
  • 用您的自定义用户属性填写User Attribute字段
  • 设置要添加到userinfo端点
  • 根据需要填写其余字段
  • 单击Save

(对于新版Keycloak UI)

  • 转到选项卡Client Scopes
  • 单击作用域<您的客户端的客户端ID> -dedicated(例如,我的示例中的test-dedicated)

如何使用Keycloak的RESTful API获取用户的自定义属性?

  • 单击Configure a new mapper(或者,如果您以前已为此客户端创建过映射器,则单击Add Mapper> By configuration

如何使用Keycloak的RESTful API获取用户的自定义属性?

  • 选择User Attribute
  • 用您的自定义用户属性填写User Attribute字段
  • 设置要添加到userinfo端点
  • 根据需要填写其余字段
  • 单击Save

这足以使your custom user attribute能够从userinfo端点检索。

英文:

You can get the user attributes with the get users endpoint from Admin Rest API:

GET /{realm}/users

with the query parameters, exact=true and username.

Step-by-Step:

You can get that information using the Keycloak Admin REST API; to call that API, you need an access token from a user with the proper permissions. For now, I will be using the admin user from the master realm:

curl https://${KEYCLOAK_HOST}/auth/realms/master/protocol/openid-connect/token \
    -d &quot;client_id=admin-cli&quot; \
    -d &quot;username=$ADMIN_NAME&quot; \
    -d &quot;password=$ADMIN_PASSWORD&quot; \
    -d &quot;grant_type=password&quot;

You will get a JSON response with the admin's token. Extract the value of property access_token from that response. Let us save it in the variable $ACCESS_TOKEN for later reference.

To get the user attributes from your realm $REALM_NAME:

curl -X GET https://${KEYCLOAK_HOST}/auth/admin/realms/${REALM_NAME}/users/?username=${USERNAME}&amp;exact=true \
     -H &quot;Content-Type: application/json&quot; \
     -H &quot;Authorization: bearer $ACCESS_TOKEN&quot;

From the response extract the user attributes for example as follows:

jq -r .[].attributes

To retrieve custom user attributes via the userinfo endpoint you need to create a protocol Mapper for the client used to authenticate the user.

That mapper can also be created with the Keycloak Admin rest API. For a more detailed answer on how to create Protocol Mappers for user-attributes (including for the old and new Keycloak APIs) please have a look at the this SO answer.

Or you can do it via Keycloak Admin UI as follows, in the Keycloak go to:

  • Select your realm
  • Go to clients
  • Select the appropriate client for your use-case

(For the OLD Keycloak UI)

  • Go to Mappers
  • Click Create
  • Select Mapper Type as User Attribute
  • Fill up the field User Attribute with your custom user attribute
  • Set to be added to the userinfo endpoint
  • Fill up the remaining fields, accordingly
  • Click on Save

(For the NEW Keycloak UI)

  • Go to the tab Client Scopes
  • Click on the scope <the client ID of your client>-dedicated (e.g., test-dedicated in my example)

如何使用Keycloak的RESTful API获取用户的自定义属性?

  • Click on Configure a new mapper (or Add Mapper > By configuration if you have already created mappers before for this client)

如何使用Keycloak的RESTful API获取用户的自定义属性?

  • Select User Attribute
  • Fill up the field User Attribute with your custom user attribute
  • Set to be added to the userinfo endpoint
  • Fill up the remaining fields, accordingly
  • Click on Save

This is enough to enabled your custom user attribute to be retrieved from the userinfo endpoint

huangapple
  • 本文由 发表于 2023年2月18日 02:29:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488065.html
匿名

发表评论

匿名网友

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

确定