【问题标题】:Keycloak: Access token client-1 to manage client-2 resourcesKeycloak:访问令牌 client-1 以管理 client-2 资源
【发布时间】:2021-03-10 07:02:43
【问题描述】:

我在 Keycloak 中有两个客户:

  1. CP:客户公开
  2. CC:客户机密启用了服务帐户和一些资源。

资源所有者是创建它们的用户,他们也管理访问权限。

创建 Resource (R) 的 User (U) 可以登录 CC 并使用访问令牌在 CP 上调用端点。

现在我希望 U 能够设置 UMA 策略,但访问令牌来自 CP,而不是来自 CC 资源所在的位置,因此 Keycloak 抱怨 de token。

org.keycloak.authorization.client.util.HttpResponseException: Unexpected response from server: 403 / Forbidden / Response from server: {\"error\":\"invalid_clientId\",\"error_description\":\"Client application [CP] is not registered as a resource server.\"}
fun onlyOwner(accessToken: String, id: String, resourceId: String) {
   val request = UmaPermissionRepresentation()
   request.name = "Only owner can view $id"
   request.description = "Only owner can view this resource"
   request.scopes = setOf(ResourceScope.VIEW)
   request.condition = ONLY_OWNER_CONDITION
   authzClient.protection(accessToken).policy(resourceId).create(request)
}

Keycloak 文档提到以下内容: " Policy API 位于: http://${host}:${port}/auth/realms/${realm_name}/authz/protection/uma-policy/{resource_id} 此 API 受不记名令牌保护,该不记名令牌必须代表用户授予资源服务器以代表他管理权限的同意。不记名令牌可以是从令牌端点获取的常规访问令牌,使用:

  • 资源所有者密码凭据授予类型
  • 令牌交换,为了将授予某些客户端(公共客户端)的访问令牌交换为受众是资源服务器的令牌

我把CP客户端换成CC客户端了:

原始令牌:

{
  "exp": 1606687405,
  "iat": 1606651407,
  "auth_time": 1606651405,
  "jti": "1e4075a9-ce49-4462-91f7-33b8963f56dd",
  "iss": "http://localhost/auth/realms/test",
  "aud": "account",
  "sub": "8381b629-5f10-401c-ae90-bb37769e5f70",
  "typ": "Bearer",
  "azp": "CP",
  "session_state": "6c2d73e7-a4bd-44da-b242-cdf26ec812bc",
  "acr": "1",
  "allowed-origins": [
    "*"
  ],
  "realm_access": {
    "roles": [
      "offline_access",
      "uma_authorization"
    ]
  },
  "resource_access": {
    "account": {
      "roles": [
        "manage-account",
        "manage-account-links",
        "view-profile"
      ]
    }
  },
  "scope": "openid email profile",
  "email_verified": true,
  "name": "Test First",
  "preferred_username": "test",
  "given_name": "Test",
  "family_name": "First",
  "email": "test@invent.com"
}

兑换代币:

{
  "exp": 1606687405,
  "iat": 1606652039,
  "auth_time": 1606651405,
  "jti": "0c84f42a-973e-4bc7-9a6d-2c4fec548512",
  "iss": "http://localhost/auth/realms/test",
  "aud": [
    "account",
    "CC"
  ],
  "sub": "8381b629-5f10-401c-ae90-bb37769e5f70",
  "typ": "Bearer",
  "azp": "CP",
  "session_state": "6c2d73e7-a4bd-44da-b242-cdf26ec812bc",
  "acr": "1",
  "allowed-origins": [
    "http://localhost"
  ],
  "realm_access": {
    "roles": [
      "offline_access",
      "uma_authorization"
    ]
  },
  "resource_access": {
    "account": {
      "roles": [
        "manage-account",
        "manage-account-links",
        "view-profile"
      ]
    }
  },
  "scope": "email profile",
  "email_verified": true,
  "name": "Test First",
  "preferred_username": "test",
  "given_name": "Test",
  "family_name": "First",
  "email": "test@invent.com"
}

但错误仍然存​​在 alghout audaccount 更改为 [account,cc]

【问题讨论】:

    标签: keycloak


    【解决方案1】:

    解决方案是使用目标客户端 (cc) 作为 client_id 和 client_secret 交换令牌。之后,您可以使用 Keycloak 返回的 access_token 作为承载令牌来创建 UMA_Policy。

    将公共客户端令牌交换为机密客户端令牌

    使用新的访问令牌创建策略

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-01
      • 2019-09-12
      • 2019-09-09
      • 1970-01-01
      • 2012-09-16
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多