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