简体   繁体   English

无法使用 Azure AD 和客户端凭据流为特定 scope 请求 OAuth2 令牌

[英]Cant request OAuth2 token for specific scope with Azure AD and Client Credentials flow

We are developing an application with a frontend and a backend.我们正在开发一个带有前端和后端的应用程序。 The backend should be accessed via Rest API with an OAuth2 token.应通过 Rest API 使用 OAuth2 令牌访问后端。 Authorization provider is Azure AD.授权提供者是 Azure AD。

In Azure we created 2 app registrations.在 Azure 中,我们创建了 2 个应用注册。 One for the API, one for the client app.一个用于 API,一个用于客户端应用程序。 The API registration defines 3 scopes (Read, Write, Delete). API 注册定义了 3 个范围(读取、写入、删除)。 The client app registration has delegated permission for these scopes.客户端应用注册已委派这些范围的权限。

We are requesting tokens with the clientID and clientSecret from the client app registration.我们从客户端应用注册请求带有 clientID 和 clientSecret 的令牌。

The problem is that we can only request tokens with scope api/.default.问题是我们只能使用 scope api/.default 请求令牌。 Eg api/read results in invalid scope error.例如 api/read 导致无效的 scope 错误。 But if we use api/.default, no scope (scp) attribute is included in the token.但如果我们使用 api/.default,则令牌中不包含 scope (scp) 属性。 Isn't that needed to check if the app consuming the API has the right permissions?不需要检查使用 API 的应用程序是否具有正确的权限吗?

I am not sure if we are doing something wrong or if we have a wrong understanding/expectation.我不确定我们是否做错了什么,或者我们是否有错误的理解/期望。

When using Client Credential flow to get Azure AD JWT token, the scope has to be in the format of使用客户端凭据流获取 Azure AD JWT 令牌时,scope 必须采用以下格式

api://<clientid of the API app registered>/.default

As per MSDN, OAUTH Client Credential Flow根据 MSDN, OAUTH 客户端凭证流

scope - Required - The value passed for the scope parameter in this request should be the resource identifier (application ID URI) of the resource you want, affixed with the.default suffix. scope - 必需 -此请求中为 scope 参数传递的值应该是您想要的资源的资源标识符(应用程序 ID URI) ,并附加 .default 后缀。 For the Microsoft Graph example, the value is https://graph.microsoft.com/.default .对于 Microsoft Graph 示例,该值为https://graph.microsoft.com/.default This value tells the Microsoft identity platform that of all the direct application permissions you have configured for your app, the endpoint should issue a token for the ones associated with the resource you want to use.此值告诉 Microsoft 标识平台,在您为应用程序配置的所有直接应用程序权限中,端点应为与您要使用的资源关联的权限颁发令牌。 To learn more about the /.default scope, see the consent documentation.要了解有关 /.default scope 的更多信息,请参阅同意文档。

The claims contained in the token returned by Azure AD depends on the OAuth2 grant type being used. Azure AD 返回的令牌中包含的声明取决于所使用的 OAuth2 授权类型。 When using a Client Credentials flow it implies that two applications, of which neither involves any user interaction, are being used.使用客户端凭据流时,这意味着正在使用两个应用程序,其中不涉及任何用户交互。 Azure documentation uses the terms daemon app and web API app. Azure 文档使用术语守护程序应用程序和 web API 应用程序。 The daemon app is the application calling API's of the web API app.守护程序应用程序是调用 web API 应用程序的 API 的应用程序。

The obvious but unfortunately wrong way is to use "Expose an API" eg on Azure portal: AD -> application -> Expose an API to create permissions.显而易见但不幸的是错误的方法是在 Azure 门户上使用“公开 API”:AD -> 应用程序 -> 公开 API 以创建权限。 The problem is that "Expose an API" creates only delegated permissions .问题是“公开 API”只创建委派权限 But delegated permissions are only relevant when a user is involved.但委托权限仅在涉及用户时才相关。 Which is not the case when using a Client Credentials grant.使用客户端凭据授权时情况并非如此。

What one needs to create are application permissions .需要创建的是应用程序权限 These permissions can, so it seems, currently only be created directly in the Manifest .目前看来,这些权限只能直接在Manifest中创建。 The following is needed to create an application permission:创建应用程序权限需要以下内容:

  1. Go to Web API app in AD -> click on Manifest Go 到 Web API 应用程序在 AD -> 点击清单
  2. Change the appRoles field to something like this:将 appRoles 字段更改为如下内容:
     { //... "appRoles": [ { "allowedMemberTypes": ["Application"], // Must be "Application" "description": "Allows Read operation", "displayName": "Read", "id": "a35fcf6e-58c4-42af-937d-f43e90103b44", // A unique UUID "isEnabled": true, "lang": null, "origin": "Application", "value": "Read" // The role one wants to create } ] //... }
  3. Save the file保存文件

More information can also be found on the official Azure documentation page Protected web API: App registration .更多信息也可以在官方 Azure 文档页面上找到受保护的 web API:应用程序注册

Now one can go to the daemon app in the AD and grant the created application permissions.现在可以 go 到 AD 中的守护程序应用程序并授予创建的应用程序权限。 In Azure portal this is done by these steps:在 Azure 门户中,这是通过以下步骤完成的:

  1. Go to Daemon app in AD Go 到 AD 中的守护程序应用程序
  2. Go to API permissions Go 到API 权限
  3. Click on Add a permission点击添加权限
  4. Select My APIs Select我的 API
  5. Select the Web API app Select Web API 应用程序
  6. Select Application permissions Select应用程序权限
  7. Now select the permissions the Daemon app gets granted现在 select 授予守护程序应用程序的权限
  8. Click on Add permission点击添加权限
  9. Grant admin consent by clicking on Grant admin consent for...通过单击授予管理员同意来授予管理员同意...

The daemon app can now request a token using client credentials grant.守护程序应用程序现在可以使用客户端凭据授予请求令牌。 The scope in the request must be '/.default'.请求中的 scope 必须是“/.default”。 (Only for delegated permissions one can ask for non-default scope.) The returned token will then contain the claim roles which is a list of granted permissions. (仅对于委派权限,可以请求非默认 scope。)然后返回的令牌将包含声明角色,这是一个授予权限的列表。 The permissions in the list are the permissions granted to the Damon app.列表中的权限是授予 Damon 应用程序的权限。 Eg例如

"roles": ["Read"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure AD OAuth 令牌错误的受众(客户端凭据流) - Azure AD OAuth token wrong audience (client credentials flow) Azure AD OAuth 客户端凭据授予流程 - Azure AD OAuth Client Credentials Grant flow 如何从 Azure AD OAuth2 隐式流中获取令牌? - How to get a token from Azure AD OAuth2 implicit flow? Azure AD B2C 在客户端凭据的 /oauth2/v2.0/token 端点中缺少随机数 - Azure AD B2C missing nonce in /oauth2/v2.0/token end point for client credentials 我可以使用azure oauth2客户端凭据流访问经典的azure服务管理rest api吗? - can i use azure oauth2 client credentials flow to access the classic azure service management rest api? 使用授权代码流从 Azure AD 请求 Oauth2 令牌时访问被拒绝 - Access denied when requesting Oauth2 token from Azure AD with Authorization code flow 是否可以仅请求用户已在 Azure AD OAuth2 隐式流中同意的范围的子集? - Is it possible to request only a subset of the scopes for which a user has granted consent in Azure AD OAuth2 implicit flow? Azure AD OAuth2访问令牌请求错误-400错误的请求 - Azure AD OAuth2 Access Token Request Error - 400 Bad Request Azure 应用 oauth2 在客户端凭据授予类型中生成错误的访问令牌 - Azure app oauth2 generating wrong access token in Client Credentials grant type Azure OAuth2 客户端凭据流 - 获取多个范围的令牌会引发错误 - Azure OAuth2 Client Credential flow - getting token for multiple scopes throws error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM