Skip to content

Commit 8386304

Browse files
authored
Merge pull request #3 from flownative/feature/collect-garbage
Add garbage collection to clean up expired authorizations
2 parents 73eee31 + 82ee347 commit 8386304

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

Classes/OAuthClient.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Doctrine\ORM\OptimisticLockException;
99
use Doctrine\ORM\ORMException;
1010
use Doctrine\ORM\TransactionRequiredException;
11-
use Flownative\OpenIdConnect\Client\OAuthProvider;
1211
use GuzzleHttp\Client;
1312
use GuzzleHttp\Exception\GuzzleException;
1413
use GuzzleHttp\Psr7\Response;
@@ -27,6 +26,8 @@
2726
use Neos\Flow\Mvc\ActionRequest;
2827
use Neos\Flow\Mvc\Routing\Exception\MissingActionNameException;
2928
use Neos\Flow\Mvc\Routing\UriBuilder;
29+
use Neos\Flow\Persistence\Doctrine\Query;
30+
use Neos\Flow\Persistence\Exception\InvalidQueryException;
3031
use Neos\Flow\Session\SessionInterface;
3132
use Psr\Http\Message\RequestInterface;
3233

@@ -52,6 +53,12 @@ abstract class OAuthClient
5253
*/
5354
protected $flowBaseUriSetting;
5455

56+
/**
57+
* @Flow\InjectConfiguration(path="garbageCollection.probability", package="Flownative.OAuth2.Client")
58+
* @var float
59+
*/
60+
protected $garbageCollectionProbability;
61+
5562
/**
5663
* @var Client
5764
*/
@@ -509,4 +516,40 @@ protected function createOAuthProvider(string $clientId, string $clientSecret):
509516
'requestFactory' => $this->getRequestFactory()
510517
]);
511518
}
519+
520+
/**
521+
* @return void
522+
* @throws ORMException
523+
* @throws InvalidQueryException
524+
*/
525+
protected function removeExpiredAuthorizations(): void
526+
{
527+
$query = new Query(Authorization::class);
528+
$authorizations = $query->matching($query->lessThan('expires', new \DateTimeImmutable()))->execute();
529+
foreach ($authorizations as $authorization) {
530+
assert($authorization instanceof Authorization);
531+
$this->entityManager->remove($authorization);
532+
}
533+
534+
$this->entityManager->flush();
535+
}
536+
537+
/**
538+
* Shuts down this client
539+
*
540+
* This method must not be called manually – it is invoked by Flow's object
541+
* management.
542+
*
543+
* @return void
544+
* @throws InvalidQueryException
545+
* @throws ORMException
546+
*/
547+
public function shutdownObject()
548+
{
549+
$decimals = (integer)strlen(strrchr($this->garbageCollectionProbability, '.')) - 1;
550+
$factor = ($decimals > -1) ? $decimals * 10 : 1;
551+
if (rand(1, 100 * $factor) <= ($this->garbageCollectionProbability * $factor)) {
552+
$this->removeExpiredAuthorizations();
553+
}
554+
}
512555
}

Configuration/Settings.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
Flownative:
22
OAuth2:
33
Client:
4+
garbageCollection:
5+
# The probability in percent of a client shutdown triggering a garbage
6+
# collection which removes expired tokens.
7+
#
8+
# Examples:
9+
# 1 (would be a 1% chance to clean up)
10+
# 20 (would be a 20% chance to clean up)
11+
# 0.42 (would be a 0.42 % chance to clean up)
12+
probability: 1
413
services: []
514
# - name: 'flownative-beach'
615
# className: 'Flownative\Beach\BeachClient'
716

8-
917
Neos:
1018
Flow:
1119
mvc:

0 commit comments

Comments
 (0)