6
6
use Firebase \JWT \ExpiredException ;
7
7
use Firebase \JWT \JWT ;
8
8
use Firebase \JWT \JWK ;
9
+
9
10
use Symfony \Component \HttpClient \HttpClient ;
11
+ use Symfony \Contracts \HttpClient \HttpClientInterface ;
10
12
use Symfony \Component \HttpClient \RetryableHttpClient ;
13
+ use Phpfastcache \CacheManager ;
14
+ use Phpfastcache \Core \Pool \ExtendedCacheItemPoolInterface ;
15
+ use Phpfastcache \Config \ConfigurationOption ;
11
16
12
17
use TRSTD \COT \Logger ;
13
18
use TRSTD \COT \AuthStorage ;
27
32
define ('RESOURCE_SERVER_BASE_URI ' , 'https://scoped-cns-data.consumer-account-test.trustedshops.com/api/v1/ ' );
28
33
}
29
34
35
+ CacheManager::setDefaultConfig (new ConfigurationOption ([
36
+ "path " => __DIR__ . "/cache "
37
+ ]));
38
+
30
39
class Client
31
40
{
32
41
private static $ identityCookie = 'TRSTD_ID_TOKEN ' ;
@@ -59,23 +68,28 @@ class Client
59
68
private $ logger ;
60
69
61
70
/**
62
- * @var RetryableHttpClient
71
+ * @var HttpClientInterface
63
72
*/
64
73
private $ authHttpClient ;
65
74
66
75
/**
67
- * @var RetryableHttpClient
76
+ * @var HttpClientInterface
68
77
*/
69
78
private $ resourceHttpClient ;
70
79
80
+ /**
81
+ * @var ExtendedCacheItemPoolInterface
82
+ */
83
+ private $ cacheItemPool ;
84
+
71
85
/**
72
86
* @param string $tsId TS ID
73
87
* @param string $clientId client ID
74
88
* @param string $clientSecret client secret
75
89
* @param AuthStorage $authStorage auth storage instance
76
90
* @throws RequiredParameterMissingException if any required parameter is missing
77
91
*/
78
- public function __construct ($ tsId , $ clientId , $ clientSecret , $ authStorage )
92
+ public function __construct ($ tsId , $ clientId , $ clientSecret , AuthStorage $ authStorage )
79
93
{
80
94
if (!$ tsId ) {
81
95
throw new RequiredParameterMissingException ('TS ID is required. ' );
@@ -99,13 +113,10 @@ public function __construct($tsId, $clientId, $clientSecret, $authStorage)
99
113
$ this ->authStorage = $ authStorage ;
100
114
$ this ->logger = new Logger ();
101
115
102
- $ this ->authHttpClient = new RetryableHttpClient (HttpClient::create ()->withOptions ([
103
- 'base_uri ' => AUTH_SERVER_BASE_URI ,
104
- ]));
116
+ $ this ->authHttpClient = HttpClient::createForBaseUri (AUTH_SERVER_BASE_URI );
117
+ $ this ->resourceHttpClient = HttpClient::createForBaseUri (RESOURCE_SERVER_BASE_URI );
105
118
106
- $ this ->resourceHttpClient = new RetryableHttpClient (HttpClient::create ()->withOptions ([
107
- 'base_uri ' => RESOURCE_SERVER_BASE_URI ,
108
- ]));
119
+ $ this ->cacheItemPool = CacheManager::getInstance ('files ' );
109
120
}
110
121
111
122
/**
@@ -136,18 +147,22 @@ public function getConnectedConsumerAnonymousData()
136
147
return null ;
137
148
}
138
149
150
+ $ cachedConsumerAnonymousDataItem = $ this ->cacheItemPool ->getItem ('consumer_anonymous_data ' );
151
+ if ($ cachedConsumerAnonymousDataItem ->isHit ()) {
152
+ return $ cachedConsumerAnonymousDataItem ->get ();
153
+ }
154
+
139
155
$ headers = [
140
156
'Content-Type: application/json ' ,
141
157
'Authorization: Bearer ' . $ accessToken ,
142
158
];
143
159
144
160
$ response = $ this ->resourceHttpClient ->request ("GET " , "anonymous-data " . ($ this ->tsId ? "?shopId= " . $ this ->tsId : "" ), ['headers ' => $ headers ]);
161
+ $ consumerAnonymousData = json_decode ($ response ->getContent ());
162
+ $ cachedConsumerAnonymousDataItem ->set ($ consumerAnonymousData )->expiresAfter (5 );
163
+ $ this ->cacheItemPool ->save ($ cachedConsumerAnonymousDataItem );
145
164
146
- if (200 !== $ response ->getStatusCode ()) {
147
- return null ;
148
- }
149
-
150
- return json_decode ($ response ->getContent ());
165
+ return $ consumerAnonymousData ;
151
166
} catch (Exception $ ex ) {
152
167
$ this ->logger ->error ($ ex ->getMessage ());
153
168
return null ;
@@ -177,14 +192,14 @@ private function disconnect()
177
192
{
178
193
if (isset ($ _COOKIE [self ::$ identityCookie ])) {
179
194
$ idToken = $ _COOKIE [self ::$ identityCookie ];
180
- $ decodedToken = $ this ->decodeToken ($ idToken );
195
+ $ decodedToken = $ this ->decodeToken ($ idToken, false );
181
196
$ this ->authStorage ->remove ($ decodedToken ->ctc_id );
182
197
$ this ->removeIdentityCookie ();
183
198
}
184
199
}
185
200
186
201
/**
187
- * @param string $codecode to get token
202
+ * @param string $code code to get token
188
203
* @return Token|null
189
204
*/
190
205
private function getToken ($ code )
@@ -203,11 +218,6 @@ private function getToken($code)
203
218
];
204
219
205
220
$ response = $ this ->authHttpClient ->request ("POST " , "token " , ['headers ' => $ headers , 'body ' => $ data ]);
206
-
207
- if (201 !== $ response ->getStatusCode ()) {
208
- return null ;
209
- }
210
-
211
221
$ responseJson = json_decode ($ response ->getContent ());
212
222
if (!$ responseJson || isset ($ responseJson ->error )) {
213
223
return null ;
@@ -234,11 +244,6 @@ private function getRefreshedToken($refreshToken)
234
244
];
235
245
236
246
$ response = $ this ->authHttpClient ->request ("POST " , "token " , ['headers ' => $ headers , 'body ' => $ data ]);
237
-
238
- if (201 !== $ response ->getStatusCode ()) {
239
- return null ;
240
- }
241
-
242
247
$ responseJson = json_decode ($ response ->getContent ());
243
248
if (!$ responseJson || isset ($ responseJson ->error )) {
244
249
return null ;
@@ -302,7 +307,7 @@ private function getOrRefreshAccessToken($idToken)
302
307
private function setTokenOnStorage (Token $ token )
303
308
{
304
309
try {
305
- $ decodedToken = $ this ->decodeToken ($ token ->idToken );
310
+ $ decodedToken = $ this ->decodeToken ($ token ->idToken , false );
306
311
$ this ->authStorage ->set ($ token , $ decodedToken ->ctc_id );
307
312
} catch (ExpiredException $ ex ) {
308
313
$ this ->logger ->debug ('id token is expired. returning... ' );
@@ -319,7 +324,7 @@ private function setTokenOnStorage(Token $token)
319
324
private function getTokenFromStorage ($ idToken )
320
325
{
321
326
try {
322
- $ decodedToken = $ this ->decodeToken ($ idToken );
327
+ $ decodedToken = $ this ->decodeToken ($ idToken, false );
323
328
return $ this ->authStorage ->getByCtcId ($ decodedToken ->ctc_id );
324
329
} catch (ExpiredException $ ex ) {
325
330
$ this ->logger ->debug ('id token is expired. returning... ' );
@@ -331,9 +336,14 @@ private function getTokenFromStorage($idToken)
331
336
return null ;
332
337
}
333
338
334
- private function decodeToken ($ token )
339
+ private function decodeToken ($ token, $ validateExp = true )
335
340
{
336
341
try {
342
+ if (!$ validateExp ) {
343
+ $ tks = explode ('. ' , $ token );
344
+ return JWT ::jsonDecode (JWT ::urlsafeB64Decode ($ tks [1 ]));
345
+ }
346
+
337
347
return JWT ::decode ($ token , $ this ->getJWKS ());
338
348
} catch (Exception $ ex ) {
339
349
$ this ->logger ->error ($ ex ->getMessage ());
@@ -343,15 +353,18 @@ private function decodeToken($token)
343
353
344
354
private function getJWKS ()
345
355
{
346
- $ response = $ this ->authHttpClient -> request ( " GET " , " certs " );
356
+ $ cachedJWKSItem = $ this ->cacheItemPool -> getItem ( ' jwks ' );
347
357
348
- if (200 !== $ response ->getStatusCode ()) {
349
- return null ;
358
+ if (!$ cachedJWKSItem ->isHit ()) {
359
+ $ response = $ this ->authHttpClient ->request ("GET " , "certs " );
360
+ $ jwks = json_decode ($ response ->getContent (), true );
361
+ $ this ->cacheItemPool ->getItem ('jwks ' )->set ($ jwks )->expiresAfter (3600 );
362
+ $ this ->cacheItemPool ->save ($ cachedJWKSItem );
350
363
}
351
364
352
- $ responseJson = json_decode ( $ response -> getContent () );
365
+ $ jwks = $ cachedJWKSItem -> get ( );
353
366
354
- return JWK ::parseKeySet ($ responseJson -> keys );
367
+ return JWK ::parseKeySet ($ jwks );
355
368
}
356
369
357
370
/**
0 commit comments