WebAuthnController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Kate Döen <kate.doeen@nextcloud.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\Settings\Controller;
  27. use OC\Authentication\WebAuthn\Manager;
  28. use OCA\Settings\AppInfo\Application;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\Attribute\OpenAPI;
  32. use OCP\AppFramework\Http\JSONResponse;
  33. use OCP\IRequest;
  34. use OCP\ISession;
  35. use OCP\IUserSession;
  36. use Psr\Log\LoggerInterface;
  37. use Webauthn\PublicKeyCredentialCreationOptions;
  38. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  39. class WebAuthnController extends Controller {
  40. private const WEBAUTHN_REGISTRATION = 'webauthn_registration';
  41. public function __construct(
  42. IRequest $request,
  43. private LoggerInterface $logger,
  44. private Manager $manager,
  45. private IUserSession $userSession,
  46. private ISession $session,
  47. ) {
  48. parent::__construct(Application::APP_ID, $request);
  49. }
  50. /**
  51. * @NoAdminRequired
  52. * @NoSubAdminRequired
  53. * @PasswordConfirmationRequired
  54. * @UseSession
  55. * @NoCSRFRequired
  56. */
  57. public function startRegistration(): JSONResponse {
  58. $this->logger->debug('Starting WebAuthn registration');
  59. $credentialOptions = $this->manager->startRegistration($this->userSession->getUser(), $this->request->getServerHost());
  60. // Set this in the session since we need it on finish
  61. $this->session->set(self::WEBAUTHN_REGISTRATION, $credentialOptions);
  62. return new JSONResponse($credentialOptions);
  63. }
  64. /**
  65. * @NoAdminRequired
  66. * @NoSubAdminRequired
  67. * @PasswordConfirmationRequired
  68. * @UseSession
  69. */
  70. public function finishRegistration(string $name, string $data): JSONResponse {
  71. $this->logger->debug('Finishing WebAuthn registration');
  72. if (!$this->session->exists(self::WEBAUTHN_REGISTRATION)) {
  73. $this->logger->debug('Trying to finish WebAuthn registration without session data');
  74. return new JSONResponse([], Http::STATUS_BAD_REQUEST);
  75. }
  76. // Obtain the publicKeyCredentialOptions from when we started the registration
  77. $publicKeyCredentialCreationOptions = PublicKeyCredentialCreationOptions::createFromArray($this->session->get(self::WEBAUTHN_REGISTRATION));
  78. $this->session->remove(self::WEBAUTHN_REGISTRATION);
  79. return new JSONResponse($this->manager->finishRegister($publicKeyCredentialCreationOptions, $name, $data));
  80. }
  81. /**
  82. * @NoAdminRequired
  83. * @NoSubAdminRequired
  84. * @PasswordConfirmationRequired
  85. */
  86. public function deleteRegistration(int $id): JSONResponse {
  87. $this->logger->debug('Finishing WebAuthn registration');
  88. $this->manager->deleteRegistration($this->userSession->getUser(), $id);
  89. return new JSONResponse([]);
  90. }
  91. }