YubikeyService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 GuzzleHttp\Client;
22
use Psr\Log\LoggerInterface;
23
use Surfnet\StepupBundle\Http\JsonHelper;
24
use Surfnet\StepupRa\RaBundle\Command\VerifyYubikeyOtpCommand;
25
26
class YubikeyService
27
{
28
    /**
29
     * @var Client
30
     */
31
    private $guzzleClient;
32
33
    /**
34
     * @var LoggerInterface
35
     */
36
    private $logger;
37
38
    /**
39
     * @param Client $guzzleClient A Guzzle client configured with the Yubikey API base URL and authentication.
40
     * @param LoggerInterface $logger
41
     */
42
    public function __construct(Client $guzzleClient, LoggerInterface $logger)
43
    {
44
        $this->guzzleClient = $guzzleClient;
45
        $this->logger = $logger;
46
    }
47
48
    /**
49
     * @param VerifyYubikeyOtpCommand $command
50
     * @return YubikeyVerificationResult
51
     */
52
    public function verify(VerifyYubikeyOtpCommand $command)
53
    {
54
        $this->logger->info('Verifying Yubikey OTP');
55
56
        $body = [
57
            'requester' => ['institution' => $command->institution, 'identity' => $command->identityId],
58
            'otp' => ['value' => $command->otp],
59
        ];
60
        $response = $this->guzzleClient->post('api/verify-yubikey', ['json' => $body, 'http_errors' => false]);
61
        $statusCode = $response->getStatusCode();
62
63
        if ($statusCode != 200) {
64
            $type = $statusCode >= 400 && $statusCode < 500 ? 'client' : 'server';
65
            $this->logger->info(sprintf('Yubikey OTP verification failed; %s error', $type));
66
67
            return new YubikeyVerificationResult(true, false);
68
        }
69
70
        try {
71
            $result = JsonHelper::decode((string) $response->getBody());
72
        } catch (\RuntimeException $e) {
73
            $this->logger->error('Yubikey OTP verification failed; server responded with malformed JSON.');
74
75
            return new YubikeyVerificationResult(false, true);
76
        }
77
78 View Code Duplication
        if (!isset($result['status'])) {
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...
79
            $this->logger->error('Yubikey OTP verification failed; server responded without status report.');
80
81
            return new YubikeyVerificationResult(false, true);
82
        }
83
84 View Code Duplication
        if ($result['status'] !== 'OK') {
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...
85
            $this->logger->error('Yubikey OTP verification failed; server responded with non-OK status report.');
86
87
            return new YubikeyVerificationResult(false, true);
88
        }
89
90
        return new YubikeyVerificationResult(false, false);
91
    }
92
}
93