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\ApiBundle\Service; |
20
|
|
|
|
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
use Surfnet\StepupBundle\Value\YubikeyOtp; |
23
|
|
|
use Surfnet\StepupBundle\Value\YubikeyPublicId; |
24
|
|
|
use Surfnet\StepupGateway\ApiBundle\Dto\Otp as OtpDto; |
25
|
|
|
use Surfnet\StepupGateway\ApiBundle\Dto\Requester; |
26
|
|
|
use Surfnet\StepupGateway\ApiBundle\Dto\YubikeyOtpVerificationResult; |
27
|
|
|
use Surfnet\YubikeyApiClient\Otp; |
28
|
|
|
use Surfnet\YubikeyApiClient\Service\OtpVerificationResult; |
29
|
|
|
use Surfnet\YubikeyApiClientBundle\Service\VerificationService; |
30
|
|
|
|
31
|
|
|
class YubikeyService implements YubikeyServiceInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var VerificationService |
35
|
|
|
*/ |
36
|
|
|
private $verificationService; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var LoggerInterface |
40
|
|
|
*/ |
41
|
|
|
private $logger; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param VerificationService $verificationService |
45
|
|
|
* @param LoggerInterface $logger |
46
|
|
|
*/ |
47
|
|
|
public function __construct(VerificationService $verificationService, LoggerInterface $logger) |
48
|
|
|
{ |
49
|
|
|
$this->verificationService = $verificationService; |
50
|
|
|
$this->logger = $logger; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param OtpDto $otp |
55
|
|
|
* @param Requester $requester |
56
|
|
|
* @return OtpVerificationResult |
57
|
|
|
*/ |
58
|
|
|
public function verifyOtp(OtpDto $otp, Requester $requester) |
59
|
|
|
{ |
60
|
|
|
$this->logger->notice('Verifying Yubikey OTP.'); |
61
|
|
|
|
62
|
|
|
if (!Otp::isValid($otp->value)) { |
63
|
|
|
return new OtpVerificationResult(OtpVerificationResult::ERROR_BAD_OTP); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$otp = Otp::fromString($otp->value); |
67
|
|
|
$result = $this->verificationService->verify($otp); |
68
|
|
|
|
69
|
|
|
if (!$result->isSuccessful()) { |
70
|
|
|
$this->logger->warning(sprintf('Yubikey OTP verification failed (%s)', $result->getError())); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param OtpDto $otp |
78
|
|
|
* @param string $secondFactorIdentifier |
79
|
|
|
* @return YubikeyOtpVerificationResult |
80
|
|
|
*/ |
81
|
|
|
public function verifyPublicId(OtpDto $otp, $secondFactorIdentifier) |
82
|
|
|
{ |
83
|
|
|
$this->logger->notice('Verifying Yubikey OTP public id matches that of the second factor identifier'); |
84
|
|
|
|
85
|
|
|
$otp = YubikeyOtp::fromString($otp->value); |
86
|
|
|
$publicId = YubikeyPublicId::fromOtp($otp); |
87
|
|
|
|
88
|
|
|
if (!$publicId->equals(new YubikeyPublicId($secondFactorIdentifier))) { |
89
|
|
|
$this->logger->warning('Yubikey OTP verification failed (Public Id did not match)'); |
90
|
|
|
return new YubikeyOtpVerificationResult( |
91
|
|
|
YubikeyOtpVerificationResult::RESULT_PUBLIC_ID_DID_NOT_MATCH, |
92
|
|
|
$publicId |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return new YubikeyOtpVerificationResult(YubikeyOtpVerificationResult::RESULT_PUBLIC_ID_MATCHED, $publicId); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|