ListCertificates.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /** @var ICertificateManager */
  33. protected $certificateManager;
  34. /** @var IL10N */
  35. protected $l;
  36. public function __construct(ICertificateManager $certificateManager, IL10N $l) {
  37. $this->certificateManager = $certificateManager;
  38. $this->l = $l;
  39. parent::__construct();
  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. }