Skip to content

Commit 776ae73

Browse files
committed
Update example for hydrating a custom cache
1 parent 57cc58f commit 776ae73

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

docs/Examples.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ $graphServiceClient = GraphServiceClient::createWithRequestAdapter($requestAdapt
8888
```
8989

9090
## Access token management
91+
9192
Using the `TokenRequestContext`, an instance of the `GraphServiceClient` requests access tokens and refresh tokens.
9293
The tokens are stored by default in an in-memory cache so that future requests using the same instance of the `GraphServiceClient` can re-use the previously acquired tokens.
9394

@@ -109,7 +110,7 @@ By default a `GraphServiceClient` instance caches access tokens in a built-in [`
109110
However, to get the cached token that the SDK requests for a user/application you
110111
can initialise an `InMemoryAccessTokenCache` or pass a custom implementation of the [`AccessTokenCache`](https://github.com/microsoft/kiota-authentication-phpleague-php/blob/dev/src/Cache/AccessTokenCache.php) interface interface and pass it as a parameter when initialising the `GraphServiceClient`. The two approaches are outlined below:
111112

112-
### Using an InMemoryAccessTokenCache instance:
113+
### Using an InMemoryAccessTokenCache instance
113114

114115
```php
115116
use Microsoft\Kiota\Authentication\Cache\InMemoryAccessTokenCache;
@@ -143,7 +144,7 @@ $accessToken = $inMemoryCache->getTokenWithContext($tokenRequestContext);
143144

144145
```
145146

146-
### Using a custom AccessTokenCache implementation:
147+
### Using a custom AccessTokenCache implementation
147148

148149
A custom [`AccessTokenCache`](https://github.com/microsoft/kiota-authentication-phpleague-php/blob/dev/src/Cache/AccessTokenCache.php) interface implementation can also be provided. After the request, the SDK persists the token in the
149150
custom cache via the `persistAccessToken()` method.
@@ -153,6 +154,8 @@ For `TokenRequestContexts` that do not require a signed in user (application per
153154
**`{tenantId}-{clientId}`** and for those that require a signed in user (delegated permissions), the cache key will be
154155
**`{tenantId}-{clientId}-{userId}`**.
155156

157+
Alternatively, you can override the default cache key
158+
156159
To retrieve the access token persisted to your custom cache for a particular user's/application's `TokenRequestContext`:
157160
```php
158161

@@ -172,7 +175,8 @@ This is also useful when re-using a previously retrieved access token for a sign
172175
The SDK will check the cache for a valid token before considering requesting a new token. If the provided token is expired
173176
and a refresh token is present, the access token will be refreshed and persisted to the cache. If no refresh token is provided, the SDK requests attempts to retrieve a new access token and persists it to the cache. In cases where a signed in user is present, e.g. authorization_code OAuth flows, the new token request will most likely fail because no valid `authorization_code` will be present meaning the user has to sign in again.
174177

175-
### Using the `InMemoryAccessTokenCache`:
178+
### Using the `InMemoryAccessTokenCache`
179+
176180
The in-memory cache can be hydrated/initialised using the `TokenRequestContext` and a PHPLeague [`AccessToken`](https://github.com/thephpleague/oauth2-client/blob/master/src/Token/AccessToken.php) object for a user/application:
177181

178182
```php
@@ -255,7 +259,7 @@ $graphServiceClient = GraphServiceClient::createWithAuthenticationProvider(
255259

256260
```
257261

258-
### Using a custom `AccessTokenCache` implementation`:
262+
### Using a custom `AccessTokenCache` implementation`
259263

260264
The SDK retrieves cached tokens using a cache key/identifier on the `TokenRequestContext`. The cache key
261265
on the `TokenRequestContext` is set using `setCacheKey()` which accepts an [`AccessToken`](https://github.com/thephpleague/oauth2-client/blob/master/src/Token/AccessToken.php) object.
@@ -269,13 +273,16 @@ For this scenario, the custom AccessTokenCache will need to be initialized in a
269273

270274
```php
271275

272-
$tokenRequestContext->setCacheKey(new AccessToken([
276+
$accessToken = new AccessToken([
273277
'access_token' => $accessToken,
274278
'refresh_token' => $refreshToken,
275279
'expires' => ...
276-
]));
280+
]);
281+
282+
$tokenRequestContext->setCacheKey($accessToken);
277283

278-
// init custom cache with tokens that map to $tokenRequestContext->getCacheKey()
284+
// init custom cache with tokens mapped to specific user/app using $tokenRequestContext->getCacheKey()
285+
$customCache = new CustomCache($tokenRequestContext->getCacheKey(), $accessToken);
279286

280287
// init graph client
281288
$graphServiceClient = GraphServiceClient::createWithAuthenticationProvider(

0 commit comments

Comments
 (0)