YubikeySecondFactorService   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 41
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A provePossession() 0 33 5
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
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
20
21
namespace Surfnet\StepupSelfService\SelfServiceBundle\Service;
22
23
use Surfnet\StepupBundle\Value\YubikeyOtp;
24
use Surfnet\StepupBundle\Value\YubikeyPublicId;
25
use Surfnet\StepupMiddlewareClientBundle\Identity\Command\ProveYubikeyPossessionCommand;
26
use Surfnet\StepupMiddlewareClientBundle\Uuid\Uuid;
27
use Surfnet\StepupSelfService\SelfServiceBundle\Command\VerifyYubikeyOtpCommand;
28
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\RuntimeException;
29
use Surfnet\StepupSelfService\SelfServiceBundle\Service\YubikeySecondFactor\ProofOfPossessionResult;
30
31
class YubikeySecondFactorService implements YubikeySecondFactorServiceInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class YubikeySecondFactorService
Loading history...
32
{
33
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
34
        private readonly YubikeyServiceInterface $yubikeyService,
35
        private readonly CommandService $commandService,
36
    ) {
37
    }
38
39
    public function provePossession(VerifyYubikeyOtpCommand $command): ProofOfPossessionResult
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function provePossession()
Loading history...
40
    {
41
        $verificationResult = $this->yubikeyService->verify($command);
42
43
        if (!$verificationResult->isSuccessful()) {
44
            if ($verificationResult->isClientError()) {
45
                return ProofOfPossessionResult::invalidOtp();
46
            } elseif ($verificationResult->isServerError()) {
47
                return ProofOfPossessionResult::otpVerificationFailed();
48
            }
49
50
            throw new RuntimeException(
51
                'Unexpected Verification result, result is not successful but has neither client nor server error'
52
            );
53
        }
54
55
        $secondFactorId = Uuid::generate();
56
57
        $otp = YubikeyOtp::fromString($command->otp);
58
        $publicId = YubikeyPublicId::fromOtp($otp);
59
60
        $provePossessionCommand = new ProveYubikeyPossessionCommand();
61
        $provePossessionCommand->identityId = $command->identity;
62
        $provePossessionCommand->secondFactorId = $secondFactorId;
63
        $provePossessionCommand->yubikeyPublicId = $publicId->getYubikeyPublicId();
64
65
        $result = $this->commandService->execute($provePossessionCommand);
66
67
        if (!$result->isSuccessful()) {
68
            return ProofOfPossessionResult::proofOfPossessionCommandFailed();
69
        }
70
71
        return ProofOfPossessionResult::secondFactorCreated($secondFactorId);
72
    }
73
}
74