You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.3 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Marcel Müller <marcel.mueller@nextcloud.com>
  5. *
  6. * @author Marcel Müller <marcel.mueller@nextcloud.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Talk\Controller;
  25. use OCA\Talk\Service\CertificateService;
  26. use OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http\DataResponse;
  28. use OCP\AppFramework\OCSController;
  29. use OCP\IL10N;
  30. use OCP\IRequest;
  31. use Psr\Log\LoggerInterface;
  32. class CertificateController extends OCSController {
  33. public function __construct(
  34. string $appName,
  35. IRequest $request,
  36. protected CertificateService $certificateService,
  37. protected IL10N $l,
  38. protected LoggerInterface $logger,
  39. ) {
  40. parent::__construct($appName, $request);
  41. }
  42. /**
  43. * Get the certificate expiration for a host
  44. * @param string $host Host to check
  45. * @return DataResponse<Http::STATUS_OK, array{expiration_in_days: ?int}, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array{message: string}, array{}>
  46. *
  47. * 200: Certificate expiration returned
  48. * 400: Getting certificate expiration is not possible
  49. */
  50. public function getCertificateExpiration(string $host): DataResponse {
  51. try {
  52. $expirationInDays = $this->certificateService->getCertificateExpirationInDays($host);
  53. return new DataResponse([
  54. 'expiration_in_days' => $expirationInDays,
  55. ]);
  56. } catch (\Exception $e) {
  57. $this->logger->error('Failed get certificate expiration', [
  58. 'exception' => $e,
  59. ]);
  60. return new DataResponse(['message' => $this->l->t('An error occurred. Please contact your administrator.')], Http::STATUS_BAD_REQUEST);
  61. }
  62. }
  63. }