|
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\Controller; |
|
20
|
|
|
|
|
21
|
|
|
use Surfnet\StepupGateway\ApiBundle\Dto\Otp; |
|
22
|
|
|
use Surfnet\StepupGateway\ApiBundle\Dto\Requester; |
|
23
|
|
|
use Surfnet\StepupGateway\ApiBundle\Service\YubikeyService; |
|
24
|
|
|
use Surfnet\YubikeyApiClient\Service\OtpVerificationResult; |
|
25
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
26
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
27
|
|
|
|
|
28
|
|
|
class YubikeyController extends Controller |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @param Otp $otp |
|
32
|
|
|
* @param Requester $requester |
|
33
|
|
|
* @return JsonResponse |
|
34
|
|
|
*/ |
|
35
|
|
|
public function verifyAction(Otp $otp, Requester $requester) |
|
36
|
|
|
{ |
|
37
|
|
|
/** @var YubikeyService $yubikeyService */ |
|
38
|
|
|
$yubikeyService = $this->get('surfnet_gateway_api.service.yubikey'); |
|
39
|
|
|
$result = $yubikeyService->verify($otp, $requester); |
|
40
|
|
|
|
|
41
|
|
|
return $this->createJsonResponseFromVerifyYubikeyResult($result); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param OtpVerificationResult $result |
|
46
|
|
|
* @return JsonResponse |
|
47
|
|
|
*/ |
|
48
|
|
|
private function createJsonResponseFromVerifyYubikeyResult(OtpVerificationResult $result) |
|
49
|
|
|
{ |
|
50
|
|
|
if ($result->isSuccessful()) { |
|
51
|
|
|
return new JsonResponse(['status' => 'OK']); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
switch ($result->getError()) { |
|
55
|
|
|
case 'BAD_OTP': |
|
56
|
|
|
case 'REPLAYED_OTP': |
|
57
|
|
|
// Bad OTP means user/client entered invalid OTP |
|
58
|
|
|
// REPLAYED_OTP can mean the user/client entered OTP and immediately pressed RETURN, causing the |
|
59
|
|
|
// form to be submitted twice. |
|
60
|
|
|
$statusCode = 400; |
|
61
|
|
|
break; |
|
62
|
|
|
default: |
|
63
|
|
|
// Other errors are Yubico server errors. |
|
64
|
|
|
$statusCode = 502; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$errorMessage = sprintf('Yubikey verification failed (%s)', $result->getError()); |
|
68
|
|
|
|
|
69
|
|
|
return new JsonResponse(['errors' => [$errorMessage]], $statusCode); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|