我们开始使用 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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试基于它创建一个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' \ …Run Code Online (Sandbox Code Playgroud)