AppPasswordController.php 3.0 KB

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