88use Doctrine \ORM \OptimisticLockException ;
99use Doctrine \ORM \ORMException ;
1010use Doctrine \ORM \TransactionRequiredException ;
11- use Flownative \OpenIdConnect \Client \OAuthProvider ;
1211use GuzzleHttp \Client ;
1312use GuzzleHttp \Exception \GuzzleException ;
1413use GuzzleHttp \Psr7 \Response ;
2726use Neos \Flow \Mvc \ActionRequest ;
2827use Neos \Flow \Mvc \Routing \Exception \MissingActionNameException ;
2928use Neos \Flow \Mvc \Routing \UriBuilder ;
29+ use Neos \Flow \Persistence \Doctrine \Query ;
30+ use Neos \Flow \Persistence \Exception \InvalidQueryException ;
3031use Neos \Flow \Session \SessionInterface ;
3132use 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}
0 commit comments