Completed
Pull Request — master (#102)
by Boy
18:16
created

AuthenticationVerificationResult   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 120
Duplicated Lines 14.17 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 13
c 7
b 0
f 0
lcom 1
cbo 2
dl 17
loc 120
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatus() 0 4 1
C from() 17 24 8
A registrationUnknown() 0 7 1
A __construct() 0 3 1
A wasSuccessful() 0 4 1
A didDeviceReportError() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\StepupGateway\U2fVerificationBundle\Service;
20
21
use Surfnet\StepupGateway\U2fVerificationBundle\Exception\LogicException;
22
use Surfnet\StepupU2fBundle\Service\AuthenticationVerificationResult as U2fAuthenticationVerificationResult;
23
24
/**
25
 * @SuppressWarnings(PHPMD.TooManyMethods)
26
 */
27
final class AuthenticationVerificationResult
28
{
29
    /**
30
     * Registration was a success.
31
     */
32
    const STATUS_SUCCESS = 'SUCCESS';
33
34
    /**
35
     * The response challenge did not match the request challenge.
36
     */
37
    const STATUS_REQUEST_RESPONSE_MISMATCH = 'REQUEST_RESPONSE_MISMATCH';
38
39
    /**
40
     * The response challenge was not for the given registration.
41
     */
42
    const STATUS_RESPONSE_REGISTRATION_MISMATCH = 'RESPONSE_REGISTRATION_MISMATCH';
43
44
    /**
45
     * The response was signed by another party than the device, indicating it was tampered with.
46
     */
47
    const STATUS_RESPONSE_NOT_SIGNED_BY_DEVICE = 'RESPONSE_NOT_SIGNED_BY_DEVICE';
48
49
    /**
50
     * The decoding of the device's public key failed.
51
     */
52
    const STATUS_PUBLIC_KEY_DECODING_FAILED = 'PUBLIC_KEY_DECODING_FAILED';
53
54
    /**
55
     * The device sent a sign counter that was equal to or lower than the previously recorded counter.
56
     */
57
    const STATUS_SIGN_COUNTER_TOO_LOW = 'SIGN_COUNTER_TOO_LOW';
58
59
    /**
60
     * The U2F device reported an error.
61
     *
62
     * @see \Surfnet\StepupU2fBundle\Dto\SignResponse::$errorCode
63
     * @see \Surfnet\StepupU2fBundle\Dto\SignResponse::ERROR_CODE_* constants
64
     */
65
    const STATUS_DEVICE_ERROR = 'DEVICE_ERROR';
66
67
    /**
68
     * The AppIDs of the server and a message did not match.
69
     */
70
    const STATUS_APP_ID_MISMATCH = 'APP_ID_MISMATCH';
71
72
    /**
73
     * No registration matching the authentication's key handle could be found.
74
     */
75
    const STATUS_REGISTRATION_UNKNOWN = 'REGISTRATION_UNKNOWN';
76
77
    /**
78
     * @var string
79
     */
80
    private $status;
81
82
    /**
83
     * @param U2fAuthenticationVerificationResult $u2fResult
84
     * @return self
85
     */
86
    public static function from(U2fAuthenticationVerificationResult $u2fResult)
87
    {
88
        $result = new self;
89
90 View Code Duplication
        if ($u2fResult->wasSuccessful()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
91
            $result->status = self::STATUS_SUCCESS;
92
        } elseif ($u2fResult->didDeviceReportAnyError()) {
93
            $result->status = self::STATUS_DEVICE_ERROR;
94
        } elseif ($u2fResult->didResponseChallengeNotMatchRequestChallenge()) {
95
            $result->status = self::STATUS_REQUEST_RESPONSE_MISMATCH;
96
        } elseif ($u2fResult->wasResponseNotSignedByDevice()) {
97
            $result->status = self::STATUS_RESPONSE_NOT_SIGNED_BY_DEVICE;
98
        } elseif ($u2fResult->didPublicKeyDecodingFail()) {
99
            $result->status = self::STATUS_PUBLIC_KEY_DECODING_FAILED;
100
        } elseif ($u2fResult->wasSignCounterTooLow()) {
101
            $result->status = self::STATUS_SIGN_COUNTER_TOO_LOW;
102
        } elseif ($u2fResult->didntAppIdsMatch()) {
103
            $result->status = self::STATUS_APP_ID_MISMATCH;
104
        } else {
105
            throw new LogicException('Unknown authentication verification result status');
106
        }
107
108
        return $result;
109
    }
110
111
    /**
112
     * @return self
113
     */
114
    public static function registrationUnknown()
115
    {
116
        $result = new self;
117
        $result->status = self::STATUS_REGISTRATION_UNKNOWN;
118
119
        return $result;
120
    }
121
122
    private function __construct()
123
    {
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    public function wasSuccessful()
130
    {
131
        return $this->status === self::STATUS_SUCCESS;
132
    }
133
134
    public function didDeviceReportError()
135
    {
136
        return $this->status === self::STATUS_DEVICE_ERROR;
137
    }
138
139
    /**
140
     * @return string
141
     */
142
    public function getStatus()
143
    {
144
        return $this->status;
145
    }
146
}
147