Skip to content

Commit 97d959e

Browse files
authored
Merge pull request #38 from flownative/task/raise-dependencies
Raise dependencies & code cleanup
2 parents 6699154 + 5f23ecc commit 97d959e

File tree

10 files changed

+86
-338
lines changed

10 files changed

+86
-338
lines changed

Classes/Authorization.php

Lines changed: 9 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
*/
1515

1616
use Doctrine\ORM\Mapping as ORM;
17-
use Exception;
18-
use InvalidArgumentException;
19-
use JsonException;
2017
use League\OAuth2\Client\Token\AccessToken;
2118
use League\OAuth2\Client\Token\AccessTokenInterface;
2219
use Neos\Flow\Annotations as Flow;
@@ -89,13 +86,6 @@ class Authorization
8986
*/
9087
protected $encryptionService;
9188

92-
/**
93-
* @param string $authorizationId
94-
* @param string $serviceName
95-
* @param string $clientId
96-
* @param string $grantType
97-
* @param string $scope
98-
*/
9989
public function __construct(string $authorizationId, string $serviceName, string $clientId, string $grantType, string $scope)
10090
{
10191
$this->authorizationId = $authorizationId;
@@ -105,9 +95,6 @@ public function __construct(string $authorizationId, string $serviceName, string
10595
$this->scope = $scope;
10696
}
10797

108-
/**
109-
* @param EncryptionService $encryptionService
110-
*/
11198
public function injectEncryptionService(EncryptionService $encryptionService): void
11299
{
113100
$this->encryptionService = $encryptionService;
@@ -116,128 +103,84 @@ public function injectEncryptionService(EncryptionService $encryptionService): v
116103
/**
117104
* Calculate an authorization identifier (for this model) from the given parameters.
118105
*
119-
* @param string $serviceType
120-
* @param string $serviceName
121-
* @param string $clientId
122-
* @return string
123106
* @throws OAuthClientException
124107
*/
125108
public static function generateAuthorizationIdForAuthorizationCodeGrant(string $serviceType, string $serviceName, string $clientId): string
126109
{
127110
try {
128111
return $serviceType . '-' . $serviceName . '-' . Uuid::uuid4()->toString();
129112
// @codeCoverageIgnoreStart
130-
} catch (Exception $e) {
113+
} catch (\Exception $e) {
131114
throw new OAuthClientException(sprintf('Failed generating authorization id for %s %s', $serviceName, $clientId), 1597311416, $e);
132115
}
133116
// @codeCoverageIgnoreEnd
134117
}
135118

136119
/**
137120
* Calculate an authorization identifier (for this model) from the given parameters.
138-
*
139-
* @param string $serviceName
140-
* @param string $clientId
141-
* @param string $clientSecret
142-
* @param string $scope
143-
* @param array $additionalParameters
144-
* @return string
145121
*/
146122
public static function generateAuthorizationIdForClientCredentialsGrant(string $serviceName, string $clientId, string $clientSecret, string $scope, array $additionalParameters = []): string
147123
{
148124
try {
149125
$additionalParametersJson = json_encode($additionalParameters, JSON_THROW_ON_ERROR);
150-
} catch (JsonException $e) {
126+
} catch (\JsonException) {
151127
$additionalParametersJson = '';
152128
}
153129
return hash('sha512', $serviceName . $clientId . $clientSecret . $scope . $additionalParametersJson . self::GRANT_CLIENT_CREDENTIALS);
154130
}
155131

156-
/**
157-
* @return string
158-
*/
159132
public function getAuthorizationId(): string
160133
{
161134
return $this->authorizationId;
162135
}
163136

164-
/**
165-
* @return string
166-
*/
167137
public function getServiceName(): string
168138
{
169139
return $this->serviceName;
170140
}
171141

172-
/**
173-
* @return string
174-
*/
175142
public function getClientId(): string
176143
{
177144
return $this->clientId;
178145
}
179146

180-
/**
181-
* @return string
182-
*/
183147
public function getGrantType(): string
184148
{
185149
return $this->grantType;
186150
}
187151

188-
/**
189-
* @return string
190-
* @return void
191-
*/
192152
public function getScope(): string
193153
{
194154
return $this->scope;
195155
}
196156

197-
/**
198-
* @param string $scope
199-
*/
200157
public function setScope(string $scope): void
201158
{
202159
$this->scope = $scope;
203160
}
204161

205-
/**
206-
* @return string
207-
*/
208162
public function getSerializedAccessToken(): string
209163
{
210164
return $this->serializedAccessToken ?? '';
211165
}
212166

213-
/**
214-
* @param string $serializedAccessToken
215-
*/
216167
public function setSerializedAccessToken(string $serializedAccessToken): void
217168
{
218169
$this->serializedAccessToken = $serializedAccessToken;
219170
}
220171

221-
/**
222-
* @return string
223-
*/
224172
public function getEncryptedSerializedAccessToken(): string
225173
{
226174
return $this->encryptedSerializedAccessToken ?? '';
227175
}
228176

229-
/**
230-
* @param string $encryptedSerializedAccessToken
231-
*/
232177
public function setEncryptedSerializedAccessToken(string $encryptedSerializedAccessToken): void
233178
{
234179
$this->encryptedSerializedAccessToken = $encryptedSerializedAccessToken;
235180
}
236181

237182
/**
238-
* @param AccessTokenInterface $accessToken
239-
* @return void
240-
* @throws InvalidArgumentException
183+
* @throws \InvalidArgumentException
241184
*/
242185
public function setAccessToken(AccessTokenInterface $accessToken): void
243186
{
@@ -248,19 +191,19 @@ public function setAccessToken(AccessTokenInterface $accessToken): void
248191

249192
try {
250193
if ($this->encryptionService !== null && $this->encryptionService->isConfigured()) {
251-
$this->encryptedSerializedAccessToken = $this->encryptionService->encryptAndEncode(json_encode($accessToken, JSON_THROW_ON_ERROR, 512));
194+
$this->encryptedSerializedAccessToken = $this->encryptionService->encryptAndEncode(json_encode($accessToken, JSON_THROW_ON_ERROR));
252195
} else {
253-
$this->serializedAccessToken = json_encode($accessToken, JSON_THROW_ON_ERROR, 512);
196+
$this->serializedAccessToken = json_encode($accessToken, JSON_THROW_ON_ERROR);
254197
}
255198
// @codeCoverageIgnoreStart
256-
} catch (JsonException | Exception $e) {
257-
throw new InvalidArgumentException('Failed serializing the given access token', 1602515717, $e);
199+
} catch (\JsonException | \Exception $e) {
200+
throw new \InvalidArgumentException('Failed serializing the given access token', 1602515717, $e);
258201
// @codeCoverageIgnoreEnd
259202
}
260203
}
261204

262205
/**
263-
* @return AccessToken
206+
* @throws \SodiumException
264207
*/
265208
public function getAccessToken(): ?AccessToken
266209
{
@@ -279,22 +222,16 @@ public function getAccessToken(): ?AccessToken
279222
$deserializedAccessToken = json_decode($this->serializedAccessToken, true, 512, JSON_THROW_ON_ERROR);
280223
return new AccessToken($deserializedAccessToken);
281224
}
282-
} catch (JsonException $e) {
225+
} catch (\JsonException) {
283226
}
284227
return null;
285228
}
286229

287-
/**
288-
* @return \DateTimeImmutable
289-
*/
290230
public function getExpires(): ?\DateTimeImmutable
291231
{
292232
return $this->expires;
293233
}
294234

295-
/**
296-
* @param \DateTimeImmutable $expires
297-
*/
298235
public function setExpires(\DateTimeImmutable $expires): void
299236
{
300237
$this->expires = $expires;

Classes/Command/OAuthCommandController.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace Flownative\OAuth2\Client\Command;
35

46
use Doctrine\ORM\EntityManagerInterface as DoctrineEntityManagerInterface;
5-
use Doctrine\ORM\OptimisticLockException;
6-
use Doctrine\ORM\ORMException;
7-
use Exception;
87
use Flownative\OAuth2\Client\Authorization;
98
use Flownative\OAuth2\Client\EncryptionService;
109
use Neos\Flow\Cli\CommandController;
1110
use Neos\Flow\Persistence\Doctrine\Query;
1211

1312
final class OAuthCommandController extends CommandController
1413
{
15-
/**
16-
* @var DoctrineEntityManagerInterface
17-
*/
18-
protected $entityManager;
14+
protected DoctrineEntityManagerInterface $entityManager;
1915

2016
/**
2117
* @param DoctrineEntityManagerInterface $entityManager
@@ -34,6 +30,7 @@ public function injectEntityManager(DoctrineEntityManagerInterface $entityManage
3430
* a hash over service name, client id, grant type and scope.
3531
*
3632
* @return void
33+
* @throws \SodiumException
3734
*/
3835
public function listAuthorizationsCommand(): void
3936
{
@@ -95,8 +92,7 @@ public function removeAuthorizationsCommand(string $id = '', bool $all = false):
9592
}
9693
try {
9794
$this->entityManager->flush();
98-
} catch (OptimisticLockException $e) {
99-
} catch (ORMException $e) {
95+
} catch (\Exception $e) {
10096
$this->outputLine('<error>Failed:</error> ' . $e->getMessage());
10197
exit(1);
10298
}
@@ -111,7 +107,7 @@ public function removeAuthorizationsCommand(string $id = '', bool $all = false):
111107
*
112108
* @param string $construction
113109
* @return void
114-
* @throws Exception
110+
* @throws \Exception
115111
*/
116112
public function generateEncryptionKeyCommand(string $construction = 'ChaCha20-Poly1305-IETF'): void
117113
{

Classes/Controller/OAuthController.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace Flownative\OAuth2\Client\Controller;
35

46
use Flownative\OAuth2\Client\OAuthClient;
57
use Flownative\OAuth2\Client\OAuthClientException;
8+
use GuzzleHttp\Exception\GuzzleException;
69
use GuzzleHttp\Psr7\Uri;
710
use Neos\Flow\Annotations\CompileStatic;
811
use Neos\Flow\Mvc\Controller\ActionController;
@@ -13,10 +16,7 @@
1316

1417
final class OAuthController extends ActionController
1518
{
16-
/**
17-
* @var array
18-
*/
19-
private $serviceTypes;
19+
private array $serviceTypes;
2020

2121
/**
2222
* @return void
@@ -38,6 +38,7 @@ public function initializeObject(): void
3838
* @throws OAuthClientException
3939
* @throws StopActionException
4040
* @throws UnsupportedRequestTypeException
41+
* @throws \DateMalformedStringException
4142
*/
4243
public function startAuthorizationAction(string $clientId, string $clientSecret, Uri $returnToUri, string $serviceType, string $serviceName, string $scope): void
4344
{
@@ -66,6 +67,7 @@ public function startAuthorizationAction(string $clientId, string $clientSecret,
6667
* @throws OAuthClientException
6768
* @throws StopActionException
6869
* @throws UnsupportedRequestTypeException
70+
* @throws GuzzleException
6971
*/
7072
public function finishAuthorizationAction(string $serviceType, string $serviceName, string $state, string $code, string $scope = ''): void
7173
{

Classes/EncryptionService.php

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace Flownative\OAuth2\Client;
45

5-
use Exception;
66
use Neos\Flow\Annotations as Flow;
77

88
/**
@@ -12,18 +12,11 @@ class EncryptionService {
1212

1313
/**
1414
* @Flow\InjectConfiguration(path="encryption.base64EncodedKey")
15-
* @var string
1615
*/
17-
protected $base64EncodedKey;
16+
protected string $base64EncodedKey = '';
1817

19-
/**
20-
* @var string
21-
*/
22-
protected $key;
18+
protected string $key;
2319

24-
/**
25-
* @return void
26-
*/
2720
public function initializeObject(): void
2821
{
2922
$this->key = base64_decode($this->base64EncodedKey, true);
@@ -32,17 +25,11 @@ public function initializeObject(): void
3225
}
3326
}
3427

35-
/**
36-
* @param string $key
37-
*/
3828
public function setKey(string $key): void
3929
{
4030
$this->key = $key;
4131
}
4232

43-
/**
44-
* @return bool
45-
*/
4633
public function isConfigured(): bool
4734
{
4835
return !empty($this->key);
@@ -52,9 +39,7 @@ public function isConfigured(): bool
5239
* Encrypts the given data using the configured encryption method and returns a string
5340
* containing the construction name and the base64-encoded nonce and encrypted data.
5441
*
55-
* @param string $data Data to encrypt
56-
* @return string Encoded, encrypted data, suitable for storage (e.g. in the database)
57-
* @throws Exception
42+
* @throws \Exception
5843
*/
5944
public function encryptAndEncode(string $data): string
6045
{
@@ -73,12 +58,12 @@ public function encryptAndEncode(string $data): string
7358
* Decrypts the given encoded and encrypted data using the configured encryption method
7459
* and returns the decrypted data.
7560
*
76-
* @param string $encodedAndEncryptedData The data originally created by encryptAndEncode()
77-
* @return string Decrypted data
61+
* @throws \SodiumException
62+
* @see encryptAndEncode()
7863
*/
7964
public function decodeAndDecrypt(string $encodedAndEncryptedData): string
8065
{
81-
list($construction, $encodedNonce, $encodedEncryptedSerializedAccessToken) = explode('$', $encodedAndEncryptedData);
66+
[$construction, $encodedNonce, $encodedEncryptedSerializedAccessToken] = explode('$', $encodedAndEncryptedData);
8267
if ($construction !== 'ChaCha20-Poly1305-IETF') {
8368
throw new \RuntimeException(sprintf('Failed decrypting serialized access token: unsupported AEAD construction "%s"', $construction), 1604938723);
8469
}
@@ -93,12 +78,10 @@ public function decodeAndDecrypt(string $encodedAndEncryptedData): string
9378
}
9479

9580
/**
96-
* @return string
97-
* @throws Exception
81+
* @throws \Exception
9882
*/
9983
public function generateEncryptionKey(): string
10084
{
10185
return sodium_crypto_aead_chacha20poly1305_ietf_keygen();
10286
}
103-
10487
}

0 commit comments

Comments
 (0)