29
29
use TRSTD \COT \Util \EncryptionUtils ;
30
30
use TRSTD \COT \Util \PKCEUtils ;
31
31
32
- if (!defined ('AUTH_SERVER_BASE_URI ' )) {
33
- define ('AUTH_SERVER_BASE_URI ' , 'https://auth-qa.trustedshops.com/auth/realms/myTS-QA/protocol/openid-connect/ ' );
34
- }
35
-
36
- if (!defined ('RESOURCE_SERVER_BASE_URI ' )) {
37
- define ('RESOURCE_SERVER_BASE_URI ' , 'https://scoped-cns-data.consumer-account-test.trustedshops.com/api/v1/ ' );
38
- }
39
-
40
32
CacheManager::setDefaultConfig (new ConfigurationOption ([
41
33
"path " => __DIR__ . "/cache "
42
34
]));
@@ -53,6 +45,24 @@ final class Client
53
45
private const CONSUMER_ANONYMOUS_DATA_CACHE_KEY = 'CONSUMER_ANONYMOUS_DATA_ ' ;
54
46
private const CONSUMER_ANONYMOUS_DATA_CACHE_TTL = 3600 ; // 1 hour
55
47
48
+ private const AUTH_SERVER_BASE_URI_DEV = 'https://auth-integr.trustedshops.com/auth/realms/myTS-DEV/protocol/openid-connect/ ' ;
49
+ private const AUTH_SERVER_BASE_URI_QA = 'https://auth-qa.trustedshops.com/auth/realms/myTS-QA/protocol/openid-connect/ ' ;
50
+ private const AUTH_SERVER_BASE_URI_PROD = 'https://auth.trustedshops.com/auth/realms/myTS/protocol/openid-connect/ ' ;
51
+
52
+ private const RESOURCE_SERVER_BASE_URI_DEV = 'https://scoped-cns-data.consumer-account-dev.trustedshops.com/api/v1/ ' ;
53
+ private const RESOURCE_SERVER_BASE_URI_QA = 'https://scoped-cns-data.consumer-account-test.trustedshops.com/api/v1/ ' ;
54
+ private const RESOURCE_SERVER_BASE_URI_PROD = 'https://scoped-cns-data.consumer-account.trustedshops.com/api/v1/ ' ;
55
+
56
+ /**
57
+ * @var string
58
+ */
59
+ private $ authServerBaseUri ;
60
+
61
+ /**
62
+ * @var string
63
+ */
64
+ private $ resourceServerBaseUri ;
65
+
56
66
/**
57
67
* @var string
58
68
*/
@@ -93,9 +103,10 @@ final class Client
93
103
* @param string $clientId client ID
94
104
* @param string $clientSecret client secret
95
105
* @param AuthStorageInterface $authStorage auth storage to store tokens
106
+ * @param string $env environment dev, qa, or prod
96
107
* @throws RequiredParameterMissingException if any required parameter is missing
97
108
*/
98
- public function __construct ($ tsId , $ clientId , $ clientSecret , AuthStorageInterface $ authStorage = null )
109
+ public function __construct ($ tsId , $ clientId , $ clientSecret , AuthStorageInterface $ authStorage = null , $ env = ' prod ' )
99
110
{
100
111
if (!$ tsId ) {
101
112
throw new RequiredParameterMissingException ('TS ID is required. ' );
@@ -113,6 +124,9 @@ public function __construct($tsId, $clientId, $clientSecret, AuthStorageInterfac
113
124
throw new RequiredParameterMissingException ('AuthStorage is required. ' );
114
125
}
115
126
127
+ $ this ->authServerBaseUri = $ this ->getAuthServerBaseUri ($ env );
128
+ $ this ->resourceServerBaseUri = $ this ->getResourceServerBaseUri ($ env );
129
+
116
130
$ this ->tsId = $ tsId ;
117
131
$ this ->clientId = $ clientId ;
118
132
$ this ->clientSecret = $ clientSecret ;
@@ -160,7 +174,7 @@ public function getAnonymousConsumerData()
160
174
'Authorization: Bearer ' . $ accessToken ,
161
175
];
162
176
163
- $ response = $ this ->httpClient ->request ("GET " , "anonymous-data " . ($ this ->tsId ? "?shopId= " . $ this ->tsId : "" ), ['headers ' => $ headers , 'base_uri ' => RESOURCE_SERVER_BASE_URI ]);
177
+ $ response = $ this ->httpClient ->request ("GET " , "anonymous-data " . ($ this ->tsId ? "?shopId= " . $ this ->tsId : "" ), ['headers ' => $ headers , 'base_uri ' => $ this -> resourceServerBaseUri ]);
164
178
$ consumerAnonymousData = json_decode ($ response ->getContent ());
165
179
166
180
// cache the consumer anonymous data
@@ -202,6 +216,40 @@ public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool)
202
216
$ this ->cacheItemPool = $ cacheItemPool ;
203
217
}
204
218
219
+ /**
220
+ * @param string $env environment dev, qa, or prod
221
+ * @return string
222
+ */
223
+ private function getAuthServerBaseUri ($ env = 'prod ' )
224
+ {
225
+ if ($ env === 'dev ' ) {
226
+ return self ::AUTH_SERVER_BASE_URI_DEV ;
227
+ } elseif ($ env === 'qa ' ) {
228
+ return self ::AUTH_SERVER_BASE_URI_QA ;
229
+ } elseif ($ env === 'prod ' ) {
230
+ return self ::AUTH_SERVER_BASE_URI_PROD ;
231
+ }
232
+
233
+ throw new Exception ("Invalid environment. " );
234
+ }
235
+
236
+ /**
237
+ * @param string $env environment dev, qa, or prod
238
+ * @return string
239
+ */
240
+ private function getResourceServerBaseUri ($ env = 'prod ' )
241
+ {
242
+ if ($ env === 'dev ' ) {
243
+ return self ::RESOURCE_SERVER_BASE_URI_DEV ;
244
+ } elseif ($ env === 'qa ' ) {
245
+ return self ::RESOURCE_SERVER_BASE_URI_QA ;
246
+ } elseif ($ env === 'prod ' ) {
247
+ return self ::RESOURCE_SERVER_BASE_URI_PROD ;
248
+ }
249
+
250
+ throw new Exception ("Invalid environment. " );
251
+ }
252
+
205
253
/**
206
254
* @param string $code code to exchange for token
207
255
* @return Token|null
@@ -251,7 +299,7 @@ private function getToken($code)
251
299
'code_verifier ' => $ this ->getCodeVerifierCookie (),
252
300
];
253
301
254
- $ response = $ this ->httpClient ->request ("POST " , "token " , ['headers ' => $ headers , 'body ' => $ data , 'base_uri ' => AUTH_SERVER_BASE_URI ]);
302
+ $ response = $ this ->httpClient ->request ("POST " , "token " , ['headers ' => $ headers , 'body ' => $ data , 'base_uri ' => $ this -> authServerBaseUri ]);
255
303
$ responseJson = json_decode ($ response ->getContent ());
256
304
if (!$ responseJson || isset ($ responseJson ->error )) {
257
305
return null ;
@@ -277,7 +325,7 @@ private function getRefreshedToken($refreshToken)
277
325
'refresh_token ' => $ refreshToken ,
278
326
];
279
327
280
- $ response = $ this ->httpClient ->request ("POST " , "token " , ['headers ' => $ headers , 'body ' => $ data , 'base_uri ' => AUTH_SERVER_BASE_URI ]);
328
+ $ response = $ this ->httpClient ->request ("POST " , "token " , ['headers ' => $ headers , 'body ' => $ data , 'base_uri ' => $ this -> authServerBaseUri ]);
281
329
$ responseJson = json_decode ($ response ->getContent ());
282
330
if (!$ responseJson || isset ($ responseJson ->error )) {
283
331
return null ;
@@ -402,7 +450,7 @@ private function getJWKS()
402
450
$ cachedJWKSItem = $ this ->cacheItemPool ->getItem (self ::JWKS_CACHE_KEY );
403
451
404
452
if (!$ cachedJWKSItem ->isHit ()) {
405
- $ response = $ this ->httpClient ->request ("GET " , "certs " , ['base_uri ' => AUTH_SERVER_BASE_URI ]);
453
+ $ response = $ this ->httpClient ->request ("GET " , "certs " , ['base_uri ' => $ this -> authServerBaseUri ]);
406
454
$ jwks = json_decode ($ response ->getContent (), true );
407
455
$ cachedJWKSItem ->set ($ jwks )->expiresAfter (self ::JWKS_CACHE_TTL );
408
456
$ this ->cacheItemPool ->save ($ cachedJWKSItem );
0 commit comments