ListCertificates.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Core\Command\Security;
  24. use OC\Core\Command\Base;
  25. use OCP\ICertificate;
  26. use OCP\ICertificateManager;
  27. use OCP\IL10N;
  28. use OCP\L10N\IFactory as IL10NFactory;
  29. use Symfony\Component\Console\Helper\Table;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class ListCertificates extends Base {
  33. protected IL10N $l;
  34. public function __construct(
  35. protected ICertificateManager $certificateManager,
  36. IL10NFactory $l10nFactory,
  37. ) {
  38. parent::__construct();
  39. $this->l = $l10nFactory->get('core');
  40. }
  41. protected function configure() {
  42. $this
  43. ->setName('security:certificates')
  44. ->setDescription('list trusted certificates');
  45. parent::configure();
  46. }
  47. protected function execute(InputInterface $input, OutputInterface $output): int {
  48. $outputType = $input->getOption('output');
  49. if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
  50. $certificates = array_map(function (ICertificate $certificate) {
  51. return [
  52. 'name' => $certificate->getName(),
  53. 'common_name' => $certificate->getCommonName(),
  54. 'organization' => $certificate->getOrganization(),
  55. 'expire' => $certificate->getExpireDate()->format(\DateTimeInterface::ATOM),
  56. 'issuer' => $certificate->getIssuerName(),
  57. 'issuer_organization' => $certificate->getIssuerOrganization(),
  58. 'issue_date' => $certificate->getIssueDate()->format(\DateTimeInterface::ATOM)
  59. ];
  60. }, $this->certificateManager->listCertificates());
  61. if ($outputType === self::OUTPUT_FORMAT_JSON) {
  62. $output->writeln(json_encode(array_values($certificates)));
  63. } else {
  64. $output->writeln(json_encode(array_values($certificates), JSON_PRETTY_PRINT));
  65. }
  66. } else {
  67. $table = new Table($output);
  68. $table->setHeaders([
  69. 'File Name',
  70. 'Common Name',
  71. 'Organization',
  72. 'Valid Until',
  73. 'Issued By'
  74. ]);
  75. $rows = array_map(function (ICertificate $certificate) {
  76. return [
  77. $certificate->getName(),
  78. $certificate->getCommonName(),
  79. $certificate->getOrganization(),
  80. $this->l->l('date', $certificate->getExpireDate()),
  81. $certificate->getIssuerName()
  82. ];
  83. }, $this->certificateManager->listCertificates());
  84. $table->setRows($rows);
  85. $table->render();
  86. }
  87. return 0;
  88. }
  89. }