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 Exception; |
22
|
|
|
use Surfnet\StepupGateway\ApiBundle\Dto\KeyHandle as KeyHandleDto; |
23
|
|
|
use Surfnet\StepupGateway\ApiBundle\Dto\Requester; |
24
|
|
|
use Surfnet\StepupGateway\U2fVerificationBundle\Service\VerificationService; |
25
|
|
|
use Surfnet\StepupGateway\U2fVerificationBundle\Value\KeyHandle; |
26
|
|
|
use Surfnet\StepupU2fBundle\Dto\RegisterRequest; |
27
|
|
|
use Surfnet\StepupU2fBundle\Dto\RegisterResponse; |
28
|
|
|
use Surfnet\StepupU2fBundle\Dto\SignRequest; |
29
|
|
|
use Surfnet\StepupU2fBundle\Dto\SignResponse; |
30
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
31
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
32
|
|
|
use Symfony\Component\HttpFoundation\Response; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) -- Mainly due to DTOs |
36
|
|
|
*/ |
37
|
|
|
class U2fVerificationController extends Controller |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @return Response |
41
|
|
|
*/ |
42
|
|
|
public function createRegisterRequestAction() |
43
|
|
|
{ |
44
|
|
|
$registerRequest = $this->getU2fVerificationService()->createRegisterRequest(); |
45
|
|
|
|
46
|
|
|
return new JsonResponse( |
47
|
|
|
[ |
48
|
|
|
'version' => $registerRequest->version, |
49
|
|
|
'challenge' => $registerRequest->challenge, |
50
|
|
|
'app_id' => $registerRequest->appId, |
51
|
|
|
], |
52
|
|
|
Response::HTTP_OK |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param RegisterRequest $registerRequest |
58
|
|
|
* @param RegisterResponse $registerResponse |
59
|
|
|
* @param Requester $requester |
60
|
|
|
* @return JsonResponse |
61
|
|
|
*/ |
62
|
|
|
public function registerAction( |
63
|
|
|
RegisterRequest $registerRequest, |
64
|
|
|
RegisterResponse $registerResponse, |
65
|
|
|
Requester $requester |
|
|
|
|
66
|
|
|
) { |
67
|
|
|
$service = $this->getU2fVerificationService(); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$result = $service->verifyRegistration($registerRequest, $registerResponse); |
71
|
|
|
} catch (Exception $e) { |
72
|
|
|
return new JsonResponse(['errors' => [$e->getMessage()]], Response::HTTP_INTERNAL_SERVER_ERROR); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($result->wasSuccessful()) { |
76
|
|
|
return new JsonResponse( |
77
|
|
|
[ |
78
|
|
|
'status' => $result->getStatus(), |
79
|
|
|
'key_handle' => $result->getRegistration()->getKeyHandle()->getKeyHandle(), |
80
|
|
|
], |
81
|
|
|
Response::HTTP_OK |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return new JsonResponse(['status' => $result->getStatus()], Response::HTTP_BAD_REQUEST); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param KeyHandleDto $keyHandle |
90
|
|
|
* @return Response |
91
|
|
|
*/ |
92
|
|
|
public function createSignRequestAction(KeyHandleDto $keyHandle) |
93
|
|
|
{ |
94
|
|
|
$service = $this->getU2fVerificationService(); |
95
|
|
|
$registration = $service->findRegistrationByKeyHandle(new KeyHandle($keyHandle->value)); |
96
|
|
|
|
97
|
|
|
if ($registration === null) { |
98
|
|
|
return new JsonResponse(['status' => 'UNKNOWN_KEY_HANDLE'], Response::HTTP_BAD_REQUEST); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$signRequest = $service->createSignRequest($registration); |
102
|
|
|
|
103
|
|
|
return new JsonResponse( |
104
|
|
|
[ |
105
|
|
|
'version' => $signRequest->version, |
106
|
|
|
'challenge' => $signRequest->challenge, |
107
|
|
|
'app_id' => $signRequest->appId, |
108
|
|
|
'key_handle' => $signRequest->keyHandle, |
109
|
|
|
], |
110
|
|
|
Response::HTTP_OK |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param SignRequest $signRequest |
116
|
|
|
* @param SignResponse $signResponse |
117
|
|
|
* @param Requester $requester |
118
|
|
|
* @return JsonResponse |
119
|
|
|
*/ |
120
|
|
|
public function verifyAuthenticationAction( |
121
|
|
|
SignRequest $signRequest, |
122
|
|
|
SignResponse $signResponse, |
123
|
|
|
Requester $requester |
|
|
|
|
124
|
|
|
) { |
125
|
|
|
try { |
126
|
|
|
$result = $this->getU2fVerificationService()->verifyAuthentication($signRequest, $signResponse); |
127
|
|
|
} catch (Exception $e) { |
128
|
|
|
return new JsonResponse(['errors' => [$e->getMessage()]], Response::HTTP_INTERNAL_SERVER_ERROR); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($result->wasSuccessful()) { |
132
|
|
|
return new JsonResponse(['status' => $result->getStatus()], Response::HTTP_OK); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return new JsonResponse(['status' => $result->getStatus()], Response::HTTP_BAD_REQUEST); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function revokeRegistrationAction(KeyHandleDto $keyHandle, Requester $requester) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$verificationService = $this->getU2fVerificationService(); |
141
|
|
|
|
142
|
|
|
try { |
143
|
|
|
$registration = $verificationService->findRegistrationByKeyHandle(new KeyHandle($keyHandle->value)); |
144
|
|
|
|
145
|
|
|
if ($registration === null) { |
146
|
|
|
return new JsonResponse(['status' => 'UNKNOWN_KEY_HANDLE'], Response::HTTP_NOT_FOUND); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$verificationService->revokeRegistration($registration); |
150
|
|
|
} catch (Exception $e) { |
151
|
|
|
return new JsonResponse(['errors' => [$e->getMessage()]], Response::HTTP_INTERNAL_SERVER_ERROR); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return new JsonResponse(['status' => 'SUCCESS'], Response::HTTP_OK); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return VerificationService |
159
|
|
|
*/ |
160
|
|
|
private function getU2fVerificationService() |
161
|
|
|
{ |
162
|
|
|
return $this->get('surfnet_stepup_u2f_verification.service.u2f_verification'); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.