VerificationService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Surfnet\YubikeyApiClientBundle\Service;
6
7
use Psr\Log\LoggerInterface;
8
use Surfnet\YubikeyApiClient\Exception\RequestResponseMismatchException;
9
use Surfnet\YubikeyApiClient\Exception\UntrustedSignatureException;
10
use Surfnet\YubikeyApiClient\Otp;
11
use Surfnet\YubikeyApiClient\Service\OtpVerificationResult;
12
use Surfnet\YubikeyApiClient\Service\VerificationServiceInterface as Service;
13
14
class VerificationService
15
{
16
    public function __construct(
17
        private Service $service,
18
        private LoggerInterface $logger,
19
    ) {
20
    }
21
22
    public function verify(Otp $otp): OtpVerificationResult
23
    {
24
        try {
25
            $result = $this->service->verify($otp);
26
        } catch (UntrustedSignatureException $e) {
27
            $this->logger->alert(sprintf('Yubico responded with invalid signature (%s)', $e->getMessage()), [
28
                'exception' => $e,
29
                'otp' => $otp->otp,
30
            ]);
31
32
            return new OtpVerificationResult(OtpVerificationResult::ERROR_BAD_SIGNATURE);
33
        } catch (RequestResponseMismatchException $e) {
34
            $this->logger->alert(sprintf('Yubico request and response didn\'t match (%s)', $e->getMessage()), [
35
                'exception' => $e,
36
                'otp' => $otp->otp,
37
            ]);
38
39
            return new OtpVerificationResult(OtpVerificationResult::ERROR_BACKEND_ERROR);
40
        }
41
42
        if ($result->isSuccessful()) {
43
            return $result;
44
        }
45
46
        $this->logger->critical(sprintf('Yubico responded with error status \'%s\'', $result->getError()), [
47
            'otp' => $otp->otp,
48
        ]);
49
50
        return $result;
51
    }
52
}
53