YubikeySecondFactorService   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 8
dl 0
loc 63
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A verifyYubikeyPublicId() 0 36 5
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\StepupRa\RaBundle\Service;
20
21
use Psr\Log\LoggerInterface;
22
use Surfnet\StepupBundle\Value\YubikeyOtp;
23
use Surfnet\StepupBundle\Value\YubikeyPublicId;
24
use Surfnet\StepupRa\RaBundle\Command\VerifyYubikeyOtpCommand;
25
use Surfnet\StepupRa\RaBundle\Command\VerifyYubikeyPublicIdCommand;
26
use Surfnet\StepupRa\RaBundle\Service\YubikeySecondFactor\VerificationResult;
27
28
class YubikeySecondFactorService
29
{
30
    /**
31
     * @var YubikeyService
32
     */
33
    private $yubikeyService;
34
35
    /**
36
     * @var LoggerInterface
37
     */
38
    private $logger;
39
40
    /**
41
     * @param YubikeyService $yubikeyService
42
     * @param LoggerInterface $logger
43
     */
44
    public function __construct(YubikeyService $yubikeyService, LoggerInterface $logger)
45
    {
46
        $this->yubikeyService = $yubikeyService;
47
        $this->logger = $logger;
48
    }
49
50
    /**
51
     * @param VerifyYubikeyPublicIdCommand $command
52
     * @return VerificationResult
53
     */
54
    public function verifyYubikeyPublicId(VerifyYubikeyPublicIdCommand $command)
55
    {
56
        $verifyOtpCommand = new VerifyYubikeyOtpCommand();
57
        $verifyOtpCommand->otp = $command->otp;
58
        $verifyOtpCommand->identityId = $command->identityId;
59
        $verifyOtpCommand->institution = $command->institution;
60
61
        $verificationResult = $this->yubikeyService->verify($verifyOtpCommand);
62
63
        if (YubikeyOtp::isValid($command->otp)) {
64
            $otp      = YubikeyOtp::fromString($command->otp);
65
            $publicId = YubikeyPublicId::fromOtp($otp);
66
        } else {
67
            $publicId = null;
68
        }
69
70
        if ($verificationResult->isServerError()) {
71
            return new VerificationResult(VerificationResult::RESULT_OTP_VERIFICATION_FAILED, $publicId);
72
        } elseif ($verificationResult->isClientError()) {
73
            return new VerificationResult(VerificationResult::RESULT_OTP_INVALID, $publicId);
74
        }
75
76
        if ($publicId->getYubikeyPublicId() !== $command->expectedPublicId) {
77
            $this->logger->notice(
78
                'Yubikey used by registrant during vetting did not match the one used during registration.'
79
            );
80
81
            return new VerificationResult(VerificationResult::RESULT_PUBLIC_ID_DID_NOT_MATCH, $publicId);
82
        }
83
84
        $this->logger->info(
85
            'Yubikey used by registrant during vetting matches the one used during registration.'
86
        );
87
88
        return new VerificationResult(VerificationResult::RESULT_PUBLIC_ID_MATCHED, $publicId);
89
    }
90
}
91