【问题标题】:Keycloak access impersonate APIKeycloak 访问模拟 API
【发布时间】:2018-09-25 09:59:23
【问题描述】:

我们开始使用 keycloak 3.4.3,我们需要在我们的应用程序中引入一个模拟功能。我们发现 keycloak 有一个模拟 api,不幸的是它没有为用户返回一个令牌,而是一个用户可以“选择”他自己的客户端的重定向链接。

我们在这里找到

https://blog.softwaremill.com/who-am-i-keycloak-impersonation-api-bfe7acaf051a

一种(在 scala 中)检索新令牌的方法(仅适用于 keycloak 3.4+):

    private def exchangeToken(token: String, userId: String): Future[TokenResponse] = {
  import io.circe.generic.auto._
  sttp
    .post(uri"${config.authServerUrl}/realms/${config.realm}/protocol/openid-connect/token")
    .body(
      "grant_type" -> "urn:ietf:params:oauth:grant-type:token-exchange",
      "client_id" -> config.clientId,
      "requested_subject" -> userId,
      "subject_token" -> token
    )
    .response(asJson[TokenResponse])
    .send()
    .flatMap {
      _.body match {
        case Left(error) => Future.failed(new RuntimeException(error))
        case Right(Left(circeError)) => Future.failed(circeError)
        case Right(Right(tokenResponse)) => Future.successful(tokenResponse)
      }
    }
}

我尝试基于它创建一个 curl 命令:

curl --verbose -X POST "http://<host>/auth/realms/master/protocol/openid-connect/token" \
 -H "Content-Type: application/x-www-form-urlencoded" \
 --data-urlencode "grant_type=urn:ietf:params:oauth:grant-type:token-exchange" \
 -d 'client_id=admin_cli' \
 -d "requested_subject=${USER_ID}" \
 -d "subject_token=${TKN}" 

但我收到错误“invalid_client_credentials”。客户端“admin_cli”的 access_type 为“public”。我尝试将授权令牌添加为不记名,但仍然出现相同的错误。

我是否错过了要配置的内容?还是 curl 命令缺少某些参数?

感谢您的帮助

【问题讨论】:

    标签: curl keycloak


    【解决方案1】:

    我解决了这个问题,这是 curl 命令 admin_cli 而不是 admin-cli 中的一个简单拼写错误。

    谢谢

    【讨论】:

      【解决方案2】:

      希望现在回答还为时不晚。我们可以通过使用 impersonator(superadmin) 的凭据来获取 impersonated(tony123) 的访问令牌。以下是我遵循的步骤。

      1. 已创建超级管理员用户。我给了他impersonation 的角色,该角色位于Users -&gt; superadmin -&gt; Role Mappings -&gt; client roles -&gt; Realm-management
      2. 使用获取超级管理员的访问令牌
      curl --location --request POST 'http://localhost:8180/auth/realms/tenant/protocol/openid-connect/token' \
      --header 'Content-Type: application/x-www-form-urlencoded' \
      --data-urlencode 'grant_type=password' \
      --data-urlencode 'username=superadmin' \
      --data-urlencode 'password=<superadmin-password>' \
      --data-urlencode 'client_id=<source-client-id>' \
      --data-urlencode 'client_secret=<source-client-secret>'
      
      1. 现在使用以下 curl 获取模拟用户的令牌
      curl --location --request POST 'http://localhost:8180/auth/realms/tenant/protocol/openid-connect/token' \
      --header 'Content-Type: application/x-www-form-urlencoded' \
      --data-urlencode 'client_id=<source-client-id>' \
      --data-urlencode 'client_secret=<source-client-secret>' \
      --data-urlencode 'grant_type=urn:ietf:params:oauth:grant-type:token-exchange' \
      --data-urlencode 'subject_token=<access token got in step one>' \
      --data-urlencode 'requested_token_type=urn:ietf:params:oauth:token-type:access_token' \
      --data-urlencode 'requested_subject=<user id of tony123>'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-25
        • 2016-01-26
        • 2018-07-08
        • 2020-06-18
        • 1970-01-01
        • 1970-01-01
        • 2018-09-26
        • 2021-08-09
        相关资源
        最近更新 更多