AjaxController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Controller;
  8. use OCA\Files_External\Lib\Auth\Password\GlobalAuth;
  9. use OCA\Files_External\Lib\Auth\PublicKey\RSA;
  10. use OCP\AppFramework\Controller;
  11. use OCP\AppFramework\Http\JSONResponse;
  12. use OCP\IGroupManager;
  13. use OCP\IRequest;
  14. use OCP\IUserSession;
  15. class AjaxController extends Controller {
  16. /** @var RSA */
  17. private $rsaMechanism;
  18. /** @var GlobalAuth */
  19. private $globalAuth;
  20. /** @var IUserSession */
  21. private $userSession;
  22. /** @var IGroupManager */
  23. private $groupManager;
  24. /**
  25. * @param string $appName
  26. * @param IRequest $request
  27. * @param RSA $rsaMechanism
  28. * @param GlobalAuth $globalAuth
  29. * @param IUserSession $userSession
  30. * @param IGroupManager $groupManager
  31. */
  32. public function __construct($appName,
  33. IRequest $request,
  34. RSA $rsaMechanism,
  35. GlobalAuth $globalAuth,
  36. IUserSession $userSession,
  37. IGroupManager $groupManager) {
  38. parent::__construct($appName, $request);
  39. $this->rsaMechanism = $rsaMechanism;
  40. $this->globalAuth = $globalAuth;
  41. $this->userSession = $userSession;
  42. $this->groupManager = $groupManager;
  43. }
  44. /**
  45. * @param int $keyLength
  46. * @return array
  47. */
  48. private function generateSshKeys($keyLength) {
  49. $key = $this->rsaMechanism->createKey($keyLength);
  50. // Replace the placeholder label with a more meaningful one
  51. $key['publickey'] = str_replace('phpseclib-generated-key', gethostname(), $key['publickey']);
  52. return $key;
  53. }
  54. /**
  55. * Generates an SSH public/private key pair.
  56. *
  57. * @NoAdminRequired
  58. * @param int $keyLength
  59. */
  60. public function getSshKeys($keyLength = 1024) {
  61. $key = $this->generateSshKeys($keyLength);
  62. return new JSONResponse(
  63. ['data' => [
  64. 'private_key' => $key['privatekey'],
  65. 'public_key' => $key['publickey']
  66. ],
  67. 'status' => 'success'
  68. ]);
  69. }
  70. /**
  71. * @NoAdminRequired
  72. *
  73. * @param string $uid
  74. * @param string $user
  75. * @param string $password
  76. * @return bool
  77. */
  78. public function saveGlobalCredentials($uid, $user, $password) {
  79. $currentUser = $this->userSession->getUser();
  80. if ($currentUser === null) {
  81. return false;
  82. }
  83. // Non-admins can only edit their own credentials
  84. // Admin can edit global credentials
  85. $allowedToEdit = $uid === ''
  86. ? $this->groupManager->isAdmin($currentUser->getUID())
  87. : $currentUser->getUID() === $uid;
  88. if ($allowedToEdit) {
  89. $this->globalAuth->saveAuth($uid, $user, $password);
  90. return true;
  91. }
  92. return false;
  93. }
  94. }