|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Whiteboard\Service; |
| 11 | + |
| 12 | +use OCA\AppAPI\Service\ExAppService as AppAPIService; |
| 13 | +use OCA\Whiteboard\Consts\ExAppConsts; |
| 14 | +use OCP\App\IAppManager; |
| 15 | +use OCP\AppFramework\Services\IInitialState; |
| 16 | +use OCP\IDBConnection; |
| 17 | +use Psr\Container\ContainerInterface; |
| 18 | +use Psr\Log\LoggerInterface; |
| 19 | +use Throwable; |
| 20 | + |
| 21 | +/** |
| 22 | + * @psalm-suppress UndefinedClass |
| 23 | + * @psalm-suppress MissingDependency |
| 24 | + */ |
| 25 | +final class ExAppService { |
| 26 | + private ?AppAPIService $appAPIService = null; |
| 27 | + |
| 28 | + public function __construct( |
| 29 | + private IAppManager $appManager, |
| 30 | + private ContainerInterface $container, |
| 31 | + private IInitialState $initialState, |
| 32 | + private LoggerInterface $logger, |
| 33 | + private IDBConnection $dbConnection, |
| 34 | + ) { |
| 35 | + $this->initAppAPIService(); |
| 36 | + } |
| 37 | + |
| 38 | + private function initAppAPIService(): void { |
| 39 | + $isAppAPIEnabled = $this->isAppAPIEnabled(); |
| 40 | + |
| 41 | + if (class_exists(AppAPIService::class) && $isAppAPIEnabled) { |
| 42 | + try { |
| 43 | + $this->appAPIService = $this->container->get(AppAPIService::class); |
| 44 | + } catch (Throwable $e) { |
| 45 | + $this->logger->error('exApp', [$e->getMessage()]); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private function isAppAPIEnabled(): bool { |
| 51 | + return $this->appManager->isEnabledForUser(ExAppConsts::APP_API_ID); |
| 52 | + } |
| 53 | + |
| 54 | + public function isExAppEnabled(string $appId): bool { |
| 55 | + if ($this->appAPIService === null) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + return $this->appAPIService->getExApp($appId)?->getEnabled() === 1; |
| 60 | + } |
| 61 | + |
| 62 | + public function isWhiteboardWebsocketEnabled(): bool { |
| 63 | + return $this->isExAppEnabled(ExAppConsts::WHITEBOARD_EX_APP_ID); |
| 64 | + } |
| 65 | + |
| 66 | + public function initFrontendState(): void { |
| 67 | + $this->initialState->provideInitialState( |
| 68 | + ExAppConsts::WHITEBOARD_EX_APP_ENABLED_KEY, |
| 69 | + $this->isWhiteboardWebsocketEnabled() |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Get the ExApp secret for the whiteboard ExApp |
| 75 | + * This secret is used for JWT authentication between Nextcloud and the ExApp |
| 76 | + */ |
| 77 | + public function getWhiteboardExAppSecret(): ?string { |
| 78 | + if ($this->appAPIService === null) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + try { |
| 83 | + $exApp = $this->appAPIService->getExApp(ExAppConsts::WHITEBOARD_EX_APP_ID); |
| 84 | + if ($exApp === null) { |
| 85 | + $this->logger->debug('ExApp not found', ['appId' => ExAppConsts::WHITEBOARD_EX_APP_ID]); |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + // Try to get the secret using the getSecret method |
| 90 | + try { |
| 91 | + if (method_exists($exApp, 'getSecret')) { |
| 92 | + $secret = $exApp->getSecret(); |
| 93 | + if ($secret !== null && $secret !== '') { |
| 94 | + $this->logger->debug('ExApp secret retrieved successfully via getSecret method'); |
| 95 | + return $secret; |
| 96 | + } |
| 97 | + } |
| 98 | + } catch (Throwable $e) { |
| 99 | + $this->logger->warning('Failed to get secret via getSecret method', [ |
| 100 | + 'error' => $e->getMessage() |
| 101 | + ]); |
| 102 | + } |
| 103 | + |
| 104 | + $this->logger->info('ExApp secret not accessible via getSecret method, trying database fallback', [ |
| 105 | + 'appId' => ExAppConsts::WHITEBOARD_EX_APP_ID |
| 106 | + ]); |
| 107 | + |
| 108 | + // Fallback: try to get secret directly from database |
| 109 | + return $this->getExAppSecretFromDatabase(ExAppConsts::WHITEBOARD_EX_APP_ID); |
| 110 | + } catch (Throwable $e) { |
| 111 | + $this->logger->error('Failed to retrieve ExApp secret', [ |
| 112 | + 'appId' => ExAppConsts::WHITEBOARD_EX_APP_ID, |
| 113 | + 'error' => $e->getMessage() |
| 114 | + ]); |
| 115 | + |
| 116 | + // Final fallback: try database access |
| 117 | + return $this->getExAppSecretFromDatabase(ExAppConsts::WHITEBOARD_EX_APP_ID); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Fallback method to get ExApp secret directly from database |
| 123 | + */ |
| 124 | + private function getExAppSecretFromDatabase(string $appId): ?string { |
| 125 | + try { |
| 126 | + $this->logger->debug('Attempting to retrieve ExApp secret from database', ['appId' => $appId]); |
| 127 | + |
| 128 | + $qb = $this->dbConnection->getQueryBuilder(); |
| 129 | + $qb->select('secret') |
| 130 | + ->from('ex_apps') |
| 131 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($appId))); |
| 132 | + |
| 133 | + $result = $qb->executeQuery(); |
| 134 | + $row = $result->fetch(); |
| 135 | + $result->closeCursor(); |
| 136 | + |
| 137 | + if ($row && isset($row['secret']) && $row['secret'] !== '') { |
| 138 | + $this->logger->debug('ExApp secret retrieved successfully from database'); |
| 139 | + return $row['secret']; |
| 140 | + } |
| 141 | + |
| 142 | + $this->logger->warning('ExApp secret not found in database or is empty', ['appId' => $appId]); |
| 143 | + return null; |
| 144 | + } catch (Throwable $e) { |
| 145 | + $this->logger->error('Failed to retrieve ExApp secret from database', [ |
| 146 | + 'appId' => $appId, |
| 147 | + 'error' => $e->getMessage() |
| 148 | + ]); |
| 149 | + return null; |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments