1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2014 SURFnet bv |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
|
|
|
|
20
|
|
|
|
21
|
|
|
namespace Surfnet\StepupSelfService\SelfServiceBundle\Service\SmsSecondFactor; |
22
|
|
|
|
23
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\InvalidArgumentException; |
24
|
|
|
|
25
|
|
|
/** |
|
|
|
|
26
|
|
|
* @SuppressWarnings(PHPMD.TooManyMethods) |
27
|
|
|
*/ |
|
|
|
|
28
|
|
|
final class ProofOfPossessionResult |
29
|
|
|
{ |
30
|
|
|
public const STATUS_CHALLENGE_OK = 0; |
31
|
|
|
public const STATUS_INCORRECT_CHALLENGE = 1; |
32
|
|
|
public const STATUS_CHALLENGE_EXPIRED = 2; |
33
|
|
|
public const STATUS_TOO_MANY_ATTEMPTS = 3; |
34
|
|
|
|
35
|
|
|
/** |
|
|
|
|
36
|
|
|
* @param int $status One of |
|
|
|
|
37
|
|
|
* @param string|null $secondFactorId |
|
|
|
|
38
|
|
|
*/ |
39
|
|
|
private function __construct(private $status, private $secondFactorId = null) |
40
|
|
|
{ |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
|
|
|
|
44
|
|
|
* @return ProofOfPossessionResult |
45
|
|
|
*/ |
46
|
|
|
public static function challengeExpired(): self |
47
|
|
|
{ |
48
|
|
|
return new self(self::STATUS_CHALLENGE_EXPIRED); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
|
|
|
|
52
|
|
|
* @return ProofOfPossessionResult |
53
|
|
|
*/ |
54
|
|
|
public static function incorrectChallenge(): self |
55
|
|
|
{ |
56
|
|
|
return new self(self::STATUS_INCORRECT_CHALLENGE); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
|
|
|
|
60
|
|
|
* @return ProofOfPossessionResult |
61
|
|
|
*/ |
62
|
|
|
public static function proofOfPossessionCommandFailed(): self |
63
|
|
|
{ |
64
|
|
|
return new self(self::STATUS_CHALLENGE_OK); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
|
|
|
|
68
|
|
|
* @param string $secondFactorId |
|
|
|
|
69
|
|
|
* @return ProofOfPossessionResult |
|
|
|
|
70
|
|
|
*/ |
71
|
|
|
public static function secondFactorCreated($secondFactorId): self |
72
|
|
|
{ |
73
|
|
|
if (!is_string($secondFactorId)) { |
|
|
|
|
74
|
|
|
throw InvalidArgumentException::invalidType('string', 'secondFactorId', $secondFactorId); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return new self(self::STATUS_CHALLENGE_OK, $secondFactorId); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
|
|
|
|
81
|
|
|
* @return ProofOfPossessionResult |
82
|
|
|
*/ |
83
|
|
|
public static function tooManyAttempts(): self |
84
|
|
|
{ |
85
|
|
|
return new self(self::STATUS_TOO_MANY_ATTEMPTS); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
|
|
|
|
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function isSuccessful(): bool |
92
|
|
|
{ |
93
|
|
|
return $this->status === self::STATUS_CHALLENGE_OK && $this->secondFactorId !== null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
|
|
|
|
97
|
|
|
* @return null|string |
98
|
|
|
*/ |
99
|
|
|
public function getSecondFactorId() |
100
|
|
|
{ |
101
|
|
|
return $this->secondFactorId; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function didProofOfPossessionFail(): bool |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
return $this->status === self::STATUS_CHALLENGE_OK && $this->secondFactorId === null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
|
|
|
|
110
|
|
|
* @return boolean |
111
|
|
|
*/ |
112
|
|
|
public function wasIncorrectChallengeResponseGiven(): bool |
113
|
|
|
{ |
114
|
|
|
return $this->status === self::STATUS_INCORRECT_CHALLENGE; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
|
|
|
|
118
|
|
|
* @return boolean |
119
|
|
|
*/ |
120
|
|
|
public function hasChallengeExpired(): bool |
121
|
|
|
{ |
122
|
|
|
return $this->status === self::STATUS_CHALLENGE_EXPIRED; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
|
|
|
|
126
|
|
|
* @return boolean |
127
|
|
|
*/ |
128
|
|
|
public function wereTooManyAttemptsMade(): bool |
129
|
|
|
{ |
130
|
|
|
return $this->status === self::STATUS_TOO_MANY_ATTEMPTS; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|