|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Utopia\Migration\Resources\Auth\OAuth2; |
| 4 | + |
| 5 | +use Utopia\Migration\Resource; |
| 6 | +use Utopia\Migration\Transfer; |
| 7 | + |
| 8 | +/** |
| 9 | + * OAuth2 provider secrets are write-only and are not migrated. |
| 10 | + */ |
| 11 | +final class OAuth2Provider extends Resource |
| 12 | +{ |
| 13 | + private const TARGET_APP_ID = 'appId'; |
| 14 | + private const TARGET_SECRET = 'secret'; |
| 15 | + |
| 16 | + /** |
| 17 | + * Allow-list of readable provider fields that are safe to migrate, keyed by |
| 18 | + * provider. Each field declares where it lands on the destination: |
| 19 | + * - target `appId` -> the provider's `{key}Appid` attribute (one per provider) |
| 20 | + * - target `secret` -> merged into the `{key}Secret` JSON blob, renamed via `key` |
| 21 | + * |
| 22 | + * Anything not listed here (clientSecret, Apple's p8File, etc.) is never copied, |
| 23 | + * so a secret field the server may add upstream cannot leak into a migration. |
| 24 | + * |
| 25 | + * @var array<string, array<string, array{target: string, key?: string}>> |
| 26 | + */ |
| 27 | + public const PROVIDERS = [ |
| 28 | + 'amazon' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 29 | + 'apple' => [ |
| 30 | + 'serviceId' => ['target' => self::TARGET_APP_ID], |
| 31 | + 'keyId' => ['target' => self::TARGET_SECRET, 'key' => 'keyID'], |
| 32 | + 'teamId' => ['target' => self::TARGET_SECRET, 'key' => 'teamID'], |
| 33 | + ], |
| 34 | + 'auth0' => ['clientId' => ['target' => self::TARGET_APP_ID], 'endpoint' => ['target' => self::TARGET_SECRET]], |
| 35 | + 'authentik' => ['clientId' => ['target' => self::TARGET_APP_ID], 'endpoint' => ['target' => self::TARGET_SECRET]], |
| 36 | + 'autodesk' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 37 | + 'bitbucket' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 38 | + 'bitly' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 39 | + 'box' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 40 | + 'dailymotion' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 41 | + 'discord' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 42 | + 'disqus' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 43 | + 'dropbox' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 44 | + 'etsy' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 45 | + 'facebook' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 46 | + 'figma' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 47 | + 'fusionauth' => ['clientId' => ['target' => self::TARGET_APP_ID], 'endpoint' => ['target' => self::TARGET_SECRET]], |
| 48 | + 'github' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 49 | + 'gitlab' => ['clientId' => ['target' => self::TARGET_APP_ID], 'endpoint' => ['target' => self::TARGET_SECRET]], |
| 50 | + 'google' => ['clientId' => ['target' => self::TARGET_APP_ID], 'prompt' => ['target' => self::TARGET_SECRET]], |
| 51 | + 'keycloak' => [ |
| 52 | + 'clientId' => ['target' => self::TARGET_APP_ID], |
| 53 | + 'endpoint' => ['target' => self::TARGET_SECRET, 'key' => 'keycloakDomain'], |
| 54 | + 'realmName' => ['target' => self::TARGET_SECRET, 'key' => 'keycloakRealm'], |
| 55 | + ], |
| 56 | + 'kick' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 57 | + 'linkedin' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 58 | + 'microsoft' => ['clientId' => ['target' => self::TARGET_APP_ID], 'tenant' => ['target' => self::TARGET_SECRET]], |
| 59 | + 'notion' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 60 | + 'oidc' => [ |
| 61 | + 'clientId' => ['target' => self::TARGET_APP_ID], |
| 62 | + 'wellKnownURL' => ['target' => self::TARGET_SECRET, 'key' => 'wellKnownEndpoint'], |
| 63 | + 'authorizationURL' => ['target' => self::TARGET_SECRET, 'key' => 'authorizationEndpoint'], |
| 64 | + 'tokenURL' => ['target' => self::TARGET_SECRET, 'key' => 'tokenEndpoint'], |
| 65 | + 'userInfoURL' => ['target' => self::TARGET_SECRET, 'key' => 'userInfoEndpoint'], |
| 66 | + ], |
| 67 | + 'okta' => [ |
| 68 | + 'clientId' => ['target' => self::TARGET_APP_ID], |
| 69 | + 'domain' => ['target' => self::TARGET_SECRET, 'key' => 'oktaDomain'], |
| 70 | + 'authorizationServerId' => ['target' => self::TARGET_SECRET], |
| 71 | + ], |
| 72 | + 'paypal' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 73 | + 'paypalSandbox' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 74 | + 'podio' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 75 | + 'salesforce' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 76 | + 'slack' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 77 | + 'spotify' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 78 | + 'stripe' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 79 | + 'tradeshift' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 80 | + 'tradeshiftBox' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 81 | + 'twitch' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 82 | + 'wordpress' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 83 | + 'x' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 84 | + 'yahoo' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 85 | + 'yandex' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 86 | + 'zoho' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 87 | + 'zoom' => ['clientId' => ['target' => self::TARGET_APP_ID]], |
| 88 | + ]; |
| 89 | + |
| 90 | + public function __construct( |
| 91 | + string $id, |
| 92 | + protected readonly string $providerKey, |
| 93 | + protected readonly bool $enabled = false, |
| 94 | + protected readonly array $settings = [], |
| 95 | + string $createdAt = '', |
| 96 | + string $updatedAt = '', |
| 97 | + ) { |
| 98 | + $this->id = $id; |
| 99 | + $this->createdAt = $createdAt; |
| 100 | + $this->updatedAt = $updatedAt; |
| 101 | + } |
| 102 | + |
| 103 | + public static function getName(): string |
| 104 | + { |
| 105 | + return Resource::TYPE_OAUTH2_PROVIDER; |
| 106 | + } |
| 107 | + |
| 108 | + public function getGroup(): string |
| 109 | + { |
| 110 | + return Transfer::GROUP_AUTH; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @param array<string, mixed> $array |
| 115 | + */ |
| 116 | + public static function fromArray(string $providerKey, array $array): ?self |
| 117 | + { |
| 118 | + $allowed = self::PROVIDERS[$providerKey] ?? null; |
| 119 | + if ($allowed === null) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + $settings = []; |
| 124 | + foreach (\array_keys($allowed) as $field) { |
| 125 | + if (\array_key_exists($field, $array)) { |
| 126 | + $settings[$field] = $array[$field]; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return new self( |
| 131 | + $array['id'], |
| 132 | + $providerKey, |
| 133 | + (bool) ($array['enabled'] ?? false), |
| 134 | + $settings, |
| 135 | + createdAt: $array['createdAt'] ?? '', |
| 136 | + updatedAt: $array['updatedAt'] ?? '', |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @return array<string, mixed> |
| 142 | + */ |
| 143 | + public function jsonSerialize(): array |
| 144 | + { |
| 145 | + return [ |
| 146 | + 'id' => $this->id, |
| 147 | + 'providerKey' => $this->providerKey, |
| 148 | + 'enabled' => $this->enabled, |
| 149 | + 'settings' => $this->settings, |
| 150 | + 'createdAt' => $this->createdAt, |
| 151 | + 'updatedAt' => $this->updatedAt, |
| 152 | + ]; |
| 153 | + } |
| 154 | + |
| 155 | + public function getProviderKey(): string |
| 156 | + { |
| 157 | + return $this->providerKey; |
| 158 | + } |
| 159 | + |
| 160 | + public function getEnabled(): bool |
| 161 | + { |
| 162 | + return $this->enabled; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * @return array<string, mixed> |
| 167 | + */ |
| 168 | + public function getSettings(): array |
| 169 | + { |
| 170 | + return $this->settings; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Value for the destination's `{key}Appid` attribute (clientId, or serviceId |
| 175 | + * for Apple). Null when unset, so callers can skip it without a separate |
| 176 | + * emptiness check. |
| 177 | + */ |
| 178 | + public function getDestinationAppId(): ?string |
| 179 | + { |
| 180 | + foreach ($this->getDescriptor() as $field => $metadata) { |
| 181 | + if ($metadata['target'] !== self::TARGET_APP_ID) { |
| 182 | + continue; |
| 183 | + } |
| 184 | + |
| 185 | + $value = $this->settings[$field] ?? null; |
| 186 | + |
| 187 | + return self::isEmpty($value) ? null : (string) $value; |
| 188 | + } |
| 189 | + |
| 190 | + return null; |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * @return array<string, mixed> |
| 195 | + */ |
| 196 | + public function getDestinationSecretFields(): array |
| 197 | + { |
| 198 | + $fields = []; |
| 199 | + foreach ($this->getDescriptor() as $field => $metadata) { |
| 200 | + if ($metadata['target'] !== self::TARGET_SECRET || !\array_key_exists($field, $this->settings)) { |
| 201 | + continue; |
| 202 | + } |
| 203 | + |
| 204 | + $value = $this->settings[$field]; |
| 205 | + if (self::isEmpty($value)) { |
| 206 | + continue; |
| 207 | + } |
| 208 | + |
| 209 | + $fields[$metadata['key'] ?? $field] = $value; |
| 210 | + } |
| 211 | + |
| 212 | + return $fields; |
| 213 | + } |
| 214 | + |
| 215 | + public function isConfigured(): bool |
| 216 | + { |
| 217 | + return $this->enabled || $this->getDestinationAppId() !== null; |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * @return array<string, array{target: string, key?: string}> |
| 222 | + */ |
| 223 | + private function getDescriptor(): array |
| 224 | + { |
| 225 | + return self::PROVIDERS[$this->providerKey] ?? []; |
| 226 | + } |
| 227 | + |
| 228 | + private static function isEmpty(mixed $value): bool |
| 229 | + { |
| 230 | + return $value === null || $value === '' || $value === []; |
| 231 | + } |
| 232 | +} |
0 commit comments