AppPasswordController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Token\IProvider;
  26. use OC\Authentication\Token\IToken;
  27. use OCP\AppFramework\Http\DataResponse;
  28. use OCP\AppFramework\OCS\OCSForbiddenException;
  29. use OCP\Authentication\Exceptions\CredentialsUnavailableException;
  30. use OCP\Authentication\Exceptions\PasswordUnavailableException;
  31. use OCP\Authentication\LoginCredentials\IStore;
  32. use OCP\IRequest;
  33. use OCP\ISession;
  34. use OCP\Security\ISecureRandom;
  35. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  36. use Symfony\Component\EventDispatcher\GenericEvent;
  37. class AppPasswordController extends \OCP\AppFramework\OCSController {
  38. /** @var ISession */
  39. private $session;
  40. /** @var ISecureRandom */
  41. private $random;
  42. /** @var IProvider */
  43. private $tokenProvider;
  44. /** @var IStore */
  45. private $credentialStore;
  46. /** @var EventDispatcherInterface */
  47. private $eventDispatcher;
  48. public function __construct(string $appName,
  49. IRequest $request,
  50. ISession $session,
  51. ISecureRandom $random,
  52. IProvider $tokenProvider,
  53. IStore $credentialStore,
  54. EventDispatcherInterface $eventDispatcher) {
  55. parent::__construct($appName, $request);
  56. $this->session = $session;
  57. $this->random = $random;
  58. $this->tokenProvider = $tokenProvider;
  59. $this->credentialStore = $credentialStore;
  60. $this->eventDispatcher = $eventDispatcher;
  61. }
  62. /**
  63. * @NoAdminRequired
  64. *
  65. * @return DataResponse
  66. * @throws OCSForbiddenException
  67. */
  68. public function getAppPassword(): DataResponse {
  69. // We do not allow the creation of new tokens if this is an app password
  70. if ($this->session->exists('app_password')) {
  71. throw new OCSForbiddenException('You cannot request an new apppassword with an apppassword');
  72. }
  73. try {
  74. $credentials = $this->credentialStore->getLoginCredentials();
  75. } catch (CredentialsUnavailableException $e) {
  76. throw new OCSForbiddenException();
  77. }
  78. try {
  79. $password = $credentials->getPassword();
  80. } catch (PasswordUnavailableException $e) {
  81. $password = null;
  82. }
  83. $userAgent = $this->request->getHeader('USER_AGENT');
  84. $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
  85. $generatedToken = $this->tokenProvider->generateToken(
  86. $token,
  87. $credentials->getUID(),
  88. $credentials->getLoginName(),
  89. $password,
  90. $userAgent,
  91. IToken::PERMANENT_TOKEN,
  92. IToken::DO_NOT_REMEMBER
  93. );
  94. $event = new GenericEvent($generatedToken);
  95. $this->eventDispatcher->dispatch('app_password_created', $event);
  96. return new DataResponse([
  97. 'apppassword' => $token
  98. ]);
  99. }
  100. }