63 lines
1.6 KiB
PHP
63 lines
1.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* This file is part of Slim HTTP Basic Authentication middleware
|
|
*
|
|
* Copyright (c) 2013-2018 Mika Tuupola
|
|
*
|
|
* Licensed under the MIT license:
|
|
* http://www.opensource.org/licenses/mit-license.php
|
|
*
|
|
* Project home:
|
|
* https://github.com/tuupola/slim-basic-auth
|
|
*
|
|
*/
|
|
|
|
namespace Tuupola\Middleware\HttpBasicAuthentication;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
/**
|
|
* Rule to decide by request path whether the request should be authenticated or not.
|
|
*/
|
|
|
|
final class RequestPathRule implements RuleInterface
|
|
{
|
|
/**
|
|
* Stores all the options passed to the rule.
|
|
*/
|
|
private $options = [
|
|
"path" => ["/"],
|
|
"ignore" => []
|
|
];
|
|
|
|
public function __construct($options = [])
|
|
{
|
|
$this->options = array_merge($this->options, $options);
|
|
}
|
|
|
|
public function __invoke(ServerRequestInterface $request): bool
|
|
{
|
|
$uri = "/" . $request->getUri()->getPath();
|
|
$uri = 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)) {
|
|
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)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|