Active Directory 是否不支持使用 PKCE 的授权代码流?

2023-12-09

我尝试使用当前推荐的授权代码流与 PKCE 来从 Active Directory 收集访问令牌。客户端将是一个公共 Angular SPA,这就是选择流程的原因。

收集 AD 中的 openid 配置以及用户的授权代码效果很好。 但我无法从以下端点请求访问令牌:

https://login.microsoftonline.com/{tenantId}/oauth2/token.

我尝试在 Postman 中重建请求:

POST /7e8c2868-7490-4dd7-82b7-f5ec29222d30/oauth2/token HTTP/1.1
Host: login.microsoftonline.com
Accept: application/json, text/plain, */*
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache

grant_type=authorization_code
code=...
code_verifier=...
client_id=...
redirect_uri=...

...并最终得到以下消息:

{
  "error": "invalid_client",
  "error_description": "AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.\r\nTrace ID: ed0413ad-89f1-4a2b-8d68-e23498701800\r\nCorrelation ID: deb53b0d-5398-4f72-a9a5-6c0863547b99\r\nTimestamp: 2020-03-06 09:30:36Z",
  "error_codes": [
    7000218
  ],
  "timestamp": "2020-03-06 09:30:36Z",
  "trace_id": "ed0413ad-89f1-4a2b-8d68-e23498701800",
  "correlation_id": "deb53b0d-5398-4f72-a9a5-6c0863547b99",
  "error_uri": "https://login.microsoftonline.com/error?code=7000218"
}

这看起来很奇怪,因为 PKCE 身份验证流程的官方规范不需要 client_secret 或 client_assertion。这仅对于默认身份验证流程是必需的。

AD 实施有问题还是我配置错误?

Web 客户端的清单如下所示:

{
  "id": "...",
  "acceptMappedClaims": null,
  "accessTokenAcceptedVersion": null, 
  "addIns": [],
  "allowPublicClient": true,
  "appId": "...",
  "appRoles": [],
  "oauth2AllowUrlPathMatching": false,
  "createdDateTime": "...",
  "groupMembershipClaims": null,
  "identifierUris": [],
  "informationalUrls": {
    "termsOfService": null,
    "support": null,
    "privacy": null,
    "marketing": null
  },
  "keyCredentials": [],
  "knownClientApplications": [],
  "logoUrl": null,
  "logoutUrl": null,
  "name": "...",
  "oauth2AllowIdTokenImplicitFlow": false,
  "oauth2AllowImplicitFlow": false,
  "oauth2Permissions": [],
  "oauth2RequirePostResponse": false,
  "optionalClaims": null,
  "orgRestrictions": [],
  "parentalControlSettings": {
    "countriesBlockedForMinors": [],
    "legalAgeGroupRule": "Allow"
  },
  "passwordCredentials": [],
  "preAuthorizedApplications": [],
  "publisherDomain": "...",
  "replyUrlsWithType": [
    {
        "url": "http://localhost:4200",
        "type": "Web"
    }
  ],
  "requiredResourceAccess": [
    {
        "resourceAppId": "00000003-0000-0000-c000-000000000000",
        "resourceAccess": [
            {
                "id": "...",
                "type": "Scope"
            }
        ]
    }
  ],
  "samlMetadataUrl": null,
  "signInUrl": null,
  "signInAudience": "AzureADMyOrg",
  "tags": [],
  "tokenEncryptionKeyId": null
}

我的应用程序在 AD 中注册为公共应用程序。

在此之前发送的身份验证请求如下所示:

GET /.../oauth2/authorize
  response_type=code
  &client_id=...
  &state=...
  &redirect_uri=http%3A%2F%2Flocalhost%3A4200
  &scope=openid%20user_impersonation%20offline_access
  &code_challenge=...
  &code_challenge_method=...
  &nonce=...

Host: login.microsoftonline.com

2021 年更新

Microsoft 最终更新了他们的门户,因此我们现在有了一个 UI,可以使用 PKCE 正确配置授权代码流。

All you have to do is open the Authentication-page of your registered AAD Application. There you can add a new Platform by clicking the button on the left: Add a SPA platform to your AAD App

选择新的单页应用程序图块并输入您的重定向 URL。

先前的答案(清单)

我刚刚在 中找到了答案@azure/msal-浏览器包裹。目前,Azure AD 似乎正在努力支持此身份验证流程。要激活它,您必须为最近添加的重定向网址设置新类型。

要将授权代码流与 PKCE 和 Azure Active Directory 结合使用,您需要:

  1. 设置将网络平台添加到您的天蓝色广告应用程序并添加重定向网址。
  2. 将这些重定向 URL 的类型从“Web”更改为“Spa”。这必须在清单中完成。更改它将使网址从身份验证页面消失。但这没关系,因为它仍然存在于清单中。
  3. 将 Web 应用程序视为公共客户端(身份验证 > 高级设置 > 默认客户端类型 -“是”)。

现在令牌端点不需要client_secret or client_assertion不再了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Active Directory 是否不支持使用 PKCE 的授权代码流? 的相关文章

随机推荐