AppPasswordController.php 4.7 KB

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