Skip to content

Commit

Permalink
fix: Restore previous behavior of SdkConfiguration::setScope() being …
Browse files Browse the repository at this point in the history
…nullable (#665)
  • Loading branch information
evansims authored Oct 24, 2022
1 parent d2d8946 commit f9fc28f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/Configuration/SdkConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ final class SdkConfiguration implements ConfigurableContract
* @param array<string>|null $audience One or more API identifiers, found in your Auth0 API settings. The SDK uses the first value for building links. If provided, at least one of these values must match the 'aud' claim to validate an ID Token successfully.
* @param array<string>|null $organization One or more Organization IDs, found in your Auth0 Organization settings. The SDK uses the first value for building links. If provided, at least one of these values must match the 'org_id' claim to validate an ID Token successfully.
* @param bool $usePkce Defaults to true. Use PKCE (Proof Key of Code Exchange) with Authorization Code Flow requests. See https://auth0.com/docs/flows/call-your-api-using-the-authorization-code-flow-with-pkce
* @param array<string> $scope One or more scopes to request for Tokens. See https://auth0.com/docs/scopes
* @param array<string>|null $scope One or more scopes to request for Tokens. See https://auth0.com/docs/scopes
* @param string $responseMode Defaults to 'query.' Where to extract request parameters from, either 'query' for GET or 'form_post' for POST requests.
* @param string $responseType Defaults to 'code.' Use 'code' for server-side flows and 'token' for application side flow.
* @param string $tokenAlgorithm Defaults to 'RS256'. Algorithm to use for Token verification. Expects either 'RS256' or 'HS256'.
Expand Down Expand Up @@ -99,7 +99,7 @@ public function __construct(
private ?array $audience = null,
private ?array $organization = null,
private bool $usePkce = true,
private array $scope = ['openid', 'profile', 'email'],
private ?array $scope = ['openid', 'profile', 'email'],
private string $responseMode = 'query',
private string $responseType = 'code',
private string $tokenAlgorithm = Token::ALGO_RS256,
Expand Down Expand Up @@ -768,9 +768,9 @@ public function hasResponseType(): bool
/**
* @param array<string> $scope An array of scopes to request during authentication steps.
*/
public function setScope(array $scope = ['openid', 'profile', 'email']): self
public function setScope(?array $scope = ['openid', 'profile', 'email']): self
{
if ([] === $scope) {
if (null === $scope || [] === $scope) {
$scope = ['openid', 'profile', 'email'];
}

Expand All @@ -783,7 +783,7 @@ public function setScope(array $scope = ['openid', 'profile', 'email']): self
*/
public function getScope(): array
{
return $this->scope;
return $this->scope ?? ['openid', 'profile', 'email'];
}

public function hasScope(): bool
Expand Down Expand Up @@ -811,7 +811,7 @@ public function pushScope(array|string $scopes): ?array
return $this->scope;
}

$this->setScope(array_merge($this->scope, $scopes));
$this->setScope(array_merge($this->getScope(), $scopes));

return $this->scope;
}
Expand Down Expand Up @@ -1321,7 +1321,7 @@ private function getPropertyValidators(): array
'audience' => fn ($value) => is_array($value) || null === $value,
'organization' => fn ($value) => is_array($value) || null === $value,
'usePkce' => fn ($value) => is_bool($value),
'scope' => fn ($value) => is_array($value),
'scope' => fn ($value) => is_array($value) || null === $value,
'responseMode' => fn ($value) => is_string($value),
'responseType' => fn ($value) => is_string($value),
'tokenAlgorithm' => fn ($value) => is_string($value),
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/Configuration/SdkConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,32 @@
expect($sdk->formatScope())->toEqual('one two three');
});

test('scope() successfully reverts to the default values when an empty array is provided', function(): void
{
$sdk = new SdkConfiguration([
'domain' => MockDomain::valid(),
'cookieSecret' => uniqid(),
'clientId' => uniqid(),
'redirectUri' => uniqid(),
'scope' => [],
]);

expect($sdk->getScope())->toEqual(['openid', 'profile', 'email']);
});

test('scope() successfully reverts to the default values when a null value is provided', function(): void
{
$sdk = new SdkConfiguration([
'domain' => MockDomain::valid(),
'cookieSecret' => uniqid(),
'clientId' => uniqid(),
'redirectUri' => uniqid(),
'scope' => null,
]);

expect($sdk->getScope())->toEqual(['openid', 'profile', 'email']);
});

test('defaultOrganization() successfully returns the first organization', function(): void
{
$sdk = new SdkConfiguration([
Expand Down

0 comments on commit f9fc28f

Please sign in to comment.