AppPasswordController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. * @author Kate Döen <kate.doeen@nextcloud.com>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OC\Core\Controller;
  28. use OC\Authentication\Events\AppPasswordCreatedEvent;
  29. use OC\Authentication\Token\IProvider;
  30. use OC\Authentication\Token\IToken;
  31. use OCP\AppFramework\Http;
  32. use OCP\AppFramework\Http\DataResponse;
  33. use OCP\AppFramework\OCS\OCSForbiddenException;
  34. use OCP\Authentication\Exceptions\CredentialsUnavailableException;
  35. use OCP\Authentication\Exceptions\InvalidTokenException;
  36. use OCP\Authentication\Exceptions\PasswordUnavailableException;
  37. use OCP\Authentication\LoginCredentials\IStore;
  38. use OCP\EventDispatcher\IEventDispatcher;
  39. use OCP\IRequest;
  40. use OCP\ISession;
  41. use OCP\Security\ISecureRandom;
  42. class AppPasswordController extends \OCP\AppFramework\OCSController {
  43. public function __construct(
  44. string $appName,
  45. IRequest $request,
  46. private ISession $session,
  47. private ISecureRandom $random,
  48. private IProvider $tokenProvider,
  49. private IStore $credentialStore,
  50. private IEventDispatcher $eventDispatcher,
  51. ) {
  52. parent::__construct($appName, $request);
  53. }
  54. /**
  55. * @NoAdminRequired
  56. * @PasswordConfirmationRequired
  57. *
  58. * Create app password
  59. *
  60. * @return DataResponse<Http::STATUS_OK, array{apppassword: string}, array{}>
  61. * @throws OCSForbiddenException Creating app password is not allowed
  62. *
  63. * 200: App password returned
  64. */
  65. public function getAppPassword(): DataResponse {
  66. // We do not allow the creation of new tokens if this is an app password
  67. if ($this->session->exists('app_password')) {
  68. throw new OCSForbiddenException('You cannot request an new apppassword with an apppassword');
  69. }
  70. try {
  71. $credentials = $this->credentialStore->getLoginCredentials();
  72. } catch (CredentialsUnavailableException $e) {
  73. throw new OCSForbiddenException();
  74. }
  75. try {
  76. $password = $credentials->getPassword();
  77. } catch (PasswordUnavailableException $e) {
  78. $password = null;
  79. }
  80. $userAgent = $this->request->getHeader('USER_AGENT');
  81. $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
  82. $generatedToken = $this->tokenProvider->generateToken(
  83. $token,
  84. $credentials->getUID(),
  85. $credentials->getLoginName(),
  86. $password,
  87. $userAgent,
  88. IToken::PERMANENT_TOKEN,
  89. IToken::DO_NOT_REMEMBER
  90. );
  91. $this->eventDispatcher->dispatchTyped(
  92. new AppPasswordCreatedEvent($generatedToken)
  93. );
  94. return new DataResponse([
  95. 'apppassword' => $token
  96. ]);
  97. }
  98. /**
  99. * @NoAdminRequired
  100. *
  101. * Delete app password
  102. *
  103. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  104. * @throws OCSForbiddenException Deleting app password is not allowed
  105. *
  106. * 200: App password deleted successfully
  107. */
  108. public function deleteAppPassword(): DataResponse {
  109. if (!$this->session->exists('app_password')) {
  110. throw new OCSForbiddenException('no app password in use');
  111. }
  112. $appPassword = $this->session->get('app_password');
  113. try {
  114. $token = $this->tokenProvider->getToken($appPassword);
  115. } catch (InvalidTokenException $e) {
  116. throw new OCSForbiddenException('could not remove apptoken');
  117. }
  118. $this->tokenProvider->invalidateTokenById($token->getUID(), $token->getId());
  119. return new DataResponse();
  120. }
  121. /**
  122. * @NoAdminRequired
  123. *
  124. * Rotate app password
  125. *
  126. * @return DataResponse<Http::STATUS_OK, array{apppassword: string}, array{}>
  127. * @throws OCSForbiddenException Rotating app password is not allowed
  128. *
  129. * 200: App password returned
  130. */
  131. public function rotateAppPassword(): DataResponse {
  132. if (!$this->session->exists('app_password')) {
  133. throw new OCSForbiddenException('no app password in use');
  134. }
  135. $appPassword = $this->session->get('app_password');
  136. try {
  137. $token = $this->tokenProvider->getToken($appPassword);
  138. } catch (InvalidTokenException $e) {
  139. throw new OCSForbiddenException('could not rotate apptoken');
  140. }
  141. $newToken = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
  142. $this->tokenProvider->rotate($token, $appPassword, $newToken);
  143. return new DataResponse([
  144. 'apppassword' => $newToken,
  145. ]);
  146. }
  147. }