15
15
16
16
use Doctrine \ORM \Mapping as ORM ;
17
17
use Exception ;
18
+ use InvalidArgumentException ;
18
19
use JsonException ;
19
20
use League \OAuth2 \Client \Token \AccessToken ;
20
21
use League \OAuth2 \Client \Token \AccessTokenInterface ;
@@ -70,6 +71,17 @@ class Authorization
70
71
*/
71
72
protected $ serializedAccessToken ;
72
73
74
+ /**
75
+ * @var string
76
+ * @ORM\Column(nullable = true, type = "text")
77
+ */
78
+ protected $ encryptedSerializedAccessToken ;
79
+
80
+ /**
81
+ * @var EncryptionService
82
+ */
83
+ protected $ encryptionService ;
84
+
73
85
/**
74
86
* @param string $authorizationId
75
87
* @param string $serviceName
@@ -86,6 +98,14 @@ public function __construct(string $authorizationId, string $serviceName, string
86
98
$ this ->scope = $ scope ;
87
99
}
88
100
101
+ /**
102
+ * @param EncryptionService $encryptionService
103
+ */
104
+ public function injectEncryptionService (EncryptionService $ encryptionService ): void
105
+ {
106
+ $ this ->encryptionService = $ encryptionService ;
107
+ }
108
+
89
109
/**
90
110
* Calculate an authorization identifier (for this model) from the given parameters.
91
111
*
@@ -99,7 +119,7 @@ public static function generateAuthorizationIdForAuthorizationCodeGrant(string $
99
119
{
100
120
try {
101
121
return $ serviceType . '- ' . $ serviceName . '- ' . Uuid::uuid4 ()->toString ();
102
- // @codeCoverageIgnoreStart
122
+ // @codeCoverageIgnoreStart
103
123
} catch (Exception $ e ) {
104
124
throw new OAuthClientException (sprintf ('Failed generating authorization id for %s %s ' , $ serviceName , $ clientId ), 1597311416 , $ e );
105
125
}
@@ -185,18 +205,38 @@ public function setSerializedAccessToken(string $serializedAccessToken): void
185
205
$ this ->serializedAccessToken = $ serializedAccessToken ;
186
206
}
187
207
208
+ /**
209
+ * @return string
210
+ */
211
+ public function getEncryptedSerializedAccessToken (): string
212
+ {
213
+ return $ this ->encryptedSerializedAccessToken ?? '' ;
214
+ }
215
+
216
+ /**
217
+ * @param string $encryptedSerializedAccessToken
218
+ */
219
+ public function setEncryptedSerializedAccessToken (string $ encryptedSerializedAccessToken ): void
220
+ {
221
+ $ this ->encryptedSerializedAccessToken = $ encryptedSerializedAccessToken ;
222
+ }
223
+
188
224
/**
189
225
* @param AccessTokenInterface $accessToken
190
226
* @return void
191
- * @throws \ InvalidArgumentException
227
+ * @throws InvalidArgumentException
192
228
*/
193
229
public function setAccessToken (AccessTokenInterface $ accessToken ): void
194
230
{
195
231
try {
196
- $ this ->serializedAccessToken = json_encode ($ accessToken , JSON_THROW_ON_ERROR , 512 );
232
+ if ($ this ->encryptionService !== null && $ this ->encryptionService ->isConfigured ()) {
233
+ $ this ->encryptedSerializedAccessToken = $ this ->encryptionService ->encryptAndEncode (json_encode ($ accessToken , JSON_THROW_ON_ERROR , 512 ));
234
+ } else {
235
+ $ this ->serializedAccessToken = json_encode ($ accessToken , JSON_THROW_ON_ERROR , 512 );
236
+ }
197
237
// @codeCoverageIgnoreStart
198
- } catch (JsonException $ e ) {
199
- throw new \ InvalidArgumentException ('Failed serializing the given access token ' , 1602515717 );
238
+ } catch (JsonException | Exception $ e ) {
239
+ throw new InvalidArgumentException ('Failed serializing the given access token ' , 1602515717 , $ e );
200
240
// @codeCoverageIgnoreEnd
201
241
}
202
242
}
@@ -206,10 +246,20 @@ public function setAccessToken(AccessTokenInterface $accessToken): void
206
246
*/
207
247
public function getAccessToken (): ?AccessToken
208
248
{
249
+ if (empty ($ this ->serializedAccessToken ) && empty ($ this ->encryptedSerializedAccessToken )) {
250
+ return null ;
251
+ }
252
+ if (!empty ($ this ->encryptedSerializedAccessToken ) && !$ this ->encryptionService ->isConfigured ()) {
253
+ return null ;
254
+ }
209
255
try {
256
+ if (!empty ($ this ->encryptedSerializedAccessToken )) {
257
+ $ deserializedAccessToken = json_decode ($ this ->encryptionService ->decodeAndDecrypt ($ this ->encryptedSerializedAccessToken ), true , 512 , JSON_THROW_ON_ERROR );
258
+ return new AccessToken ($ deserializedAccessToken );
259
+ }
210
260
if (!empty ($ this ->serializedAccessToken )) {
211
- $ unserializedAccessToken = json_decode ($ this ->serializedAccessToken , true , 512 , JSON_THROW_ON_ERROR );
212
- return new AccessToken ($ unserializedAccessToken );
261
+ $ deserializedAccessToken = json_decode ($ this ->serializedAccessToken , true , 512 , JSON_THROW_ON_ERROR );
262
+ return new AccessToken ($ deserializedAccessToken );
213
263
}
214
264
} catch (JsonException $ e ) {
215
265
}
0 commit comments