CertificateController.php 5.6 KB

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