AppPasswordController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /** @var ISession */
  42. private $session;
  43. /** @var ISecureRandom */
  44. private $random;
  45. /** @var IProvider */
  46. private $tokenProvider;
  47. /** @var IStore */
  48. private $credentialStore;
  49. /** @var IEventDispatcher */
  50. private $eventDispatcher;
  51. public function __construct(string $appName,
  52. IRequest $request,
  53. ISession $session,
  54. ISecureRandom $random,
  55. IProvider $tokenProvider,
  56. IStore $credentialStore,
  57. IEventDispatcher $eventDispatcher) {
  58. parent::__construct($appName, $request);
  59. $this->session = $session;
  60. $this->random = $random;
  61. $this->tokenProvider = $tokenProvider;
  62. $this->credentialStore = $credentialStore;
  63. $this->eventDispatcher = $eventDispatcher;
  64. }
  65. /**
  66. * @NoAdminRequired
  67. *
  68. * @return DataResponse
  69. * @throws OCSForbiddenException
  70. */
  71. public function getAppPassword(): DataResponse {
  72. // We do not allow the creation of new tokens if this is an app password
  73. if ($this->session->exists('app_password')) {
  74. throw new OCSForbiddenException('You cannot request an new apppassword with an apppassword');
  75. }
  76. try {
  77. $credentials = $this->credentialStore->getLoginCredentials();
  78. } catch (CredentialsUnavailableException $e) {
  79. throw new OCSForbiddenException();
  80. }
  81. try {
  82. $password = $credentials->getPassword();
  83. } catch (PasswordUnavailableException $e) {
  84. $password = null;
  85. }
  86. $userAgent = $this->request->getHeader('USER_AGENT');
  87. $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
  88. $generatedToken = $this->tokenProvider->generateToken(
  89. $token,
  90. $credentials->getUID(),
  91. $credentials->getLoginName(),
  92. $password,
  93. $userAgent,
  94. IToken::PERMANENT_TOKEN,
  95. IToken::DO_NOT_REMEMBER
  96. );
  97. $this->eventDispatcher->dispatchTyped(
  98. new AppPasswordCreatedEvent($generatedToken)
  99. );
  100. return new DataResponse([
  101. 'apppassword' => $token
  102. ]);
  103. }
  104. /**
  105. * @NoAdminRequired
  106. *
  107. * @return DataResponse
  108. */
  109. public function deleteAppPassword() {
  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 remove apptoken');
  118. }
  119. $this->tokenProvider->invalidateTokenById($token->getUID(), $token->getId());
  120. return new DataResponse();
  121. }
  122. /**
  123. * @NoAdminRequired
  124. */
  125. public function rotateAppPassword(): DataResponse {
  126. if (!$this->session->exists('app_password')) {
  127. throw new OCSForbiddenException('no app password in use');
  128. }
  129. $appPassword = $this->session->get('app_password');
  130. try {
  131. $token = $this->tokenProvider->getToken($appPassword);
  132. } catch (InvalidTokenException $e) {
  133. throw new OCSForbiddenException('could not rotate apptoken');
  134. }
  135. $newToken = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
  136. $this->tokenProvider->rotate($token, $appPassword, $newToken);
  137. return new DataResponse([
  138. 'apppassword' => $newToken,
  139. ]);
  140. }
  141. }