【问题标题】:ForgeRock - Invalid Token ExchangeForgeRock - 无效的代币交换
【发布时间】:2021-10-26 14:54:46
【问题描述】:

我正在使用 ForgeRock v7.1.0,在 MacOS 11.5.2 (Big Sur) 上的 Docker v3.6.0 中运行。

我正在尝试交换我已经检索到的 OAuth 2.0 访问令牌(主题令牌)作为 ID 令牌(带有 JWT 有效负载),并且每次调用它时都会给我一个错误,我相信特别相关到subject_token_type 参数。

我遵循的步骤如下:

  1. 生成 OAuth2 访问令牌:
curl --location --request POST 'http://am.example.com:8080/am/oauth2/realms/root/access_token' \
--header 'Authorization: Basic c3RldmU6cGFzc3dvcmQ=' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username=user1' \
--data-urlencode 'password=7fYCi0Frhcq5p3gCXGxJ2B' \
--data-urlencode 'scope=cn'

返回以下内容:

{
    "access_token": "BS8vVbJ4EEygzdEE3jQH-xsKW9w",
    "scope": "cn",
    "token_type": "Bearer",
    "expires_in": 3599
}
  1. 自省返回的令牌以检查其是否有效:
curl --location --request POST 'http://am.example.com:8080/am/oauth2/realms/root/introspect?token=BS8vVbJ4EEygzdEE3jQH-xsKW9w' \
--header 'Authorization: Basic c3RldmU6cGFzc3dvcmQ='

返回以下内容:

{
    "active": true,
    "scope": "cn",
    "realm": "/",
    "client_id": "steve",
    "user_id": "user1",
    "token_type": "Bearer",
    "exp": 1629990663,
    "sub": "(usr!user1)",
    "subname": "user1",
    "iss": "http://am.example.com:8080/am/oauth2",
    "auth_level": 0,
    "authGrantId": "whjnenzHCH96TyaxfuefiOcBfm8",
    "auditTrackingId": "4d353b7e-6cd5-4289-884a-39c50396ed0c-116027"
}
  1. 现在这是我在尝试交换令牌时遇到错误的地方:
curl --location --request POST 'http://am.example.com:8080/am/oauth2/realms/root/access_token' \
--header 'Authorization: Basic c3RldmU6cGFzc3dvcmQ=' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=urn:ietf:params:oauth:grant-type:token-exchange' \
--data-urlencode 'subject_token_type=urn:ietf:params:oauth:token-type:access_token' \
--data-urlencode 'subject_token=BS8vVbJ4EEygzdEE3jQH-xsKW9w' \
--data-urlencode 'scope=cn' \
--data-urlencode 'requested_token_type=urn:ietf:params:oauth:token-type:id_token'

这给了我错误:

{
    "error_description": "Invalid token exchange.",
    "error": "invalid_request"
}

如果我没有指定 subject_token_type 它反而会给我错误:

{
    "error_description": "Subject token type is required.",
    "error": "invalid_request"
}

这让我相信要么我使用了错误的类型,要么我的本地 ForgeRock 实例中的某些设置不正确。

我查看了此处描述的错误响应可能性:https://backstage.forgerock.com/docs/am/7.1/oauth2-guide/token-exchange-flows.html,但它没有那么有用。

感谢您的任何帮助!谢谢史蒂夫

【问题讨论】:

    标签: oauth-2.0 oauth forgerock


    【解决方案1】:

    哇!始终如一,在发布问题后,您设法解决了问题!

    好的,这就是我需要做的:

    1. 导航到领域 > [领域名称] > 脚本

    2. 将“OAuth 2.0 May Act”Groovy 脚本修改为如下所示(酌情更改):

    import org.forgerock.json.JsonValue
    
    token.setMayAct(
        JsonValue.json(JsonValue.object(
            JsonValue.field("client_id", "steve"), 
            JsonValue.field("sub", "(usr!user1)"))))
    
    1. 验证然后保存更改

    2. 导航到领域 > [领域名称] > 服务 > OAuth 2.0 提供者

    3. 将“核心”选项卡底部的两个“.. Token May Act Script”下拉菜单设置为新修改的“OAuth2 May Act”脚本

    4. 重新生成访问令牌并重复令牌交换调用,它应该可以正常工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-23
      • 2021-07-17
      • 1970-01-01
      • 2022-08-13
      • 1970-01-01
      • 2020-11-21
      • 2022-07-29
      • 2018-11-26
      相关资源
      最近更新 更多