1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LM\AuthAbstractor\Implementation; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use LM\AuthAbstractor\Model\IAuthenticationProcess; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; |
11
|
|
|
use LM\AuthAbstractor\Model\IChallengeResponse; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The only use of this class is to be returned by challenges. It accepts |
15
|
|
|
* Symfony responses objects but return ResponseInterface objects. |
16
|
|
|
* |
17
|
|
|
* They contain the HTTP response, the new authentication process, and whether |
18
|
|
|
* the request was a failed attempt and is finished. For instance, a challenge |
19
|
|
|
* can return after having checked that the user entered a valid password: |
20
|
|
|
* |
21
|
|
|
* return new ChallengeResponse( |
22
|
|
|
* $authProcess, // the updated authentication process |
23
|
|
|
* null, // the HTTP response |
24
|
|
|
* true, // whether the request was a submission |
25
|
|
|
* true // whether the submission was valid (e.g. a valid password) |
26
|
|
|
* ); |
27
|
|
|
* |
28
|
|
|
* @todo Move in Implementation |
29
|
|
|
*/ |
30
|
|
|
class ChallengeResponse implements IChallengeResponse |
31
|
|
|
{ |
32
|
|
|
/** @var IAuthenticationProcess */ |
33
|
|
|
private $authenticationProcess; |
34
|
|
|
|
35
|
|
|
/** @var null|Response */ |
36
|
|
|
private $httpResponse; |
37
|
|
|
|
38
|
|
|
/** @var bool */ |
39
|
|
|
private $isFailedAttempt; |
40
|
|
|
|
41
|
|
|
/** @var bool */ |
42
|
|
|
private $isFinished; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param IAuthenticationProcess $authenticationProcess The authentication |
46
|
|
|
* process. |
47
|
|
|
* @param null|Response $httpResponse The HTTP response. |
48
|
|
|
* @param bool $isFailedAttempt Whether the HTTP request was a failed |
49
|
|
|
* submission. |
50
|
|
|
* @param bool $isFinished Whether the current challenge is finished. |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
IAuthenticationProcess $authenticationProcess, |
54
|
|
|
?Response $httpResponse, |
55
|
|
|
bool $isFailedAttempt, |
56
|
|
|
bool $isFinished |
57
|
|
|
) { |
58
|
|
|
$this->authenticationProcess = $authenticationProcess; |
59
|
|
|
$this->httpResponse = $httpResponse; |
60
|
|
|
$this->isFailedAttempt = $isFailedAttempt; |
61
|
|
|
$this->isFinished = $isFinished; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getAuthenticationProcess(): IAuthenticationProcess |
65
|
|
|
{ |
66
|
|
|
return $this->authenticationProcess; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getHttpResponse(): ?ResponseInterface |
70
|
|
|
{ |
71
|
|
|
if (null === $this->httpResponse) { |
72
|
|
|
return null; |
73
|
|
|
} else { |
74
|
|
|
$diactorosFactory = new DiactorosFactory(); |
75
|
|
|
|
76
|
|
|
return $diactorosFactory->createResponse($this->httpResponse); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function isFailedAttempt(): bool |
81
|
|
|
{ |
82
|
|
|
return $this->isFailedAttempt; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function isFinished(): bool |
86
|
|
|
{ |
87
|
|
|
return $this->isFinished; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|