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
|
|
|
namespace Phauthentic\Authentication\Middleware; |
16
|
|
|
|
17
|
|
|
use Phauthentic\Authentication\AuthenticationServiceProviderInterface; |
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
19
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
20
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
21
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
22
|
|
|
use RuntimeException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* PSR 15 Authenticator Middleware |
26
|
|
|
*/ |
27
|
|
|
class AuthenticationMiddleware implements MiddlewareInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var \Phauthentic\Authentication\AuthenticationServiceProviderInterface |
31
|
|
|
*/ |
32
|
|
|
protected AuthenticationServiceProviderInterface $provider; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected string $serviceAttribute = 'authentication'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Request attribute for the identity |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected string $identityAttribute = 'identity'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor. |
48
|
|
|
* |
49
|
|
|
* @param \Phauthentic\Authentication\AuthenticationServiceProviderInterface $provider Provider. |
50
|
|
|
*/ |
51
|
2 |
|
public function __construct( |
52
|
|
|
AuthenticationServiceProviderInterface $provider |
53
|
|
|
) { |
54
|
2 |
|
$this->provider = $provider; |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Sets request attribute name for authentication service. |
59
|
|
|
* |
60
|
|
|
* @param string $attribute Attribute name. |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function setServiceAttribute(string $attribute): self |
64
|
|
|
{ |
65
|
|
|
$this->serviceAttribute = $attribute; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Sets the identity attribute |
72
|
|
|
* |
73
|
|
|
* @param string $attribute Attribute name |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function setIdentityAttribute(string $attribute): self |
77
|
|
|
{ |
78
|
|
|
$this->identityAttribute = $attribute; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Adds an attribute to the request and returns a modified request. |
85
|
|
|
* |
86
|
|
|
* @param ServerRequestInterface $request Request. |
87
|
|
|
* @param string $name Attribute name. |
88
|
|
|
* @param mixed $value Attribute value. |
89
|
|
|
* @return ServerRequestInterface |
90
|
|
|
* @throws RuntimeException When attribute is present. |
91
|
|
|
*/ |
92
|
2 |
|
protected function addAttribute(ServerRequestInterface $request, string $name, $value): ServerRequestInterface |
93
|
|
|
{ |
94
|
2 |
|
if ($request->getAttribute($name)) { |
95
|
|
|
$message = sprintf('Request attribute `%s` already exists.', $name); |
96
|
|
|
throw new RuntimeException($message); |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
return $request->withAttribute($name, $value); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
* |
105
|
|
|
* @throws RuntimeException When request attribute exists. |
106
|
|
|
*/ |
107
|
2 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
108
|
|
|
{ |
109
|
2 |
|
$service = $this->provider->getAuthenticationService($request); |
110
|
2 |
|
$request = $this->addAttribute($request, $this->serviceAttribute, $service); |
111
|
|
|
|
112
|
2 |
|
$service->authenticate($request); |
113
|
|
|
|
114
|
2 |
|
$identity = $service->getIdentity(); |
115
|
2 |
|
$request = $this->addAttribute($request, $this->identityAttribute, $identity); |
116
|
|
|
|
117
|
2 |
|
$response = $handler->handle($request); |
118
|
|
|
|
119
|
2 |
|
$result = $service->persistIdentity($request, $response); |
120
|
|
|
|
121
|
2 |
|
return $result->getResponse(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|