From c38ba8e689f42a58752806312b9851151644e0c9 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Sun, 15 Sep 2024 12:30:06 +0200 Subject: [PATCH] Improve data validation and error messages --- README.md | 2 +- src/Credentials.php | 4 +++- src/Options.php | 3 +++ tests/Unit/OptionsTest.php | 28 ++++++++++++++++++++++------ 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 614bda9..df02f29 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ This helper takes care of all the headaches and boilerplate code with a simple a - Expires option - Credential token key - Access token key -- Improve data validation and error messages +~~- Improve data validation and error messages~~ - Write/update readme - Make the cache store configurable - Maybe: add more tests diff --git a/src/Credentials.php b/src/Credentials.php index 8426ec0..b14d143 100644 --- a/src/Credentials.php +++ b/src/Credentials.php @@ -158,9 +158,11 @@ public function addAuthToBody(array $requestBody, Options $options): array public function setRefreshToken(string $token): void { $this->token = $token; + /* if (empty($this->options->authType)) { - //$this->options->authType = self::AUTH_TYPE_BEARER; + $this->options->authType = self::AUTH_TYPE_BEARER; } + */ } public function setClientCredentialsPair(string $clientId, string $clientSecret): void diff --git a/src/Options.php b/src/Options.php index d87d976..66a90e8 100644 --- a/src/Options.php +++ b/src/Options.php @@ -41,7 +41,9 @@ public function toArray(): array protected function validateOptions(): void { + // Note: closures can't be checked at this point since we don't have access to the response objects Validator::make((array) $this, [ + 'scopes.*' => 'string', 'authType' => Rule::in([ Credentials::AUTH_TYPE_BEARER, Credentials::AUTH_TYPE_BODY, @@ -58,6 +60,7 @@ protected function validateOptions(): void AccessToken::TYPE_QUERY, AccessToken::TYPE_CUSTOM, ]), + 'tokenName' => 'string', ])->validate(); } diff --git a/tests/Unit/OptionsTest.php b/tests/Unit/OptionsTest.php index e2ea065..52273d4 100644 --- a/tests/Unit/OptionsTest.php +++ b/tests/Unit/OptionsTest.php @@ -11,7 +11,7 @@ $this->expectException(\Illuminate\Validation\ValidationException::class); $this->expectExceptionMessage('The selected grant type is invalid'); - $options = new Options( + new Options( grantType: 'invalid', tokenType: AccessToken::TYPE_BEARER, ); @@ -21,7 +21,7 @@ $this->expectException(\Illuminate\Validation\ValidationException::class); $this->expectExceptionMessage('The selected token type is invalid'); - $options = new Options( + new Options( grantType: Credentials::GRANT_TYPE_CLIENT_CREDENTIALS, tokenType: 'invalid', ); @@ -31,19 +31,35 @@ $this->expectException(ValidationException::class); $this->expectExceptionMessage('The selected auth type is invalid'); - $options = new Options( + new Options( authType: 'invalid', ); }); +it('checks for integers in scopes when creating an option object', function () { + $this->expectException(ValidationException::class); + $this->expectExceptionMessage('The scopes.1 field must be a string.'); + + new Options( + scopes: ['valid', 1], + ); +}); +it('checks for objects in scopes when creating an option object', function () { + + $this->expectException(ValidationException::class); + $this->expectExceptionMessage('The scopes.2 field must be a string.'); + + new Options( + scopes: ['valid', 'also_valid', new stdClass()], + ); +}); + it('can create an option object', function () { $this->expectException(\Illuminate\Validation\ValidationException::class); $this->expectExceptionMessage('The selected token type is invalid'); - $options = new Options( + new Options( grantType: Credentials::GRANT_TYPE_CLIENT_CREDENTIALS, tokenType: 'invalid', ); - - //dd($options->toArray()); });