ListCertificates.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command\Security;
  8. use OC\Core\Command\Base;
  9. use OCP\ICertificate;
  10. use OCP\ICertificateManager;
  11. use OCP\IL10N;
  12. use OCP\L10N\IFactory as IL10NFactory;
  13. use Symfony\Component\Console\Helper\Table;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class ListCertificates extends Base {
  17. protected IL10N $l;
  18. public function __construct(
  19. protected ICertificateManager $certificateManager,
  20. IL10NFactory $l10nFactory,
  21. ) {
  22. parent::__construct();
  23. $this->l = $l10nFactory->get('core');
  24. }
  25. protected function configure() {
  26. $this
  27. ->setName('security:certificates')
  28. ->setDescription('list trusted certificates');
  29. parent::configure();
  30. }
  31. protected function execute(InputInterface $input, OutputInterface $output): int {
  32. $outputType = $input->getOption('output');
  33. if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
  34. $certificates = array_map(function (ICertificate $certificate) {
  35. return [
  36. 'name' => $certificate->getName(),
  37. 'common_name' => $certificate->getCommonName(),
  38. 'organization' => $certificate->getOrganization(),
  39. 'expire' => $certificate->getExpireDate()->format(\DateTimeInterface::ATOM),
  40. 'issuer' => $certificate->getIssuerName(),
  41. 'issuer_organization' => $certificate->getIssuerOrganization(),
  42. 'issue_date' => $certificate->getIssueDate()->format(\DateTimeInterface::ATOM)
  43. ];
  44. }, $this->certificateManager->listCertificates());
  45. if ($outputType === self::OUTPUT_FORMAT_JSON) {
  46. $output->writeln(json_encode(array_values($certificates)));
  47. } else {
  48. $output->writeln(json_encode(array_values($certificates), JSON_PRETTY_PRINT));
  49. }
  50. } else {
  51. $table = new Table($output);
  52. $table->setHeaders([
  53. 'File Name',
  54. 'Common Name',
  55. 'Organization',
  56. 'Valid Until',
  57. 'Issued By'
  58. ]);
  59. $rows = array_map(function (ICertificate $certificate) {
  60. return [
  61. $certificate->getName(),
  62. $certificate->getCommonName(),
  63. $certificate->getOrganization(),
  64. $this->l->l('date', $certificate->getExpireDate()),
  65. $certificate->getIssuerName()
  66. ];
  67. }, $this->certificateManager->listCertificates());
  68. $table->setRows($rows);
  69. $table->render();
  70. }
  71. return 0;
  72. }
  73. }