You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.0 KiB

  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OC\Settings\Controller;
  22. use OCP\AppFramework\Controller;
  23. use OCP\AppFramework\Http;
  24. use OCP\AppFramework\Http\DataResponse;
  25. use OCP\ICertificateManager;
  26. use OCP\IL10N;
  27. use OCP\IRequest;
  28. /**
  29. * @package OC\Settings\Controller
  30. */
  31. class CertificateController extends Controller {
  32. /** @var ICertificateManager */
  33. private $certificateManager;
  34. /** @var IL10N */
  35. private $l10n;
  36. /**
  37. * @param string $appName
  38. * @param IRequest $request
  39. * @param ICertificateManager $certificateManager
  40. * @param IL10N $l10n
  41. */
  42. public function __construct($appName,
  43. IRequest $request,
  44. ICertificateManager $certificateManager,
  45. IL10N $l10n) {
  46. parent::__construct($appName, $request);
  47. $this->certificateManager = $certificateManager;
  48. $this->l10n = $l10n;
  49. }
  50. /**
  51. * Add a new personal root certificate to the users' trust store
  52. *
  53. * @NoAdminRequired
  54. * @return array
  55. */
  56. public function addPersonalRootCertificate() {
  57. $file = $this->request->getUploadedFile('rootcert_import');
  58. if(empty($file)) {
  59. return new DataResponse(['message' => 'No file uploaded'], Http::STATUS_UNPROCESSABLE_ENTITY);
  60. }
  61. try {
  62. $certificate = $this->certificateManager->addCertificate(file_get_contents($file['tmp_name']), $file['name']);
  63. return new DataResponse([
  64. 'name' => $certificate->getName(),
  65. 'commonName' => $certificate->getCommonName(),
  66. 'organization' => $certificate->getOrganization(),
  67. 'validFrom' => $certificate->getIssueDate()->getTimestamp(),
  68. 'validTill' => $certificate->getExpireDate()->getTimestamp(),
  69. 'validFromString' => $this->l10n->l('date', $certificate->getIssueDate()),
  70. 'validTillString' => $this->l10n->l('date', $certificate->getExpireDate()),
  71. 'issuer' => $certificate->getIssuerName(),
  72. 'issuerOrganization' => $certificate->getIssuerOrganization(),
  73. ]);
  74. } catch (\Exception $e) {
  75. return new DataResponse('An error occurred.', Http::STATUS_UNPROCESSABLE_ENTITY);
  76. }
  77. }
  78. /**
  79. * Removes a personal root certificate from the users' trust store
  80. *
  81. * @NoAdminRequired
  82. * @param string $certificateIdentifier
  83. * @return DataResponse
  84. */
  85. public function removePersonalRootCertificate($certificateIdentifier) {
  86. $this->certificateManager->removeCertificate($certificateIdentifier);
  87. return new DataResponse();
  88. }
  89. }