Skip to content

Commit 13e7877

Browse files
committed
Track last result when processing request as PSR-15 middleware
1 parent 06529a8 commit 13e7877

13 files changed

+170
-150
lines changed

src/Adapter/BrowserSessionAdapter.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ public function applyToResponse(ResponseInterface $response, TransportInterface
122122
} elseif ($transport instanceof DeauthenticationTransportInterface
123123
|| $transport instanceof CleanUpTransportInterface
124124
) {
125-
return ($this->cookies->createRemover($this->session_cookie_name))->applyToResponse($response);
125+
return $this->cookies
126+
->createRemover($this->session_cookie_name)
127+
->applyToResponse($response);
126128
}
127129

128130
return $response;

src/Authentication.php

+29-20
Original file line numberDiff line numberDiff line change
@@ -114,36 +114,38 @@ public function __invoke(
114114
return $response;
115115
}
116116

117+
public $lastProcessingResult;
118+
117119
public function process(
118120
ServerRequestInterface $request,
119121
RequestHandlerInterface $handler
120122
): ResponseInterface
121123
{
122-
$auth_result = $this->authenticatedUsingAdapters($request);
124+
$this->lastProcessingResult = $this->authenticatedUsingAdapters($request);
123125

124-
if ($auth_result instanceof TransportInterface
125-
&& !$auth_result->isEmpty()
126+
if ($this->lastProcessingResult instanceof TransportInterface
127+
&& !$this->lastProcessingResult->isEmpty()
126128
) {
127-
if ($auth_result instanceof AuthenticationTransportInterface) {
128-
$this->setAuthenticatedUser($auth_result->getAuthenticatedUser());
129-
$this->setAuthenticatedWith($auth_result->getAuthenticatedWith());
129+
if ($this->lastProcessingResult instanceof AuthenticationTransportInterface) {
130+
$this->setAuthenticatedUser($this->lastProcessingResult->getAuthenticatedUser());
131+
$this->setAuthenticatedWith($this->lastProcessingResult->getAuthenticatedWith());
130132

131133
$this->triggerEvent(
132134
'user_authenticated',
133-
$auth_result->getAuthenticatedUser(),
134-
$auth_result->getAuthenticatedWith()
135+
$this->lastProcessingResult->getAuthenticatedUser(),
136+
$this->lastProcessingResult->getAuthenticatedWith()
135137
);
136138
}
137139

138-
$request = $auth_result->applyToRequest($request);
140+
$request = $this->lastProcessingResult->applyToRequest($request);
139141
}
140142

141143
$response = $handler->handle($request);
142144

143-
if ($auth_result instanceof TransportInterface
144-
&& !$auth_result->isEmpty()
145+
if ($this->lastProcessingResult instanceof TransportInterface
146+
&& !$this->lastProcessingResult->isEmpty()
145147
) {
146-
$response = $auth_result->applyToResponse($response);
148+
$response = $this->lastProcessingResult->applyToResponse($response);
147149
}
148150

149151
return $response;
@@ -158,11 +160,14 @@ public function authorize(
158160
{
159161
try {
160162
$user = $authorizer->verifyCredentials($credentials);
161-
$authenticated_with = $adapter->authenticate($user, $credentials);
163+
$authenticatedWith = $adapter->authenticate($user, $credentials);
162164

163-
$this->triggerEvent('user_authorized', $user, $authenticated_with);
165+
$this->triggerEvent('user_authorized', $user, $authenticatedWith);
164166

165-
return new AuthorizationTransport($adapter, $user, $authenticated_with, $payload);
167+
$authorizationResult = new AuthorizationTransport($adapter, $user, $authenticatedWith, $payload);
168+
$this->lastProcessingResult = $authorizationResult;
169+
170+
return $authorizationResult;
166171
} catch (Exception $e) {
167172
$this->triggerEvent('user_authorization_failed', $credentials, $e);
168173

@@ -172,14 +177,18 @@ public function authorize(
172177

173178
public function terminate(
174179
AdapterInterface $adapter,
175-
AuthenticationResultInterface $authenticated_with
180+
AuthenticationResultInterface $authenticatedWith
176181
): TransportInterface
177182
{
178-
$termination_result = $adapter->terminate($authenticated_with);
183+
$terminationResult = $adapter->terminate($authenticatedWith);
184+
185+
$this->setAuthenticatedUser(null);
186+
$this->setAuthenticatedWith(null);
179187

180-
$this->triggerEvent('user_deauthenticated', $authenticated_with);
188+
$this->triggerEvent('user_deauthenticated', $authenticatedWith);
189+
$this->lastProcessingResult = $terminationResult;
181190

182-
return $termination_result;
191+
return $terminationResult;
183192
}
184193

185194
/**
@@ -242,7 +251,7 @@ public function getAuthenticatedWith(): ?AuthenticationResultInterface
242251
return $this->authenticated_with;
243252
}
244253

245-
public function setAuthenticatedWith(AuthenticationResultInterface $value): AuthenticationInterface
254+
public function setAuthenticatedWith(?AuthenticationResultInterface $value): AuthenticationInterface
246255
{
247256
$this->authenticated_with = $value;
248257

src/AuthenticationInterface.php

+2-46
Original file line numberDiff line numberDiff line change
@@ -36,68 +36,24 @@ public function authorize(
3636

3737
public function terminate(
3838
AdapterInterface $adapter,
39-
AuthenticationResultInterface $authenticated_with
39+
AuthenticationResultInterface $authenticatedWith
4040
): TransportInterface;
4141

4242
/**
4343
* @return AdapterInterface[]|iterable
4444
*/
4545
public function getAdapters(): iterable;
4646

47-
/**
48-
* Return authenticated in user.
49-
*
50-
* @return AuthenticatedUserInterface
51-
*/
5247
public function getAuthenticatedUser(): ?AuthenticatedUserInterface;
53-
54-
/**
55-
* Override authentication adapter and force set logged user for this request.
56-
*
57-
* @param AuthenticatedUserInterface|null $user
58-
* @return $this
59-
*/
6048
public function setAuthenticatedUser(AuthenticatedUserInterface $user = null): AuthenticationInterface;
6149

62-
/**
63-
* @return AuthenticationResultInterface|null
64-
*/
6550
public function getAuthenticatedWith(): ?AuthenticationResultInterface;
51+
public function setAuthenticatedWith(?AuthenticationResultInterface $value): AuthenticationInterface;
6652

67-
/**
68-
* @param AuthenticationResultInterface $value
69-
* @return $this
70-
*/
71-
public function setAuthenticatedWith(AuthenticationResultInterface $value): AuthenticationInterface;
72-
73-
/**
74-
* @param callable $value
75-
* @return $this
76-
*/
7753
public function onUserAuthenticated(callable $value): AuthenticationInterface;
78-
79-
/**
80-
* @param callable $value
81-
* @return $this
82-
*/
8354
public function onUserAuthorized(callable $value): AuthenticationInterface;
84-
85-
/**
86-
* @param callable $value
87-
* @return $this
88-
*/
8955
public function onUserAuthorizationFailed(callable $value): AuthenticationInterface;
90-
91-
/**
92-
* @param callable $value
93-
* @return $this
94-
*/
9556
public function onUserSet(callable $value): AuthenticationInterface;
96-
97-
/**
98-
* @param callable $value
99-
* @return $this
100-
*/
10157
public function onUserDeauthenticated(callable $value): AuthenticationInterface;
10258

10359
/**

src/AuthenticationResult/Transport/Authentication/AuthenticationTransport.php

+10-27
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,39 @@
66
* (c) A51 doo <[email protected]>. All rights reserved.
77
*/
88

9+
declare(strict_types=1);
10+
911
namespace ActiveCollab\Authentication\AuthenticationResult\Transport\Authentication;
1012

1113
use ActiveCollab\Authentication\Adapter\AdapterInterface;
1214
use ActiveCollab\Authentication\AuthenticatedUser\AuthenticatedUserInterface;
1315
use ActiveCollab\Authentication\AuthenticationResult\AuthenticationResultInterface;
1416
use ActiveCollab\Authentication\AuthenticationResult\Transport\Transport;
1517

16-
/**
17-
* @package ActiveCollab\Authentication\AuthenticationResult\Transport\Authentication
18-
*/
1918
class AuthenticationTransport extends Transport implements AuthenticationTransportInterface
2019
{
21-
/**
22-
* @var AuthenticatedUserInterface
23-
*/
2420
private $authenticated_user;
25-
26-
/**
27-
* @var AuthenticationResultInterface
28-
*/
2921
private $authenticated_with;
3022

31-
/**
32-
* AuthenticationTransport constructor.
33-
*
34-
* @param AdapterInterface $adapter
35-
* @param AuthenticatedUserInterface|null $authenticated_user
36-
* @param AuthenticationResultInterface|null $authenticated_with
37-
* @param mixed $payload
38-
*/
39-
public function __construct(AdapterInterface $adapter, AuthenticatedUserInterface $authenticated_user = null, AuthenticationResultInterface $authenticated_with = null, $payload = null)
23+
public function __construct(
24+
AdapterInterface $adapter,
25+
AuthenticatedUserInterface $authenticated_user = null,
26+
AuthenticationResultInterface $authenticated_with = null,
27+
$payload = null
28+
)
4029
{
4130
parent::__construct($adapter, $payload);
4231

4332
$this->authenticated_user = $authenticated_user;
4433
$this->authenticated_with = $authenticated_with;
4534
}
4635

47-
/**
48-
* {@inheritdoc}
49-
*/
50-
public function getAuthenticatedUser()
36+
public function getAuthenticatedUser(): ?AuthenticatedUserInterface
5137
{
5238
return $this->authenticated_user;
5339
}
5440

55-
/**
56-
* {@inheritdoc}
57-
*/
58-
public function getAuthenticatedWith()
41+
public function getAuthenticatedWith(): ?AuthenticationResultInterface
5942
{
6043
return $this->authenticated_with;
6144
}

src/AuthenticationResult/Transport/Authentication/AuthenticationTransportInterface.php

+4-12
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,16 @@
66
* (c) A51 doo <[email protected]>. All rights reserved.
77
*/
88

9+
declare(strict_types=1);
10+
911
namespace ActiveCollab\Authentication\AuthenticationResult\Transport\Authentication;
1012

1113
use ActiveCollab\Authentication\AuthenticatedUser\AuthenticatedUserInterface;
1214
use ActiveCollab\Authentication\AuthenticationResult\AuthenticationResultInterface;
1315
use ActiveCollab\Authentication\AuthenticationResult\Transport\TransportInterface;
1416

15-
/**
16-
* @package ActiveCollab\Authentication\AuthenticationResult\Transport\Authentication
17-
*/
1817
interface AuthenticationTransportInterface extends TransportInterface
1918
{
20-
/**
21-
* @return AuthenticatedUserInterface|null
22-
*/
23-
public function getAuthenticatedUser();
24-
25-
/**
26-
* @return AuthenticationResultInterface|null
27-
*/
28-
public function getAuthenticatedWith();
19+
public function getAuthenticatedUser(): ?AuthenticatedUserInterface;
20+
public function getAuthenticatedWith(): ?AuthenticationResultInterface;
2921
}

src/AuthenticationResult/Transport/Authorization/AuthorizationTransport.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
* (c) A51 doo <[email protected]>. All rights reserved.
77
*/
88

9+
declare(strict_types=1);
10+
911
namespace ActiveCollab\Authentication\AuthenticationResult\Transport\Authorization;
1012

1113
use ActiveCollab\Authentication\Adapter\AdapterInterface;
1214
use ActiveCollab\Authentication\AuthenticatedUser\AuthenticatedUserInterface;
1315
use ActiveCollab\Authentication\AuthenticationResult\AuthenticationResultInterface;
1416
use ActiveCollab\Authentication\AuthenticationResult\Transport\Transport;
1517

16-
/**
17-
* @package ActiveCollab\Authentication\AuthenticationResult\Transport\Authorization
18-
*/
1918
class AuthorizationTransport extends Transport implements AuthorizationTransportInterface
2019
{
2120
/**
@@ -44,10 +43,7 @@ public function __construct(AdapterInterface $adapter, AuthenticatedUserInterfac
4443
$this->authenticated_with = $authenticated_with;
4544
}
4645

47-
/**
48-
* {@inheritdoc}
49-
*/
50-
public function getAuthenticatedUser()
46+
public function getAuthenticatedUser(): ?AuthenticatedUserInterface
5147
{
5248
return $this->authenticated_user;
5349
}

src/AuthenticationResult/Transport/Authorization/AuthorizationTransportInterface.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
* (c) A51 doo <[email protected]>. All rights reserved.
77
*/
88

9+
declare(strict_types=1);
10+
911
namespace ActiveCollab\Authentication\AuthenticationResult\Transport\Authorization;
1012

1113
use ActiveCollab\Authentication\AuthenticatedUser\AuthenticatedUserInterface;
1214
use ActiveCollab\Authentication\AuthenticationResult\AuthenticationResultInterface;
1315
use ActiveCollab\Authentication\AuthenticationResult\Transport\TransportInterface;
1416

15-
/**
16-
* @package ActiveCollab\Authentication\AuthenticationResult\Transport\Authorization
17-
*/
1817
interface AuthorizationTransportInterface extends TransportInterface
1918
{
2019
/**

src/AuthenticationResult/Transport/CleanUp/CleanUpTransport.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
* (c) A51 doo <[email protected]>. All rights reserved.
77
*/
88

9+
declare(strict_types=1);
10+
911
namespace ActiveCollab\Authentication\AuthenticationResult\Transport\CleanUp;
1012

1113
use ActiveCollab\Authentication\AuthenticationResult\Transport\Transport;
1214

13-
/**
14-
* @package ActiveCollab\Authentication\AuthenticationResult\Transport\Deauthentication
15-
*/
1615
class CleanUpTransport extends Transport implements CleanUpTransportInterface
1716
{
1817
}

src/AuthenticationResult/Transport/CleanUp/CleanUpTransportInterface.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
* (c) A51 doo <[email protected]>. All rights reserved.
77
*/
88

9+
declare(strict_types=1);
10+
911
namespace ActiveCollab\Authentication\AuthenticationResult\Transport\CleanUp;
1012

1113
use ActiveCollab\Authentication\AuthenticationResult\Transport\TransportInterface;
1214

13-
/**
14-
* @package ActiveCollab\Authentication\AuthenticationResult\Transport\CleanUp
15-
*/
1615
interface CleanUpTransportInterface extends TransportInterface
1716
{
1817
}

src/AuthenticationResult/Transport/Deauthentication/DeauthenticationTransport.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
* (c) A51 doo <[email protected]>. All rights reserved.
77
*/
88

9+
declare(strict_types=1);
10+
911
namespace ActiveCollab\Authentication\AuthenticationResult\Transport\Deauthentication;
1012

1113
use ActiveCollab\Authentication\AuthenticationResult\Transport\Transport;
1214

13-
/**
14-
* @package ActiveCollab\Authentication\AuthenticationResult\Transport\Deauthentication
15-
*/
1615
class DeauthenticationTransport extends Transport implements DeauthenticationTransportInterface
1716
{
1817
}

src/AuthenticationResult/Transport/Deauthentication/DeauthenticationTransportInterface.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
* (c) A51 doo <[email protected]>. All rights reserved.
77
*/
88

9+
declare(strict_types=1);
10+
911
namespace ActiveCollab\Authentication\AuthenticationResult\Transport\Deauthentication;
1012

1113
use ActiveCollab\Authentication\AuthenticationResult\Transport\TransportInterface;
1214

13-
/**
14-
* @package ActiveCollab\Authentication\AuthenticationResult\Transport\Deauthentication
15-
*/
1615
interface DeauthenticationTransportInterface extends TransportInterface
1716
{
1817
}

0 commit comments

Comments
 (0)