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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Controller;
  25. use OC\Authentication\Exceptions\InvalidTokenException;
  26. use OC\Authentication\Token\IProvider;
  27. use OC\Authentication\Token\IToken;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\AppFramework\OCS\OCSForbiddenException;
  30. use OCP\Authentication\Exceptions\CredentialsUnavailableException;
  31. use OCP\Authentication\Exceptions\PasswordUnavailableException;
  32. use OCP\Authentication\LoginCredentials\IStore;
  33. use OCP\IRequest;
  34. use OCP\ISession;
  35. use OCP\Security\ISecureRandom;
  36. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  37. use Symfony\Component\EventDispatcher\GenericEvent;
  38. class AppPasswordController extends \OCP\AppFramework\OCSController {
  39. /** @var ISession */
  40. private $session;
  41. /** @var ISecureRandom */
  42. private $random;
  43. /** @var IProvider */
  44. private $tokenProvider;
  45. /** @var IStore */
  46. private $credentialStore;
  47. /** @var EventDispatcherInterface */
  48. private $eventDispatcher;
  49. public function __construct(string $appName,
  50. IRequest $request,
  51. ISession $session,
  52. ISecureRandom $random,
  53. IProvider $tokenProvider,
  54. IStore $credentialStore,
  55. EventDispatcherInterface $eventDispatcher) {
  56. parent::__construct($appName, $request);
  57. $this->session = $session;
  58. $this->random = $random;
  59. $this->tokenProvider = $tokenProvider;
  60. $this->credentialStore = $credentialStore;
  61. $this->eventDispatcher = $eventDispatcher;
  62. }
  63. /**
  64. * @NoAdminRequired
  65. *
  66. * @return DataResponse
  67. * @throws OCSForbiddenException
  68. */
  69. public function getAppPassword(): DataResponse {
  70. // We do not allow the creation of new tokens if this is an app password
  71. if ($this->session->exists('app_password')) {
  72. throw new OCSForbiddenException('You cannot request an new apppassword with an apppassword');
  73. }
  74. try {
  75. $credentials = $this->credentialStore->getLoginCredentials();
  76. } catch (CredentialsUnavailableException $e) {
  77. throw new OCSForbiddenException();
  78. }
  79. try {
  80. $password = $credentials->getPassword();
  81. } catch (PasswordUnavailableException $e) {
  82. $password = null;
  83. }
  84. $userAgent = $this->request->getHeader('USER_AGENT');
  85. $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
  86. $generatedToken = $this->tokenProvider->generateToken(
  87. $token,
  88. $credentials->getUID(),
  89. $credentials->getLoginName(),
  90. $password,
  91. $userAgent,
  92. IToken::PERMANENT_TOKEN,
  93. IToken::DO_NOT_REMEMBER
  94. );
  95. $event = new GenericEvent($generatedToken);
  96. $this->eventDispatcher->dispatch('app_password_created', $event);
  97. return new DataResponse([
  98. 'apppassword' => $token
  99. ]);
  100. }
  101. /**
  102. * @NoAdminRequired
  103. *
  104. * @return DataResponse
  105. */
  106. public function deleteAppPassword() {
  107. if (!$this->session->exists('app_password')) {
  108. throw new OCSForbiddenException('no app password in use');
  109. }
  110. $appPassword = $this->session->get('app_password');
  111. try {
  112. $token = $this->tokenProvider->getToken($appPassword);
  113. } catch (InvalidTokenException $e) {
  114. throw new OCSForbiddenException('could not remove apptoken');
  115. }
  116. $this->tokenProvider->invalidateTokenById($token->getUID(), $token->getId());
  117. return new DataResponse();
  118. }
  119. }