Skip to content

Commit aa2ff91

Browse files
committed
BUGFIX: Make expires an explicit property of Authorization
With that, OAuthClient::removeExpiredAuthorizations is able to select expired records. resolves: #9
1 parent 6e60d06 commit aa2ff91

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

Classes/Authorization.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ class Authorization
6161
*/
6262
protected $scope;
6363

64+
/**
65+
* @var \DateTimeImmutable
66+
* @ORM\Column(nullable = true)
67+
*/
68+
protected $expires;
69+
6470
/**
6571
* @var array
6672
* @ORM\Column(type="json_array", nullable = true)
@@ -93,7 +99,7 @@ public function __construct(string $serviceName, string $clientId, string $grant
9399
*/
94100
public static function calculateAuthorizationId(string $serviceName, string $clientId, string $scope, string $grantType): string
95101
{
96-
return sha1($serviceName . $clientId . $scope. $grantType);
102+
return sha1($serviceName . $clientId . $scope . $grantType);
97103
}
98104

99105
/**
@@ -193,4 +199,20 @@ public function getAccessToken(): ?AccessToken
193199
{
194200
return !empty($this->serializedAccessToken) ? new AccessToken($this->serializedAccessToken) : null;
195201
}
202+
203+
/**
204+
* @return \DateTimeImmutable
205+
*/
206+
public function getExpires(): \DateTimeImmutable
207+
{
208+
return $this->expires;
209+
}
210+
211+
/**
212+
* @param \DateTimeImmutable $expires
213+
*/
214+
public function setExpires(\DateTimeImmutable $expires): void
215+
{
216+
$this->expires = $expires;
217+
}
196218
}

Classes/OAuthClient.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Neos\Flow\Annotations as Flow;
1919
use Neos\Flow\Core\Bootstrap;
2020
use Neos\Flow\Http\HttpRequestHandlerInterface;
21+
use Neos\Flow\Log\Utility\LogEnvironment;
2122
use Neos\Flow\Mvc\ActionRequest;
2223
use Neos\Flow\Mvc\Routing\Exception\MissingActionNameException;
2324
use Neos\Flow\Mvc\Routing\UriBuilder;
@@ -217,20 +218,21 @@ public function getRequestFactory(): RequestFactory
217218
public function requestAccessToken(string $serviceName, string $clientId, string $clientSecret, string $scope, string $grantType, array $additionalParameters = []): void
218219
{
219220
$authorizationId = Authorization::calculateAuthorizationId($serviceName, $clientId, $scope, $grantType);
220-
$this->logger->info(sprintf('OAuth (%s): Retrieving access token using %s grant for client "%s" using a %s bytes long secret. (authorization id: %s)', $this->getServiceType(), $grantType, $clientId, strlen($clientSecret), $authorizationId));
221+
$this->logger->info(sprintf('OAuth (%s): Retrieving access token using %s grant for client "%s" using a %s bytes long secret. (authorization id: %s)', $this->getServiceType(), $grantType, $clientId, strlen($clientSecret), $authorizationId), LogEnvironment::fromMethodName(__METHOD__));
221222

222223
$existingAuthorization = $this->getAuthorization($authorizationId);
223224
if ($existingAuthorization !== null) {
224225
$this->entityManager->remove($existingAuthorization);
225226
$this->entityManager->flush();
226227

227-
$this->logger->info(sprintf('OAuth (%s): Removed old OAuth token for client "%s". (authorization id: %s)', $this->getServiceType(), $clientId, $authorizationId));
228+
$this->logger->info(sprintf('OAuth (%s): Removed old OAuth token for client "%s". (authorization id: %s)', $this->getServiceType(), $clientId, $authorizationId), LogEnvironment::fromMethodName(__METHOD__));
228229
}
229230

230231
$accessToken = $this->createOAuthProvider($clientId, $clientSecret)->getAccessToken($grantType, $additionalParameters);
232+
231233
$authorization = $this->createNewAuthorization($serviceName, $clientId, $scope, $grantType, $accessToken);
232234

233-
$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));
235+
$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__));
234236

235237
$this->entityManager->persist($authorization);
236238
$this->entityManager->flush();
@@ -249,7 +251,7 @@ public function requestAccessToken(string $serviceName, string $clientId, string
249251
public function startAuthorization(string $clientId, string $clientSecret, UriInterface $returnToUri, string $scope): UriInterface
250252
{
251253
$authorization = new Authorization($this->getServiceType(), $clientId, Authorization::GRANT_AUTHORIZATION_CODE, $scope);
252-
$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));
254+
$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), LogEnvironment::fromMethodName(__METHOD__));
253255

254256
try {
255257
$oldAuthorization = $this->entityManager->find(Authorization::class, $authorization->getAuthorizationId());
@@ -361,7 +363,7 @@ public function refreshAuthorization(string $authorizationId, string $clientId,
361363
try {
362364
$accessToken = $oAuthProvider->getAccessToken('refresh_token', ['refresh_token' => $authorization->refreshToken]);
363365
$authorization->accessToken = $accessToken->getToken();
364-
$authorization->expires = ($accessToken->getExpires() ? \DateTimeImmutable::createFromFormat('U', $accessToken->getExpires()) : null);
366+
$authorization->setExpires($accessToken->getExpires() ? \DateTimeImmutable::createFromFormat('U', $accessToken->getExpires()) : null);
365367

366368
$this->logger->debug(sprintf($this->getServiceType() . ': New access token is "%s", refresh token is "%s".', $authorization->accessToken, $authorization->refreshToken));
367369

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
namespace Neos\Flow\Persistence\Doctrine\Migrations;
3+
4+
use Doctrine\Migrations\AbstractMigration;
5+
use Doctrine\DBAL\Schema\Schema;
6+
use Doctrine\DBAL\Migrations\AbortMigrationException;
7+
8+
/**
9+
* Auto-generated Migration: Please modify to your needs! This block will be used as the migration description if getDescription() is not used.
10+
*/
11+
class Version20200716150451 extends AbstractMigration
12+
{
13+
14+
/**
15+
* @return string
16+
*/
17+
public function getDescription(): string
18+
{
19+
return '';
20+
}
21+
22+
/**
23+
* @param Schema $schema
24+
* @return void
25+
* @throws AbortMigrationException
26+
*/
27+
public function up(Schema $schema): void
28+
{
29+
// this up() migration is autogenerated, please modify it to your needs
30+
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on "mysql".');
31+
$this->addSql('ALTER TABLE flownative_oauth2_client_authorization ADD expires DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\'');
32+
}
33+
34+
/**
35+
* @param Schema $schema
36+
* @return void
37+
* @throws AbortMigrationException
38+
*/
39+
public function down(Schema $schema): void
40+
{
41+
// this down() migration is autogenerated, please modify it to your needs
42+
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on "mysql".');
43+
44+
$this->addSql('ALTER TABLE flownative_oauth2_client_authorization DROP expires');
45+
}
46+
}

0 commit comments

Comments
 (0)