Skip to content

Commit bcfdae7

Browse files
authored
[SDK-3633] Treat passing an empty string to SdkConfiguration as the default undefined value type of NULL (#643)
* [SDK-3633] Treat passing an empty string to SdkConfiguration as the default undefined value type of NULL * Adjustments to tests and domain validator to accommodate change * fix: Psalm/Stan linter warnings * tests: Update to use properly formatted domains * fix: Rector linter warnings * fix: Rector linter fix breaking unit tests
1 parent 89b8d8f commit bcfdae7

File tree

12 files changed

+66
-39
lines changed

12 files changed

+66
-39
lines changed

rector.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
56
use Rector\Config\RectorConfig;
67
use Rector\Core\ValueObject\PhpVersion;
78
use Rector\Php74\Rector\Property\TypedPropertyRector;
@@ -14,6 +15,13 @@
1415
SetList::CODE_QUALITY,
1516
]);
1617

18+
$rectorConfig->skip([
19+
CompleteDynamicPropertiesRector::class => [
20+
// Breaks PEST
21+
__DIR__ . '/tests/Utilities/MockApi.php'
22+
]
23+
]);
24+
1725
$rectorConfig->rule(TypedPropertyRector::class);
1826

1927
$rectorConfig->phpVersion(PhpVersion::PHP_74);

src/Configuration/SdkConfiguration.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -473,18 +473,19 @@ private function onStateChange(
473473
throw \Auth0\SDK\Exception\ConfigurationException::validationFailed($propertyName);
474474
}
475475

476-
if ($propertyName === 'cookieSecret') {
477-
if (is_string($propertyValue) && mb_strlen($propertyValue) !== 0) {
478-
return $propertyValue;
479-
}
476+
if ($propertyName === 'domain' || $propertyName === 'customDomain') {
477+
if (is_string($propertyValue)) {
478+
$propertyValue = trim($propertyValue);
480479

481-
throw \Auth0\SDK\Exception\ConfigurationException::validationFailed($propertyName);
482-
}
480+
if (strlen($propertyValue) !== 0) {
481+
$host = preg_replace('#^[^:/.]*[:/]+#i', '', $propertyValue);
482+
$host = parse_url('https://' . $host, PHP_URL_HOST);
483+
$host = filter_var($host, FILTER_SANITIZE_URL, FILTER_NULL_ON_FAILURE);
483484

484-
if ($propertyName === 'domain' || $propertyName === 'customDomain') {
485-
if (is_string($propertyValue) && mb_strlen($propertyValue) !== 0) {
486-
$host = parse_url($propertyValue, PHP_URL_HOST);
487-
return $host ?? $propertyValue;
485+
if (is_string($host) && strlen($host) !== 0 && filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) !== false) {
486+
return $host;
487+
}
488+
}
488489
}
489490

490491
throw \Auth0\SDK\Exception\ConfigurationException::validationFailed($propertyName);

src/Mixins/ConfigurableMixin.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ private function changeState(
264264
throw \Auth0\SDK\Exception\ConfigurationException::setIncompatible($propertyName, (string) $expectedType, $propertyType);
265265
}
266266

267+
if ($propertyType === 'string' && is_string($propertyValue) && trim($propertyValue) === '' && in_array('NULL', $allowedTypes, true)) {
268+
$propertyValue = null;
269+
}
270+
267271
if (method_exists($this, 'onStateChange')) {
268272
$propertyValue = $this->onStateChange($propertyName, $propertyValue);
269273
}

tests/Pest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
define('AUTH0_TESTS_DIR', dirname(__FILE__));
66

7-
require_once join(DIRECTORY_SEPARATOR, [AUTH0_TESTS_DIR, '..', 'vendor', 'autoload.php']);
7+
require_once implode(DIRECTORY_SEPARATOR, [AUTH0_TESTS_DIR, '..', 'vendor', 'autoload.php']);
88

99
// For unit tests, use a mock network client rather than sending real requests.
1010
uses()

tests/Unit/Auth0Test.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
$_COOKIE = [];
1616

1717
$this->configuration = [
18-
'domain' => '__test_domain__',
18+
'domain' => 'domain.test',
1919
'clientId' => '__test_client_id__',
2020
'cookieSecret' => uniqid(),
2121
'clientSecret' => '__test_client_secret__',
@@ -110,7 +110,7 @@ public function defer(
110110

111111
expect($url)
112112
->scheme->toEqual('https')
113-
->host->toEqual('__test_domain__')
113+
->host->toEqual('domain.test')
114114
->path->toEqual('/authorize')
115115
->query
116116
->toContain('scope=openid%20profile%20email')
@@ -134,7 +134,7 @@ public function defer(
134134

135135
expect($url)
136136
->scheme->toEqual('https')
137-
->host->toEqual('__test_domain__')
137+
->host->toEqual('domain.test')
138138
->path->toEqual('/authorize')
139139
->query
140140
->toContain('scope=openid%20profile%20email')
@@ -161,7 +161,7 @@ public function defer(
161161

162162
expect($url)
163163
->scheme->toEqual('https')
164-
->host->toEqual('__test_domain__')
164+
->host->toEqual('domain.test')
165165
->path->toEqual('/authorize')
166166
->query
167167
->toContain('scope=' . $params['scope'])
@@ -178,7 +178,7 @@ public function defer(
178178

179179
expect($url)
180180
->scheme->toEqual('https')
181-
->host->toEqual('__test_domain__')
181+
->host->toEqual('domain.test')
182182
->path->toEqual('/authorize')
183183
->query
184184
->toContain('state=')
@@ -192,7 +192,7 @@ public function defer(
192192

193193
expect($url)
194194
->scheme->toEqual('https')
195-
->host->toEqual('__test_domain__')
195+
->host->toEqual('domain.test')
196196
->path->toEqual('/authorize')
197197
->query
198198
->toContain('code_challenge=')
@@ -208,7 +208,7 @@ public function defer(
208208

209209
expect($url)
210210
->scheme->toEqual('https')
211-
->host->toEqual('__test_domain__')
211+
->host->toEqual('domain.test')
212212
->path->toEqual('/authorize')
213213
->query
214214
->toContain('max_age=1000');
@@ -225,7 +225,7 @@ public function defer(
225225

226226
expect($url)
227227
->scheme->toEqual('https')
228-
->host->toEqual('__test_domain__')
228+
->host->toEqual('domain.test')
229229
->path->toEqual('/authorize')
230230
->query
231231
->toContain('max_age=1001');
@@ -238,7 +238,7 @@ public function defer(
238238

239239
expect($url)
240240
->scheme->toEqual('https')
241-
->host->toEqual('__test_domain__')
241+
->host->toEqual('domain.test')
242242
->path->toEqual('/authorize')
243243
->query
244244
->toContain('screen_hint=signup');
@@ -255,7 +255,7 @@ public function defer(
255255

256256
expect($url)
257257
->scheme->toEqual('https')
258-
->host->toEqual('__test_domain__')
258+
->host->toEqual('domain.test')
259259
->path->toEqual('/authorize')
260260
->query
261261
->toContain('invitation=__test_invitation__')
@@ -280,7 +280,7 @@ public function defer(
280280

281281
expect($url)
282282
->scheme->toEqual('https')
283-
->host->toEqual('__test_domain__')
283+
->host->toEqual('domain.test')
284284
->path->toEqual('/v2/logout')
285285
->query
286286
->toContain('returnTo=' . $returnUrl)
@@ -684,7 +684,7 @@ public function defer(
684684
expect($requestBody['client_secret'])->toEqual('__test_client_secret__');
685685
expect($requestBody['client_id'])->toEqual('__test_client_id__');
686686
expect($requestBody['refresh_token'])->toEqual('2.3.4');
687-
expect($request->getUri()->__toString())->toEqual('https://__test_domain__/oauth/token');
687+
expect($request->getUri()->__toString())->toEqual('https://domain.test/oauth/token');
688688
});
689689

690690
test('getCredentials() returns null when a session is not available', function(): void {
@@ -1002,7 +1002,7 @@ public function defer(
10021002
$_GET[$testParameterName] = $candidate->token;
10031003

10041004
$auth0 = new \Auth0\SDK\Auth0(array_merge($this->configuration, [
1005-
'domain' => '__bad_domain__',
1005+
'domain' => 'domain.bad',
10061006
'tokenJwksUri' => $candidate->jwks,
10071007
'tokenCache' => $candidate->cached
10081008
]));

tests/Unit/Configuration/SdkConfigurationTest.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@
7474
'clientId' => $clientId,
7575
'redirectUri' => $redirectUri,
7676
]);
77+
})->throws(\Auth0\SDK\Exception\ConfigurationException::class, \Auth0\SDK\Exception\ConfigurationException::MSG_REQUIRES_DOMAIN);
78+
79+
test('__construct() throws an exception if domain is an invalid uri', function(): void {
80+
$cookieSecret = uniqid();
81+
$clientId = uniqid();
82+
$redirectUri = uniqid();
83+
84+
$sdk = new SdkConfiguration([
85+
'domain' => '',
86+
'cookieSecret' => $cookieSecret,
87+
'clientId' => $clientId,
88+
'redirectUri' => $redirectUri,
89+
]);
7790
})->throws(\Auth0\SDK\Exception\ConfigurationException::class, sprintf(\Auth0\SDK\Exception\ConfigurationException::MSG_VALIDATION_FAILED, 'domain'));
7891

7992
test('__construct() throws an exception if cookieSecret is undefined', function(): void {
@@ -100,7 +113,7 @@
100113
'clientId' => $clientId,
101114
'redirectUri' => $redirectUri,
102115
]);
103-
})->throws(\Auth0\SDK\Exception\ConfigurationException::class, sprintf(\Auth0\SDK\Exception\ConfigurationException::MSG_VALIDATION_FAILED, 'cookieSecret'));
116+
})->throws(\Auth0\SDK\Exception\ConfigurationException::class, \Auth0\SDK\Exception\ConfigurationException::MSG_REQUIRES_COOKIE_SECRET);
104117

105118
test('__construct() throws an exception if an invalid token algorithm is specified', function(): void {
106119
$domain = uniqid();
@@ -311,12 +324,12 @@
311324

312325
test('formatDomain() returns the custom domain when a custom domain is configured', function(): void
313326
{
314-
$domain = uniqid();
315-
$customDomain = uniqid();
327+
$domain = uniqid() . '.test';
328+
$customDomain = uniqid() . '.test';
316329

317330
$sdk = new SdkConfiguration([
318331
'domain' => $domain,
319-
'customDomain' => 'test://' . $customDomain,
332+
'customDomain' => $customDomain,
320333
'cookieSecret' => uniqid(),
321334
'clientId' => uniqid(),
322335
'redirectUri' => uniqid(),

tests/Unit/Token/VerifierTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
dataset('tokenHs256', static function () {
2727
$token = (new TokenGenerator())->withHs256([]);
2828
[$headers, $claims, $signature] = explode('.', $token);
29-
$payload = join('.', [$headers, $claims]);
29+
$payload = implode('.', [$headers, $claims]);
3030
$signature = TokenGenerator::decodePart($signature, false);
3131

3232
yield [ $token, $payload, $signature, $headers ];
@@ -36,7 +36,7 @@
3636
$keyPair = TokenGenerator::generateRsaKeyPair();
3737
$token = (new TokenGenerator())->withRs256([], $keyPair['private'], ['kid' => '__test_kid__']);
3838
[$headers, $claims, $signature] = explode('.', $token);
39-
$payload = join('.', [$headers, $claims]);
39+
$payload = implode('.', [$headers, $claims]);
4040
$signature = TokenGenerator::decodePart($signature, false);
4141

4242
// Mimic JWKS response format: strip opening and closing comment lines from public key, remove line breaks.

tests/Unit/TokenTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function(): SdkConfiguration {
142142
expect($token->validate(null, null, ['__test_org__'], $claims['nonce'], 100))->toEqual($token);
143143
})->with(['mocked data' => [
144144
function(): SdkConfiguration {
145-
$this->configuration->setDomain('__test_domain__');
145+
$this->configuration->setDomain('domain.test');
146146
$this->configuration->setClientId('__test_client_id__');
147147
$this->configuration->setTokenAlgorithm('HS256');
148148
$this->configuration->setClientSecret('__test_client_secret__');
@@ -161,7 +161,7 @@ function(): SdkConfiguration {
161161
$token->validate(null, [ $claims['aud'] ]);
162162
})->with(['mocked data' => [
163163
function(): SdkConfiguration {
164-
$this->configuration->setDomain('__test_domain__');
164+
$this->configuration->setDomain('domain.test');
165165
$this->configuration->setClientId('__diff_client_id__');
166166
$this->configuration->setTokenAlgorithm('HS256');
167167
return $this->configuration;

tests/Unit/Utility/HttpClientTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@
6767

6868
for ($i=0; $i < 10; $i++) {
6969
$this->client->mockResponse(clone $this->httpResponse429);
70-
$baseWait = intval(100 * pow(2, $i));
70+
$baseWait = (int) (100 * pow(2, $i));
7171
$baseWaits[] = $baseWait;
72-
$baseWaitSum = $baseWaitSum + $baseWait;
72+
$baseWaitSum += $baseWait;
7373
}
7474

7575
$response = $this->client->method('get')

tests/Unit/Utility/ToolkitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
$items = ['a', 'b', 'c'];
2929

3030
Toolkit::each($items, function(&$item, $key) {
31-
$item = $item . $key;
31+
$item .= $key;
3232
});
3333

3434
expect($items)->toEqual(['a0', 'b1', 'c2']);
@@ -38,7 +38,7 @@
3838
$items = ['a', 'b', 'c'];
3939

4040
Toolkit::each($items, function(&$item, $key) {
41-
$item = $item . $key;
41+
$item .= $key;
4242
return false;
4343
});
4444

0 commit comments

Comments
 (0)