Resource server access other resources usage is an extension scenario of the aad-resource-server sample. Similarly, this sample illustrates how to protect a Java web API by restricting access to its resources to authorized accounts, and the restricted resource will access other restricted resource, such as Graph API and Custom API.
Assume that the user has been authenticated on an application using the OAuth 2.0 authorization code grant flow or another login flow. At this point, the application has an access token for API A (token A) with the user's claims and consent to access the middle-tier web API (API A). Now, API A needs to make an authenticated request to the downstream web API B(API B). The following steps constitute the OBO process and the client credential process, as shown in the following figure.
- The client application makes a request to API A with token A (with an aud claim of API A).
- API A authenticates to the Microsoft identity platform token issuance endpoint and requests a token to access API B.
- The Microsoft identity platform token issuance endpoint validates API A's credentials along with token A and issues the access token for API B (token B) to API A.
- Token B is set by API A in the authorization header of the request to API B.
- Data from the secured resource is returned by API B to API A, and from there to the client.
We will prepare two applications to demonstrate the dependent calls of resources.
- Configure
Web API B
Resource Server: aad-resource-server
- Expose two scopes for Web API A,
Obo.Graph.Read
andObo.WebApiA.ExampleScope
. The Application ID URI is generated by default.
- Select API permissions > Add a permission > My APIs, select Web API B application name.
- Delegated permissions is selected by default, Select WebApiB.ExampleScope permission, select Add permission to complete the process.
- Grant admin consent for Web API B permissions and Microsoft Graph permissions.
See OAuth 2.0 On-Behalf-Of flow for more information about OBO.
spring:
cloud:
azure:
active-directory:
enabled: true
credential:
client-id: ${AZURE_CLIENT_ID}
client-secret: ${AZURE_CLIENT_SECRET}
profile:
tenant-id: ${AZURE_TENANT_ID}
app-id-uri: ${WEB_API_A_APP_ID_URL}
authorization-clients:
graph:
scopes:
- https://graph.microsoft.com/User.Read
webapiB: # When authorization-grant-type is null, on behalf of flow is used by default
authorization-grant-type: on_behalf_of
scopes:
- ${WEB_API_B_APP_ID_URL}/WebApiB.ExampleScope
cd azure-spring-boot-samples/aad/spring-cloud-azure-starter-active-directory/web-client-access-resource-server/aad-resource-server-obo
mvn spring-boot:run
- Web API A will call Graph resource.
- Get access-token:
curl -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=password&client_id=<web-apiA-client-id>&scope=<app-id-uri>/Obo.Graph.Read&client_secret=<web-apiA-client-secret>&username=<username>&password=<password>' 'https://login.microsoftonline.com/organizations/oauth2/v2.0/token'
- Access endpoint by access-token:
curl localhost:8081/call-graph -H "Authorization: Bearer <access-token>"
- Verify response:
Graph response success.
- Web API A will call Graph resource through
OAuth2AuthorizedClientManager
.
- Get access-token:
curl -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=password&client_id=<web-apiA-client-id>&scope=<app-id-uri>/Obo.Graph.Read&client_secret=<web-apiA-client-secret>&username=<username>&password=<password>' 'https://login.microsoftonline.com/organizations/oauth2/v2.0/token'
- Access endpoint by access-token:
curl localhost:8081/call-graph-with-authorized-client-manager -H "Authorization: Bearer <access-token>"
- Verify response:
Graph response success.
- Web API A will call Custom(Web API B) resources.
- Get access-token:
curl -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=password&client_id=<web-apiA-client-id>&scope=<app-id-uri>/Obo.WebApiA.ExampleScope&client_secret=<web-apiA-client-secret>&username=<username>&password=<password>' 'https://login.microsoftonline.com/organizations/oauth2/v2.0/token'
- Access endpoint by access-token:
curl localhost:8081/webapiA/webapiB -H "Authorization: Bearer <access-token>"
- Verify response:
webapiB response success.