1
0

CertificateController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Settings\Controller;
  26. use OCP\App\IAppManager;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\ICertificateManager;
  31. use OCP\IL10N;
  32. use OCP\IRequest;
  33. /**
  34. * @package OC\Settings\Controller
  35. */
  36. class CertificateController extends Controller {
  37. /** @var ICertificateManager */
  38. private $userCertificateManager;
  39. /** @var ICertificateManager */
  40. private $systemCertificateManager;
  41. /** @var IL10N */
  42. private $l10n;
  43. /** @var IAppManager */
  44. private $appManager;
  45. /**
  46. * @param string $appName
  47. * @param IRequest $request
  48. * @param ICertificateManager $userCertificateManager
  49. * @param ICertificateManager $systemCertificateManager
  50. * @param IL10N $l10n
  51. * @param IAppManager $appManager
  52. */
  53. public function __construct($appName,
  54. IRequest $request,
  55. ICertificateManager $userCertificateManager,
  56. ICertificateManager $systemCertificateManager,
  57. IL10N $l10n,
  58. IAppManager $appManager) {
  59. parent::__construct($appName, $request);
  60. $this->userCertificateManager = $userCertificateManager;
  61. $this->systemCertificateManager = $systemCertificateManager;
  62. $this->l10n = $l10n;
  63. $this->appManager = $appManager;
  64. }
  65. /**
  66. * Add a new personal root certificate to the users' trust store
  67. *
  68. * @NoAdminRequired
  69. * @NoSubadminRequired
  70. * @return array
  71. */
  72. public function addPersonalRootCertificate() {
  73. return $this->addCertificate($this->userCertificateManager);
  74. }
  75. /**
  76. * Add a new root certificate to a trust store
  77. *
  78. * @param ICertificateManager $certificateManager
  79. * @return DataResponse
  80. */
  81. private function addCertificate(ICertificateManager $certificateManager) {
  82. $headers = [];
  83. if ($this->isCertificateImportAllowed() === false) {
  84. return new DataResponse(['message' => 'Individual certificate management disabled'], Http::STATUS_FORBIDDEN, $headers);
  85. }
  86. $file = $this->request->getUploadedFile('rootcert_import');
  87. if (empty($file)) {
  88. return new DataResponse(['message' => 'No file uploaded'], Http::STATUS_UNPROCESSABLE_ENTITY, $headers);
  89. }
  90. try {
  91. $certificate = $certificateManager->addCertificate(file_get_contents($file['tmp_name']), $file['name']);
  92. return new DataResponse(
  93. [
  94. 'name' => $certificate->getName(),
  95. 'commonName' => $certificate->getCommonName(),
  96. 'organization' => $certificate->getOrganization(),
  97. 'validFrom' => $certificate->getIssueDate()->getTimestamp(),
  98. 'validTill' => $certificate->getExpireDate()->getTimestamp(),
  99. 'validFromString' => $this->l10n->l('date', $certificate->getIssueDate()),
  100. 'validTillString' => $this->l10n->l('date', $certificate->getExpireDate()),
  101. 'issuer' => $certificate->getIssuerName(),
  102. 'issuerOrganization' => $certificate->getIssuerOrganization(),
  103. ],
  104. Http::STATUS_OK,
  105. $headers
  106. );
  107. } catch (\Exception $e) {
  108. return new DataResponse('An error occurred.', Http::STATUS_UNPROCESSABLE_ENTITY, $headers);
  109. }
  110. }
  111. /**
  112. * Removes a personal root certificate from the users' trust store
  113. *
  114. * @NoAdminRequired
  115. * @NoSubadminRequired
  116. * @param string $certificateIdentifier
  117. * @return DataResponse
  118. */
  119. public function removePersonalRootCertificate($certificateIdentifier) {
  120. if ($this->isCertificateImportAllowed() === false) {
  121. return new DataResponse('Individual certificate management disabled', Http::STATUS_FORBIDDEN);
  122. }
  123. $this->userCertificateManager->removeCertificate($certificateIdentifier);
  124. return new DataResponse();
  125. }
  126. /**
  127. * check if certificate import is allowed
  128. *
  129. * @return bool
  130. */
  131. protected function isCertificateImportAllowed() {
  132. $externalStorageEnabled = $this->appManager->isEnabledForUser('files_external');
  133. if ($externalStorageEnabled) {
  134. /** @var \OCA\Files_External\Service\BackendService $backendService */
  135. $backendService = \OC_Mount_Config::$app->getContainer()->query('\OCA\Files_External\Service\BackendService');
  136. if ($backendService->isUserMountingAllowed()) {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142. /**
  143. * Add a new personal root certificate to the system's trust store
  144. *
  145. * @return array
  146. */
  147. public function addSystemRootCertificate() {
  148. return $this->addCertificate($this->systemCertificateManager);
  149. }
  150. /**
  151. * Removes a personal root certificate from the users' trust store
  152. *
  153. * @param string $certificateIdentifier
  154. * @return DataResponse
  155. */
  156. public function removeSystemRootCertificate($certificateIdentifier) {
  157. if ($this->isCertificateImportAllowed() === false) {
  158. return new DataResponse('Individual certificate management disabled', Http::STATUS_FORBIDDEN);
  159. }
  160. $this->systemCertificateManager->removeCertificate($certificateIdentifier);
  161. return new DataResponse();
  162. }
  163. }