You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a full example of using the `AuthorizationCode` grant type with a `RefreshToken` in the `examples/` directory.
148
+
146
149
### Grant Types
147
150
The following OAuth grant types are supported directly, and you can always create your own by implementing `kamermans\OAuth2\GrantType\GrantTypeInterface`:
148
151
-`AuthorizationCode`
@@ -205,8 +208,9 @@ By default, access tokens are not persisted anywhere. There are some built-in m
205
208
-`DoctrineCacheTokenPersistence` Takes a `Doctrine\Common\Cache\Cache` object and optionally a key name (default: `guzzle-oauth2-token`) where the access token will be saved.
206
209
-`SimpleCacheTokenPersistence` Takes a PSR-16 SimpleCache and optionally a key name (default: `guzzle-oauth2-token`) where the access token will be saved. This allows any PSR-16 compatible cache to be used.
207
210
-`Laravel5CacheTokenPersistence` Takes an `Illuminate\Contracts\Cache\Repository` object and optionally a key name (default: `guzzle-oauth2-token`) where the access token will be saved.
211
+
-`ClosureTokenPersistence` Allows you to define a token persistence provider by providing closures to handle the persistence functions.
208
212
209
-
If you want to use your own persistence layer, you should write your own class that implements `TokenPersistenceInterface`.
213
+
If you want to use your own persistence layer, you should write your own class that implements `TokenPersistenceInterface` or use the `ClosureTokenPersistence` provider, which is described at the end of this section.
210
214
211
215
To enable token persistence, you must use the `OAuth2Middleware::setTokenPersistence()` or `OAuth2Subscriber::setTokenPersistence()` method, like this:
212
216
@@ -220,6 +224,40 @@ $grant_type = new ClientCredentials($reauth_client, $reauth_config);
220
224
$oauth = new OAuth2Middleware($grant_type);
221
225
$oauth->setTokenPersistence($token_persistence);
222
226
```
227
+
### Closure-Based Token Persistence
228
+
There are plenty of cases where you would like to use your own caching layer to store the OAuth2 data, but there is no adapter included that works with your cache provider. The `ClosureTokenPersistence` provider makes this case easier by allowing you to define closures that handle the OAuth2 persistence data, as shown in the example below.
229
+
230
+
```php
231
+
// We'll store everything in an array, but you can use any provider you want
232
+
$cache = [];
233
+
$cache_key = "foo";
234
+
235
+
// Returns true if the item exists in cache
236
+
$exists = function() use (&$cache, $cache_key) {
237
+
return array_key_exists($cache_key, $cache);
238
+
};
239
+
240
+
// Sets the given $value array in cache
241
+
$set = function(array $value) use (&$cache, $cache_key) {
242
+
$cache[$cache_key] = $value;
243
+
};
244
+
245
+
// Gets the previously-stored value from cache (or null)
246
+
$get = function() use (&$cache, $cache_key, $exists) {
247
+
return $exists()? $cache[$cache_key]: null;
248
+
};
249
+
250
+
// Deletes the previously-stored value from cache (if exists)
251
+
$delete = function() use (&$cache, $cache_key, $exists) {
252
+
if ($exists()) {
253
+
unset($cache[$cache_key]);
254
+
}
255
+
};
256
+
257
+
$persistence = new ClosureTokenPersistence($set, $get, $delete, $exists);
258
+
```
259
+
260
+
> Note: The format of the token data is a PHP associative array. You can flatten the array with `serialize()` or `json_encode()` or whatever else you want before storing it, but remember to decode it back to an array in `get()` before returning it! Also, the above example is not very thread-safe, so if you have a high level of concurrency, you will need to find more atomic ways to handle this logic, or at least wrap things with `try/catch` and handle things gracefully.
223
261
224
262
Please see the `src/Persistence/` directory for more information on persistence.
0 commit comments