OCSController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\BruteForceProtection;
  12. use OCP\AppFramework\Http\Attribute\OpenAPI;
  13. use OCP\AppFramework\Http\Attribute\PublicPage;
  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. ) {
  27. parent::__construct($appName, $request);
  28. }
  29. #[PublicPage]
  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. * Get the capabilities
  44. *
  45. * @return DataResponse<Http::STATUS_OK, array{version: array{major: int, minor: int, micro: int, string: string, edition: '', extendedSupport: bool}, capabilities: array<string, mixed>}, array{}>
  46. *
  47. * 200: Capabilities returned
  48. */
  49. #[PublicPage]
  50. #[ApiRoute(verb: 'GET', url: '/capabilities', root: '/cloud')]
  51. public function getCapabilities(): DataResponse {
  52. $result = [];
  53. [$major, $minor, $micro] = \OCP\Util::getVersion();
  54. $result['version'] = [
  55. 'major' => (int)$major,
  56. 'minor' => (int)$minor,
  57. 'micro' => (int)$micro,
  58. 'string' => \OC_Util::getVersionString(),
  59. 'edition' => '',
  60. 'extendedSupport' => \OCP\Util::hasExtendedSupport()
  61. ];
  62. if ($this->userSession->isLoggedIn()) {
  63. $result['capabilities'] = $this->capabilitiesManager->getCapabilities();
  64. } else {
  65. $result['capabilities'] = $this->capabilitiesManager->getCapabilities(true);
  66. }
  67. $response = new DataResponse($result);
  68. $response->setETag(md5(json_encode($result)));
  69. return $response;
  70. }
  71. #[PublicPage]
  72. #[BruteForceProtection(action: 'login')]
  73. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  74. #[ApiRoute(verb: 'POST', url: '/check', root: '/person')]
  75. public function personCheck(string $login = '', string $password = ''): DataResponse {
  76. if ($login !== '' && $password !== '') {
  77. if ($this->userManager->checkPassword($login, $password)) {
  78. return new DataResponse([
  79. 'person' => [
  80. 'personid' => $login
  81. ]
  82. ]);
  83. }
  84. $response = new DataResponse([], 102);
  85. $response->throttle();
  86. return $response;
  87. }
  88. return new DataResponse([], 101);
  89. }
  90. #[PublicPage]
  91. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  92. #[ApiRoute(verb: 'GET', url: '/key/{cloudId}', root: '/identityproof')]
  93. public function getIdentityProof(string $cloudId): DataResponse {
  94. $userObject = $this->userManager->get($cloudId);
  95. if ($userObject !== null) {
  96. $key = $this->keyManager->getKey($userObject);
  97. $data = [
  98. 'public' => $key->getPublic(),
  99. ];
  100. return new DataResponse($data);
  101. }
  102. return new DataResponse(['Account not found'], 404);
  103. }
  104. }