Skip to content

Commit

Permalink
Improve data validation and error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed Sep 15, 2024
1 parent de60d9f commit c38ba8e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/Credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -58,6 +60,7 @@ protected function validateOptions(): void
AccessToken::TYPE_QUERY,
AccessToken::TYPE_CUSTOM,
]),
'tokenName' => 'string',
])->validate();
}

Expand Down
28 changes: 22 additions & 6 deletions tests/Unit/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -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',
);
Expand All @@ -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());
});

0 comments on commit c38ba8e

Please sign in to comment.