didDeviceReportAnyError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\StepupU2fBundle\Service;
20
21
use Surfnet\StepupU2fBundle\Dto\Registration;
22
use Surfnet\StepupU2fBundle\Dto\SignResponse;
23
use Surfnet\StepupU2fBundle\Exception\InvalidArgumentException;
24
use Surfnet\StepupU2fBundle\Exception\LogicException;
25
26
/**
27
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
28
 */
29
final class AuthenticationVerificationResult
30
{
31
    /**
32
     * Registration was a success.
33
     */
34
    const STATUS_SUCCESS = 0;
35
36
    /**
37
     * The response challenge did not match the request challenge.
38
     */
39
    const STATUS_REQUEST_RESPONSE_MISMATCH = 1;
40
41
    /**
42
     * The response challenge was not for the given registration.
43
     */
44
    const STATUS_RESPONSE_REGISTRATION_MISMATCH = 2;
45
46
    /**
47
     * The response was signed by another party than the device, indicating it was tampered with.
48
     */
49
    const STATUS_RESPONSE_NOT_SIGNED_BY_DEVICE = 3;
50
51
    /**
52
     * The decoding of the device's public key failed.
53
     */
54
    const STATUS_PUBLIC_KEY_DECODING_FAILED = 4;
55
56
    /**
57
     * The U2F device reported an error.
58
     *
59
     * @see \Surfnet\StepupU2fBundle\Dto\SignResponse::$errorCode
60
     * @see \Surfnet\StepupU2fBundle\Dto\SignResponse::ERROR_CODE_* constants
61
     */
62
    const STATUS_DEVICE_ERROR = 5;
63
64
    /**
65
     * The AppIDs of the server and a message did not match.
66
     */
67
    const STATUS_APP_ID_MISMATCH = 6;
68
69
    /**
70
     * The sign response's counter was lower than the given registration's sign counter.
71
     */
72
    const STATUS_SIGN_COUNTER_TOO_LOW = 7;
73
74
    /**
75
     * @var int
76
     */
77
    private $status;
78
79
    /**
80
     * @var Registration|null
81
     */
82
    private $registration;
83
84
    /**
85
     * @var int|null
86
     */
87
    private $deviceErrorCode;
88
89
    /**
90
     * @param Registration $registration
91
     * @return self
92
     */
93
    public static function success(Registration $registration)
94
    {
95
        $result = new self(self::STATUS_SUCCESS);
96
        $result->registration = $registration;
97
98
        return $result;
99
    }
100
101
    /**
102
     * @param int $errorCode
103
     * @return self
104
     */
105 View Code Duplication
    public static function deviceReportedError($errorCode)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        $validErrorCodes = [
108
            SignResponse::ERROR_CODE_OK,
109
            SignResponse::ERROR_CODE_OTHER_ERROR,
110
            SignResponse::ERROR_CODE_BAD_REQUEST,
111
            SignResponse::ERROR_CODE_CONFIGURATION_UNSUPPORTED,
112
            SignResponse::ERROR_CODE_DEVICE_INELIGIBLE,
113
            SignResponse::ERROR_CODE_TIMEOUT,
114
        ];
115
116
        if (!in_array($errorCode, $validErrorCodes, true)) {
117
            throw new InvalidArgumentException('Device error code is not one of the known error codes');
118
        }
119
120
        $result = new self(self::STATUS_DEVICE_ERROR);
121
        $result->deviceErrorCode = $errorCode;
122
123
        return $result;
124
    }
125
126
    /**
127
     * @return self
128
     */
129
    public static function requestResponseMismatch()
130
    {
131
        return new self(self::STATUS_REQUEST_RESPONSE_MISMATCH);
132
    }
133
134
    /**
135
     * @return self
136
     */
137
    public static function responseRegistrationMismatch()
138
    {
139
        return new self(self::STATUS_RESPONSE_REGISTRATION_MISMATCH);
140
    }
141
142
    /**
143
     * @return self
144
     */
145
    public static function responseWasNotSignedByDevice()
146
    {
147
        return new self(self::STATUS_RESPONSE_NOT_SIGNED_BY_DEVICE);
148
    }
149
150
    /**
151
     * @return self
152
     */
153
    public static function publicKeyDecodingFailed()
154
    {
155
        return new self(self::STATUS_PUBLIC_KEY_DECODING_FAILED);
156
    }
157
158
    /**
159
     * @return self
160
     */
161
    public static function appIdMismatch()
162
    {
163
        return new self(self::STATUS_APP_ID_MISMATCH);
164
    }
165
166
    /**
167
     * @return self
168
     */
169
    public static function signCounterTooLow()
170
    {
171
        return new self(self::STATUS_SIGN_COUNTER_TOO_LOW);
172
    }
173
174
    /**
175
     * @param int $status
176
     */
177
    private function __construct($status)
178
    {
179
        $this->status = $status;
180
    }
181
182
    /**
183
     * @return bool
184
     */
185
    public function wasSuccessful()
186
    {
187
        return $this->status === self::STATUS_SUCCESS;
188
    }
189
190
    /**
191
     * @return Registration|null
192
     */
193
    public function getRegistration()
194
    {
195
        if (!$this->wasSuccessful()) {
196
            throw new LogicException('The authentication was unsuccessful and the registration data is not available');
197
        }
198
199
        return $this->registration;
200
    }
201
202
    /**
203
     * @return bool
204
     */
205
    public function didDeviceReportABadRequest()
206
    {
207
        return $this->didDeviceReportError(SignResponse::ERROR_CODE_BAD_REQUEST);
208
    }
209
210
    /**
211
     * @return bool
212
     */
213
    public function wasClientConfigurationUnsupported()
214
    {
215
        return $this->didDeviceReportError(SignResponse::ERROR_CODE_CONFIGURATION_UNSUPPORTED);
216
    }
217
218
    /**
219
     * @return bool
220
     */
221
    public function wasKeyHandleUnknownToDevice()
222
    {
223
        return $this->didDeviceReportError(SignResponse::ERROR_CODE_DEVICE_INELIGIBLE);
224
    }
225
226
    /**
227
     * @return bool
228
     */
229
    public function didDeviceTimeOut()
230
    {
231
        return $this->didDeviceReportError(SignResponse::ERROR_CODE_TIMEOUT);
232
    }
233
234
    /**
235
     * @return bool
236
     */
237
    public function didDeviceReportAnUnknownError()
238
    {
239
        return $this->didDeviceReportError(SignResponse::ERROR_CODE_OTHER_ERROR);
240
    }
241
242
    /**
243
     * @return bool
244
     */
245
    public function didDeviceReportAnyError()
246
    {
247
        return $this->status === self::STATUS_DEVICE_ERROR;
248
    }
249
250
    /**
251
     * @return bool
252
     */
253
    public function didResponseChallengeNotMatchRequestChallenge()
254
    {
255
        return $this->status === self::STATUS_REQUEST_RESPONSE_MISMATCH;
256
    }
257
258
    /**
259
     * @return bool
260
     */
261
    public function didResponseChallengeNotMatchRegistration()
262
    {
263
        return $this->status === self::STATUS_RESPONSE_REGISTRATION_MISMATCH;
264
    }
265
266
    /**
267
     * @return bool
268
     */
269
    public function wasResponseNotSignedByDevice()
270
    {
271
        return $this->status === self::STATUS_RESPONSE_NOT_SIGNED_BY_DEVICE;
272
    }
273
274
    /**
275
     * @return bool
276
     */
277
    public function didPublicKeyDecodingFail()
278
    {
279
        return $this->status === self::STATUS_PUBLIC_KEY_DECODING_FAILED;
280
    }
281
282
    /**
283
     * @return bool
284
     */
285
    public function didntAppIdsMatch()
286
    {
287
        return $this->status === self::STATUS_APP_ID_MISMATCH;
288
    }
289
290
    /**
291
     * @return bool
292
     */
293
    public function wasSignCounterTooLow()
294
    {
295
        return $this->status === self::STATUS_SIGN_COUNTER_TOO_LOW;
296
    }
297
298
    /**
299
     * @param int $errorCode
300
     * @return bool
301
     */
302
    private function didDeviceReportError($errorCode)
303
    {
304
        return $this->status === self::STATUS_DEVICE_ERROR && $this->deviceErrorCode === $errorCode;
305
    }
306
}
307