AjaxController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Martin Mattel <martin.mattel@diemattels.at>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Ross Nicoll <jrn@jrn.me.uk>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files_External\Controller;
  29. use OCA\Files_External\Lib\Auth\Password\GlobalAuth;
  30. use OCA\Files_External\Lib\Auth\PublicKey\RSA;
  31. use OCP\AppFramework\Controller;
  32. use OCP\AppFramework\Http\JSONResponse;
  33. use OCP\IGroupManager;
  34. use OCP\IRequest;
  35. use OCP\IUserSession;
  36. class AjaxController extends Controller {
  37. /** @var RSA */
  38. private $rsaMechanism;
  39. /** @var GlobalAuth */
  40. private $globalAuth;
  41. /** @var IUserSession */
  42. private $userSession;
  43. /** @var IGroupManager */
  44. private $groupManager;
  45. /**
  46. * @param string $appName
  47. * @param IRequest $request
  48. * @param RSA $rsaMechanism
  49. * @param GlobalAuth $globalAuth
  50. * @param IUserSession $userSession
  51. * @param IGroupManager $groupManager
  52. */
  53. public function __construct($appName,
  54. IRequest $request,
  55. RSA $rsaMechanism,
  56. GlobalAuth $globalAuth,
  57. IUserSession $userSession,
  58. IGroupManager $groupManager) {
  59. parent::__construct($appName, $request);
  60. $this->rsaMechanism = $rsaMechanism;
  61. $this->globalAuth = $globalAuth;
  62. $this->userSession = $userSession;
  63. $this->groupManager = $groupManager;
  64. }
  65. /**
  66. * @param int $keyLength
  67. * @return array
  68. */
  69. private function generateSshKeys($keyLength) {
  70. $key = $this->rsaMechanism->createKey($keyLength);
  71. // Replace the placeholder label with a more meaningful one
  72. $key['publickey'] = str_replace('phpseclib-generated-key', gethostname(), $key['publickey']);
  73. return $key;
  74. }
  75. /**
  76. * Generates an SSH public/private key pair.
  77. *
  78. * @NoAdminRequired
  79. * @param int $keyLength
  80. */
  81. public function getSshKeys($keyLength = 1024) {
  82. $key = $this->generateSshKeys($keyLength);
  83. return new JSONResponse(
  84. ['data' => [
  85. 'private_key' => $key['privatekey'],
  86. 'public_key' => $key['publickey']
  87. ],
  88. 'status' => 'success'
  89. ]);
  90. }
  91. /**
  92. * @NoAdminRequired
  93. *
  94. * @param string $uid
  95. * @param string $user
  96. * @param string $password
  97. * @return bool
  98. */
  99. public function saveGlobalCredentials($uid, $user, $password) {
  100. $currentUser = $this->userSession->getUser();
  101. // Non-admins can only edit their own credentials
  102. $allowedToEdit = ($this->groupManager->isAdmin($currentUser->getUID()) || $currentUser->getUID() === $uid);
  103. if ($allowedToEdit) {
  104. $this->globalAuth->saveAuth($uid, $user, $password);
  105. return true;
  106. } else {
  107. return false;
  108. }
  109. }
  110. }