Skip to content

Commit c1f0182

Browse files
committed
Refactor to use PSR logger
1 parent 93fdd28 commit c1f0182

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

Classes/OAuthClient.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
use Neos\Flow\Http\HttpRequestHandlerInterface;
2323
use Neos\Flow\Http\Request;
2424
use Neos\Flow\Http\Uri;
25-
use Neos\Flow\Log\SystemLoggerInterface;
25+
use Neos\Flow\Log\PsrSystemLoggerInterface;
26+
use Neos\Flow\Log\Utility\LogEnvironment;
2627
use Neos\Flow\Mvc\ActionRequest;
2728
use Neos\Flow\Mvc\Routing\Exception\MissingActionNameException;
2829
use Neos\Flow\Mvc\Routing\UriBuilder;
@@ -77,7 +78,7 @@ abstract class OAuthClient
7778

7879
/**
7980
* @Flow\Inject
80-
* @var SystemLoggerInterface
81+
* @var PsrSystemLoggerInterface
8182
*/
8283
protected $logger;
8384

@@ -136,7 +137,7 @@ abstract public function getBaseUri(): string;
136137
/**
137138
* Returns the current client id (for sending authenticated requests)
138139
*
139-
* @return string The client id which is known by the OAuth2 server
140+
* @return string The client id which is known by the OAuth server
140141
*/
141142
abstract public function getClientId(): string;
142143

@@ -202,21 +203,22 @@ public function addClientCredentials(string $clientId, string $clientSecret, str
202203
{
203204
$oAuthProvider = $this->createOAuthProvider($clientId, $clientSecret);
204205

206+
205207
try {
206-
$this->logger->log(sprintf($this->getServiceType() . 'Setting client credentials for client "%s" using a %s bytes long secret.', $clientId, strlen($clientSecret)), LOG_INFO);
208+
$this->logger->info(sprintf('OAuth (%s): Retrieving client credentials for client "%s" using a %s bytes long secret.', $this->getServiceType(), $clientId, strlen($clientSecret)), LogEnvironment::fromMethodName(__METHOD__));
207209

208-
$oldOAuthToken = $this->getAuthorization();
210+
$oldOAuthToken = $this->getAuthorization($authorizationId);
209211
if ($oldOAuthToken !== null) {
210212
$this->entityManager->remove($oldOAuthToken);
211213
$this->entityManager->flush();
212214

213-
$this->logger->log(sprintf($this->getServiceType() . 'Removed old OAuth token for client "%s".', $clientId), LOG_INFO);
215+
$this->logger->info(sprintf('OAuth (%s): Removed old OAuth token for client "%s".', $this->getServiceType(), $clientId), LogEnvironment::fromMethodName(__METHOD__));
214216
}
215217

216218
$accessToken = $oAuthProvider->getAccessToken('client_credentials');
217219
$authorization = $this->createNewAuthorization($authorizationId, $clientId, $clientSecret, 'client_credentials', $accessToken, $scope);
218220

219-
$this->logger->log(sprintf($this->getServiceType() . 'Persisted new OAuth authorization %s for client "%s" with expiry time %s.', $authorizationId, $clientId, $accessToken->getExpires()), LOG_INFO);
221+
$this->logger->info(sprintf('OAuth (%s): Persisted new OAuth authorization %s for client "%s" with expiry time %s.', $this->getServiceType(), $authorizationId, $clientId, $accessToken->getExpires()), LogEnvironment::fromMethodName(__METHOD__));
220222

221223
$this->entityManager->persist($authorization);
222224
$this->entityManager->flush();
@@ -226,7 +228,7 @@ public function addClientCredentials(string $clientId, string $clientSecret, str
226228
}
227229

228230
/**
229-
* Start OAuth authorization
231+
* Start OAuth authorization with the Authorization Code Flow
230232
*
231233
* @param string $clientId The client id, as provided by the OAuth server
232234
* @param string $clientSecret The client secret, provided by the OAuth server
@@ -251,10 +253,10 @@ public function startAuthorization(string $clientId, string $clientSecret, Uri $
251253
]
252254
);
253255
} catch (Exception $exception) {
254-
throw new OAuthClientException(sprintf('Failed setting cache entry for OAuth2 authorization: %s', $exception->getMessage()), 1560178858);
256+
throw new OAuthClientException(sprintf('Failed setting cache entry for OAuth authorization: %s', $exception->getMessage()), 1560178858);
255257
}
256258

257-
$this->logger->log(sprintf('Flownative OAuth2 Client (%s): Starting authorization %s using client id "%s" and a %s bytes long secret, returning to "%s".', $this->getServiceType(), $stateIdentifier, $clientId, strlen($clientSecret), $returnToUri), LOG_INFO);
259+
$this->logger->info(sprintf('OAuth (%s): Starting authorization %s using client id "%s" and a %s bytes long secret, returning to "%s".', $this->getServiceType(), $stateIdentifier, $clientId, strlen($clientSecret), $returnToUri), LogEnvironment::fromMethodName(__METHOD__));
258260
return $authorizationUri;
259261
}
260262

@@ -274,27 +276,27 @@ public function finishAuthorization(string $code, string $stateIdentifier, strin
274276
{
275277
$stateFromCache = $this->stateCache->get($stateIdentifier);
276278
if (empty($stateFromCache)) {
277-
throw new OAuthClientException(sprintf('Flownative OAuth2 Client: Finishing authorization failed because oAuth state %s could not be retrieved from the state cache.', $stateIdentifier), 1558956494);
279+
throw new OAuthClientException(sprintf('OAuth2: Finishing authorization failed because oAuth state %s could not be retrieved from the state cache.', $stateIdentifier), 1558956494);
278280
}
279281

280282
$clientId = $stateFromCache['clientId'];
281283
$clientSecret = $stateFromCache['clientSecret'];
282284
$oAuthProvider = $this->createOAuthProvider($clientId, $clientSecret);
283285

284286
try {
285-
$this->logger->log(sprintf('Flownative OAuth2 Client (%s): Finishing authorization for client "%s", state "%s", using a %s bytes long secret.', $this->getServiceType(), $clientId, $stateIdentifier, strlen($clientSecret)), LOG_INFO);
287+
$this->logger->info(sprintf('OAuth (%s): Finishing authorization for client "%s", state "%s", using a %s bytes long secret.', $this->getServiceType(), $clientId, $stateIdentifier, strlen($clientSecret)), LogEnvironment::fromMethodName(__METHOD__));
286288

287289
$oldOAuthToken = $this->entityManager->find(Authorization::class, ['authorizationId' => $stateIdentifier]);
288290
if ($oldOAuthToken !== null) {
289291
$this->entityManager->remove($oldOAuthToken);
290292
$this->entityManager->flush();
291293

292-
$this->logger->log(sprintf($this->getServiceType() . ': Removed old OAuth token "%s".', $stateIdentifier), LOG_INFO);
294+
$this->logger->info(sprintf('OAuth (%s): Removed old OAuth token "%s".', $this->getServiceType(), $stateIdentifier), LogEnvironment::fromMethodName(__METHOD__));
293295
}
294296
$accessToken = $oAuthProvider->getAccessToken('authorization_code', ['code' => $code]);
295297
$authorization = $this->createNewAuthorization($stateIdentifier, $clientId, $clientSecret, 'authorization_code', $accessToken, $scope);
296298

297-
$this->logger->log(sprintf('Flownative OAuth2 Client: Persisted new OAuth token for authorization "%s" with expiry time %s.', $stateIdentifier, $accessToken->getExpires()), LOG_INFO);
299+
$this->logger->info(sprintf('OAuth (%s): Persisted new OAuth token for authorization "%s" with expiry time %s.', $this->getServiceType(), $stateIdentifier, $accessToken->getExpires()), LogEnvironment::fromMethodName(__METHOD__));
298300

299301
$this->entityManager->persist($authorization);
300302
$this->entityManager->flush();
@@ -322,18 +324,18 @@ public function refreshAuthorization(string $authorizationId, string $clientId,
322324
{
323325
$authorization = $this->entityManager->find(Authorization::class, ['authorizationId' => $authorizationId]);
324326
if (!$authorization instanceof Authorization) {
325-
throw new OAuthClientException(sprintf('Flownative OAuth2 Client: Could not refresh OAuth2 token because authorization %s was not found in our database.', $authorization), 1505317044316);
327+
throw new OAuthClientException(sprintf('OAuth2: Could not refresh OAuth token because authorization %s was not found in our database.', $authorization), 1505317044316);
326328
}
327329
$oAuthProvider = $this->createOAuthProvider($authorizationId, $clientId, $authorization->clientSecret);
328330

329-
$this->logger->log(sprintf($this->getServiceType() . ': Refreshing authorization %s for client "%s" using a %s bytes long secret and refresh token "%s".', $authorizationId, $clientId, strlen($authorization->clientSecret), $authorization->refreshToken), LOG_INFO);
331+
$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->clientSecret), $authorization->refreshToken), LogEnvironment::fromMethodName(__METHOD__));
330332

331333
try {
332334
$accessToken = $oAuthProvider->getAccessToken('refresh_token', ['refresh_token' => $authorization->refreshToken]);
333335
$authorization->accessToken = $accessToken->getToken();
334336
$authorization->expires = ($accessToken->getExpires() ? \DateTimeImmutable::createFromFormat('U', $accessToken->getExpires()) : null);
335337

336-
$this->logger->log(sprintf($this->getServiceType() . ': New access token is "%s", refresh token is "%s".', $authorization->accessToken, $authorization->refreshToken), LOG_DEBUG);
338+
$this->logger->debug(sprintf($this->getServiceType() . ': New access token is "%s", refresh token is "%s".', $authorization->accessToken, $authorization->refreshToken), LogEnvironment::fromMethodName(__METHOD__));
337339

338340
$this->entityManager->persist($authorization);
339341
$this->entityManager->flush();
@@ -389,14 +391,14 @@ public function getAuthenticatedRequest(string $relativeUri, string $method = 'G
389391
try {
390392
$newAccessToken = $oAuthProvider->getAccessToken('client_credentials');
391393
} catch (IdentityProviderException $exception) {
392-
$this->logger->log(sprintf($this->getServiceType() . 'Failed retrieving new OAuth access token for client "%s" (client credentials grant): %s', $oAuthToken->clientId, $exception->getMessage()), LOG_ERR);
394+
$this->logger->error(sprintf($this->getServiceType() . 'Failed retrieving new OAuth access token for client "%s" (client credentials grant): %s', $oAuthToken->clientId, $exception->getMessage()), LogEnvironment::fromMethodName(__METHOD__));
393395
throw $exception;
394396
}
395397

396398
$oAuthToken->accessToken = $newAccessToken->getToken();
397399
$oAuthToken->expires = ($newAccessToken->getExpires() ? \DateTimeImmutable::createFromFormat('U', $newAccessToken->getExpires()) : null);
398400

399-
$this->logger->log(sprintf($this->getServiceType() . 'Persisted new OAuth token for client "%s" with expiry time %s.', $oAuthToken->clientId, $newAccessToken->getExpires()), LOG_INFO);
401+
$this->logger->info(sprintf('OAuth (%s): Persisted new OAuth token for client "%s" with expiry time %s.', $this->getServiceType(), $oAuthToken->clientId, $newAccessToken->getExpires()), LogEnvironment::fromMethodName(__METHOD__));
400402

401403
$this->entityManager->persist($oAuthToken);
402404
$this->entityManager->flush();

0 commit comments

Comments
 (0)