eventSource = $this->eventSourceFactory->create(); } #[NoCSRFRequired] public function generateCertificateCfssl( array $rootCert, string $cfsslUri = '', string $configPath = '' ): DataResponse { return $this->generateCertificate($rootCert, [ 'engine' => 'cfssl', 'configPath' => trim($configPath), 'cfsslUri' => trim($cfsslUri), ]); } #[NoCSRFRequired] public function generateCertificateOpenSsl( array $rootCert, string $configPath = '' ): DataResponse { return $this->generateCertificate($rootCert, [ 'engine' => 'openssl', 'configPath' => trim($configPath), ]); } private function generateCertificate( array $rootCert, array $properties = [], ): DataResponse { try { $names = []; foreach ($rootCert['names'] as $item) { $names[$item['id']]['value'] = $this->trimAndThrowIfEmpty($item['id'], $item['value']); } $this->installService->generate( $this->trimAndThrowIfEmpty('commonName', $rootCert['commonName']), $names ?? [], $properties, ); return new DataResponse([ 'data' => $this->certificateEngineHandler->getEngine()->toArray(), ]); } catch (\Exception $exception) { return new DataResponse( [ 'message' => $exception->getMessage() ], Http::STATUS_UNAUTHORIZED ); } } #[NoCSRFRequired] public function loadCertificate(): DataResponse { $engine = $this->certificateEngineHandler->getEngine(); $certificate = $engine->toArray(); $configureResult = $engine->configureCheck(); $success = array_filter( $configureResult, function (ConfigureCheckHelper $config) { return $config->getStatus() === 'success'; } ); $certificate['generated'] = count($success) === count($configureResult); return new DataResponse($certificate); } private function trimAndThrowIfEmpty(string $key, $value): string { if (empty($value)) { throw new LibresignException("parameter '{$key}' is required!", 400); } return trim($value); } #[NoCSRFRequired] public function configureCheck(): DataResponse { return new DataResponse( $this->configureCheckService->checkAll() ); } #[NoCSRFRequired] public function installAndValidate(): void { try { $async = \function_exists('proc_open'); $this->installService->installJava($async); $this->installService->installJSignPdf($async); $this->installService->installPdftk($async); $this->installService->installCfssl($async); $this->eventSource->send('configure_check', $this->configureCheckService->checkAll()); $seconds = 0; while ($this->installService->isDownloadWip()) { $totalSize = $this->installService->getTotalSize(); $this->eventSource->send('total_size', json_encode($totalSize)); if ($errors = $this->installService->getErrorMessages()) { $this->eventSource->send('errors', json_encode($errors)); } usleep(200000); // 0.2 seconds $seconds += 0.2; if ($seconds === 5) { $this->eventSource->send('configure_check', $this->configureCheckService->checkAll()); $seconds = 0; } } if ($errors = $this->installService->getErrorMessages()) { $this->eventSource->send('errors', json_encode($errors)); } } catch (\Exception $exception) { $this->eventSource->send('errors', json_encode([ $this->l10n->t('Could not download binaries.'), $exception->getMessage(), ])); } $this->eventSource->send('configure_check', $this->configureCheckService->checkAll()); $this->eventSource->send('done', ''); $this->eventSource->close(); // Nextcloud inject a lot of headers that is incompatible with SSE exit(); } }