AuthenticationMiddleware   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 65.22%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
eloc 21
c 4
b 0
f 0
dl 0
loc 95
ccs 15
cts 23
cp 0.6522
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setServiceAttribute() 0 5 1
A setIdentityAttribute() 0 5 1
A addAttribute() 0 8 2
A process() 0 15 1
A __construct() 0 4 1
1
<?php
2
3
/**
4
 * Copyright (c) Phauthentic (https://github.com/Phauthentic)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Phauthentic (https://github.com/Phauthentic)
11
 * @link          https://github.com/Phauthentic
12
 * @license       https://opensource.org/licenses/mit-license.php MIT License
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phauthentic\Authentication\Middleware;
18
19
use Phauthentic\Authentication\AuthenticationServiceProviderInterface;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use Psr\Http\Server\MiddlewareInterface;
23
use Psr\Http\Server\RequestHandlerInterface;
24
use RuntimeException;
25
26
/**
27
 * PSR 15 Authenticator Middleware
28
 */
29
class AuthenticationMiddleware implements MiddlewareInterface
30
{
31
    /**
32
     * @var \Phauthentic\Authentication\AuthenticationServiceProviderInterface
33
     */
34
    protected AuthenticationServiceProviderInterface $provider;
35
36
    /**
37
     * @var string
38
     */
39
    protected string $serviceAttribute = 'authentication';
40
41
    /**
42
     * Request attribute for the identity
43
     *
44
     * @var string
45
     */
46
    protected string $identityAttribute = 'identity';
47
48
    /**
49
     * Constructor.
50
     *
51
     * @param \Phauthentic\Authentication\AuthenticationServiceProviderInterface $provider Provider.
52
     */
53
    public function __construct(
54 4
        AuthenticationServiceProviderInterface $provider
55
    ) {
56
        $this->provider = $provider;
57 4
    }
58 4
59
    /**
60
     * Sets request attribute name for authentication service.
61
     *
62
     * @param string $attribute Attribute name.
63
     * @return $this
64
     */
65
    public function setServiceAttribute(string $attribute): self
66
    {
67
        $this->serviceAttribute = $attribute;
68
69
        return $this;
70
    }
71
72
    /**
73
     * Sets the identity attribute
74
     *
75
     * @param string $attribute Attribute name
76
     * @return $this
77
     */
78
    public function setIdentityAttribute(string $attribute): self
79
    {
80
        $this->identityAttribute = $attribute;
81
82
        return $this;
83
    }
84
85
    /**
86
     * Adds an attribute to the request and returns a modified request.
87
     *
88
     * @param ServerRequestInterface $request Request.
89
     * @param string $name Attribute name.
90
     * @param mixed $value Attribute value.
91
     * @return ServerRequestInterface
92
     * @throws RuntimeException When attribute is present.
93
     */
94
    protected function addAttribute(ServerRequestInterface $request, string $name, $value): ServerRequestInterface
95 4
    {
96
        if ($request->getAttribute($name)) {
97 4
            $message = sprintf('Request attribute `%s` already exists.', $name);
98
            throw new RuntimeException($message);
99
        }
100
101
        return $request->withAttribute($name, $value);
102 4
    }
103
104
    /**
105
     * {@inheritDoc}
106
     *
107
     * @throws RuntimeException When request attribute exists.
108
     */
109
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
110 4
    {
111
        $service = $this->provider->getAuthenticationService($request);
112 4
        $request = $this->addAttribute($request, $this->serviceAttribute, $service);
113 4
114
        $service->authenticate($request);
115 4
116
        $identity = $service->getIdentity();
117 4
        $request = $this->addAttribute($request, $this->identityAttribute, $identity);
118 4
119
        $response = $handler->handle($request);
120 4
121 4
        $result = $service->persistIdentity($request, $response);
122 4
123
        return $result->getResponse();
124 4
    }
125
}
126