diff --git a/.gitattributes b/.gitattributes index e648067..22211a4 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,9 +1,14 @@ +/.github export-ignore +/.vitepress export-ignore +/docs export-ignore /tests export-ignore +/.editorconfig export-ignore /.gitattributes export-ignore /.gitignore export-ignore -/package.json export-ignore -/phpunit.xml.dist export-ignore -/README.md export-ignore +/.php-cs-fixer.dist.php export-ignore +/CHANGELOG.md export-ignore /CONTRIBUTING.md export-ignore /codecov.yml export-ignore /Makefile export-ignore +/package.json export-ignore +/phpunit.xml.dist export-ignore diff --git a/.gitignore b/.gitignore index 0e64fb5..f9ee470 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,16 @@ +.vitepress/dist +.vitepress/cache /node_modules/ -/vendor/ -/demo/vendor/ -/demo/.vagrant/ -/report/ -.DS_Store +/report +/vendor +# PHP CS Fixer configuration +/.php-cs-fixer.cache +/.php-cs-fixer.php +.phpunit.result.cache composer.lock -phpunit.xml -/tests/_output/ -.phplint-cache coverage.xml -.phpunit.result.cache +package-lock.json +.DS_Store +phpunit.xml -.vitepress/dist -.vitepress/cache +.idea diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..ec87fa8 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,32 @@ +setParallelConfig(ParallelConfigFactory::detect()) + ->setRiskyAllowed(false) + ->setRules([ + '@PSR12' => true, +// '@PSR12:risky' => true, + '@PhpCsFixer' => true, +// '@PhpCsFixer:risky' => true, + 'yoda_style' => false, + 'concat_space' => ['spacing' => 'one'], + 'global_namespace_import' => [ + 'import_classes' => true, + 'import_constants' => true, + 'import_functions' => true, + ], + 'multiline_whitespace_before_semicolons' => ['strategy' => 'no_multi_line'], + 'php_unit_test_class_requires_covers' => false, // bug # +// 'declare_strict_types' => true, + ]) + ->setFinder( + (new Finder()) + // 💡 root folder to check + ->in(__DIR__) + ); diff --git a/Makefile b/Makefile index 8787d72..6c8e0e6 100644 --- a/Makefile +++ b/Makefile @@ -19,18 +19,17 @@ vendor: $(wildcard composer.lock) composer install --prefer-dist lint: vendor - vendor/bin/phplint . --exclude=vendor/ - vendor/bin/ecs check src tests + vendor/bin/php-cs-fixer check lint-fix: vendor - vendor/bin/ecs check src tests --fix + vendor/bin/php-cs-fixer fix --diff rector: vendor/bin/rector --clear-cache --dry-run rector-fix: vendor/bin/rector --clear-cache - vendor/bin/ecs check src tests --fix + vendor/bin/php-cs-fixer fix --diff unit: vendor phpdbg -qrr vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml --coverage-html=./report/ diff --git a/composer.json b/composer.json index b0f4d6d..0d34576 100644 --- a/composer.json +++ b/composer.json @@ -38,12 +38,12 @@ }, "require-dev": { "equip/dispatch": "^2.0", + "friendsofphp/php-cs-fixer": "^3.95", "laminas/laminas-diactoros": "^1.3|^2.0|^3.0", "overtrue/phplint": "^3.0|^4.0|^5.0|^6.0", "phpstan/phpstan": "^1.11", "phpunit/phpunit": "^8.5.30|^9.0", - "rector/rector": "^0.14.5", - "symplify/easy-coding-standard": "^11.1" + "rector/rector": "^0.14.5" }, "config": { "sort-packages": true diff --git a/ecs.php b/ecs.php deleted file mode 100644 index b0a040a..0000000 --- a/ecs.php +++ /dev/null @@ -1,29 +0,0 @@ -paths([__DIR__ . '/src', __DIR__ . '/tests']); - - $ecsConfig->skip([ - NotOperatorWithSuccessorSpaceFixer::class, - ]); - - $ecsConfig->ruleWithConfiguration(PhpdocLineSpanFixer::class, [ - "property" => "single", - "const" => "single", - ]); - - $ecsConfig->sets([ - SetList::SPACES, - SetList::ARRAY, - SetList::DOCBLOCK, - SetList::PSR_12, - ]); -}; diff --git a/src/HttpBasicAuthentication.php b/src/HttpBasicAuthentication.php index a996c38..f33a94f 100644 --- a/src/HttpBasicAuthentication.php +++ b/src/HttpBasicAuthentication.php @@ -24,11 +24,6 @@ */ -/** - * @see https://github.com/tuupola/slim-basic-auth - * @license https://www.opensource.org/licenses/mit-license.php - */ - declare(strict_types=1); namespace Tuupola\Middleware; @@ -38,15 +33,19 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; +use RuntimeException; use SplStack; use Tuupola\Http\Factory\ResponseFactory; -use Tuupola\Middleware\DoublePassTrait; use Tuupola\Middleware\HttpBasicAuthentication\ArrayAuthenticator; -use Tuupola\Middleware\HttpBasicAuthentication\AuthenticatorInterface; use Tuupola\Middleware\HttpBasicAuthentication\RequestMethodRule; use Tuupola\Middleware\HttpBasicAuthentication\RequestPathRule; use Tuupola\Middleware\HttpBasicAuthentication\RuleInterface; +/** + * @see https://github.com/tuupola/slim-basic-auth + * + * @license https://www.opensource.org/licenses/mit-license.php + */ final class HttpBasicAuthentication implements MiddlewareInterface { use DoublePassTrait; @@ -60,16 +59,16 @@ final class HttpBasicAuthentication implements MiddlewareInterface * @var mixed[] */ private $options = [ - "secure" => true, - "relaxed" => ["localhost", "127.0.0.1"], - "users" => null, - "path" => null, - "ignore" => null, - "realm" => "Protected", - "authenticator" => null, - "before" => null, - "after" => null, - "error" => null, + 'secure' => true, + 'relaxed' => ['localhost', '127.0.0.1'], + 'users' => null, + 'path' => null, + 'ignore' => null, + 'realm' => 'Protected', + 'authenticator' => null, + 'before' => null, + 'after' => null, + 'error' => null, ]; /** @@ -77,38 +76,38 @@ final class HttpBasicAuthentication implements MiddlewareInterface */ public function __construct(array $options = []) { - /* Setup stack for rules */ + // Setup stack for rules $this->rules = new SplStack(); - /* Store passed in options overwriting any defaults */ + // Store passed in options overwriting any defaults $this->hydrate($options); - /* If array of users was passed in options create an authenticator */ - if (is_array($this->options["users"])) { - $this->options["authenticator"] = new ArrayAuthenticator([ - "users" => $this->options["users"], + // If array of users was passed in options create an authenticator + if (is_array($this->options['users'])) { + $this->options['authenticator'] = new ArrayAuthenticator([ + 'users' => $this->options['users'], ]); } - /* If nothing was passed in options add default rules. */ - if (!isset($options["rules"])) { + // If nothing was passed in options add default rules. + if (!isset($options['rules'])) { $this->rules->push(new RequestMethodRule([ - "ignore" => ["OPTIONS"], + 'ignore' => ['OPTIONS'], ])); } - /* If path was given in easy mode add rule for it. */ - if (null !== $this->options["path"]) { + // If path was given in easy mode add rule for it. + if (null !== $this->options['path']) { $this->rules->push(new RequestPathRule([ - "path" => $this->options["path"], - "ignore" => $this->options["ignore"], + 'path' => $this->options['path'], + 'ignore' => $this->options['ignore'], ])); } - /* There must be an authenticator either passed via options */ - /* or added because $this->options["users"] was an array. */ - if (null === $this->options["authenticator"]) { - throw new \RuntimeException("Authenticator or users array must be given"); + // There must be an authenticator either passed via options + // or added because $this->options["users"] was an array. + if (null === $this->options['authenticator']) { + throw new RuntimeException('Authenticator or users array must be given'); } } @@ -121,21 +120,21 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $scheme = $request->getUri()->getScheme(); $server_params = $request->getServerParams(); - /* If rules say we should not authenticate call next and return. */ + // If rules say we should not authenticate call next and return. if (false === $this->shouldAuthenticate($request)) { return $handler->handle($request); } - /* HTTP allowed only if secure is false or server is in relaxed array. */ - if ("https" !== $scheme && true === $this->options["secure"]) { - $allowedHost = in_array($host, $this->options["relaxed"]); + // HTTP allowed only if secure is false or server is in relaxed array. + if ('https' !== $scheme && true === $this->options['secure']) { + $allowedHost = in_array($host, $this->options['relaxed']); - /* if 'headers' is in the 'relaxed' key, then we check for forwarding */ + // if 'headers' is in the 'relaxed' key, then we check for forwarding $allowedForward = false; - if (in_array("headers", $this->options["relaxed"])) { + if (in_array('headers', $this->options['relaxed'])) { if ( - $request->getHeaderLine("X-Forwarded-Proto") === "https" - && $request->getHeaderLine('X-Forwarded-Port') === "443" + $request->getHeaderLine('X-Forwarded-Proto') === 'https' + && $request->getHeaderLine('X-Forwarded-Port') === '443' ) { $allowedForward = true; } @@ -143,57 +142,58 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface if (!($allowedHost || $allowedForward)) { $message = sprintf( - "Insecure use of middleware over %s denied by configuration.", + 'Insecure use of middleware over %s denied by configuration.', strtoupper($scheme) ); - throw new \RuntimeException($message); + + throw new RuntimeException($message); } } - /* Just in case. */ + // Just in case. $params = [ - "user" => null, - "password" => null, + 'user' => null, + 'password' => null, ]; - if (preg_match("/Basic\s+(.*)$/i", $request->getHeaderLine("Authorization"), $matches)) { - $explodedCredential = explode(":", base64_decode($matches[1]), 2); + if (preg_match('/Basic\s+(.*)$/i', $request->getHeaderLine('Authorization'), $matches)) { + $explodedCredential = explode(':', base64_decode($matches[1]), 2); if (count($explodedCredential) == 2) { - [$params["user"], $params["password"]] = $explodedCredential; + [$params['user'], $params['password']] = $explodedCredential; } } - /* Check if user authenticates. */ - if (false === $this->options["authenticator"]($params)) { - /* Set response headers before giving it to error callback */ + // Check if user authenticates. + if (false === $this->options['authenticator']($params)) { + // Set response headers before giving it to error callback $response = (new ResponseFactory()) ->createResponse(401) ->withHeader( - "WWW-Authenticate", - sprintf('Basic realm="%s"', $this->options["realm"]) + 'WWW-Authenticate', + sprintf('Basic realm="%s"', $this->options['realm']) ); return $this->processError($response, [ - "message" => "Authentication failed", - "params" => $params, + 'message' => 'Authentication failed', + 'params' => $params, ]); } - /* Modify $request before calling next middleware. */ - if (is_callable($this->options["before"])) { + // Modify $request before calling next middleware. + if (is_callable($this->options['before'])) { $response = (new ResponseFactory())->createResponse(200); - $before_request = $this->options["before"]($request, $params); + $before_request = $this->options['before']($request, $params); if ($before_request instanceof ServerRequestInterface) { $request = $before_request; } } - /* Everything ok, call next middleware. */ + // Everything ok, call next middleware. $response = $handler->handle($request); - /* Modify $response before returning. */ - if (is_callable($this->options["after"])) { - $after_response = $this->options["after"]($response, $params); + // Modify $response before returning. + if (is_callable($this->options['after'])) { + $after_response = $this->options['after']($response, $params); if ($after_response instanceof ResponseInterface) { return $after_response; } @@ -202,6 +202,45 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface return $response; } + /** + * Set the rules which determine if current request should be authenticated. + * + * Rules must be callables which return a boolean. If any of the rules return + * boolean false current request will not be authenticated. + * + * @param RuleInterface[] $rules + */ + public function withRules(array $rules): self + { + $new = clone $this; + // Clear the stack + unset($new->rules); + $new->rules = new SplStack(); + + // Add the rules + foreach ($rules as $callable) { + $new = $new->addRule($callable); + } + + return $new; + } + + /** + * Add a rule to the rules stack. + * + * Rules must be callables which return a boolean. If any of the rules return + * boolean false current request will not be authenticated. + */ + public function addRule(callable $callable): self + { + $new = clone $this; + $new->rules = clone $this->rules; + // @phpstan-ignore-next-line + $new->rules->push($callable); + + return $new; + } + /** * Hydrate all options from given array. * @@ -210,16 +249,16 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface private function hydrate(array $data = []): void { foreach ($data as $key => $value) { - /* https://github.com/facebook/hhvm/issues/6368 */ - $key = str_replace(".", " ", $key); + // https://github.com/facebook/hhvm/issues/6368 + $key = str_replace('.', ' ', $key); $method = lcfirst(ucwords($key)); - $method = str_replace(" ", "", $method); + $method = str_replace(' ', '', $method); if (method_exists($this, $method)) { - /* Try to use setter */ - /** @phpstan-ignore-next-line */ + // Try to use setter + // @phpstan-ignore-next-line call_user_func([$this, $method], $value); } else { - /* Or fallback to setting option directly */ + // Or fallback to setting option directly $this->options[$key] = $value; } } @@ -230,12 +269,13 @@ private function hydrate(array $data = []): void */ private function shouldAuthenticate(ServerRequestInterface $request): bool { - /* If any of the rules in stack return false will not authenticate */ + // If any of the rules in stack return false will not authenticate foreach ($this->rules as $callable) { if (false === $callable($request)) { return false; } } + return true; } @@ -246,12 +286,13 @@ private function shouldAuthenticate(ServerRequestInterface $request): bool */ private function processError(ResponseInterface $response, array $arguments): ResponseInterface { - if (is_callable($this->options["error"])) { - $handler_response = $this->options["error"]($response, $arguments); + if (is_callable($this->options['error'])) { + $handler_response = $this->options['error']($response, $arguments); if ($handler_response instanceof ResponseInterface) { return $handler_response; } } + return $response; } @@ -259,22 +300,24 @@ private function processError(ResponseInterface $response, array $arguments): Re * Set path where middleware should bind to. * * @param string|string[] $path + * * @phpstan-ignore method.unused */ private function path($path): void { - $this->options["path"] = (array) $path; + $this->options['path'] = (array) $path; } /** * Set path which middleware ignores. * * @param string[] $ignore + * * @phpstan-ignore method.unused */ private function ignore($ignore): void { - $this->options["ignore"] = (array) $ignore; + $this->options['ignore'] = (array) $ignore; } /** @@ -284,18 +327,19 @@ private function ignore($ignore): void */ private function authenticator(callable $authenticator): void { - $this->options["authenticator"] = $authenticator; + $this->options['authenticator'] = $authenticator; } /** * Set the users array. * * @param string[] $users + * * @phpstan-ignore method.unused */ private function users(array $users): void { - $this->options["users"] = $users; + $this->options['users'] = $users; } /** @@ -305,18 +349,19 @@ private function users(array $users): void */ private function secure(bool $secure): void { - $this->options["secure"] = $secure; + $this->options['secure'] = $secure; } /** * Set hosts where secure rule is relaxed. * * @param string[] $relaxed + * * @phpstan-ignore method.unused */ private function relaxed(array $relaxed): void { - $this->options["relaxed"] = $relaxed; + $this->options['relaxed'] = $relaxed; } /** @@ -326,7 +371,7 @@ private function relaxed(array $relaxed): void */ private function before(Closure $before): void { - $this->options["before"] = $before->bindTo($this); + $this->options['before'] = $before->bindTo($this); } /** @@ -336,7 +381,7 @@ private function before(Closure $before): void */ private function after(Closure $after): void { - $this->options["after"] = $after->bindTo($this); + $this->options['after'] = $after->bindTo($this); } /** @@ -346,13 +391,14 @@ private function after(Closure $after): void */ private function error(callable $error): void { - $this->options["error"] = $error; + $this->options['error'] = $error; } /** - * Set the rules + * Set the rules. * * @param RuleInterface[] $rules + * * @phpstan-ignore method.unused */ private function rules(array $rules): void @@ -362,41 +408,4 @@ private function rules(array $rules): void $this->rules->push($callable); } } - - /** - * Set the rules which determine if current request should be authenticated. - * - * Rules must be callables which return a boolean. If any of the rules return - * boolean false current request will not be authenticated. - * - * @param RuleInterface[] $rules - */ - public function withRules(array $rules): self - { - $new = clone $this; - /* Clear the stack */ - unset($new->rules); - $new->rules = new SplStack(); - - /* Add the rules */ - foreach ($rules as $callable) { - $new = $new->addRule($callable); - } - return $new; - } - - /** - * Add a rule to the rules stack. - * - * Rules must be callables which return a boolean. If any of the rules return - * boolean false current request will not be authenticated. - */ - public function addRule(callable $callable): self - { - $new = clone $this; - $new->rules = clone $this->rules; - /* @phpstan-ignore-next-line */ - $new->rules->push($callable); - return $new; - } } diff --git a/src/HttpBasicAuthentication/ArrayAuthenticator.php b/src/HttpBasicAuthentication/ArrayAuthenticator.php index 6e86dc0..2637ece 100644 --- a/src/HttpBasicAuthentication/ArrayAuthenticator.php +++ b/src/HttpBasicAuthentication/ArrayAuthenticator.php @@ -24,19 +24,20 @@ */ -/** - * @see https://github.com/tuupola/slim-basic-auth - * @license https://www.opensource.org/licenses/mit-license.php - */ - declare(strict_types=1); namespace Tuupola\Middleware\HttpBasicAuthentication; +/** + * @see https://github.com/tuupola/slim-basic-auth + * + * @license https://www.opensource.org/licenses/mit-license.php + */ final class ArrayAuthenticator implements AuthenticatorInterface { /** * Stores all the options passed to the authenticator. + * * @var mixed[] */ private $options; @@ -46,9 +47,9 @@ final class ArrayAuthenticator implements AuthenticatorInterface */ public function __construct(array $options = []) { - /* Default options. */ + // Default options. $this->options = [ - "users" => [], + 'users' => [], ]; if ($options) { @@ -61,21 +62,21 @@ public function __construct(array $options = []) */ public function __invoke(array $arguments): bool { - $user = $arguments["user"] ?? ''; - $password = $arguments["password"] ?? ''; + $user = $arguments['user'] ?? ''; + $password = $arguments['password'] ?? ''; - /* Unknown user. */ - if (!isset($this->options["users"][$user])) { + // Unknown user. + if (!isset($this->options['users'][$user])) { return false; } - if (self::isHash($this->options["users"][$user])) { - /* Hashed password. */ - return password_verify($password, $this->options["users"][$user]); - } else { - /* Cleartext password. */ - return $this->options["users"][$user] === $password; + if (self::isHash($this->options['users'][$user])) { + // Hashed password. + return password_verify($password, $this->options['users'][$user]); } + + // Cleartext password. + return $this->options['users'][$user] === $password; } private static function isHash(string $password): bool diff --git a/src/HttpBasicAuthentication/AuthenticatorInterface.php b/src/HttpBasicAuthentication/AuthenticatorInterface.php index b5f3b89..82cf44b 100644 --- a/src/HttpBasicAuthentication/AuthenticatorInterface.php +++ b/src/HttpBasicAuthentication/AuthenticatorInterface.php @@ -24,15 +24,15 @@ */ -/** - * @see https://github.com/tuupola/slim-basic-auth - * @license https://www.opensource.org/licenses/mit-license.php - */ - declare(strict_types=1); namespace Tuupola\Middleware\HttpBasicAuthentication; +/** + * @see https://github.com/tuupola/slim-basic-auth + * + * @license https://www.opensource.org/licenses/mit-license.php + */ interface AuthenticatorInterface { /** diff --git a/src/HttpBasicAuthentication/PdoAuthenticator.php b/src/HttpBasicAuthentication/PdoAuthenticator.php index 2567ba1..5644868 100644 --- a/src/HttpBasicAuthentication/PdoAuthenticator.php +++ b/src/HttpBasicAuthentication/PdoAuthenticator.php @@ -24,19 +24,22 @@ */ -/** - * @see https://github.com/tuupola/slim-basic-auth - * @license https://www.opensource.org/licenses/mit-license.php - */ - declare(strict_types=1); namespace Tuupola\Middleware\HttpBasicAuthentication; +use PDO; + +/** + * @see https://github.com/tuupola/slim-basic-auth + * + * @license https://www.opensource.org/licenses/mit-license.php + */ final class PdoAuthenticator implements AuthenticatorInterface { /** * Stores all the options passed to the authenticator. + * * @var mixed[] */ private $options; @@ -46,11 +49,11 @@ final class PdoAuthenticator implements AuthenticatorInterface */ public function __construct(array $options = []) { - /* Default options. */ + // Default options. $this->options = [ - "table" => "users", - "user" => "user", - "hash" => "hash", + 'table' => 'users', + 'user' => 'user', + 'hash' => 'hash', ]; if ($options) { @@ -63,16 +66,16 @@ public function __construct(array $options = []) */ public function __invoke(array $arguments): bool { - $user = $arguments["user"]; - $password = $arguments["password"]; + $user = $arguments['user']; + $password = $arguments['password']; $sql = $this->sql(); - $statement = $this->options["pdo"]->prepare($sql); + $statement = $this->options['pdo']->prepare($sql); $statement->execute([$user]); - if ($user = $statement->fetch(\PDO::FETCH_ASSOC)) { - return password_verify($password, $user[$this->options["hash"]]); + if ($user = $statement->fetch(PDO::FETCH_ASSOC)) { + return password_verify($password, $user[$this->options['hash']]); } return false; @@ -80,26 +83,26 @@ public function __invoke(array $arguments): bool public function sql(): string { - $driver = $this->options["pdo"]->getAttribute(\PDO::ATTR_DRIVER_NAME); + $driver = $this->options['pdo']->getAttribute(PDO::ATTR_DRIVER_NAME); - /* Workaround to test without sqlsrv with */ - if (defined("__PHPUNIT_ATTR_DRIVER_NAME__")) { + // Workaround to test without sqlsrv with + if (defined('__PHPUNIT_ATTR_DRIVER_NAME__')) { $driver = __PHPUNIT_ATTR_DRIVER_NAME__; } - if ("sqlsrv" === $driver) { - $sql = - "SELECT TOP 1 * + if ('sqlsrv' === $driver) { + $sql + = "SELECT TOP 1 * FROM {$this->options['table']} WHERE {$this->options['user']} = ?"; } else { - $sql = - "SELECT * + $sql + = "SELECT * FROM {$this->options['table']} WHERE {$this->options['user']} = ? LIMIT 1"; } - return (string) preg_replace("!\s+!", " ", $sql); + return (string) preg_replace('!\s+!', ' ', $sql); } } diff --git a/src/HttpBasicAuthentication/RequestMethodRule.php b/src/HttpBasicAuthentication/RequestMethodRule.php index 7d89edf..eb14e84 100644 --- a/src/HttpBasicAuthentication/RequestMethodRule.php +++ b/src/HttpBasicAuthentication/RequestMethodRule.php @@ -24,25 +24,26 @@ */ -/** - * @see https://github.com/tuupola/slim-basic-auth - * @license https://www.opensource.org/licenses/mit-license.php - */ - declare(strict_types=1); namespace Tuupola\Middleware\HttpBasicAuthentication; use Psr\Http\Message\ServerRequestInterface; +/** + * @see https://github.com/tuupola/slim-basic-auth + * + * @license https://www.opensource.org/licenses/mit-license.php + */ final class RequestMethodRule implements RuleInterface { /** * Stores all the options passed to the rule. + * * @var mixed[] */ - protected $options = [ - "ignore" => ["OPTIONS"], + private $options = [ + 'ignore' => ['OPTIONS'], ]; /** @@ -55,6 +56,6 @@ public function __construct(array $options = []) public function __invoke(ServerRequestInterface $request): bool { - return !in_array($request->getMethod(), $this->options["ignore"]); + return !in_array($request->getMethod(), $this->options['ignore']); } } diff --git a/src/HttpBasicAuthentication/RequestPathRule.php b/src/HttpBasicAuthentication/RequestPathRule.php index 0af3f87..e1d4101 100644 --- a/src/HttpBasicAuthentication/RequestPathRule.php +++ b/src/HttpBasicAuthentication/RequestPathRule.php @@ -24,11 +24,6 @@ */ -/** - * @see https://github.com/tuupola/slim-basic-auth - * @license https://www.opensource.org/licenses/mit-license.php - */ - declare(strict_types=1); namespace Tuupola\Middleware\HttpBasicAuthentication; @@ -37,17 +32,21 @@ /** * Rule to decide by request path whether the request should be authenticated or not. + * + * @see https://github.com/tuupola/slim-basic-auth + * + * @license https://www.opensource.org/licenses/mit-license.php */ - final class RequestPathRule implements RuleInterface { /** * Stores all the options passed to the rule. + * * @var mixed[] */ private $options = [ - "path" => ["/"], - "ignore" => [], + 'path' => ['/'], + 'ignore' => [], ]; /** @@ -60,24 +59,25 @@ public function __construct(array $options = []) public function __invoke(ServerRequestInterface $request): bool { - $uri = "/" . $request->getUri()->getPath(); - $uri = (string) preg_replace("#/+#", "/", $uri); + $uri = '/' . $request->getUri()->getPath(); + $uri = (string) preg_replace('#/+#', '/', $uri); - /* If request path is matches ignore should not authenticate. */ - foreach ((array) $this->options["ignore"] as $ignore) { - $ignore = rtrim($ignore, "/"); - if (!!preg_match("@^{$ignore}(/.*)?$@", $uri)) { + // If request path is matches ignore should not authenticate. + foreach ((array) $this->options['ignore'] as $ignore) { + $ignore = rtrim($ignore, '/'); + if ((bool) preg_match("@^{$ignore}(/.*)?$@", $uri)) { return false; } } - /* Otherwise check if path matches and we should authenticate. */ - foreach ((array) $this->options["path"] as $path) { - $path = rtrim($path, "/"); - if (!!preg_match("@^{$path}(/.*)?$@", $uri)) { + // Otherwise check if path matches and we should authenticate. + foreach ((array) $this->options['path'] as $path) { + $path = rtrim($path, '/'); + if ((bool) preg_match("@^{$path}(/.*)?$@", $uri)) { return true; } } + return false; } } diff --git a/src/HttpBasicAuthentication/RuleInterface.php b/src/HttpBasicAuthentication/RuleInterface.php index 22e0789..d909375 100644 --- a/src/HttpBasicAuthentication/RuleInterface.php +++ b/src/HttpBasicAuthentication/RuleInterface.php @@ -24,17 +24,17 @@ */ -/** - * @see https://github.com/tuupola/slim-basic-auth - * @license https://www.opensource.org/licenses/mit-license.php - */ - declare(strict_types=1); namespace Tuupola\Middleware\HttpBasicAuthentication; use Psr\Http\Message\ServerRequestInterface; +/** + * @see https://github.com/tuupola/slim-basic-auth + * + * @license https://www.opensource.org/licenses/mit-license.php + */ interface RuleInterface { public function __invoke(ServerRequestInterface $request): bool; diff --git a/tests/ArrayAuthenticatorTest.php b/tests/ArrayAuthenticatorTest.php index e7f58ed..4a19813 100644 --- a/tests/ArrayAuthenticatorTest.php +++ b/tests/ArrayAuthenticatorTest.php @@ -24,67 +24,69 @@ */ -/** - * @see https://github.com/tuupola/slim-basic-auth - * @license https://www.opensource.org/licenses/mit-license.php - */ - namespace Tuupola\Middleware\HttpBasicAuthentication; use PHPUnit\Framework\TestCase; +/** + * @see https://github.com/tuupola/slim-basic-auth + * + * @license https://www.opensource.org/licenses/mit-license.php + * + * @internal + */ class ArrayAuthenticatorTest extends TestCase { public function testShouldReturnTrue() { $authenticator = new ArrayAuthenticator([ - "users" => [ - "root" => "t00r", - "somebody" => "passw0rd", - "wheel" => '$2y$10$Tm03qGT4FLqobzbZcfLDcOVIwZEpg20QZYffleeA2jfcClLpufYpy', - "dovecot" => '$2y$12$BlC21Ah2CuO7xlplqyysEejr1rwnj.uh2IEW9TX0JPgTnLNJk6XOC', + 'users' => [ + 'root' => 't00r', + 'somebody' => 'passw0rd', + 'wheel' => '$2y$10$Tm03qGT4FLqobzbZcfLDcOVIwZEpg20QZYffleeA2jfcClLpufYpy', + 'dovecot' => '$2y$12$BlC21Ah2CuO7xlplqyysEejr1rwnj.uh2IEW9TX0JPgTnLNJk6XOC', ], ]); $this->assertTrue($authenticator([ - "user" => "root", - "password" => "t00r", + 'user' => 'root', + 'password' => 't00r', ])); $this->assertTrue($authenticator([ - "user" => "somebody", - "password" => "passw0rd", + 'user' => 'somebody', + 'password' => 'passw0rd', ])); $this->assertTrue($authenticator([ - "user" => "wheel", - "password" => "gashhash", + 'user' => 'wheel', + 'password' => 'gashhash', ])); $this->assertTrue($authenticator([ - "user" => "dovecot", - "password" => "prettyfly", + 'user' => 'dovecot', + 'password' => 'prettyfly', ])); } public function testShouldReturnFalse() { $authenticator = new ArrayAuthenticator([ - "users" => [ - "root" => "t00r", - "somebody" => "passw0rd", - "luser" => '$2y$10$Tm03qGT4FLqobzbZcfLDcOVIwZEpg20QZYffleeA2jfcClLpufYpy', + 'users' => [ + 'root' => 't00r', + 'somebody' => 'passw0rd', + 'luser' => '$2y$10$Tm03qGT4FLqobzbZcfLDcOVIwZEpg20QZYffleeA2jfcClLpufYpy', ], ]); $this->assertFalse($authenticator([ - "user" => "root", - "password" => "nosuch", + 'user' => 'root', + 'password' => 'nosuch', ])); $this->assertFalse($authenticator([ - "user" => "nosuch", - "password" => "nosuch", + 'user' => 'nosuch', + 'password' => 'nosuch', ])); - /* Should handle as hash and not cleartext */ + // Should handle as hash and not cleartext $this->assertFalse($authenticator([ - "user" => "luser", - "password" => '$2y$10$Tm03qGT4FLqobzbZcfLDcOVIwZEpg20QZYffleeA2jfcClLpufYpy', + 'user' => 'luser', + 'password' => '$2y$10$Tm03qGT4FLqobzbZcfLDcOVIwZEpg20QZYffleeA2jfcClLpufYpy', ])); } } diff --git a/tests/BasicAuthenticationTest.php b/tests/BasicAuthenticationTest.php index 79045fd..a88d2a7 100644 --- a/tests/BasicAuthenticationTest.php +++ b/tests/BasicAuthenticationTest.php @@ -24,11 +24,6 @@ */ -/** - * @see https://github.com/tuupola/slim-basic-auth - * @license https://www.opensource.org/licenses/mit-license.php - */ - namespace Tuupola\Middleware\HttpBasicAuthentication; use Equip\Dispatch\MiddlewareCollection; @@ -37,7 +32,6 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Test\FalseAuthenticator; -use Test\FalseRule; use Test\TrueAuthenticator; use Test\TrueRule; use Tuupola\Http\Factory\ResponseFactory; @@ -45,137 +39,149 @@ use Tuupola\Http\Factory\StreamFactory; use Tuupola\Middleware\HttpBasicAuthentication; +/** + * @internal + * + * @see https://github.com/tuupola/slim-basic-auth + * + * @license https://www.opensource.org/licenses/mit-license.php + * / + */ class HttpBasicAuthenticationTest extends TestCase { public function testShouldFailWithoutAuthenticator() { - $this->expectException("RuntimeException"); + $this->expectException('RuntimeException'); $auth = new HttpBasicAuthentication(); } public function testShouldReturn200WithoutPassword() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/public"); + ->createServerRequest('GET', 'https://example.com/public'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/admin", - "realm" => "Protected", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => '/admin', + 'realm' => 'Protected', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals("Success", $response->getBody()); + $this->assertEquals('Success', $response->getBody()); } public function testShouldReturn401WithoutPassword() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/admin/item"); + ->createServerRequest('GET', 'https://example.com/admin/item'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => ["/admin"], - "realm" => "Not sure", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => ['/admin'], + 'realm' => 'Not sure', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(401, $response->getStatusCode()); - $this->assertEquals('Basic realm="Not sure"', $response->getHeaderline("WWW-Authenticate")); - $this->assertEquals("", $response->getBody()); + $this->assertEquals('Basic realm="Not sure"', $response->getHeaderline('WWW-Authenticate')); + $this->assertEquals('', $response->getBody()); } public function testShouldReturn200WithPassword() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/admin/item") - ->withHeader("Authorization", "Basic cm9vdDp0MDBy"); + ->createServerRequest('GET', 'https://example.com/admin/item') + ->withHeader('Authorization', 'Basic cm9vdDp0MDBy'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/admin", - "realm" => "Protected", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => '/admin', + 'realm' => 'Protected', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals("Success", $response->getBody()); + $this->assertEquals('Success', $response->getBody()); } public function testShouldReturn200WithOptions() { $request = (new ServerRequestFactory()) - ->createServerRequest("OPTIONS", "https://example.com/admin/item"); + ->createServerRequest('OPTIONS', 'https://example.com/admin/item'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/admin", - "realm" => "Protected", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => '/admin', + 'realm' => 'Protected', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals("Success", $response->getBody()); + $this->assertEquals('Success', $response->getBody()); } public function testShouldReturn200WithoutPasswordWithAnonymousFunction() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/admin/item"); + ->createServerRequest('GET', 'https://example.com/admin/item'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/admin", - "realm" => "Protected", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => '/admin', + 'realm' => 'Protected', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]); @@ -184,164 +190,171 @@ public function testShouldReturn200WithoutPasswordWithAnonymousFunction() }); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals("Success", $response->getBody()); + $this->assertEquals('Success', $response->getBody()); } public function testShouldReturn200WithIgnore() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/admin/ping"); + ->createServerRequest('GET', 'https://example.com/admin/ping'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/admin", - "ignore" => "/admin/ping", - "realm" => "Protected", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => '/admin', + 'ignore' => '/admin/ping', + 'realm' => 'Protected', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals("Success", $response->getBody()); + $this->assertEquals('Success', $response->getBody()); } public function testShouldReturn401WithFromAfter() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/admin/item") - ->withHeader("Authorization", "Basic cm9vdDp0MDBy"); + ->createServerRequest('GET', 'https://example.com/admin/item') + ->withHeader('Authorization', 'Basic cm9vdDp0MDBy'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/admin", - "realm" => "Protected", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => '/admin', + 'realm' => 'Protected', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], - "after" => function ($response, $arguments) { + 'after' => function ($response, $arguments) { return $response ->withBody((new StreamFactory())->createStream()) ->withStatus(401) - ->withHeader("WWW-Authenticate", 'Basic realm="Go away!"'); + ->withHeader('WWW-Authenticate', 'Basic realm="Go away!"'); }, ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(401, $response->getStatusCode()); - $this->assertEquals('Basic realm="Go away!"', $response->getHeaderline("WWW-Authenticate")); - $this->assertEquals("", (string) $response->getBody()); + $this->assertEquals('Basic realm="Go away!"', $response->getHeaderline('WWW-Authenticate')); + $this->assertEquals('', (string) $response->getBody()); } public function testShouldAlterResponseWithAfter() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/admin/item") - ->withHeader("Authorization", "Basic cm9vdDp0MDBy"); + ->createServerRequest('GET', 'https://example.com/admin/item') + ->withHeader('Authorization', 'Basic cm9vdDp0MDBy'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/admin", - "realm" => "Protected", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => '/admin', + 'realm' => 'Protected', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], - "after" => function ($response, $arguments) { - return $response->withHeader("X-Brawndo", "plants crave"); + 'after' => function ($response, $arguments) { + return $response->withHeader('X-Brawndo', 'plants crave'); }, ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals("plants crave", (string) $response->getHeaderLine("X-Brawndo")); + $this->assertEquals('plants crave', (string) $response->getHeaderLine('X-Brawndo')); } public function testShouldCallErrorHandlerWith401() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/admin/item"); + ->createServerRequest('GET', 'https://example.com/admin/item'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/admin", - "realm" => "Protected", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => '/admin', + 'realm' => 'Protected', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], - "error" => function ($response, $arguments) { - $response->getBody()->write("ERROR: " . $arguments["message"]); + 'error' => function ($response, $arguments) { + $response->getBody()->write('ERROR: ' . $arguments['message']); + return $response; }, ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(401, $response->getStatusCode()); - $this->assertEquals("ERROR: Authentication failed", $response->getBody()); + $this->assertEquals('ERROR: Authentication failed', $response->getBody()); } public function testErrorHandlerShouldAlterHeaders() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/admin/item"); + ->createServerRequest('GET', 'https://example.com/admin/item'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/admin", - "realm" => "Protected", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => '/admin', + 'realm' => 'Protected', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], - "error" => function ($response, $arguments) { + 'error' => function ($response, $arguments) { return $response ->withStatus(302) - ->withHeader("Location", "/foo/bar"); + ->withHeader('Location', '/foo/bar'); }, ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; @@ -350,160 +363,166 @@ public function testErrorHandlerShouldAlterHeaders() $this->assertEquals(302, $response->getStatusCode()); } - /*** OTHER *************************************************************/ + // OTHER public function testShouldReturn200WithTrueAuthenticator() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/admin/item"); + ->createServerRequest('GET', 'https://example.com/admin/item'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/admin", - "realm" => "Protected", - "authenticator" => new \Test\TrueAuthenticator(), + 'path' => '/admin', + 'realm' => 'Protected', + 'authenticator' => new TrueAuthenticator(), ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals("Success", $response->getBody()); + $this->assertEquals('Success', $response->getBody()); } public function testShouldReturn401WithFalseAuthenticator() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/admin/item"); + ->createServerRequest('GET', 'https://example.com/admin/item'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/admin", - "realm" => "Protected", - "authenticator" => new \Test\FalseAuthenticator(), + 'path' => '/admin', + 'realm' => 'Protected', + 'authenticator' => new FalseAuthenticator(), ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(401, $response->getStatusCode()); - $this->assertEquals("", $response->getBody()); + $this->assertEquals('', $response->getBody()); } public function testShouldReturn200WithAnonymousFunction() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/admin/item"); + ->createServerRequest('GET', 'https://example.com/admin/item'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/admin", - "realm" => "Protected", - "authenticator" => function ($arguments) { + 'path' => '/admin', + 'realm' => 'Protected', + 'authenticator' => function ($arguments) { return true; }, ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals("Success", $response->getBody()); + $this->assertEquals('Success', $response->getBody()); } public function testShouldReturn401WithAnonymousFunction() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/admin/item") - ->withHeader("Authorization", "Basic cm9vdDp0MDBy"); + ->createServerRequest('GET', 'https://example.com/admin/item') + ->withHeader('Authorization', 'Basic cm9vdDp0MDBy'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/admin", - "realm" => "Protected", - "authenticator" => function ($arguments) { + 'path' => '/admin', + 'realm' => 'Protected', + 'authenticator' => function ($arguments) { return false; }, ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(401, $response->getStatusCode()); - $this->assertEquals("", $response->getBody()); + $this->assertEquals('', $response->getBody()); } public function testShouldModifyRequestUsingBefore() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/admin/item") - ->withHeader("Authorization", "Basic cm9vdDp0MDBy"); + ->createServerRequest('GET', 'https://example.com/admin/item') + ->withHeader('Authorization', 'Basic cm9vdDp0MDBy'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/admin", - "realm" => "Protected", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => '/admin', + 'realm' => 'Protected', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], - "before" => function ($request, $arguments) { - return $request->withAttribute("user", $arguments["user"]); + 'before' => function ($request, $arguments) { + return $request->withAttribute('user', $arguments['user']); }, ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $user = $request->getAttribute("user"); + $user = $request->getAttribute('user'); $response->getBody()->write($user); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals("root", (string) $response->getBody()); + $this->assertEquals('root', (string) $response->getBody()); } public function testShouldNotAllowInsecure() { - $this->expectException("RuntimeException"); + $this->expectException('RuntimeException'); $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "http://example.com/api"); + ->createServerRequest('GET', 'http://example.com/api'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/api", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => '/api', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; @@ -513,21 +532,22 @@ public function testShouldNotAllowInsecure() public function testShouldRelaxInsecureInLocalhost() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "http://localhost/api"); + ->createServerRequest('GET', 'http://localhost/api'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "secure" => true, - "path" => "/api", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'secure' => true, + 'path' => '/api', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; @@ -539,22 +559,23 @@ public function testShouldRelaxInsecureInLocalhost() public function testShouldRelaxInsecureViaSetting() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "http://example.com/api"); + ->createServerRequest('GET', 'http://example.com/api'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "secure" => true, - "relaxed" => ["localhost", "example.com"], - "path" => "/api", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'secure' => true, + 'relaxed' => ['localhost', 'example.com'], + 'path' => '/api', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; @@ -566,24 +587,25 @@ public function testShouldRelaxInsecureViaSetting() public function testShouldRelaxForwardedViaSetting() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "http://example.com/api") - ->withHeader("X-Forwarded-Proto", "https") - ->withHeader("X-Forwarded-Port", "443"); + ->createServerRequest('GET', 'http://example.com/api') + ->withHeader('X-Forwarded-Proto', 'https') + ->withHeader('X-Forwarded-Port', '443'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "secure" => true, - "relaxed" => ["localhost", "headers"], - "path" => "/api", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'secure' => true, + 'relaxed' => ['localhost', 'headers'], + 'path' => '/api', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; @@ -595,17 +617,17 @@ public function testShouldRelaxForwardedViaSetting() public function testShouldBeImmutable() { $auth = new HttpBasicAuthentication([ - "path" => "/api", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => '/api', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]); $auth2 = $auth->addRule(new TrueRule()); $auth3 = $auth->withRules([new TrueRule()]); - /* Closure kludge to test private properties. */ + // Closure kludge to test private properties. $self = $this; $closure = function () use ($self) { @@ -627,52 +649,54 @@ public function testShouldBeImmutable() public function testShouldHandlePsr15() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/"); + ->createServerRequest('GET', 'https://example.com/'); $response = (new ResponseFactory())->createResponse(); $default = function (RequestInterface $request) { $response = (new ResponseFactory())->createResponse(); - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $collection = new MiddlewareCollection([ new HttpBasicAuthentication([ - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]), ]); $response = $collection->dispatch($request, $default); $this->assertEquals(401, $response->getStatusCode()); - $this->assertEquals("", $response->getBody()); + $this->assertEquals('', $response->getBody()); } public function testShouldHandleRulesArrayBug() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/api"); + ->createServerRequest('GET', 'https://example.com/api'); $default = function (ServerRequestInterface $request) { $response = (new ResponseFactory())->createResponse(); - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $collection = new MiddlewareCollection([ new HttpBasicAuthentication([ - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], - "rules" => [ + 'rules' => [ new RequestPathRule([ - "path" => ["/api"], - "ignore" => ["/api/login"], + 'path' => ['/api'], + 'ignore' => ['/api/login'], ]), new RequestMethodRule([ - "ignore" => ["OPTIONS"], + 'ignore' => ['OPTIONS'], ]), ], ]), @@ -681,178 +705,184 @@ public function testShouldHandleRulesArrayBug() $response = $collection->dispatch($request, $default); $this->assertEquals(401, $response->getStatusCode()); - $this->assertEquals("", $response->getBody()); + $this->assertEquals('', $response->getBody()); $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/api/login"); + ->createServerRequest('GET', 'https://example.com/api/login'); $response = $collection->dispatch($request, $default); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals("Success", $response->getBody()); + $this->assertEquals('Success', $response->getBody()); } - /*** BUGS *************************************************************/ + // BUGS public function testBug2UrlShouldMatchRegex() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/status/foo"); + ->createServerRequest('GET', 'https://example.com/status/foo'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/stat", - "realm" => "Protected", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => '/stat', + 'realm' => 'Protected', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals("Success", $response->getBody()); + $this->assertEquals('Success', $response->getBody()); } public function testBug3ShouldReturn401WithoutTrailingSlash() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/admin"); + ->createServerRequest('GET', 'https://example.com/admin'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/", - "realm" => "Protected", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => '/', + 'realm' => 'Protected', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(401, $response->getStatusCode()); - $this->assertEquals("", $response->getBody()); + $this->assertEquals('', $response->getBody()); } public function testBug3ShouldReturn401WithTrailingSlash() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/admin"); + ->createServerRequest('GET', 'https://example.com/admin'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => "/", - "realm" => "Protected", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => '/', + 'realm' => 'Protected', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(401, $response->getStatusCode()); - $this->assertEquals("", $response->getBody()); + $this->assertEquals('', $response->getBody()); } public function testBug9ShouldAllowUnauthenticatedHttp() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/public/foo"); + ->createServerRequest('GET', 'https://example.com/public/foo'); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => ["/api", "/bar"], - "realm" => "Protected", - "users" => [ - "root" => "t00r", - "user" => "passw0rd", + 'path' => ['/api', '/bar'], + 'realm' => 'Protected', + 'users' => [ + 'root' => 't00r', + 'user' => 'passw0rd', ], ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals("Success", $response->getBody()); + $this->assertEquals('Success', $response->getBody()); } public function testBug31ShouldAllowColonInPassword() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/api/foo") - ->withHeader("Authorization", "Basic Zm9vOmJhcjpwb3A="); + ->createServerRequest('GET', 'https://example.com/api/foo') + ->withHeader('Authorization', 'Basic Zm9vOmJhcjpwb3A='); $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => ["/api", "/bar"], - "realm" => "Protected", - "users" => [ - "foo" => "bar:pop", + 'path' => ['/api', '/bar'], + 'realm' => 'Protected', + 'users' => [ + 'foo' => 'bar:pop', ], ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals("Success", $response->getBody()); + $this->assertEquals('Success', $response->getBody()); } public function testPull59ShouldNotErrorWithMalformedCredentials() { $request = (new ServerRequestFactory()) - ->createServerRequest("GET", "https://example.com/api/foo") - ->withHeader("Authorization", "Basic Zm9vCg=="); /* foo */ + ->createServerRequest('GET', 'https://example.com/api/foo') + ->withHeader('Authorization', 'Basic Zm9vCg=='); // foo $response = (new ResponseFactory())->createResponse(); $auth = new HttpBasicAuthentication([ - "path" => ["/api", "/bar"], - "realm" => "Protected", - "users" => [ - "foo" => "bar", + 'path' => ['/api', '/bar'], + 'realm' => 'Protected', + 'users' => [ + 'foo' => 'bar', ], ]); $next = function (ServerRequestInterface $request, ResponseInterface $response) { - $response->getBody()->write("Success"); + $response->getBody()->write('Success'); + return $response; }; $response = $auth($request, $response, $next); $this->assertEquals(401, $response->getStatusCode()); - $this->assertEquals("", $response->getBody()); + $this->assertEquals('', $response->getBody()); } } diff --git a/tests/PdoAuthenticatorTest.php b/tests/PdoAuthenticatorTest.php index e5d7ed4..8af25d4 100644 --- a/tests/PdoAuthenticatorTest.php +++ b/tests/PdoAuthenticatorTest.php @@ -24,33 +24,36 @@ */ -/** - * @see https://github.com/tuupola/slim-basic-auth - * @license https://www.opensource.org/licenses/mit-license.php - */ - namespace Tuupola\Middleware\HttpBasicAuthentication; +use PDO; use PHPUnit\Framework\TestCase; +/** + * @internal + * + * @see https://github.com/tuupola/slim-basic-auth + * + * @license https://www.opensource.org/licenses/mit-license.php + */ class PdoAuthenticatorTest extends TestCase { public $pdo; public function setup(): void { - $this->pdo = new \PDO("sqlite::memory:"); - //$this->pdo = new \PDO("sqlite:/tmp/test.db"); + $this->pdo = new PDO('sqlite::memory:'); + // $this->pdo = new \PDO("sqlite:/tmp/test.db"); $this->pdo->exec( - "CREATE TABLE users ( + 'CREATE TABLE users ( user VARCHAR(32) NOT NULL, hash VARCHAR(255) NOT NULL - )" + )' ); - $user = "root"; - $hash = password_hash("t00r", PASSWORD_DEFAULT); + $user = 'root'; + $hash = password_hash('t00r', PASSWORD_DEFAULT); $status = $this->pdo->exec( "INSERT INTO users (user, hash) VALUES ('{$user}', '{$hash}')" @@ -60,51 +63,51 @@ public function setup(): void public function testShouldReturnTrue() { $authenticator = new PdoAuthenticator([ - "pdo" => $this->pdo, + 'pdo' => $this->pdo, ]); $this->assertTrue($authenticator([ - "user" => "root", - "password" => "t00r", + 'user' => 'root', + 'password' => 't00r', ])); } public function testShouldReturnFalse() { $authenticator = new PdoAuthenticator([ - "pdo" => $this->pdo, + 'pdo' => $this->pdo, ]); $this->assertFalse($authenticator([ - "user" => "root", - "password" => "nosuch", + 'user' => 'root', + 'password' => 'nosuch', ])); $this->assertFalse($authenticator([ - "user" => "nosuch", - "password" => "nosuch", + 'user' => 'nosuch', + 'password' => 'nosuch', ])); } public function testShouldUseLimit() { $authenticator = new PdoAuthenticator([ - "pdo" => $this->pdo, + 'pdo' => $this->pdo, ]); $this->assertEquals( - "SELECT * FROM users WHERE user = ? LIMIT 1", + 'SELECT * FROM users WHERE user = ? LIMIT 1', $authenticator->sql() ); } public function testShouldUseTop() { - /* Workaround to test without sqlsrv with */ - define("__PHPUNIT_ATTR_DRIVER_NAME__", "sqlsrv"); + // Workaround to test without sqlsrv with + define('__PHPUNIT_ATTR_DRIVER_NAME__', 'sqlsrv'); $authenticator = new PdoAuthenticator([ - "pdo" => $this->pdo, + 'pdo' => $this->pdo, ]); $this->assertEquals( - "SELECT TOP 1 * FROM users WHERE user = ?", + 'SELECT TOP 1 * FROM users WHERE user = ?', $authenticator->sql() ); } diff --git a/tests/RequestMethodRuleTest.php b/tests/RequestMethodRuleTest.php index bb1e4d6..2e76627 100644 --- a/tests/RequestMethodRuleTest.php +++ b/tests/RequestMethodRuleTest.php @@ -24,26 +24,27 @@ */ -/** - * @see https://github.com/tuupola/slim-basic-auth - * @license https://www.opensource.org/licenses/mit-license.php - */ - namespace Tuupola\Middleware\HttpBasicAuthentication; use Laminas\Diactoros\Response; use Laminas\Diactoros\ServerRequest; -use Laminas\Diactoros\ServerRequestFactory; use Laminas\Diactoros\Uri; use PHPUnit\Framework\TestCase; +/** + * @internal + * + * @see https://github.com/tuupola/slim-basic-auth + * + * @license https://www.opensource.org/licenses/mit-license.php + */ class RequestMethodRuleTest extends TestCase { public function testShouldNotAuthenticateOptions() { $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/api")) - ->withMethod("OPTIONS"); + ->withUri(new Uri('https://example.com/api')) + ->withMethod('OPTIONS'); $response = new Response(); $rule = new RequestMethodRule(); @@ -54,8 +55,8 @@ public function testShouldNotAuthenticateOptions() public function testShouldAuthenticatePost() { $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/api")) - ->withMethod("POST"); + ->withUri(new Uri('https://example.com/api')) + ->withMethod('POST'); $response = new Response(); $rule = new RequestMethodRule(); @@ -66,8 +67,8 @@ public function testShouldAuthenticatePost() public function testShouldAuthenticateGet() { $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/api")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/api')) + ->withMethod('GET'); $response = new Response(); $rule = new RequestMethodRule(); @@ -78,14 +79,14 @@ public function testShouldAuthenticateGet() public function testShouldConfigureIgnore() { $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/api")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/api')) + ->withMethod('GET'); $response = new Response(); $rule = new RequestMethodRule(); $rule = new RequestMethodRule([ - "ignore" => ["GET"], + 'ignore' => ['GET'], ]); $this->assertFalse($rule($request)); diff --git a/tests/RequestPathRuleTest.php b/tests/RequestPathRuleTest.php index 3244e9b..4042952 100644 --- a/tests/RequestPathRuleTest.php +++ b/tests/RequestPathRuleTest.php @@ -24,34 +24,34 @@ */ -/** - * @see https://github.com/tuupola/slim-basic-auth - * @license https://www.opensource.org/licenses/mit-license.php - */ - namespace Tuupola\Middleware\HttpBasicAuthentication; -use Laminas\Diactoros\Response; use Laminas\Diactoros\ServerRequest; -use Laminas\Diactoros\ServerRequestFactory; use Laminas\Diactoros\Uri; use PHPUnit\Framework\TestCase; +/** + * @internal + * + * @see https://github.com/tuupola/slim-basic-auth + * + * @license https://www.opensource.org/licenses/mit-license.php + */ class RequestPathRuleTest extends TestCase { public function testShouldAcceptArrayAndStringAsPath() { $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/admin/protected")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/admin/protected')) + ->withMethod('GET'); $rule = new RequestPathRule([ - "path" => "/admin", + 'path' => '/admin', ]); $this->assertTrue($rule($request)); $rule = new RequestPathRule([ - "path" => ["/admin"], + 'path' => ['/admin'], ]); $this->assertTrue($rule($request)); } @@ -59,17 +59,17 @@ public function testShouldAcceptArrayAndStringAsPath() public function testShouldAuthenticateEverything() { $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/')) + ->withMethod('GET'); $rule = new RequestPathRule([ - "path" => "/", + 'path' => '/', ]); $this->assertTrue($rule($request)); $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/api")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/api')) + ->withMethod('GET'); $this->assertTrue($rule($request)); } @@ -77,68 +77,68 @@ public function testShouldAuthenticateEverything() public function testShouldAuthenticateOnlyApi() { $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/')) + ->withMethod('GET'); $rule = new RequestPathRule([ - "path" => "/api", + 'path' => '/api', ]); $this->assertFalse($rule($request)); $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/api")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/api')) + ->withMethod('GET'); $this->assertTrue($rule($request)); } public function testShouldAuthenticateCreateAndList() { - /* Authenticate only create and list actions */ + // Authenticate only create and list actions $rule = new RequestPathRule([ - "path" => ["/api/create", "/api/list"], + 'path' => ['/api/create', '/api/list'], ]); - /* Should not authenticate */ + // Should not authenticate $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/api")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/api')) + ->withMethod('GET'); $this->assertFalse($rule($request)); - /* Should authenticate */ + // Should authenticate $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/api/create")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/api/create')) + ->withMethod('GET'); $this->assertTrue($rule($request)); - /* Should authenticate */ + // Should authenticate $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/api/list")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/api/list')) + ->withMethod('GET'); $this->assertTrue($rule($request)); - /* Should not authenticate */ + // Should not authenticate $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/api/ping")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/api/ping')) + ->withMethod('GET'); $this->assertFalse($rule($request)); } public function testShouldIgnoreLogin() { $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/api")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/api')) + ->withMethod('GET'); $rule = new RequestPathRule([ - "path" => "/api", - "ignore" => ["/api/login"], + 'path' => '/api', + 'ignore' => ['/api/login'], ]); $this->assertTrue($rule($request)); $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/api/login")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/api/login')) + ->withMethod('GET'); $this->assertFalse($rule($request)); } @@ -146,31 +146,31 @@ public function testShouldIgnoreLogin() public function testBug50ShouldAuthenticateMultipleSlashes() { $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/')) + ->withMethod('GET'); $rule = new RequestPathRule([ - "path" => "/v1/api", + 'path' => '/v1/api', ]); $this->assertFalse($rule($request)); $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/v1/api")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/v1/api')) + ->withMethod('GET'); $this->assertTrue($rule($request)); $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/v1//api")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/v1//api')) + ->withMethod('GET'); $this->assertTrue($rule($request)); $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com/v1//////api")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com/v1//////api')) + ->withMethod('GET'); $this->assertTrue($rule($request)); $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com//v1/api")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com//v1/api')) + ->withMethod('GET'); $this->assertTrue($rule($request)); $request = (new ServerRequest()) - ->withUri(new Uri("https://example.com//////v1/api")) - ->withMethod("GET"); + ->withUri(new Uri('https://example.com//////v1/api')) + ->withMethod('GET'); $this->assertTrue($rule($request)); } } diff --git a/tests/lib/FalseAuthenticator.php b/tests/lib/FalseAuthenticator.php index 5594db0..77b31f1 100644 --- a/tests/lib/FalseAuthenticator.php +++ b/tests/lib/FalseAuthenticator.php @@ -24,15 +24,15 @@ */ -/** - * @see https://github.com/tuupola/slim-basic-auth - * @license https://www.opensource.org/licenses/mit-license.php - */ - namespace Test; use Tuupola\Middleware\HttpBasicAuthentication\AuthenticatorInterface; +/** + * @see https://github.com/tuupola/slim-basic-auth + * + * @license https://www.opensource.org/licenses/mit-license.php + */ class FalseAuthenticator implements AuthenticatorInterface { public function __invoke(array $arguments): bool diff --git a/tests/lib/FalseRule.php b/tests/lib/FalseRule.php index 275d394..709428d 100644 --- a/tests/lib/FalseRule.php +++ b/tests/lib/FalseRule.php @@ -26,6 +26,7 @@ /** * @see https://github.com/tuupola/slim-basic-auth + * * @license https://www.opensource.org/licenses/mit-license.php */ diff --git a/tests/lib/TrueAuthenticator.php b/tests/lib/TrueAuthenticator.php index 8ede907..53f4fa8 100644 --- a/tests/lib/TrueAuthenticator.php +++ b/tests/lib/TrueAuthenticator.php @@ -24,15 +24,15 @@ */ -/** - * @see https://github.com/tuupola/slim-basic-auth - * @license https://www.opensource.org/licenses/mit-license.php - */ - namespace Test; use Tuupola\Middleware\HttpBasicAuthentication\AuthenticatorInterface; +/** + * @see https://github.com/tuupola/slim-basic-auth + * + * @license https://www.opensource.org/licenses/mit-license.php + */ class TrueAuthenticator implements AuthenticatorInterface { public function __invoke(array $arguments): bool diff --git a/tests/lib/TrueRule.php b/tests/lib/TrueRule.php index ad813be..734271c 100644 --- a/tests/lib/TrueRule.php +++ b/tests/lib/TrueRule.php @@ -24,16 +24,16 @@ */ -/** - * @see https://github.com/tuupola/slim-basic-auth - * @license https://www.opensource.org/licenses/mit-license.php - */ - namespace Test; use Psr\Http\Message\ServerRequestInterface; use Tuupola\Middleware\HttpBasicAuthentication\RuleInterface; +/** + * @see https://github.com/tuupola/slim-basic-auth + * + * @license https://www.opensource.org/licenses/mit-license.php + */ class TrueRule implements RuleInterface { public function __invoke(ServerRequestInterface $request): bool