OCSController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Core\Controller;
  7. use OC\App\AppManager;
  8. use OC\CapabilitiesManager;
  9. use OC\Security\IdentityProof\Manager;
  10. use OC_App;
  11. use OCP\AppFramework\Http;
  12. use OCP\AppFramework\Http\Attribute\ApiRoute;
  13. use OCP\AppFramework\Http\Attribute\OpenAPI;
  14. use OCP\AppFramework\Http\DataResponse;
  15. use OCP\IRequest;
  16. use OCP\IUserManager;
  17. use OCP\IUserSession;
  18. class OCSController extends \OCP\AppFramework\OCSController {
  19. public function __construct(
  20. string $appName,
  21. IRequest $request,
  22. private CapabilitiesManager $capabilitiesManager,
  23. private IUserSession $userSession,
  24. private IUserManager $userManager,
  25. private Manager $keyManager,
  26. private AppManager $appManager,
  27. ) {
  28. parent::__construct($appName, $request);
  29. }
  30. /**
  31. * @PublicPage
  32. */
  33. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  34. #[ApiRoute(verb: 'GET', url: '/config', root: '')]
  35. public function getConfig(): DataResponse {
  36. $data = [
  37. 'version' => '1.7',
  38. 'website' => 'Nextcloud',
  39. 'host' => $this->request->getServerHost(),
  40. 'contact' => '',
  41. 'ssl' => 'false',
  42. ];
  43. return new DataResponse($data);
  44. }
  45. /**
  46. * @PublicPage
  47. *
  48. * Get the capabilities
  49. *
  50. * @return DataResponse<Http::STATUS_OK, array{version: array{major: int, minor: int, micro: int, string: string, edition: '', extendedSupport: bool}, capabilities: array<string, mixed>, features: array<string, list<string>>, apps: array<string, array{version: string, api_versions: list<string>}>}, array{}>
  51. *
  52. * 200: Capabilities returned
  53. */
  54. #[ApiRoute(verb: 'GET', url: '/capabilities', root: '/cloud')]
  55. public function getCapabilities(): DataResponse {
  56. $result = [];
  57. [$major, $minor, $micro] = \OCP\Util::getVersion();
  58. $result['version'] = [
  59. 'major' => (int)$major,
  60. 'minor' => (int)$minor,
  61. 'micro' => (int)$micro,
  62. 'string' => \OC_Util::getVersionString(),
  63. 'edition' => '',
  64. 'extendedSupport' => \OCP\Util::hasExtendedSupport()
  65. ];
  66. if ($this->userSession->isLoggedIn()) {
  67. $result['capabilities'] = $this->capabilitiesManager->getCapabilities();
  68. } else {
  69. $result['capabilities'] = $this->capabilitiesManager->getCapabilities(true);
  70. }
  71. $result['features'] = $this->capabilitiesManager->getFeatures();
  72. $result['apps'] = [];
  73. foreach (OC_App::getEnabledApps() as $app) {
  74. $info = $this->appManager->getAppInfo($app);
  75. $result['apps'][$app]['version'] = (string)$info['version'];
  76. $result['apps'][$app]['api_versions'] = array_values(array_map(static fn ($apiVersion) => (string)$apiVersion, (array)$info['api-version']));
  77. }
  78. $response = new DataResponse($result);
  79. $response->setETag(md5(json_encode($result)));
  80. return $response;
  81. }
  82. /**
  83. * @PublicPage
  84. * @BruteForceProtection(action=login)
  85. */
  86. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  87. #[ApiRoute(verb: 'POST', url: '/check', root: '/person')]
  88. public function personCheck(string $login = '', string $password = ''): DataResponse {
  89. if ($login !== '' && $password !== '') {
  90. if ($this->userManager->checkPassword($login, $password)) {
  91. return new DataResponse([
  92. 'person' => [
  93. 'personid' => $login
  94. ]
  95. ]);
  96. }
  97. $response = new DataResponse([], 102);
  98. $response->throttle();
  99. return $response;
  100. }
  101. return new DataResponse([], 101);
  102. }
  103. /**
  104. * @PublicPage
  105. */
  106. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  107. #[ApiRoute(verb: 'GET', url: '/key/{cloudId}', root: '/identityproof')]
  108. public function getIdentityProof(string $cloudId): DataResponse {
  109. $userObject = $this->userManager->get($cloudId);
  110. if ($userObject !== null) {
  111. $key = $this->keyManager->getKey($userObject);
  112. $data = [
  113. 'public' => $key->getPublic(),
  114. ];
  115. return new DataResponse($data);
  116. }
  117. return new DataResponse(['Account not found'], 404);
  118. }
  119. }