ListCertificates.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 Symfony\Component\Console\Helper\Table;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. class ListCertificates extends Base {
  32. protected ICertificateManager $certificateManager;
  33. protected IL10N $l;
  34. public function __construct(ICertificateManager $certificateManager, IL10N $l) {
  35. $this->certificateManager = $certificateManager;
  36. $this->l = $l;
  37. parent::__construct();
  38. }
  39. protected function configure() {
  40. $this
  41. ->setName('security:certificates')
  42. ->setDescription('list trusted certificates');
  43. parent::configure();
  44. }
  45. protected function execute(InputInterface $input, OutputInterface $output): int {
  46. $outputType = $input->getOption('output');
  47. if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
  48. $certificates = array_map(function (ICertificate $certificate) {
  49. return [
  50. 'name' => $certificate->getName(),
  51. 'common_name' => $certificate->getCommonName(),
  52. 'organization' => $certificate->getOrganization(),
  53. 'expire' => $certificate->getExpireDate()->format(\DateTimeInterface::ATOM),
  54. 'issuer' => $certificate->getIssuerName(),
  55. 'issuer_organization' => $certificate->getIssuerOrganization(),
  56. 'issue_date' => $certificate->getIssueDate()->format(\DateTimeInterface::ATOM)
  57. ];
  58. }, $this->certificateManager->listCertificates());
  59. if ($outputType === self::OUTPUT_FORMAT_JSON) {
  60. $output->writeln(json_encode(array_values($certificates)));
  61. } else {
  62. $output->writeln(json_encode(array_values($certificates), JSON_PRETTY_PRINT));
  63. }
  64. } else {
  65. $table = new Table($output);
  66. $table->setHeaders([
  67. 'File Name',
  68. 'Common Name',
  69. 'Organization',
  70. 'Valid Until',
  71. 'Issued By'
  72. ]);
  73. $rows = array_map(function (ICertificate $certificate) {
  74. return [
  75. $certificate->getName(),
  76. $certificate->getCommonName(),
  77. $certificate->getOrganization(),
  78. $this->l->l('date', $certificate->getExpireDate()),
  79. $certificate->getIssuerName()
  80. ];
  81. }, $this->certificateManager->listCertificates());
  82. $table->setRows($rows);
  83. $table->render();
  84. }
  85. return 0;
  86. }
  87. }