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