You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AppPasswordController.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\Events\AppPasswordCreatedEvent;
  27. use OC\Authentication\Exceptions\InvalidTokenException;
  28. use OC\Authentication\Token\IProvider;
  29. use OC\Authentication\Token\IToken;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\OCS\OCSForbiddenException;
  32. use OCP\Authentication\Exceptions\CredentialsUnavailableException;
  33. use OCP\Authentication\Exceptions\PasswordUnavailableException;
  34. use OCP\Authentication\LoginCredentials\IStore;
  35. use OCP\EventDispatcher\IEventDispatcher;
  36. use OCP\IRequest;
  37. use OCP\ISession;
  38. use OCP\Security\ISecureRandom;
  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 IEventDispatcher */
  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. IEventDispatcher $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. $this->eventDispatcher->dispatchTyped(
  97. new AppPasswordCreatedEvent($generatedToken)
  98. );
  99. return new DataResponse([
  100. 'apppassword' => $token
  101. ]);
  102. }
  103. /**
  104. * @NoAdminRequired
  105. *
  106. * @return DataResponse
  107. */
  108. public function deleteAppPassword() {
  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. public function rotateAppPassword(): DataResponse {
  125. if (!$this->session->exists('app_password')) {
  126. throw new OCSForbiddenException('no app password in use');
  127. }
  128. $appPassword = $this->session->get('app_password');
  129. try {
  130. $token = $this->tokenProvider->getToken($appPassword);
  131. } catch (InvalidTokenException $e) {
  132. throw new OCSForbiddenException('could not rotate apptoken');
  133. }
  134. $newToken = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
  135. $this->tokenProvider->rotate($token, $appPassword, $newToken);
  136. return new DataResponse([
  137. 'apppassword' => $newToken,
  138. ]);
  139. }
  140. }