|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LM\AuthAbstractor\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
use LM\AuthAbstractor\Model\IAuthenticationCallback; |
|
10
|
|
|
use LM\AuthAbstractor\Model\IAuthenticationProcess; |
|
11
|
|
|
use LM\AuthAbstractor\Model\IAuthentifierResponse; |
|
12
|
|
|
use LM\AuthAbstractor\Implementation\AuthentifierResponse; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This is a class used by AuthenticationKernel to handle requests. |
|
16
|
|
|
* |
|
17
|
|
|
* @internal |
|
18
|
|
|
*/ |
|
19
|
|
|
class AuthenticationProcessHandler |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var ContainerInterface */ |
|
22
|
|
|
private $container; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param ContainerInterface $container The container of auth-abtractor. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(ContainerInterface $container) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->container = $container; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Used by the kernel to "apply" the current HTTP request on the current challenge. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function handleAuthenticationProcess( |
|
36
|
|
|
?ServerRequestInterface $httpRequest, |
|
37
|
|
|
IAuthenticationProcess $process, |
|
38
|
|
|
IAuthenticationCallback $callback |
|
39
|
|
|
): IAuthentifierResponse { |
|
40
|
|
|
if ($process->isOngoing()) { |
|
41
|
|
|
$challenge = $this |
|
42
|
|
|
->container |
|
43
|
|
|
->get($process->getCurrentChallenge()) |
|
44
|
|
|
; |
|
45
|
|
|
$challengeResponse = $challenge->process($process, $httpRequest); |
|
46
|
|
|
|
|
47
|
|
|
$psrHttpResponse = $challengeResponse->getHttpResponse(); |
|
48
|
|
|
|
|
49
|
|
|
if ($challengeResponse->isFinished()) { |
|
50
|
|
|
return new AuthentifierResponse( |
|
51
|
|
|
$challengeResponse |
|
52
|
|
|
->getAuthenticationProcess() |
|
53
|
|
|
->resetNFailedAttempts() |
|
54
|
|
|
->setToNextChallenge(), |
|
55
|
|
|
null |
|
56
|
|
|
); |
|
57
|
|
|
} elseif ($challengeResponse->isFailedAttempt()) { |
|
58
|
|
|
$updatedProcess = $challengeResponse |
|
59
|
|
|
->getAuthenticationProcess() |
|
60
|
|
|
->incrementNFailedAttempts() |
|
61
|
|
|
; |
|
62
|
|
|
if ($updatedProcess->isFailed()) { |
|
63
|
|
|
return new AuthentifierResponse( |
|
64
|
|
|
$updatedProcess, |
|
65
|
|
|
null |
|
66
|
|
|
); |
|
67
|
|
|
} else { |
|
68
|
|
|
return new AuthentifierResponse( |
|
69
|
|
|
$updatedProcess, |
|
70
|
|
|
$psrHttpResponse |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
} else { |
|
74
|
|
|
return new AuthentifierResponse( |
|
75
|
|
|
$challengeResponse |
|
76
|
|
|
->getAuthenticationProcess(), |
|
77
|
|
|
$psrHttpResponse |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
} elseif ($process->isFailed()) { |
|
81
|
|
|
return new AuthentifierResponse( |
|
82
|
|
|
$process, |
|
83
|
|
|
$callback->handleFailedProcess($process) |
|
84
|
|
|
); |
|
85
|
|
|
} elseif ($process->isSucceeded()) { |
|
86
|
|
|
return new AuthentifierResponse( |
|
87
|
|
|
$process, |
|
88
|
|
|
$callback->handleSuccessfulProcess($process) |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: