|
20 | 20 | use OCA\TwoFactorWebauthn\Db\PublicKeyCredentialEntityMapper; |
21 | 21 | use OCA\TwoFactorWebauthn\Event\DisabledByAdmin; |
22 | 22 | use OCA\TwoFactorWebauthn\Event\StateChanged; |
| 23 | +use OCA\TwoFactorWebauthn\Model\Device; |
23 | 24 | use OCA\TwoFactorWebauthn\Repository\WebauthnPublicKeyCredentialSourceRepository; |
| 25 | +use OCP\AppFramework\Utility\ITimeFactory; |
24 | 26 | use OCP\EventDispatcher\IEventDispatcher; |
25 | 27 | use OCP\ISession; |
26 | 28 | use OCP\IUser; |
@@ -78,6 +80,7 @@ public function __construct( |
78 | 80 | PublicKeyCredentialEntityMapper $mapper, |
79 | 81 | IEventDispatcher $eventDispatcher, |
80 | 82 | LoggerInterface $logger, |
| 83 | + private readonly ITimeFactory $time, |
81 | 84 | ) { |
82 | 85 | $this->session = $session; |
83 | 86 | $this->repository = $repository; |
@@ -169,7 +172,7 @@ public function buildPublicKeyCredentialLoader(AttestationStatementSupportManage |
169 | 172 | return $publicKeyCredentialLoader; |
170 | 173 | } |
171 | 174 |
|
172 | | - public function finishRegister(IUser $user, string $name, $data): array { |
| 175 | + public function finishRegister(IUser $user, string $name, $data): Device { |
173 | 176 | if (!$this->session->exists(self::TWOFACTORAUTH_WEBAUTHN_REGISTRATION)) { |
174 | 177 | throw new Exception('Twofactor Webauthn registration process was not properly initialized'); |
175 | 178 | } |
@@ -212,30 +215,24 @@ public function finishRegister(IUser $user, string $name, $data): array { |
212 | 215 | $request |
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 | 225 |
|
219 | | - return [ |
220 | | - 'entityId' => $entity->getId(), |
221 | | - 'id' => base64_encode($publicKeyCredentialSource->getPublicKeyCredentialId()), |
222 | | - 'name' => $name, |
223 | | - 'createdAt' => $entity !== null ? $entity->getCreatedAt() : null, |
224 | | - 'active' => true, |
225 | | - ]; |
| 226 | + return Device::fromPublicKeyCredentialEntity($entity); |
226 | 227 | } |
227 | 228 |
|
| 229 | + /** |
| 230 | + * @param IUser $user |
| 231 | + * @return Device[] |
| 232 | + */ |
228 | 233 | public function getDevices(IUser $user): array { |
229 | 234 | $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); |
| 235 | + return array_map(Device::fromPublicKeyCredentialEntity(...), $credentials); |
239 | 236 | } |
240 | 237 |
|
241 | 238 | private function stripPort(string $serverHost): string { |
|
0 commit comments