Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth\Basic: add input validation to constructor #574

Merged
merged 3 commits into from
Nov 5, 2021

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Oct 11, 2021

The Basic::__construct() method expects to receive an array of exactly two element and did validate the input received, but only threw an exception when the passed parameter did not match the expected element count, not when the passed parameter wasn't an array.

At the same time, the default value of $args is null, not an empty array and the $user and $pass properties are both public.

The only reason I can think of to have these properties public and the default value set to null, is to allow for delayed setting of the $user and $pass, after the class has already been instantiated.

To maintain the existing behaviour, but still add input validation, an exception is now thrown when the $args parameter is neither an array, nor null.

Includes;

  • A dedicated test for the input validation.
  • An additional test with Basic authentication with an instantiated class, but with delayed setting of the $user and $pass directly on the instantiated object. This test is solely added to document that this is a supported feature (and to safeguard against regressions in that respect).

@jrfnl jrfnl added this to the 2.0.0 milestone Oct 11, 2021
@jrfnl jrfnl requested a review from schlessera October 11, 2021 16:07
src/Auth/Basic.php Outdated Show resolved Hide resolved
The `Basic::__construct()` method expects to receive an array of exactly two element and did validate the input received, but only threw an exception when the passed parameter did not match the expected element count, not when the passed parameter wasn't an array.

At the same time, the default value of `$args` is `null`, not an empty array and the `$user` and `$pass` properties are both public.

The only reason I can think of to have these properties `public` and the default value set to `null`, is to allow for delayed setting of the `$user` and `$pass`, after the class has already been instantiated.

To maintain the existing behaviour, but still add input validation, an exception is now thrown when the `$args` parameter is neither an array, nor `null`.

Includes;
* A dedicated test for the input validation.
* An additional test with Basic authentication with an instantiated class, but with delayed setting of the `$user` and `$pass` directly on the instantiated object. This test is solely added to document that this is a supported feature (and to safeguard against regressions in that respect).
... for use when an invalid number of arguments are received for a method.

Along the same lines as the `InvalidArgument` exception, this class contains a `create()` method to standardize the text of the exception message.

The exception extends the Request native base `Exception` to
* Allow for introducing this as a (more specific) replacement for some existing exceptions without BC-break.
* Allow for maintaining the "exception type" indicators as were used before.

Includes dedicated unit test.
This replaces two pre-existing generic Requests `Exception` usage instances with use of the new `ArgumentCount` exception.

Includes:
* Updating the parameter description for the `$args` parameter of the `Proxy\Http::__construct()` method as those docs were plain incorrect to begin with.
* Updated and improved documentation for these exceptions in the function doc blocks.
    Again, the documentation for the exception thrown in the `Proxy\Http` class was incorrect to begin with.
* Updated unit tests for the affected classes to reflect these changes.
@jrfnl jrfnl force-pushed the feature/auth-basic-add-input-validation branch from d94b648 to 6a66c0c Compare November 1, 2021 02:31
@schlessera schlessera merged commit ee658fb into develop Nov 5, 2021
@schlessera schlessera deleted the feature/auth-basic-add-input-validation branch November 5, 2021 09:17
@jrfnl jrfnl mentioned this pull request Nov 6, 2021
28 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants