WebAuthnController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Core\Controller;
  26. use OC\Authentication\Login\LoginData;
  27. use OC\Authentication\Login\WebAuthnChain;
  28. use OC\Authentication\WebAuthn\Manager;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\JSONResponse;
  32. use OCP\ILogger;
  33. use OCP\IRequest;
  34. use OCP\ISession;
  35. use OCP\Util;
  36. use Webauthn\PublicKeyCredentialRequestOptions;
  37. class WebAuthnController extends Controller {
  38. private const WEBAUTHN_LOGIN = 'webauthn_login';
  39. private const WEBAUTHN_LOGIN_UID = 'webauthn_login_uid';
  40. /** @var Manager */
  41. private $webAuthnManger;
  42. /** @var ISession */
  43. private $session;
  44. /** @var ILogger */
  45. private $logger;
  46. /** @var WebAuthnChain */
  47. private $webAuthnChain;
  48. public function __construct($appName, IRequest $request, Manager $webAuthnManger, ISession $session, ILogger $logger, WebAuthnChain $webAuthnChain) {
  49. parent::__construct($appName, $request);
  50. $this->webAuthnManger = $webAuthnManger;
  51. $this->session = $session;
  52. $this->logger = $logger;
  53. $this->webAuthnChain = $webAuthnChain;
  54. }
  55. /**
  56. * @NoAdminRequired
  57. * @PublicPage
  58. * @UseSession
  59. */
  60. public function startAuthentication(string $loginName): JSONResponse {
  61. $this->logger->debug('Starting WebAuthn login');
  62. $this->logger->debug('Converting login name to UID');
  63. $uid = $loginName;
  64. Util::emitHook(
  65. '\OCA\Files_Sharing\API\Server2Server',
  66. 'preLoginNameUsedAsUserName',
  67. ['uid' => &$uid]
  68. );
  69. $this->logger->debug('Got UID: ' . $uid);
  70. $publicKeyCredentialRequestOptions = $this->webAuthnManger->startAuthentication($uid, $this->request->getServerHost());
  71. $this->session->set(self::WEBAUTHN_LOGIN, json_encode($publicKeyCredentialRequestOptions));
  72. $this->session->set(self::WEBAUTHN_LOGIN_UID, $uid);
  73. return new JSONResponse($publicKeyCredentialRequestOptions);
  74. }
  75. /**
  76. * @NoAdminRequired
  77. * @PublicPage
  78. * @UseSession
  79. */
  80. public function finishAuthentication(string $data): JSONResponse {
  81. $this->logger->debug('Validating WebAuthn login');
  82. if (!$this->session->exists(self::WEBAUTHN_LOGIN) || !$this->session->exists(self::WEBAUTHN_LOGIN_UID)) {
  83. $this->logger->debug('Trying to finish WebAuthn login without session data');
  84. return new JSONResponse([], Http::STATUS_BAD_REQUEST);
  85. }
  86. // Obtain the publicKeyCredentialOptions from when we started the registration
  87. $publicKeyCredentialRequestOptions = PublicKeyCredentialRequestOptions::createFromString($this->session->get(self::WEBAUTHN_LOGIN));
  88. $uid = $this->session->get(self::WEBAUTHN_LOGIN_UID);
  89. $this->webAuthnManger->finishAuthentication($publicKeyCredentialRequestOptions, $data, $uid);
  90. //TODO: add other parameters
  91. $loginData = new LoginData(
  92. $this->request,
  93. $uid,
  94. ''
  95. );
  96. $this->webAuthnChain->process($loginData);
  97. return new JSONResponse([]);
  98. }
  99. }