AppPasswordController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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 OC\Core\Controller;
  27. use OC\Authentication\Events\AppPasswordCreatedEvent;
  28. use OC\Authentication\Exceptions\InvalidTokenException;
  29. use OC\Authentication\Token\IProvider;
  30. use OC\Authentication\Token\IToken;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\AppFramework\OCS\OCSForbiddenException;
  33. use OCP\Authentication\Exceptions\CredentialsUnavailableException;
  34. use OCP\Authentication\Exceptions\PasswordUnavailableException;
  35. use OCP\Authentication\LoginCredentials\IStore;
  36. use OCP\EventDispatcher\IEventDispatcher;
  37. use OCP\IRequest;
  38. use OCP\ISession;
  39. use OCP\Security\ISecureRandom;
  40. class AppPasswordController extends \OCP\AppFramework\OCSController {
  41. public function __construct(
  42. string $appName,
  43. IRequest $request,
  44. private ISession $session,
  45. private ISecureRandom $random,
  46. private IProvider $tokenProvider,
  47. private IStore $credentialStore,
  48. private IEventDispatcher $eventDispatcher,
  49. ) {
  50. parent::__construct($appName, $request);
  51. }
  52. /**
  53. * @NoAdminRequired
  54. *
  55. * @throws OCSForbiddenException
  56. */
  57. public function getAppPassword(): DataResponse {
  58. // We do not allow the creation of new tokens if this is an app password
  59. if ($this->session->exists('app_password')) {
  60. throw new OCSForbiddenException('You cannot request an new apppassword with an apppassword');
  61. }
  62. try {
  63. $credentials = $this->credentialStore->getLoginCredentials();
  64. } catch (CredentialsUnavailableException $e) {
  65. throw new OCSForbiddenException();
  66. }
  67. try {
  68. $password = $credentials->getPassword();
  69. } catch (PasswordUnavailableException $e) {
  70. $password = null;
  71. }
  72. $userAgent = $this->request->getHeader('USER_AGENT');
  73. $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
  74. $generatedToken = $this->tokenProvider->generateToken(
  75. $token,
  76. $credentials->getUID(),
  77. $credentials->getLoginName(),
  78. $password,
  79. $userAgent,
  80. IToken::PERMANENT_TOKEN,
  81. IToken::DO_NOT_REMEMBER
  82. );
  83. $this->eventDispatcher->dispatchTyped(
  84. new AppPasswordCreatedEvent($generatedToken)
  85. );
  86. return new DataResponse([
  87. 'apppassword' => $token
  88. ]);
  89. }
  90. /**
  91. * @NoAdminRequired
  92. */
  93. public function deleteAppPassword(): DataResponse {
  94. if (!$this->session->exists('app_password')) {
  95. throw new OCSForbiddenException('no app password in use');
  96. }
  97. $appPassword = $this->session->get('app_password');
  98. try {
  99. $token = $this->tokenProvider->getToken($appPassword);
  100. } catch (InvalidTokenException $e) {
  101. throw new OCSForbiddenException('could not remove apptoken');
  102. }
  103. $this->tokenProvider->invalidateTokenById($token->getUID(), $token->getId());
  104. return new DataResponse();
  105. }
  106. /**
  107. * @NoAdminRequired
  108. */
  109. public function rotateAppPassword(): DataResponse {
  110. if (!$this->session->exists('app_password')) {
  111. throw new OCSForbiddenException('no app password in use');
  112. }
  113. $appPassword = $this->session->get('app_password');
  114. try {
  115. $token = $this->tokenProvider->getToken($appPassword);
  116. } catch (InvalidTokenException $e) {
  117. throw new OCSForbiddenException('could not rotate apptoken');
  118. }
  119. $newToken = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
  120. $this->tokenProvider->rotate($token, $appPassword, $newToken);
  121. return new DataResponse([
  122. 'apppassword' => $newToken,
  123. ]);
  124. }
  125. }