-
Notifications
You must be signed in to change notification settings - Fork 8
/
OAuthClient.php
626 lines (551 loc) · 25.5 KB
/
OAuthClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
<?php
declare(strict_types=1);
namespace Flownative\OAuth2\Client;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Uri;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Provider\GenericProvider;
use League\OAuth2\Client\Tool\RequestFactory;
use Neos\Cache\Exception;
use Neos\Cache\Frontend\VariableFrontend;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Http\HttpRequestHandlerInterface;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Flow\Mvc\Routing\Exception\MissingActionNameException;
use Neos\Flow\Mvc\Routing\UriBuilder;
use Neos\Flow\Persistence\Doctrine\Query;
use Neos\Flow\Persistence\Exception\InvalidQueryException;
use Neos\Flow\Session\SessionInterface;
use Neos\Http\Factories\ServerRequestFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;
/**
* Base class for an OAuth client
*/
abstract class OAuthClient
{
/**
* Name of the HTTP query parameter used for passing around the authorization id
*
* @const string
*/
public const AUTHORIZATION_ID_QUERY_PARAMETER_NAME_PREFIX = 'flownative_oauth2_authorization_id';
/**
* @var string
*/
protected $serviceName;
/**
* @Flow\Inject
* @var UriBuilder
*/
protected $uriBuilder;
/**
* @Flow\Inject
* @var Bootstrap
*/
protected $bootstrap;
/**
* @Flow\Inject
* @var ServerRequestFactory
*/
protected $serverRequestFactory;
/**
* @Flow\InjectConfiguration(path="http.baseUri", package="Neos.Flow")
* @var string
*/
protected $flowBaseUriSetting;
/**
* @Flow\InjectConfiguration(path="garbageCollection.probability", package="Flownative.OAuth2.Client")
* @var float
*/
protected $garbageCollectionProbability;
/**
* @Flow\InjectConfiguration(path="token.defaultLifetime", package="Flownative.OAuth2.Client")
* @var int|null
*/
protected $defaultTokenLifetime;
/**
* @var Client
*/
protected $httpClient;
/**
* @Flow\Inject
* @var SessionInterface
*/
protected $session;
/**
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* @Flow\Inject
* @var LoggerInterface
*/
protected $logger;
/**
* @Flow\Inject
* @var VariableFrontend
*/
protected $stateCache;
/**
* @param string $serviceName
*/
public function __construct(string $serviceName)
{
$this->serviceName = $serviceName;
}
/**
* @param EntityManagerInterface $entityManager
* @return void
*/
public function injectEntityManager(EntityManagerInterface $entityManager): void
{
$this->entityManager = $entityManager;
}
/**
* Returns the service type, i.e. a specific implementation of this client to use
*
* @return string For example, "Github", "oidc", ...
*/
abstract public static function getServiceType(): string;
/**
* Returns the service name, i.e. something like an instance name of the concrete implementation of this client
*
* @return string For example, "SpecificGithubConnection", "MySpecialService", ...
*/
public function getServiceName(): string
{
return $this->serviceName;
}
/**
* Returns the OAuth server's base URI
*
* @return string For example https://myservice.flownative.com
*/
abstract public function getBaseUri(): string;
/**
* Returns the current client id (for sending authenticated requests)
*
* @return string The client id which is known by the OAuth server
*/
abstract public function getClientId(): string;
/**
* Returns the OAuth service endpoint for the access token.
* Override this method if needed.
*
* @return string
*/
public function getAccessTokenUri(): string
{
return trim($this->getBaseUri(), '/') . '/oauth/token';
}
/**
* Returns the OAuth service endpoint for authorizing a token.
* Override this method if needed.
*
* @return string
*/
public function getAuthorizeTokenUri(): string
{
return trim($this->getBaseUri(), '/') . '/oauth/token/authorize';
}
/**
* Returns the OAuth service endpoint for accessing the resource owner details.
* Override this method if needed.
*
* @return string
*/
public function getResourceOwnerUri(): string
{
return trim($this->getBaseUri(), '/') . '/oauth/token/resource';
}
/**
* Returns a factory for requests used by this OAuth client.
*
* You may override this method an provide a custom request factory, for example for adding
* additional headers (e.g. User-Agent) to every request.
*
* @return RequestFactory
*/
public function getRequestFactory(): RequestFactory
{
return new RequestFactory();
}
/**
* Generates the URL query parameter name which is used for passing the authorization id of a
* finishing authorization to Flow (via the "Return URL").
*
* @param string $serviceType "Class" of the of the service, for example, "Github", "oidc", ...
* @return string
*/
public static function generateAuthorizationIdQueryParameterName(string $serviceType): string
{
return self::AUTHORIZATION_ID_QUERY_PARAMETER_NAME_PREFIX . '_' . $serviceType;
}
/**
* Requests an access token.
*
* This method is used using the OAuth Client Credentials Flow for machine-to-machine applications.
* Therefore the grant type must be Authorization::GRANT_CLIENT_CREDENTIALS. You need to specify the
* client identifier and client secret and may optionally specify a scope.
*
* @param string $serviceName
* @param string $clientId Client ID
* @param string $clientSecret Client Secret
* @param string $scope Scope which may consist of multiple identifiers, separated by comma
* @param array $additionalParameters Additional parameters to provide in the request body while requesting the token. For example ['audience' => 'https://www.example.com/api/v1']
* @return void
* @throws IdentityProviderException
*/
public function requestAccessToken(string $serviceName, string $clientId, string $clientSecret, string $scope, array $additionalParameters = []): void
{
$authorizationId = Authorization::generateAuthorizationIdForClientCredentialsGrant($serviceName, $clientId, $clientSecret, $scope, $additionalParameters);
$this->logger->info(sprintf('OAuth (%s): Retrieving access token using client credentials grant for client "%s" using a %s bytes long secret. (authorization id: %s)', $this->getServiceType(), $clientId, strlen($clientSecret), $authorizationId));
$existingAuthorization = $this->getAuthorization($authorizationId);
if ($existingAuthorization !== null) {
$this->entityManager->remove($existingAuthorization);
$this->entityManager->flush();
$this->logger->info(sprintf('OAuth (%s): Removed old OAuth token for client "%s". (authorization id: %s)', $this->getServiceType(), $clientId, $authorizationId), LogEnvironment::fromMethodName(__METHOD__));
}
$accessToken = $this->createOAuthProvider($clientId, $clientSecret)->getAccessToken(Authorization::GRANT_CLIENT_CREDENTIALS, $additionalParameters);
$authorization = new Authorization($authorizationId, $serviceName, $clientId, Authorization::GRANT_CLIENT_CREDENTIALS, $scope);
$authorization->setAccessToken($accessToken);
$this->logger->info(sprintf('OAuth (%s): Persisted new OAuth authorization %s for client "%s" with expiry time %s. (authorization id: %s)', $this->getServiceType(), $authorizationId, $clientId, $accessToken->getExpires(), $authorizationId), LogEnvironment::fromMethodName(__METHOD__));
$this->entityManager->persist($authorization);
$this->entityManager->flush();
}
/**
* Returns an authorization id taking the service type and service name into account.
*
* @param string $clientId
* @return string
* @throws OAuthClientException
*/
public function generateAuthorizationIdForAuthorizationCodeGrant(string $clientId): string
{
return Authorization::generateAuthorizationIdForAuthorizationCodeGrant($this->getServiceType(), $this->getServiceName(), $clientId);
}
/**
* Start OAuth authorization with the Authorization Code flow
*
* @param string $clientId The client id, as provided by the OAuth server
* @param string $clientSecret The client secret, provided by the OAuth server
* @param UriInterface $returnToUri URI to return to when authorization is finished
* @param string $scope Scope to request for authorization. Must be scope ids separated by space, e.g. "openid profile email"
* @return UriInterface The URL the browser should redirect to, asking the user to authorize
* @throws OAuthClientException
*/
public function startAuthorization(string $clientId, string $clientSecret, UriInterface $returnToUri, string $scope): UriInterface
{
$authorizationId = $this->generateAuthorizationIdForAuthorizationCodeGrant($clientId);
return $this->startAuthorizationWithId($authorizationId, $clientId, $clientSecret, $returnToUri, $scope);
}
/**
* Start OAuth authorization with the Authorization Code flow
* based on a specified authorization identifier.
*
* Note that, if you use this method, it is your responsibility to provide a
* meaningful authorization id. You might weaken the security of your
* application if you use an id which is deterministic or can be guessed by
* an attacker.
*
* If in doubt, always use startAuthorization() instead.
*
* @param string $clientId The client id, as provided by the OAuth server
* @param string $clientSecret The client secret, provided by the OAuth server
* @param UriInterface $returnToUri URI to return to when authorization is finished
* @param string $scope Scope to request for authorization. Must be scope ids separated by space, e.g. "openid profile email"
* @return UriInterface The URL the browser should redirect to, asking the user to authorize
* @throws OAuthClientException
*/
public function startAuthorizationWithId(string $authorizationId, string $clientId, string $clientSecret, UriInterface $returnToUri, string $scope): UriInterface
{
$authorization = new Authorization($authorizationId, $this->getServiceType(), $clientId, Authorization::GRANT_AUTHORIZATION_CODE, $scope);
if ($this->defaultTokenLifetime !== null) {
$authorization->setExpires(new \DateTimeImmutable('+ ' . $this->defaultTokenLifetime . ' seconds'));
}
$this->logger->info(sprintf('OAuth (%s): Starting authorization %s using client id "%s", a %s bytes long secret and scope "%s".', $this->getServiceType(), $authorization->getAuthorizationId(), $clientId, strlen($clientSecret), $scope));
try {
$oldAuthorization = $this->entityManager->find(Authorization::class, $authorization->getAuthorizationId());
if ($oldAuthorization !== null) {
$authorization = $oldAuthorization;
}
$this->entityManager->persist($authorization);
$this->entityManager->flush();
} catch (ORMException $exception) {
throw new OAuthClientException(sprintf('OAuth (%s): Failed storing authorization in database: %s', $this->getServiceType(), $exception->getMessage()), 1568727133);
}
$oAuthProvider = $this->createOAuthProvider($clientId, $clientSecret);
$authorizationUri = new Uri($oAuthProvider->getAuthorizationUrl(['scope' => $scope]));
if ($clientId === $clientSecret) {
$this->logger->error(sprintf('OAuth (%s): Client ID and Client secret are the same! Please check your configuration.', $this->getServiceType()));
}
try {
$this->stateCache->set(
$oAuthProvider->getState(),
[
'authorizationId' => $authorization->getAuthorizationId(),
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'returnToUri' => (string)$returnToUri
]
);
} catch (Exception $exception) {
throw new OAuthClientException(sprintf('OAuth (%s): Failed setting cache entry for authorization: %s', $this->getServiceType(), $exception->getMessage()), 1560178858);
}
return $authorizationUri;
}
/**
* Finish an OAuth authorization with the Authorization Code flow
*
* @param string $stateIdentifier The state identifier, passed back by the OAuth server as the "state" parameter
* @param string $code The authorization code given by the OAuth server
* @param string $scope The scope granted by the OAuth server
* @return UriInterface The URI to return to
* @throws OAuthClientException
*/
public function finishAuthorization(string $stateIdentifier, string $code, string $scope): UriInterface
{
$stateFromCache = $this->stateCache->get($stateIdentifier);
if (empty($stateFromCache)) {
throw new OAuthClientException(sprintf('OAuth: Finishing authorization failed because oAuth state %s could not be retrieved from the state cache.', $stateIdentifier), 1558956494);
}
$authorizationId = $stateFromCache['authorizationId'];
$clientId = $stateFromCache['clientId'];
$clientSecret = $stateFromCache['clientSecret'];
$oAuthProvider = $this->createOAuthProvider($clientId, $clientSecret);
$this->logger->info(sprintf('OAuth (%s): Finishing authorization for client "%s", authorization id "%s", using state %s.', $this->getServiceType(), $clientId, $authorizationId, $stateIdentifier));
try {
$authorization = $this->entityManager->find(Authorization::class, $authorizationId);
if (!$authorization instanceof Authorization) {
throw new OAuthClientException(sprintf('OAuth2 (%s): Finishing authorization failed because authorization %s could not be retrieved from the database.', $this->getServiceType(), $authorizationId), 1568710771);
}
if ($authorization->getGrantType() !== Authorization::GRANT_AUTHORIZATION_CODE) {
throw new OAuthClientException(sprintf('OAuth2 (%s): Finishing authorization failed because authorization %s does not have the authorization code flow type!', $this->getServiceType(), $authorizationId), 1597312780);
}
$this->logger->debug(sprintf('OAuth (%s): Retrieving an OAuth access token for authorization "%s" in exchange for the code %s', $this->getServiceType(), $authorizationId, str_repeat('*', strlen($code) - 3) . substr($code, -3, 3)));
$accessToken = $oAuthProvider->getAccessToken(Authorization::GRANT_AUTHORIZATION_CODE, ['code' => $code]);
$this->logger->info(sprintf('OAuth (%s): Persisting OAuth token for authorization "%s" with expiry time %s.', $this->getServiceType(), $authorizationId, $accessToken->getExpires()));
$authorization->setAccessToken($accessToken);
$accessTokenValues = $accessToken->getValues();
$scope = $accessTokenValues['scope'] ?? $scope;
$authorization->setScope($scope);
$this->entityManager->persist($authorization);
$this->entityManager->flush();
} catch (IdentityProviderException $exception) {
throw new OAuthClientException($exception->getMessage(), 1511187001671, $exception);
}
$returnToUri = new Uri($stateFromCache['returnToUri']);
$returnToUri = $returnToUri->withQuery(trim($returnToUri->getQuery() . '&' . self::generateAuthorizationIdQueryParameterName($this->getServiceType()) . '=' . $authorizationId, '&'));
$this->logger->debug(sprintf('OAuth (%s): Finished authorization "%s", $returnToUri is %s.', $this->getServiceType(), $authorizationId, $returnToUri));
return $returnToUri;
}
/**
* Refresh an OAuth authorization
*
* @param string $authorizationId
* @param string $clientId
* @param string $returnToUri
* @return string
* @throws OAuthClientException
*/
public function refreshAuthorization(string $authorizationId, string $clientId, string $returnToUri): string
{
throw new OAuthClientException('refreshAuthorization is currently not implemented', 1602519541);
$authorization = $this->entityManager->find(Authorization::class, ['authorizationId' => $authorizationId]);
if (!$authorization instanceof Authorization) {
throw new OAuthClientException(sprintf('OAuth2: Could not refresh OAuth token because authorization %s was not found in our database.', $authorization), 1505317044316);
}
$oAuthProvider = $this->createOAuthProvider($clientId, $authorization->getClientSecret()); // FIXME
$this->logger->info(sprintf('OAuth (%s): Refreshing authorization %s for client "%s" using a %s bytes long secret and refresh token "%s".', $this->getServiceType(), $authorizationId, $clientId, strlen($authorization->getClientSecret()), $authorization->refreshToken));
try {
$accessToken = $oAuthProvider->getAccessToken('refresh_token', ['refresh_token' => $authorization->refreshToken]);
$authorization->accessToken = $accessToken->getToken();
$authorization->setExpires($accessToken->getExpires() ? \DateTimeImmutable::createFromFormat('U', $accessToken->getExpires()) : null);
$this->logger->debug(sprintf($this->getServiceType() . ': New access token is "%s", refresh token is "%s".', $authorization->accessToken, $authorization->refreshToken));
$this->entityManager->persist($authorization);
$this->entityManager->flush();
} catch (IdentityProviderException $exception) {
throw new OAuthClientException($exception->getMessage(), 1511187196454, $exception);
}
return $returnToUri;
}
/**
* Returns the specified Authorization record, if it exists
*
* @param string $authorizationId
* @return Authorization|null
*/
public function getAuthorization(string $authorizationId): ?Authorization
{
$oAuthToken = $this->entityManager->getRepository(Authorization::class)->find(['authorizationId' => $authorizationId]);
return ($oAuthToken instanceof Authorization) ? $oAuthToken : null;
}
/**
* Removes the specified Authorization record
*
* @param string $authorizationId
* @return void
*/
public function removeAuthorization(string $authorizationId): void
{
$existingAuthorization = $this->getAuthorization($authorizationId);
if ($existingAuthorization !== null) {
$this->entityManager->remove($existingAuthorization);
$this->entityManager->flush();
$this->logger->debug(sprintf('OAuth (%s): Removed authorization id %s', $this->getServiceType(), $authorizationId), LogEnvironment::fromMethodName(__METHOD__));
}
}
/**
* Returns a prepared request to an OAuth 2.0 service provider using Bearer token authentication
*
* @param Authorization $authorization
* @param string $relativeUri A relative URI of the web server, prepended by the base URI
* @param string $method The HTTP method, for example "GET" or "POST"
* @param array $bodyFields Associative array of body fields to send (optional)
* @return RequestInterface
* @throws OAuthClientException
*/
public function getAuthenticatedRequest(Authorization $authorization, string $relativeUri, string $method = 'GET', array $bodyFields = []): RequestInterface
{
throw new OAuthClientException('getAuthenticatedRequest is currently not implemented', 1631826501);
$accessToken = $authorization->getAccessToken();
if ($accessToken === null) {
throw new OAuthClientException(sprintf($this->getServiceType() . 'Failed getting an authenticated request for client ID "%s" because the authorization contained no access token', $authorization->getClientId()), 1589300319);
}
$oAuthProvider = $this->createOAuthProvider($authorization->getClientId(), $authorization->getClientSecret());
return $oAuthProvider->getAuthenticatedRequest(
$method,
$this->getBaseUri() . $relativeUri,
$authorization->getAccessToken(),
[
'headers' => [
'Content-Type' => 'application/json'
],
'body' => ($bodyFields !== [] ? \GuzzleHttp\json_encode($bodyFields) : '')
]
);
}
/**
* Sends an HTTP request to an OAuth 2.0 service provider using Bearer token authentication
*
* @param Authorization $authorization
* @param string $relativeUri
* @param string $method
* @param array $bodyFields
* @return Response
* @throws GuzzleException
* @throws OAuthClientException
*/
public function sendAuthenticatedRequest(Authorization $authorization, string $relativeUri, string $method = 'GET', array $bodyFields = []): Response
{
throw new OAuthClientException('sendAuthenticatedRequest is currently not implemented', 1631826483);
if ($this->httpClient === null) {
$this->httpClient = new Client(['allow_redirects' => false]);
}
return $this->httpClient->send($this->getAuthenticatedRequest($authorization, $relativeUri, $method, $bodyFields));
}
/**
* @return string
*/
public function renderFinishAuthorizationUri(): string
{
$currentRequestHandler = $this->bootstrap->getActiveRequestHandler();
if ($currentRequestHandler instanceof HttpRequestHandlerInterface) {
$httpRequest = $currentRequestHandler->getHttpRequest();
} else {
putenv('FLOW_REWRITEURLS=1');
$httpRequest = $this->serverRequestFactory->createServerRequest('GET', new Uri($this->flowBaseUriSetting));
}
$actionRequest = ActionRequest::fromHttpRequest($httpRequest);
$this->uriBuilder->reset();
$this->uriBuilder->setRequest($actionRequest);
$this->uriBuilder->setCreateAbsoluteUri(true);
try {
$uri = $this->uriBuilder
->reset()
->setCreateAbsoluteUri(true)
->uriFor('finishAuthorization', ['serviceType' => $this->getServiceType(), 'serviceName' => $this->getServiceName()], 'OAuth', 'Flownative.OAuth2.Client');
return $uri;
} catch (MissingActionNameException $e) {
return '';
}
}
/**
* Helper method to set metadata on an Authorization instance. Changes are
* persisted immediately.
*
* @param string $authorizationId
* @param string $metadata
* @return void
*/
public function setAuthorizationMetadata(string $authorizationId, string $metadata): void
{
$authorization = $this->getAuthorization($authorizationId);
if ($authorization === null) {
throw new \RuntimeException(sprintf('Failed setting authorization metadata: authorization %s was not found', $authorizationId), 1631821719);
}
$authorization->setMetadata($metadata);
$this->entityManager->persist($authorization);
$this->entityManager->flush();
}
/**
* @param string $clientId
* @param string $clientSecret
* @return GenericProvider
*/
protected function createOAuthProvider(string $clientId, string $clientSecret): GenericProvider
{
return new GenericProvider([
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'redirectUri' => $this->renderFinishAuthorizationUri(),
'urlAuthorize' => $this->getAuthorizeTokenUri(),
'urlAccessToken' => $this->getAccessTokenUri(),
'urlResourceOwnerDetails' => $this->getResourceOwnerUri(),
], [
'requestFactory' => $this->getRequestFactory()
]);
}
/**
* @return void
* @throws InvalidQueryException
*/
protected function removeExpiredAuthorizations(): void
{
$query = new Query(Authorization::class);
$authorizations = $query->matching($query->lessThan('expires', new \DateTimeImmutable()))->execute();
foreach ($authorizations as $authorization) {
assert($authorization instanceof Authorization);
$this->entityManager->remove($authorization);
}
$this->entityManager->flush();
}
/**
* Shuts down this client
*
* This method must not be called manually – it is invoked by Flow's object
* management.
*
* @return void
*/
public function shutdownObject(): void
{
$garbageCollectionProbability = (string)$this->garbageCollectionProbability;
$decimals = strlen(strrchr($garbageCollectionProbability, '.') ?: '') - 1;
$factor = ($decimals > -1) ? $decimals * 10 : 1;
try {
if (random_int(1, 100 * $factor) <= ($this->garbageCollectionProbability * $factor)) {
$this->removeExpiredAuthorizations();
}
} catch (\Exception $e) {
}
}
}