【问题标题】:How does the consent flow determine which resources to ask for?同意流程如何确定请求哪些资源?
【发布时间】:2020-08-03 03:54:30
【问题描述】:

我有一个公开自定义 API 的多租户服务主体。使用 MSAL.js 的 UserAgentApplication,我可以在首次使用 loginPopup 时征求我的同意。但是,我对在请求中指定哪些资源感到困惑。例如,假设我使用以下弹出窗口(注意缺少范围):

await this.userAgentApplication.loginPopup({
    prompt: 'consent',
    authority: "https://login.microsoftonline.com/organizations"
})

应用程序只会请求用户的个人资料。很公平。

但是,假设我将弹出窗口配置如下:

await this.userAgentApplication.loginPopup({
  scopes: ["api://xyz/Some.Scope"]
  prompt: "consent"
  authority: "https://login.microsoftonline.com/organizations"
})

这会导致异常:

用户或管理员未同意使用 ID 为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx”、名为“XYZ”的应用程序。为此用户和资源发送交互式授权请求。

为什么我在使用全局管理员帐户登录时仍会收到此错误?

最后,除了我们自己的 API 数据之外,我们还需要能够在应用程序上下文中读取图形组,因此我使用 .default 端点请求了这些(权限在服务主体注册中指定)。我使用以下弹出窗口做到了这一点:

await this.userAgentApplication.loginPopup({
    scopes: ["https://graph.microsoft.com/.default"],
    prompt: 'consent',
    authority: "https://login.microsoftonline.com/organizations"
})

最后一次尝试的结果是……我希望的所有权限!

登录并阅读用户资料

读写所有组

我的自定义 API 范围(应用程序名称)

但为什么我们的 Graph 同意请求会自动包含对其他自定义范围的请求?

【问题讨论】:

  • 您是否在应用注册中指定了任何必需的权限?
  • 您也可以尝试使用最后一个中的特定范围而不是 .default,因为它会触发 v1 行为

标签: javascript azure azure-active-directory msal


【解决方案1】:

如果您对资源使用管理员同意,这是预期的行为。

***/.default设置为范围时,相当于在Azure门户中执行“Grant admin consent for {your tenant}”。

因此,无论它们是否来自所需资源,它都会征得管理员同意所有必需的权限。

但是如果你设置了https://graph.microsoft.com/user.read,它只会要求你同意user.read的权限。

所以在这种情况下,一旦你使用最后一个做管理员同意,api://xyz/Some.Scope 也会生效。

我还对我的自定义 API api://***/.defaultapi://***/user.write 进行了测试,两者都按预期工作。

您也可以尝试使用以下请求进行管理员同意:

https://login.microsoftonline.com/jmaster.onmicrosoft.com/oauth2/v2.0/authorize?
client_id={client id}
&response_type=code
&redirect_uri={redirect url}
&response_mode=query
&scope=api://xyz/Some.Scope
&state=12345
&prompt=consent

请使用api://***/.default 重试。如果它不起作用也不要担心,因为api://xyz/Some.Scope也会使用最后一个代码sn-p生效。

【讨论】:

  • 谢谢!您关于在使用 .default 时请求“完全”权限的应用程序的论点对我来说很有意义。但是,当还涉及到委派权限(不仅仅是应用程序)时,是否可以使用自定义 API 的 .default 请求同意?我尝试并收到以下错误:“应用程序 [...] 正在为自己请求令牌。仅当使用基于 GUID 的应用程序标识符指定资源时才支持此方案。”
  • 更新:基于this question/answer,我意识到在请求管理员同意整个自定义 API 时,您必须使用应用 ID,而不是 api:// url。所以在我的 loginPopup 中我必须提供 scopes: ["xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx/.default"] 而不是 scopes: ["api://"xxxxxxxx-xxxx -xxxx-xxxx-xxxxxxxxxx/.default"].
猜你喜欢
  • 2021-05-20
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多