@@ -57,219 +57,219 @@ |
||
57 | 57 | |
58 | 58 | class Manager { |
59 | 59 | |
60 | - /** @var CredentialRepository */ |
|
61 | - private $repository; |
|
62 | - |
|
63 | - /** @var PublicKeyCredentialMapper */ |
|
64 | - private $credentialMapper; |
|
65 | - |
|
66 | - /** @var ILogger */ |
|
67 | - private $logger; |
|
68 | - |
|
69 | - /** @var IConfig */ |
|
70 | - private $config; |
|
71 | - |
|
72 | - public function __construct( |
|
73 | - CredentialRepository $repository, |
|
74 | - PublicKeyCredentialMapper $credentialMapper, |
|
75 | - ILogger $logger, |
|
76 | - IConfig $config |
|
77 | - ) { |
|
78 | - $this->repository = $repository; |
|
79 | - $this->credentialMapper = $credentialMapper; |
|
80 | - $this->logger = $logger; |
|
81 | - $this->config = $config; |
|
82 | - } |
|
83 | - |
|
84 | - public function startRegistration(IUser $user, string $serverHost): PublicKeyCredentialCreationOptions { |
|
85 | - $rpEntity = new PublicKeyCredentialRpEntity( |
|
86 | - 'Nextcloud', //Name |
|
87 | - $this->stripPort($serverHost), //ID |
|
88 | - null //Icon |
|
89 | - ); |
|
90 | - |
|
91 | - $userEntity = new PublicKeyCredentialUserEntity( |
|
92 | - $user->getUID(), //Name |
|
93 | - $user->getUID(), //ID |
|
94 | - $user->getDisplayName() //Display name |
|
60 | + /** @var CredentialRepository */ |
|
61 | + private $repository; |
|
62 | + |
|
63 | + /** @var PublicKeyCredentialMapper */ |
|
64 | + private $credentialMapper; |
|
65 | + |
|
66 | + /** @var ILogger */ |
|
67 | + private $logger; |
|
68 | + |
|
69 | + /** @var IConfig */ |
|
70 | + private $config; |
|
71 | + |
|
72 | + public function __construct( |
|
73 | + CredentialRepository $repository, |
|
74 | + PublicKeyCredentialMapper $credentialMapper, |
|
75 | + ILogger $logger, |
|
76 | + IConfig $config |
|
77 | + ) { |
|
78 | + $this->repository = $repository; |
|
79 | + $this->credentialMapper = $credentialMapper; |
|
80 | + $this->logger = $logger; |
|
81 | + $this->config = $config; |
|
82 | + } |
|
83 | + |
|
84 | + public function startRegistration(IUser $user, string $serverHost): PublicKeyCredentialCreationOptions { |
|
85 | + $rpEntity = new PublicKeyCredentialRpEntity( |
|
86 | + 'Nextcloud', //Name |
|
87 | + $this->stripPort($serverHost), //ID |
|
88 | + null //Icon |
|
89 | + ); |
|
90 | + |
|
91 | + $userEntity = new PublicKeyCredentialUserEntity( |
|
92 | + $user->getUID(), //Name |
|
93 | + $user->getUID(), //ID |
|
94 | + $user->getDisplayName() //Display name |
|
95 | 95 | // 'https://foo.example.co/avatar/123e4567-e89b-12d3-a456-426655440000' //Icon |
96 | - ); |
|
97 | - |
|
98 | - $challenge = random_bytes(32); |
|
99 | - |
|
100 | - $publicKeyCredentialParametersList = [ |
|
101 | - new PublicKeyCredentialParameters('public-key', Algorithms::COSE_ALGORITHM_ES256), |
|
102 | - new PublicKeyCredentialParameters('public-key', Algorithms::COSE_ALGORITHM_RS256), |
|
103 | - ]; |
|
104 | - |
|
105 | - $timeout = 60000; |
|
106 | - |
|
107 | - $excludedPublicKeyDescriptors = [ |
|
108 | - ]; |
|
109 | - |
|
110 | - $authenticatorSelectionCriteria = new AuthenticatorSelectionCriteria( |
|
111 | - null, |
|
112 | - false, |
|
113 | - AuthenticatorSelectionCriteria::USER_VERIFICATION_REQUIREMENT_DISCOURAGED |
|
114 | - ); |
|
115 | - |
|
116 | - return new PublicKeyCredentialCreationOptions( |
|
117 | - $rpEntity, |
|
118 | - $userEntity, |
|
119 | - $challenge, |
|
120 | - $publicKeyCredentialParametersList, |
|
121 | - $timeout, |
|
122 | - $excludedPublicKeyDescriptors, |
|
123 | - $authenticatorSelectionCriteria, |
|
124 | - PublicKeyCredentialCreationOptions::ATTESTATION_CONVEYANCE_PREFERENCE_NONE, |
|
125 | - null |
|
126 | - ); |
|
127 | - } |
|
128 | - |
|
129 | - public function finishRegister(PublicKeyCredentialCreationOptions $publicKeyCredentialCreationOptions, string $name, string $data): PublicKeyCredentialEntity { |
|
130 | - $tokenBindingHandler = new TokenBindingNotSupportedHandler(); |
|
131 | - |
|
132 | - $attestationStatementSupportManager = new AttestationStatementSupportManager(); |
|
133 | - $attestationStatementSupportManager->add(new NoneAttestationStatementSupport()); |
|
134 | - |
|
135 | - $attestationObjectLoader = new AttestationObjectLoader($attestationStatementSupportManager); |
|
136 | - $publicKeyCredentialLoader = new PublicKeyCredentialLoader($attestationObjectLoader); |
|
137 | - |
|
138 | - // Extension Output Checker Handler |
|
139 | - $extensionOutputCheckerHandler = new ExtensionOutputCheckerHandler(); |
|
140 | - |
|
141 | - // Authenticator Attestation Response Validator |
|
142 | - $authenticatorAttestationResponseValidator = new AuthenticatorAttestationResponseValidator( |
|
143 | - $attestationStatementSupportManager, |
|
144 | - $this->repository, |
|
145 | - $tokenBindingHandler, |
|
146 | - $extensionOutputCheckerHandler |
|
147 | - ); |
|
148 | - |
|
149 | - try { |
|
150 | - // Load the data |
|
151 | - $publicKeyCredential = $publicKeyCredentialLoader->load($data); |
|
152 | - $response = $publicKeyCredential->getResponse(); |
|
153 | - |
|
154 | - // Check if the response is an Authenticator Attestation Response |
|
155 | - if (!$response instanceof AuthenticatorAttestationResponse) { |
|
156 | - throw new \RuntimeException('Not an authenticator attestation response'); |
|
157 | - } |
|
158 | - |
|
159 | - // Check the response against the request |
|
160 | - $request = ServerRequest::fromGlobals(); |
|
161 | - |
|
162 | - $publicKeyCredentialSource = $authenticatorAttestationResponseValidator->check( |
|
163 | - $response, |
|
164 | - $publicKeyCredentialCreationOptions, |
|
165 | - $request); |
|
166 | - } catch (\Throwable $exception) { |
|
167 | - throw $exception; |
|
168 | - } |
|
169 | - |
|
170 | - // Persist the data |
|
171 | - return $this->repository->saveAndReturnCredentialSource($publicKeyCredentialSource, $name); |
|
172 | - } |
|
173 | - |
|
174 | - private function stripPort(string $serverHost): string { |
|
175 | - return preg_replace('/(:\d+$)/', '', $serverHost); |
|
176 | - } |
|
177 | - |
|
178 | - public function startAuthentication(string $uid, string $serverHost): PublicKeyCredentialRequestOptions { |
|
179 | - // List of registered PublicKeyCredentialDescriptor classes associated to the user |
|
180 | - $registeredPublicKeyCredentialDescriptors = array_map(function (PublicKeyCredentialEntity $entity) { |
|
181 | - $credential = $entity->toPublicKeyCredentialSource(); |
|
182 | - return new PublicKeyCredentialDescriptor( |
|
183 | - $credential->getType(), |
|
184 | - $credential->getPublicKeyCredentialId() |
|
185 | - ); |
|
186 | - }, $this->credentialMapper->findAllForUid($uid)); |
|
187 | - |
|
188 | - // Public Key Credential Request Options |
|
189 | - return new PublicKeyCredentialRequestOptions( |
|
190 | - random_bytes(32), // Challenge |
|
191 | - 60000, // Timeout |
|
192 | - $this->stripPort($serverHost), // Relying Party ID |
|
193 | - $registeredPublicKeyCredentialDescriptors, // Registered PublicKeyCredentialDescriptor classes |
|
194 | - AuthenticatorSelectionCriteria::USER_VERIFICATION_REQUIREMENT_DISCOURAGED |
|
195 | - ); |
|
196 | - } |
|
197 | - |
|
198 | - public function finishAuthentication(PublicKeyCredentialRequestOptions $publicKeyCredentialRequestOptions, string $data, string $uid) { |
|
199 | - $attestationStatementSupportManager = new AttestationStatementSupportManager(); |
|
200 | - $attestationStatementSupportManager->add(new NoneAttestationStatementSupport()); |
|
201 | - |
|
202 | - $attestationObjectLoader = new AttestationObjectLoader($attestationStatementSupportManager); |
|
203 | - $publicKeyCredentialLoader = new PublicKeyCredentialLoader($attestationObjectLoader); |
|
204 | - |
|
205 | - $tokenBindingHandler = new TokenBindingNotSupportedHandler(); |
|
206 | - $extensionOutputCheckerHandler = new ExtensionOutputCheckerHandler(); |
|
207 | - $algorithmManager = new \Cose\Algorithm\Manager(); |
|
208 | - $algorithmManager->add(new ES256()); |
|
209 | - $algorithmManager->add(new RS256()); |
|
210 | - |
|
211 | - $authenticatorAssertionResponseValidator = new AuthenticatorAssertionResponseValidator( |
|
212 | - $this->repository, |
|
213 | - $tokenBindingHandler, |
|
214 | - $extensionOutputCheckerHandler, |
|
215 | - $algorithmManager |
|
216 | - ); |
|
217 | - |
|
218 | - try { |
|
219 | - $this->logger->debug('Loading publickey credentials from: ' . $data); |
|
220 | - |
|
221 | - // Load the data |
|
222 | - $publicKeyCredential = $publicKeyCredentialLoader->load($data); |
|
223 | - $response = $publicKeyCredential->getResponse(); |
|
224 | - |
|
225 | - // Check if the response is an Authenticator Attestation Response |
|
226 | - if (!$response instanceof AuthenticatorAssertionResponse) { |
|
227 | - throw new \RuntimeException('Not an authenticator attestation response'); |
|
228 | - } |
|
229 | - |
|
230 | - // Check the response against the request |
|
231 | - $request = ServerRequest::fromGlobals(); |
|
232 | - |
|
233 | - $publicKeyCredentialSource = $authenticatorAssertionResponseValidator->check( |
|
234 | - $publicKeyCredential->getRawId(), |
|
235 | - $response, |
|
236 | - $publicKeyCredentialRequestOptions, |
|
237 | - $request, |
|
238 | - $uid |
|
239 | - ); |
|
240 | - } catch (\Throwable $e) { |
|
241 | - throw $e; |
|
242 | - } |
|
243 | - |
|
244 | - |
|
245 | - |
|
246 | - return true; |
|
247 | - } |
|
248 | - |
|
249 | - public function deleteRegistration(IUser $user, int $id): void { |
|
250 | - try { |
|
251 | - $entry = $this->credentialMapper->findById($user->getUID(), $id); |
|
252 | - } catch (DoesNotExistException $e) { |
|
253 | - $this->logger->warning("WebAuthn device $id does not exist, can't delete it"); |
|
254 | - return; |
|
255 | - } |
|
256 | - |
|
257 | - $this->credentialMapper->delete($entry); |
|
258 | - } |
|
259 | - |
|
260 | - public function isWebAuthnAvailable(): bool { |
|
261 | - if (!extension_loaded('bcmath')) { |
|
262 | - return false; |
|
263 | - } |
|
264 | - |
|
265 | - if (!extension_loaded('gmp')) { |
|
266 | - return false; |
|
267 | - } |
|
268 | - |
|
269 | - if (!$this->config->getSystemValueBool('auth.webauthn.enabled', true)) { |
|
270 | - return false; |
|
271 | - } |
|
272 | - |
|
273 | - return true; |
|
274 | - } |
|
96 | + ); |
|
97 | + |
|
98 | + $challenge = random_bytes(32); |
|
99 | + |
|
100 | + $publicKeyCredentialParametersList = [ |
|
101 | + new PublicKeyCredentialParameters('public-key', Algorithms::COSE_ALGORITHM_ES256), |
|
102 | + new PublicKeyCredentialParameters('public-key', Algorithms::COSE_ALGORITHM_RS256), |
|
103 | + ]; |
|
104 | + |
|
105 | + $timeout = 60000; |
|
106 | + |
|
107 | + $excludedPublicKeyDescriptors = [ |
|
108 | + ]; |
|
109 | + |
|
110 | + $authenticatorSelectionCriteria = new AuthenticatorSelectionCriteria( |
|
111 | + null, |
|
112 | + false, |
|
113 | + AuthenticatorSelectionCriteria::USER_VERIFICATION_REQUIREMENT_DISCOURAGED |
|
114 | + ); |
|
115 | + |
|
116 | + return new PublicKeyCredentialCreationOptions( |
|
117 | + $rpEntity, |
|
118 | + $userEntity, |
|
119 | + $challenge, |
|
120 | + $publicKeyCredentialParametersList, |
|
121 | + $timeout, |
|
122 | + $excludedPublicKeyDescriptors, |
|
123 | + $authenticatorSelectionCriteria, |
|
124 | + PublicKeyCredentialCreationOptions::ATTESTATION_CONVEYANCE_PREFERENCE_NONE, |
|
125 | + null |
|
126 | + ); |
|
127 | + } |
|
128 | + |
|
129 | + public function finishRegister(PublicKeyCredentialCreationOptions $publicKeyCredentialCreationOptions, string $name, string $data): PublicKeyCredentialEntity { |
|
130 | + $tokenBindingHandler = new TokenBindingNotSupportedHandler(); |
|
131 | + |
|
132 | + $attestationStatementSupportManager = new AttestationStatementSupportManager(); |
|
133 | + $attestationStatementSupportManager->add(new NoneAttestationStatementSupport()); |
|
134 | + |
|
135 | + $attestationObjectLoader = new AttestationObjectLoader($attestationStatementSupportManager); |
|
136 | + $publicKeyCredentialLoader = new PublicKeyCredentialLoader($attestationObjectLoader); |
|
137 | + |
|
138 | + // Extension Output Checker Handler |
|
139 | + $extensionOutputCheckerHandler = new ExtensionOutputCheckerHandler(); |
|
140 | + |
|
141 | + // Authenticator Attestation Response Validator |
|
142 | + $authenticatorAttestationResponseValidator = new AuthenticatorAttestationResponseValidator( |
|
143 | + $attestationStatementSupportManager, |
|
144 | + $this->repository, |
|
145 | + $tokenBindingHandler, |
|
146 | + $extensionOutputCheckerHandler |
|
147 | + ); |
|
148 | + |
|
149 | + try { |
|
150 | + // Load the data |
|
151 | + $publicKeyCredential = $publicKeyCredentialLoader->load($data); |
|
152 | + $response = $publicKeyCredential->getResponse(); |
|
153 | + |
|
154 | + // Check if the response is an Authenticator Attestation Response |
|
155 | + if (!$response instanceof AuthenticatorAttestationResponse) { |
|
156 | + throw new \RuntimeException('Not an authenticator attestation response'); |
|
157 | + } |
|
158 | + |
|
159 | + // Check the response against the request |
|
160 | + $request = ServerRequest::fromGlobals(); |
|
161 | + |
|
162 | + $publicKeyCredentialSource = $authenticatorAttestationResponseValidator->check( |
|
163 | + $response, |
|
164 | + $publicKeyCredentialCreationOptions, |
|
165 | + $request); |
|
166 | + } catch (\Throwable $exception) { |
|
167 | + throw $exception; |
|
168 | + } |
|
169 | + |
|
170 | + // Persist the data |
|
171 | + return $this->repository->saveAndReturnCredentialSource($publicKeyCredentialSource, $name); |
|
172 | + } |
|
173 | + |
|
174 | + private function stripPort(string $serverHost): string { |
|
175 | + return preg_replace('/(:\d+$)/', '', $serverHost); |
|
176 | + } |
|
177 | + |
|
178 | + public function startAuthentication(string $uid, string $serverHost): PublicKeyCredentialRequestOptions { |
|
179 | + // List of registered PublicKeyCredentialDescriptor classes associated to the user |
|
180 | + $registeredPublicKeyCredentialDescriptors = array_map(function (PublicKeyCredentialEntity $entity) { |
|
181 | + $credential = $entity->toPublicKeyCredentialSource(); |
|
182 | + return new PublicKeyCredentialDescriptor( |
|
183 | + $credential->getType(), |
|
184 | + $credential->getPublicKeyCredentialId() |
|
185 | + ); |
|
186 | + }, $this->credentialMapper->findAllForUid($uid)); |
|
187 | + |
|
188 | + // Public Key Credential Request Options |
|
189 | + return new PublicKeyCredentialRequestOptions( |
|
190 | + random_bytes(32), // Challenge |
|
191 | + 60000, // Timeout |
|
192 | + $this->stripPort($serverHost), // Relying Party ID |
|
193 | + $registeredPublicKeyCredentialDescriptors, // Registered PublicKeyCredentialDescriptor classes |
|
194 | + AuthenticatorSelectionCriteria::USER_VERIFICATION_REQUIREMENT_DISCOURAGED |
|
195 | + ); |
|
196 | + } |
|
197 | + |
|
198 | + public function finishAuthentication(PublicKeyCredentialRequestOptions $publicKeyCredentialRequestOptions, string $data, string $uid) { |
|
199 | + $attestationStatementSupportManager = new AttestationStatementSupportManager(); |
|
200 | + $attestationStatementSupportManager->add(new NoneAttestationStatementSupport()); |
|
201 | + |
|
202 | + $attestationObjectLoader = new AttestationObjectLoader($attestationStatementSupportManager); |
|
203 | + $publicKeyCredentialLoader = new PublicKeyCredentialLoader($attestationObjectLoader); |
|
204 | + |
|
205 | + $tokenBindingHandler = new TokenBindingNotSupportedHandler(); |
|
206 | + $extensionOutputCheckerHandler = new ExtensionOutputCheckerHandler(); |
|
207 | + $algorithmManager = new \Cose\Algorithm\Manager(); |
|
208 | + $algorithmManager->add(new ES256()); |
|
209 | + $algorithmManager->add(new RS256()); |
|
210 | + |
|
211 | + $authenticatorAssertionResponseValidator = new AuthenticatorAssertionResponseValidator( |
|
212 | + $this->repository, |
|
213 | + $tokenBindingHandler, |
|
214 | + $extensionOutputCheckerHandler, |
|
215 | + $algorithmManager |
|
216 | + ); |
|
217 | + |
|
218 | + try { |
|
219 | + $this->logger->debug('Loading publickey credentials from: ' . $data); |
|
220 | + |
|
221 | + // Load the data |
|
222 | + $publicKeyCredential = $publicKeyCredentialLoader->load($data); |
|
223 | + $response = $publicKeyCredential->getResponse(); |
|
224 | + |
|
225 | + // Check if the response is an Authenticator Attestation Response |
|
226 | + if (!$response instanceof AuthenticatorAssertionResponse) { |
|
227 | + throw new \RuntimeException('Not an authenticator attestation response'); |
|
228 | + } |
|
229 | + |
|
230 | + // Check the response against the request |
|
231 | + $request = ServerRequest::fromGlobals(); |
|
232 | + |
|
233 | + $publicKeyCredentialSource = $authenticatorAssertionResponseValidator->check( |
|
234 | + $publicKeyCredential->getRawId(), |
|
235 | + $response, |
|
236 | + $publicKeyCredentialRequestOptions, |
|
237 | + $request, |
|
238 | + $uid |
|
239 | + ); |
|
240 | + } catch (\Throwable $e) { |
|
241 | + throw $e; |
|
242 | + } |
|
243 | + |
|
244 | + |
|
245 | + |
|
246 | + return true; |
|
247 | + } |
|
248 | + |
|
249 | + public function deleteRegistration(IUser $user, int $id): void { |
|
250 | + try { |
|
251 | + $entry = $this->credentialMapper->findById($user->getUID(), $id); |
|
252 | + } catch (DoesNotExistException $e) { |
|
253 | + $this->logger->warning("WebAuthn device $id does not exist, can't delete it"); |
|
254 | + return; |
|
255 | + } |
|
256 | + |
|
257 | + $this->credentialMapper->delete($entry); |
|
258 | + } |
|
259 | + |
|
260 | + public function isWebAuthnAvailable(): bool { |
|
261 | + if (!extension_loaded('bcmath')) { |
|
262 | + return false; |
|
263 | + } |
|
264 | + |
|
265 | + if (!extension_loaded('gmp')) { |
|
266 | + return false; |
|
267 | + } |
|
268 | + |
|
269 | + if (!$this->config->getSystemValueBool('auth.webauthn.enabled', true)) { |
|
270 | + return false; |
|
271 | + } |
|
272 | + |
|
273 | + return true; |
|
274 | + } |
|
275 | 275 | } |
@@ -84,13 +84,13 @@ discard block |
||
84 | 84 | public function startRegistration(IUser $user, string $serverHost): PublicKeyCredentialCreationOptions { |
85 | 85 | $rpEntity = new PublicKeyCredentialRpEntity( |
86 | 86 | 'Nextcloud', //Name |
87 | - $this->stripPort($serverHost), //ID |
|
87 | + $this->stripPort($serverHost), //ID |
|
88 | 88 | null //Icon |
89 | 89 | ); |
90 | 90 | |
91 | 91 | $userEntity = new PublicKeyCredentialUserEntity( |
92 | - $user->getUID(), //Name |
|
93 | - $user->getUID(), //ID |
|
92 | + $user->getUID(), //Name |
|
93 | + $user->getUID(), //ID |
|
94 | 94 | $user->getDisplayName() //Display name |
95 | 95 | // 'https://foo.example.co/avatar/123e4567-e89b-12d3-a456-426655440000' //Icon |
96 | 96 | ); |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | |
178 | 178 | public function startAuthentication(string $uid, string $serverHost): PublicKeyCredentialRequestOptions { |
179 | 179 | // List of registered PublicKeyCredentialDescriptor classes associated to the user |
180 | - $registeredPublicKeyCredentialDescriptors = array_map(function (PublicKeyCredentialEntity $entity) { |
|
180 | + $registeredPublicKeyCredentialDescriptors = array_map(function(PublicKeyCredentialEntity $entity) { |
|
181 | 181 | $credential = $entity->toPublicKeyCredentialSource(); |
182 | 182 | return new PublicKeyCredentialDescriptor( |
183 | 183 | $credential->getType(), |
@@ -187,10 +187,10 @@ discard block |
||
187 | 187 | |
188 | 188 | // Public Key Credential Request Options |
189 | 189 | return new PublicKeyCredentialRequestOptions( |
190 | - random_bytes(32), // Challenge |
|
191 | - 60000, // Timeout |
|
192 | - $this->stripPort($serverHost), // Relying Party ID |
|
193 | - $registeredPublicKeyCredentialDescriptors, // Registered PublicKeyCredentialDescriptor classes |
|
190 | + random_bytes(32), // Challenge |
|
191 | + 60000, // Timeout |
|
192 | + $this->stripPort($serverHost), // Relying Party ID |
|
193 | + $registeredPublicKeyCredentialDescriptors, // Registered PublicKeyCredentialDescriptor classes |
|
194 | 194 | AuthenticatorSelectionCriteria::USER_VERIFICATION_REQUIREMENT_DISCOURAGED |
195 | 195 | ); |
196 | 196 | } |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | ); |
217 | 217 | |
218 | 218 | try { |
219 | - $this->logger->debug('Loading publickey credentials from: ' . $data); |
|
219 | + $this->logger->debug('Loading publickey credentials from: '.$data); |
|
220 | 220 | |
221 | 221 | // Load the data |
222 | 222 | $publicKeyCredential = $publicKeyCredentialLoader->load($data); |