OCSController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\CapabilitiesManager;
  8. use OC\Security\IdentityProof\Manager;
  9. use OCP\AppFramework\Http;
  10. use OCP\AppFramework\Http\Attribute\ApiRoute;
  11. use OCP\AppFramework\Http\Attribute\OpenAPI;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\IRequest;
  14. use OCP\IUserManager;
  15. use OCP\IUserSession;
  16. class OCSController extends \OCP\AppFramework\OCSController {
  17. public function __construct(
  18. string $appName,
  19. IRequest $request,
  20. private CapabilitiesManager $capabilitiesManager,
  21. private IUserSession $userSession,
  22. private IUserManager $userManager,
  23. private Manager $keyManager,
  24. ) {
  25. parent::__construct($appName, $request);
  26. }
  27. /**
  28. * @PublicPage
  29. */
  30. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  31. #[ApiRoute(verb: 'GET', url: '/config', root: '')]
  32. public function getConfig(): DataResponse {
  33. $data = [
  34. 'version' => '1.7',
  35. 'website' => 'Nextcloud',
  36. 'host' => $this->request->getServerHost(),
  37. 'contact' => '',
  38. 'ssl' => 'false',
  39. ];
  40. return new DataResponse($data);
  41. }
  42. /**
  43. * @PublicPage
  44. *
  45. * Get the capabilities
  46. *
  47. * @return DataResponse<Http::STATUS_OK, array{version: array{major: int, minor: int, micro: int, string: string, edition: '', extendedSupport: bool}, capabilities: array<string, mixed>}, array{}>
  48. *
  49. * 200: Capabilities returned
  50. */
  51. #[ApiRoute(verb: 'GET', url: '/capabilities', root: '/cloud')]
  52. public function getCapabilities(): DataResponse {
  53. $result = [];
  54. [$major, $minor, $micro] = \OCP\Util::getVersion();
  55. $result['version'] = [
  56. 'major' => (int)$major,
  57. 'minor' => (int)$minor,
  58. 'micro' => (int)$micro,
  59. 'string' => \OC_Util::getVersionString(),
  60. 'edition' => '',
  61. 'extendedSupport' => \OCP\Util::hasExtendedSupport()
  62. ];
  63. if ($this->userSession->isLoggedIn()) {
  64. $result['capabilities'] = $this->capabilitiesManager->getCapabilities();
  65. } else {
  66. $result['capabilities'] = $this->capabilitiesManager->getCapabilities(true);
  67. }
  68. $response = new DataResponse($result);
  69. $response->setETag(md5(json_encode($result)));
  70. return $response;
  71. }
  72. /**
  73. * @PublicPage
  74. * @BruteForceProtection(action=login)
  75. */
  76. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  77. #[ApiRoute(verb: 'POST', url: '/check', root: '/person')]
  78. public function personCheck(string $login = '', string $password = ''): DataResponse {
  79. if ($login !== '' && $password !== '') {
  80. if ($this->userManager->checkPassword($login, $password)) {
  81. return new DataResponse([
  82. 'person' => [
  83. 'personid' => $login
  84. ]
  85. ]);
  86. }
  87. $response = new DataResponse([], 102);
  88. $response->throttle();
  89. return $response;
  90. }
  91. return new DataResponse([], 101);
  92. }
  93. /**
  94. * @PublicPage
  95. */
  96. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  97. #[ApiRoute(verb: 'GET', url: '/key/{cloudId}', root: '/identityproof')]
  98. public function getIdentityProof(string $cloudId): DataResponse {
  99. $userObject = $this->userManager->get($cloudId);
  100. if ($userObject !== null) {
  101. $key = $this->keyManager->getKey($userObject);
  102. $data = [
  103. 'public' => $key->getPublic(),
  104. ];
  105. return new DataResponse($data);
  106. }
  107. return new DataResponse(['Account not found'], 404);
  108. }
  109. }