我正在使用 ForgeRock v7.1.0,在 MacOS 11.5.2 (Big Sur) 上的 Docker v3.6.0 中运行。
我正在尝试交换我已经检索到的 OAuth 2.0 访问令牌(主题令牌)作为 ID 令牌(带有 JWT 有效负载),并且每次调用它时都会给我一个错误,特别是我相信与subject_token_type
参数相关.
我遵循的步骤如下:
- 生成 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
}
- 内省返回的令牌以检查其是否有效:
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"
}
- 现在,当我尝试交换令牌时,这是我得到错误的地方:
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但这并没有那么有用。
任何帮助都感激不尽!谢谢史蒂夫