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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 OC\Core\Controller;
  25. use OC\Authentication\Login\LoginData;
  26. use OC\Authentication\Login\WebAuthnChain;
  27. use OC\Authentication\WebAuthn\Manager;
  28. use OCP\AppFramework\Controller;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\JSONResponse;
  31. use OCP\ILogger;
  32. use OCP\IRequest;
  33. use OCP\ISession;
  34. use OCP\Util;
  35. use Webauthn\PublicKeyCredentialRequestOptions;
  36. class WebAuthnController extends Controller {
  37. private const WEBAUTHN_LOGIN = 'webauthn_login';
  38. private const WEBAUTHN_LOGIN_UID = 'webauthn_login_uid';
  39. /** @var Manager */
  40. private $webAuthnManger;
  41. /** @var ISession */
  42. private $session;
  43. /** @var ILogger */
  44. private $logger;
  45. /** @var WebAuthnChain */
  46. private $webAuthnChain;
  47. public function __construct($appName, IRequest $request, Manager $webAuthnManger, ISession $session, ILogger $logger, WebAuthnChain $webAuthnChain) {
  48. parent::__construct($appName, $request);
  49. $this->webAuthnManger = $webAuthnManger;
  50. $this->session = $session;
  51. $this->logger = $logger;
  52. $this->webAuthnChain = $webAuthnChain;
  53. }
  54. /**
  55. * @NoAdminRequired
  56. * @PublicPage
  57. * @UseSession
  58. */
  59. public function startAuthentication(string $loginName): JSONResponse {
  60. $this->logger->debug('Starting WebAuthn login');
  61. $this->logger->debug('Converting login name to UID');
  62. $uid = $loginName;
  63. Util::emitHook(
  64. '\OCA\Files_Sharing\API\Server2Server',
  65. 'preLoginNameUsedAsUserName',
  66. array('uid' => &$uid)
  67. );
  68. $this->logger->debug('Got UID: ' . $uid);
  69. $publicKeyCredentialRequestOptions = $this->webAuthnManger->startAuthentication($uid, $this->request->getServerHost());
  70. $this->session->set(self::WEBAUTHN_LOGIN, json_encode($publicKeyCredentialRequestOptions));
  71. $this->session->set(self::WEBAUTHN_LOGIN_UID, $uid);
  72. return new JSONResponse($publicKeyCredentialRequestOptions);
  73. }
  74. /**
  75. * @NoAdminRequired
  76. * @PublicPage
  77. * @UseSession
  78. */
  79. public function finishAuthentication(string $data): JSONResponse {
  80. $this->logger->debug('Validating WebAuthn login');
  81. if (!$this->session->exists(self::WEBAUTHN_LOGIN) || !$this->session->exists(self::WEBAUTHN_LOGIN_UID)) {
  82. $this->logger->debug('Trying to finish WebAuthn login without session data');
  83. return new JSONResponse([], Http::STATUS_BAD_REQUEST);
  84. }
  85. // Obtain the publicKeyCredentialOptions from when we started the registration
  86. $publicKeyCredentialRequestOptions = PublicKeyCredentialRequestOptions::createFromString($this->session->get(self::WEBAUTHN_LOGIN));
  87. $uid = $this->session->get(self::WEBAUTHN_LOGIN_UID);
  88. $this->webAuthnManger->finishAuthentication($publicKeyCredentialRequestOptions, $data, $uid);
  89. //TODO: add other parameters
  90. $loginData = new LoginData(
  91. $this->request,
  92. $uid,
  93. ''
  94. );
  95. $this->webAuthnChain->process($loginData);
  96. return new JSONResponse([]);
  97. }
  98. }