You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WebAuthnController.php 3.4KB

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