ListCertificates.php 3.1 KB

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