Skip to content

Commit f2cf317

Browse files
committed
fix: Update docs on OnBehalfOf authentication flow
1 parent 5fa3df1 commit f2cf317

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

docs/Examples.md

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,74 @@ $graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
2323

2424
```
2525

26-
To make requests on behalf of an already signed in user, where your front-end application has already acquired an access token for the user, you can use the `OnBehalfOfContext` which uses the [On-Behalf-Of flow](https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-on-behalf-of-flow) to fetch
27-
an access token for your backend application to access the Microsoft Graph API. To do this, you pass the already acquired access token as the "assertion";
26+
To make requests without a signed-in user (using application permissions), you can initialise a `ClientCredentialsContext` object:
2827

2928
```php
3029
use Microsoft\Graph\GraphServiceClient;
31-
use Microsoft\Kiota\Authentication\Oauth\OnBehalfOfContext;
30+
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
3231

33-
$tokenRequestContext = new OnBehalfOfContext(
32+
// Uses https://graph.microsoft.com/.default scopes if none are specified
33+
$tokenRequestContext = new ClientCredentialContext(
3434
'tenantId',
3535
'clientId',
36-
'clientSecret',
37-
'assertion'
36+
'clientSecret'
3837
);
39-
40-
$scopes = ['User.Read', 'Mail.ReadWrite'];
41-
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
38+
$graphServiceClient = new GraphServiceClient($tokenRequestContext);
4239

4340
```
4441

42+
To make requests on behalf of a signed in user, you can use the `OnBehalfOfContext` which uses the [On-Behalf-Of flow](https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-on-behalf-of-flow) to fetch
43+
an access token for your backend application to access the Microsoft Graph API.
4544

46-
To make requests without a signed-in user (using application permissions), you can initialise a `ClientCredentialsContext` object:
45+
This is useful when you would like your user to log in once and have your application do some background work
46+
on behalf of the user without asking them to log in again.
47+
48+
See the following guides on how to expose an API using your application registration:
49+
- https://learn.microsoft.com/en-us/answers/questions/1412022/aadsts50013-assertion-failed-signature-validation
50+
- https://stackoverflow.com/questions/77000068/acquire-azure-on-behalf-of-access-token-in-react-next-app/77002285#77002285
51+
- https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-configure-app-expose-web-apis
52+
53+
Execute the initial authentication request for your user e.g. via `AuthorizationCodeContext`:
54+
55+
```php
56+
57+
$oAuthClient = ProviderFactory::create(new AuthorizationCodeContext($tenantId, $clientId, $clientSecret, "", $redirectUri));
58+
$authorizationUrl = $oAuthClient->getAuthorizationUrl();
59+
60+
header("Location: $authorizationUrl");
61+
$code = $_GET['code'];
62+
63+
$authCodeContext = new AuthorizationCodeContext(
64+
$tenantId,
65+
$clientId,
66+
$clientSecret,
67+
$code,
68+
$redirectUri
69+
);
70+
71+
$scopes = ["api://{your-app-id}/.default"];
72+
73+
$tokenProvider = new GraphPhpLeagueAccessTokenProvider($authCodeContext, $scopes);
74+
75+
// access token containing your app as the audience (`aud`) claim
76+
$assertion = $tokenProvider->getAuthorizationTokenAsync("https://graph.microsoft.com")->wait();
77+
```
78+
79+
For future token requests, pass the previously acquired access token as the `assertion` and the Microsoft Identity platform will return an access token valid for accessing Microsoft Graph data;
4780

4881
```php
4982
use Microsoft\Graph\GraphServiceClient;
50-
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
83+
use Microsoft\Kiota\Authentication\Oauth\OnBehalfOfContext;
5184

52-
// Uses https://graph.microsoft.com/.default scopes if none are specified
53-
$tokenRequestContext = new ClientCredentialContext(
85+
$tokenRequestContext = new OnBehalfOfContext(
5486
'tenantId',
5587
'clientId',
56-
'clientSecret'
88+
'clientSecret',
89+
'assertion'
5790
);
58-
$graphServiceClient = new GraphServiceClient($tokenRequestContext);
91+
92+
$scopes = ['User.Read', 'Mail.ReadWrite'];
93+
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
5994

6095
```
6196

docs/expose-api.png

161 KB
Loading

0 commit comments

Comments
 (0)