|
18 | 18 | use OCA\TwoFactorWebauthn\Db\PublicKeyCredentialEntity; |
19 | 19 | use OCA\TwoFactorWebauthn\Db\PublicKeyCredentialEntityMapper; |
20 | 20 | use OCA\TwoFactorWebauthn\Event\StateChanged; |
| 21 | +use OCA\TwoFactorWebauthn\Model\Device; |
21 | 22 | use OCA\TwoFactorWebauthn\Repository\WebauthnPublicKeyCredentialSourceRepository; |
| 23 | +use OCP\AppFramework\Utility\ITimeFactory; |
22 | 24 | use OCP\EventDispatcher\IEventDispatcher; |
23 | 25 | use OCP\IRequest; |
24 | 26 | use OCP\ISession; |
@@ -80,6 +82,7 @@ public function __construct( |
80 | 82 | LoggerInterface $logger, |
81 | 83 | private readonly IRequest $request, |
82 | 84 | private readonly ISecureRandom $random, |
| 85 | + private readonly ITimeFactory $time, |
83 | 86 | ) { |
84 | 87 | $this->session = $session; |
85 | 88 | $this->repository = $repository; |
@@ -171,7 +174,7 @@ public function buildPublicKeyCredentialLoader(AttestationStatementSupportManage |
171 | 174 | return $publicKeyCredentialLoader; |
172 | 175 | } |
173 | 176 |
|
174 | | - public function finishRegister(IUser $user, string $name, $data): array { |
| 177 | + public function finishRegister(IUser $user, string $name, $data): Device { |
175 | 178 | if (!$this->session->exists(self::TWOFACTORAUTH_WEBAUTHN_REGISTRATION)) { |
176 | 179 | throw new Exception('Twofactor Webauthn registration process was not properly initialized'); |
177 | 180 | } |
@@ -212,30 +215,23 @@ public function finishRegister(IUser $user, string $name, $data): array { |
212 | 215 | $this->stripPort($this->request->getServerHost()), |
213 | 216 | ); |
214 | 217 |
|
215 | | - $this->repository->saveCredentialSource($publicKeyCredentialSource, $name); |
216 | | - $entity = $this->mapper->findPublicKeyCredential(base64_encode($publicKeyCredentialSource->getPublicKeyCredentialId())); |
| 218 | + $entity = PublicKeyCredentialEntity::fromPublicKeyCrendentialSource( |
| 219 | + $name, |
| 220 | + $publicKeyCredentialSource, |
| 221 | + $this->time->getTime(), |
| 222 | + ); |
| 223 | + $entity = $this->mapper->insert($entity); |
217 | 224 | $this->eventDispatcher->dispatch(StateChanged::class, new StateChanged($user, true)); |
218 | | - |
219 | | - return [ |
220 | | - 'entityId' => $entity->getId(), |
221 | | - 'id' => base64_encode($publicKeyCredentialSource->getPublicKeyCredentialId()), |
222 | | - 'name' => $name, |
223 | | - 'createdAt' => $entity->getCreatedAt(), |
224 | | - 'active' => true, |
225 | | - ]; |
| 225 | + return Device::fromPublicKeyCredentialEntity($entity); |
226 | 226 | } |
227 | 227 |
|
| 228 | + /** |
| 229 | + * @param IUser $user |
| 230 | + * @return Device[] |
| 231 | + */ |
228 | 232 | public function getDevices(IUser $user): array { |
229 | 233 | $credentials = $this->mapper->findPublicKeyCredentials($user->getUID()); |
230 | | - return array_map(function (PublicKeyCredentialEntity $credential) { |
231 | | - return [ |
232 | | - 'entityId' => $credential->getId(), |
233 | | - 'id' => $credential->getPublicKeyCredentialId(), |
234 | | - 'name' => $credential->getName(), |
235 | | - 'createdAt' => $credential->getCreatedAt(), |
236 | | - 'active' => ($credential->isActive() === true) |
237 | | - ]; |
238 | | - }, $credentials); |
| 234 | + return array_map(Device::fromPublicKeyCredentialEntity(...), $credentials); |
239 | 235 | } |
240 | 236 |
|
241 | 237 | private function stripPort(string $serverHost): string { |
|
0 commit comments