Connection records keyed by site UID */ private array $_connections = []; private ?string $_activeSiteUid = null; private ?bool $_schemaReady = null; /** * Set the site whose connection the lower-level services (API, OAuth) should * operate on for the duration of the current operation. */ public function setActiveSiteUid(?string $siteUid): void { $this->_activeSiteUid = $siteUid; } /** * Resolve a usable site UID: explicit > active > current request site. */ private function resolveSiteUid(?string $siteUid): string { return $siteUid ?? $this->_activeSiteUid ?? Craft::$app->getSites()->getCurrentSite()->uid; } /** * Get the connection record for a site, or null if that site isn't connected. */ public function getConnection(?string $siteUid = null): ?ConnectionRecord { // During an upgrade the entry-save event can fire (e.g. a core migration // re-saves a section) before this plugin's own migration has added the // per-site siteUid column. Degrade gracefully instead of querying a // column that doesn't exist yet. if (!$this->schemaReady()) { return null; } $siteUid = $this->resolveSiteUid($siteUid); if (!array_key_exists($siteUid, $this->_connections)) { /** @var ConnectionRecord|null $connection */ $connection = ConnectionRecord::find()->where(['siteUid' => $siteUid])->one(); $this->_connections[$siteUid] = $connection; } return $this->_connections[$siteUid]; } /** * Whether the connections table has the per-site siteUid column yet. Cached * per request; the column is missing only mid-upgrade before m260529 runs. */ private function schemaReady(): bool { if ($this->_schemaReady === null) { $this->_schemaReady = Craft::$app->getDb()->columnExists('{{%standardsite_connections}}', 'siteUid'); } return $this->_schemaReady; } /** * Check if a site has an active connection. */ public function isConnected(?string $siteUid = null): bool { $connection = $this->getConnection($siteUid); return $connection !== null && !empty($connection->did) && !empty($connection->accessToken); } /** * Get the DID from a site's connection. */ public function getDid(?string $siteUid = null): ?string { return $this->getConnection($siteUid)?->did; } /** * Get the PDS URL from a site's connection. */ public function getPdsUrl(?string $siteUid = null): ?string { return $this->getConnection($siteUid)?->pdsUrl; } /** * Get the AT Protocol handle from a site's connection. */ public function getHandle(?string $siteUid = null): ?string { return $this->getConnection($siteUid)?->handle; } /** * Get the Craft site handle used during OAuth (for correct client_id on refresh). */ public function getSiteHandle(?string $siteUid = null): ?string { return $this->getConnection($siteUid)?->siteHandle; } /** * Store a new connection for a site after successful OAuth. */ public function saveConnection(string $siteUid, string $handle, string $did, string $pdsUrl, array $tokens, array $dpopKey, ?string $siteHandle = null): void { $encryption = StandardSite::getInstance()->encryption; $record = $this->getConnection($siteUid) ?? new ConnectionRecord(); $record->siteUid = $siteUid; $record->handle = $handle; $record->siteHandle = $siteHandle; $record->did = $did; $record->pdsUrl = $pdsUrl; $record->accessToken = $encryption->encrypt($tokens['access_token']); $record->refreshToken = $encryption->encrypt($tokens['refresh_token']); $record->dpopKey = $encryption->encrypt(json_encode($dpopKey, JSON_THROW_ON_ERROR)); $record->tokenExpiresAt = time() + ($tokens['expires_in'] ?? 3600); $record->save(); // Refresh cache $this->_connections[$siteUid] = $record; } /** * Update tokens after a refresh. */ public function updateTokens(array $tokens, ?string $siteUid = null): void { $connection = $this->getConnection($siteUid); if (!$connection) { throw new \RuntimeException('No connection to update'); } $encryption = StandardSite::getInstance()->encryption; $connection->accessToken = $encryption->encrypt($tokens['access_token']); $connection->refreshToken = $encryption->encrypt($tokens['refresh_token']); $connection->tokenExpiresAt = time() + ($tokens['expires_in'] ?? 3600); $connection->save(); } /** * Get a valid decrypted access token for a site. */ public function getAccessToken(?string $siteUid = null): ?string { $connection = $this->getConnection($siteUid); if (!$connection?->accessToken) { return null; } return StandardSite::getInstance()->encryption->decrypt($connection->accessToken); } /** * Get the decrypted DPoP key for a site. */ public function getDpopKey(?string $siteUid = null): ?array { $connection = $this->getConnection($siteUid); if (!$connection?->dpopKey) { return null; } $json = StandardSite::getInstance()->encryption->decrypt($connection->dpopKey); return $json ? json_decode($json, true, 512, JSON_THROW_ON_ERROR) : null; } /** * Get the decrypted refresh token for a site. */ public function getRefreshToken(?string $siteUid = null): ?string { $connection = $this->getConnection($siteUid); if (!$connection?->refreshToken) { return null; } return StandardSite::getInstance()->encryption->decrypt($connection->refreshToken); } /** * Get token expiry timestamp for a site. */ public function getTokenExpiresAt(?string $siteUid = null): ?int { return $this->getConnection($siteUid)?->tokenExpiresAt; } /** * Delete a site's connection (disconnect). */ public function deleteConnection(?string $siteUid = null): void { $siteUid = $this->resolveSiteUid($siteUid); $connection = $this->getConnection($siteUid); if ($connection) { $connection->delete(); } unset($this->_connections[$siteUid]); } }