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
|
|
|
*/ |
|
|
|
|
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 |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
public function __construct( |
|
|
|
|
34
|
|
|
private readonly YubikeyServiceInterface $yubikeyService, |
35
|
|
|
private readonly CommandService $commandService, |
36
|
|
|
) { |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function provePossession(VerifyYubikeyOtpCommand $command): ProofOfPossessionResult |
|
|
|
|
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
|
|
|
|