Passed
Pull Request — master (#18)
by Alexander
01:20
created

Authentication::withOptional()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Auth\Middleware;
6
7
use Psr\Http\Message\ResponseFactoryInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Yiisoft\Auth\AuthenticationMethodInterface;
13
use Yiisoft\Auth\Handler\AuthenticationFailureHandler;
14
use Yiisoft\Strings\StringHelper;
15
16
/**
17
 * Authentication middleware tries to authenticate and identity using request data.
18
 * If identity is found, it is set to request attribute allowing further middleware to obtain and use it.
19
 * If identity is not found failure handler is called. By default it is {@see AuthenticationFailureHandler}.
20
 */
21
final class Authentication implements MiddlewareInterface
22
{
23
    private AuthenticationMethodInterface $authenticationMethod;
24
25
    /**
26
     * @var RequestHandlerInterface A handler that is called when there is a failure authenticating an identity.
27
     */
28
    private RequestHandlerInterface $failureHandler;
29
30
    private array $optional = [];
31
32 4
    public function __construct(
33
        AuthenticationMethodInterface $authenticationMethod,
34
        ResponseFactoryInterface $responseFactory,
35
        RequestHandlerInterface $authenticationFailureHandler = null
36
    ) {
37 4
        $this->authenticationMethod = $authenticationMethod;
38 4
        $this->failureHandler = $authenticationFailureHandler ?? new AuthenticationFailureHandler(
39 3
            $responseFactory
40
        );
41 4
    }
42
43 4
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
44
    {
45 4
        $identity = $this->authenticationMethod->authenticate($request);
46 4
        $request = $request->withAttribute(self::class, $identity);
47
48 4
        if ($identity === null && !$this->isOptional($request)) {
49 2
            return $this->authenticationMethod->challenge(
50 2
                $this->failureHandler->handle($request)
51
            );
52
        }
53
54 2
        return $handler->handle($request);
55
    }
56
57 1
    public function withOptional(array $optional): self
58
    {
59 1
        $new = clone $this;
60 1
        $new->optional = $optional;
61 1
        return $new;
62
    }
63
64
    /**
65
     * Checks, whether authentication is optional for the given action.
66
     */
67 3
    private function isOptional(ServerRequestInterface $request): bool
68
    {
69 3
        $path = $request->getUri()->getPath();
70 3
        foreach ($this->optional as $pattern) {
71 1
            if (StringHelper::matchWildcard($pattern, $path)) {
72 1
                return true;
73
            }
74
        }
75 2
        return false;
76
    }
77
}
78