【发布时间】: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 命令缺少某些参数?
感谢您的帮助
【问题讨论】: