Skip to content

Commit 8b1f2d2

Browse files
authored
feat: Add support for multiple environments (#6)
* feat: Add support for multiple environments * docs: Update README to include optional environment parameter for client initialization * fix: Update getAuthServerBaseUri and getResourceServerBaseUri to return string and handle invalid environments * fix: Update expected URL in ClientTest to reflect the correct authentication endpoint * fix: Update expected URL in ClientTest to reflect the correct authentication realm * docs: Fix formatting in README for AUTH_STORAGE_INSTANCE parameter
1 parent db43458 commit 8b1f2d2

File tree

3 files changed

+64
-15
lines changed

3 files changed

+64
-15
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ $client = new Client(
3636
<TSID>, // Trusted Shops ID - e.g. 'X1234567890123456789012345678901'
3737
<CLIENT_ID>, // Client ID - e.g. 'cot-switch-X1234567890123456789012345678901'
3838
<CLIENT_SECRET>, // Client Secret - e.g. '1234567890123456789012345678901234567890123456789012345678901234'
39-
<AUTH_STORAGE_INSTANCE> // It can be any storage option implementing AuthStorageInterface - e.g. new DatabaseAuthStorage()
39+
<AUTH_STORAGE_INSTANCE>, // It can be any storage option implementing AuthStorageInterface - e.g. new DatabaseAuthStorage()
40+
<ENV> // Environment (optional) - dev, qa, or prod, defaults to prod
4041
);
4142

4243
// Invoke handleCallback function to handle code coming from the authentication server

src/Client.php

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@
2929
use TRSTD\COT\Util\EncryptionUtils;
3030
use TRSTD\COT\Util\PKCEUtils;
3131

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-
4032
CacheManager::setDefaultConfig(new ConfigurationOption([
4133
"path" => __DIR__ . "/cache"
4234
]));
@@ -53,6 +45,24 @@ final class Client
5345
private const CONSUMER_ANONYMOUS_DATA_CACHE_KEY = 'CONSUMER_ANONYMOUS_DATA_';
5446
private const CONSUMER_ANONYMOUS_DATA_CACHE_TTL = 3600; // 1 hour
5547

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+
5666
/**
5767
* @var string
5868
*/
@@ -93,9 +103,10 @@ final class Client
93103
* @param string $clientId client ID
94104
* @param string $clientSecret client secret
95105
* @param AuthStorageInterface $authStorage auth storage to store tokens
106+
* @param string $env environment dev, qa, or prod
96107
* @throws RequiredParameterMissingException if any required parameter is missing
97108
*/
98-
public function __construct($tsId, $clientId, $clientSecret, AuthStorageInterface $authStorage = null)
109+
public function __construct($tsId, $clientId, $clientSecret, AuthStorageInterface $authStorage = null, $env = 'prod')
99110
{
100111
if (!$tsId) {
101112
throw new RequiredParameterMissingException('TS ID is required.');
@@ -113,6 +124,9 @@ public function __construct($tsId, $clientId, $clientSecret, AuthStorageInterfac
113124
throw new RequiredParameterMissingException('AuthStorage is required.');
114125
}
115126

127+
$this->authServerBaseUri = $this->getAuthServerBaseUri($env);
128+
$this->resourceServerBaseUri = $this->getResourceServerBaseUri($env);
129+
116130
$this->tsId = $tsId;
117131
$this->clientId = $clientId;
118132
$this->clientSecret = $clientSecret;
@@ -160,7 +174,7 @@ public function getAnonymousConsumerData()
160174
'Authorization: Bearer ' . $accessToken,
161175
];
162176

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]);
164178
$consumerAnonymousData = json_decode($response->getContent());
165179

166180
// cache the consumer anonymous data
@@ -202,6 +216,40 @@ public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool)
202216
$this->cacheItemPool = $cacheItemPool;
203217
}
204218

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+
205253
/**
206254
* @param string $code code to exchange for token
207255
* @return Token|null
@@ -251,7 +299,7 @@ private function getToken($code)
251299
'code_verifier' => $this->getCodeVerifierCookie(),
252300
];
253301

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]);
255303
$responseJson = json_decode($response->getContent());
256304
if (!$responseJson || isset($responseJson->error)) {
257305
return null;
@@ -277,7 +325,7 @@ private function getRefreshedToken($refreshToken)
277325
'refresh_token' => $refreshToken,
278326
];
279327

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]);
281329
$responseJson = json_decode($response->getContent());
282330
if (!$responseJson || isset($responseJson->error)) {
283331
return null;
@@ -402,7 +450,7 @@ private function getJWKS()
402450
$cachedJWKSItem = $this->cacheItemPool->getItem(self::JWKS_CACHE_KEY);
403451

404452
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]);
406454
$jwks = json_decode($response->getContent(), true);
407455
$cachedJWKSItem->set($jwks)->expiresAfter(self::JWKS_CACHE_TTL);
408456
$this->cacheItemPool->save($cachedJWKSItem);

tests/ClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testHandleCallbackWithCode()
7676
$expectedRequests = [
7777
function ($method, $url, $options): MockResponse {
7878
$this->assertSame('POST', $method);
79-
$this->assertSame('https://auth-qa.trustedshops.com/auth/realms/myTS-QA/protocol/openid-connect/token', $url);
79+
$this->assertSame('https://auth.trustedshops.com/auth/realms/myTS/protocol/openid-connect/token', $url);
8080
$this->assertArrayHasKey('body', $options);
8181
$this->assertArrayHasKey('headers', $options);
8282

0 commit comments

Comments
 (0)