wasClientConfigurationUnsupported()   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\RegisterResponse;
22
use Surfnet\StepupU2fBundle\Dto\Registration;
23
use Surfnet\StepupU2fBundle\Exception\InvalidArgumentException;
24
use Surfnet\StepupU2fBundle\Exception\LogicException;
25
26
/**
27
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
28
 */
29
final class RegistrationVerificationResult
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_UNMATCHED_REGISTRATION_CHALLENGE = 1;
40
41
    /**
42
     * The response was signed by another party than the device, indicating it was tampered with.
43
     */
44
    const STATUS_RESPONSE_NOT_SIGNED_BY_DEVICE = 2;
45
46
    /**
47
     * The device has not been manufactured by a trusted party.
48
     */
49
    const STATUS_UNTRUSTED_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\RegisterResponse::$errorCode
60
     * @see \Surfnet\StepupU2fBundle\Dto\RegisterResponse::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
     * @var int
71
     */
72
    private $status;
73
74
    /**
75
     * @var Registration|null
76
     */
77
    private $registration;
78
79
    /**
80
     * @var int|null
81
     */
82
    private $deviceErrorCode;
83
84
    /**
85
     * @param Registration $registration
86
     * @return RegistrationVerificationResult
87
     */
88
    public static function success(Registration $registration)
89
    {
90
        $result = new self(self::STATUS_SUCCESS);
91
        $result->registration = $registration;
92
93
        return $result;
94
    }
95
96
    /**
97
     * @param int $errorCode
98
     * @return RegistrationVerificationResult
99
     */
100 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...
101
    {
102
        $validErrorCodes = [
103
            RegisterResponse::ERROR_CODE_OK,
104
            RegisterResponse::ERROR_CODE_OTHER_ERROR,
105
            RegisterResponse::ERROR_CODE_BAD_REQUEST,
106
            RegisterResponse::ERROR_CODE_CONFIGURATION_UNSUPPORTED,
107
            RegisterResponse::ERROR_CODE_DEVICE_INELIGIBLE,
108
            RegisterResponse::ERROR_CODE_TIMEOUT,
109
        ];
110
111
        if (!in_array($errorCode, $validErrorCodes)) {
112
            throw new InvalidArgumentException(
113
                sprintf('Device error code (%s) is not one of the known error codes', $errorCode)
114
            );
115
        }
116
117
        $result = new self(self::STATUS_DEVICE_ERROR);
118
        $result->deviceErrorCode = $errorCode;
119
120
        return $result;
121
    }
122
123
    /**
124
     * @return RegistrationVerificationResult
125
     */
126
    public static function responseChallengeDidNotMatchRequestChallenge()
127
    {
128
        return new self(self::STATUS_UNMATCHED_REGISTRATION_CHALLENGE);
129
    }
130
131
    /**
132
     * @return RegistrationVerificationResult
133
     */
134
    public static function responseWasNotSignedByDevice()
135
    {
136
        return new self(self::STATUS_RESPONSE_NOT_SIGNED_BY_DEVICE);
137
    }
138
139
    /**
140
     * @return RegistrationVerificationResult
141
     */
142
    public static function deviceCannotBeTrusted()
143
    {
144
        return new self(self::STATUS_UNTRUSTED_DEVICE);
145
    }
146
147
    /**
148
     * @return RegistrationVerificationResult
149
     */
150
    public static function publicKeyDecodingFailed()
151
    {
152
        return new self(self::STATUS_PUBLIC_KEY_DECODING_FAILED);
153
    }
154
155
    /**
156
     * @return RegistrationVerificationResult
157
     */
158
    public static function appIdMismatch()
159
    {
160
        return new self(self::STATUS_APP_ID_MISMATCH);
161
    }
162
163
    /**
164
     * @param int $status
165
     */
166
    private function __construct($status)
167
    {
168
        $this->status = $status;
169
    }
170
171
    /**
172
     * @return bool
173
     */
174
    public function wasSuccessful()
175
    {
176
        return $this->status === self::STATUS_SUCCESS;
177
    }
178
179
    /**
180
     * @return Registration|null
181
     */
182
    public function getRegistration()
183
    {
184
        if (!$this->wasSuccessful()) {
185
            throw new LogicException('The registration was unsuccessful and the registration data is not available');
186
        }
187
188
        return $this->registration;
189
    }
190
191
    /**
192
     * @return bool
193
     */
194
    public function didDeviceReportABadRequest()
195
    {
196
        return $this->didDeviceReportError(RegisterResponse::ERROR_CODE_BAD_REQUEST);
197
    }
198
199
    /**
200
     * @return bool
201
     */
202
    public function wasClientConfigurationUnsupported()
203
    {
204
        return $this->didDeviceReportError(RegisterResponse::ERROR_CODE_CONFIGURATION_UNSUPPORTED);
205
    }
206
207
    /**
208
     * @return bool
209
     */
210
    public function wasDeviceAlreadyRegistered()
211
    {
212
        return $this->didDeviceReportError(RegisterResponse::ERROR_CODE_DEVICE_INELIGIBLE);
213
    }
214
215
    /**
216
     * @return bool
217
     */
218
    public function didDeviceTimeOut()
219
    {
220
        return $this->didDeviceReportError(RegisterResponse::ERROR_CODE_TIMEOUT);
221
    }
222
223
    /**
224
     * @return bool
225
     */
226
    public function didDeviceReportAnUnknownError()
227
    {
228
        return $this->didDeviceReportError(RegisterResponse::ERROR_CODE_OTHER_ERROR);
229
    }
230
231
    /**
232
     * @return bool
233
     */
234
    public function didDeviceReportAnyError()
235
    {
236
        return $this->status === self::STATUS_DEVICE_ERROR;
237
    }
238
239
    /**
240
     * @return bool
241
     */
242
    public function didResponseChallengeNotMatchRequestChallenge()
243
    {
244
        return $this->status === self::STATUS_UNMATCHED_REGISTRATION_CHALLENGE;
245
    }
246
247
    /**
248
     * @return bool
249
     */
250
    public function wasResponseNotSignedByDevice()
251
    {
252
        return $this->status === self::STATUS_RESPONSE_NOT_SIGNED_BY_DEVICE;
253
    }
254
255
    /**
256
     * @return bool
257
     */
258
    public function canDeviceNotBeTrusted()
259
    {
260
        return $this->status === self::STATUS_UNTRUSTED_DEVICE;
261
    }
262
263
    /**
264
     * @return bool
265
     */
266
    public function didPublicKeyDecodingFail()
267
    {
268
        return $this->status === self::STATUS_PUBLIC_KEY_DECODING_FAILED;
269
    }
270
271
    /**
272
     * @return bool
273
     */
274
    public function didntAppIdsMatch()
275
    {
276
        return $this->status === self::STATUS_APP_ID_MISMATCH;
277
    }
278
279
    /**
280
     * @param int $errorCode
281
     * @return bool
282
     */
283
    private function didDeviceReportError($errorCode)
284
    {
285
        return $this->status === self::STATUS_DEVICE_ERROR && $this->deviceErrorCode === $errorCode;
286
    }
287
}
288