Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
45926dc
Add OAuth providers migration
premtsd-code May 26, 2026
327bcc3
Merge remote-tracking branch 'origin/add-email-templates-migration' i…
premtsd-code May 28, 2026
f60dc3b
Use listOAuth2Providers SDK call; migrate enabled flag only (SDK 24 m…
premtsd-code May 28, 2026
72324bd
Address Greptile review: reorder OAuth export block; guard empty appI…
premtsd-code May 28, 2026
b0820de
OAuth: skip enabling providers without destination credentials (avoid…
premtsd-code May 28, 2026
3269cf3
OAuth2: switch to per-provider Resource classes; migrate all readable…
premtsd-code May 29, 2026
e809d36
OAuth2: collapse 40 TYPE constants to single TYPE_OAUTH2_PROVIDER (st…
premtsd-code May 29, 2026
4829ace
OAuth2: drop conditional-enable guard; propagate enabled as-is across…
premtsd-code May 29, 2026
69dabe5
OAuth2: derive provider key from class instead of duplicating in sour…
premtsd-code May 29, 2026
ce19cdd
OAuth2: match sibling migration style for dispatch, report, and export
premtsd-code May 31, 2026
d13d2d5
OAuth2: DRY secret merge, surface unmapped providers, add tests
premtsd-code May 31, 2026
4168789
Drop OAuth2 tests to match the lib's existing migration-resource cove…
premtsd-code May 31, 2026
0d537b6
Merge branch 'add-email-templates-migration' into add-oauth-providers…
premtsd-code Jun 1, 2026
5d63dc6
Register TYPE_OAUTH2_PROVIDER in mock source/destination supported re…
premtsd-code Jun 1, 2026
d1387d9
Merge branch 'add-email-templates-migration' into add-oauth-providers…
premtsd-code Jun 1, 2026
a1afab9
Trim OAuth2 comments to the critical why; de-duplicate shared rationale
premtsd-code Jun 1, 2026
4ac6933
Add PaypalSandbox/TradeshiftSandbox OAuth2 providers (server enables …
premtsd-code Jun 1, 2026
d2a941d
OAuth2: migrate only configured providers (enabled or appId set)
premtsd-code Jun 1, 2026
e0741a5
Merge branch 'add-email-templates-migration' into add-oauth-providers…
premtsd-code Jun 2, 2026
6e4e416
OAuth2 export: null-guard listOAuth2Providers response, matching report
premtsd-code Jun 2, 2026
3ce16c5
Consolidate OAuth2 provider resources into a single class
premtsd-code Jun 3, 2026
5810023
Merge branch 'main' into add-oauth-providers-migration
premtsd-code Jun 3, 2026
99def10
Trim OAuth provider migration comments
premtsd-code Jun 3, 2026
09334e6
Improve OAuth provider migration mapping
premtsd-code Jun 3, 2026
b797f38
Tidy OAuth2Provider: drop dead getter and de-duplicate emptiness check
premtsd-code Jun 3, 2026
dede00a
Fix OAuth provider secret field mappings
premtsd-code Jun 3, 2026
f5a9cd9
Remove OAuth provider unit tests
premtsd-code Jun 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use Utopia\Migration\Resources\Auth\AuthMethods;
use Utopia\Migration\Resources\Auth\Hash;
use Utopia\Migration\Resources\Auth\Membership;
use Utopia\Migration\Resources\Auth\OAuthProviders;
use Utopia\Migration\Resources\Auth\Policies;
use Utopia\Migration\Resources\Auth\Team;
use Utopia\Migration\Resources\Auth\User;
Expand Down Expand Up @@ -259,6 +260,7 @@ public static function getSupportedResources(): array
Resource::TYPE_TEAM,
Resource::TYPE_MEMBERSHIP,
Resource::TYPE_AUTH_METHODS,
Resource::TYPE_OAUTH_PROVIDERS,
Resource::TYPE_POLICIES,

// Database
Expand Down Expand Up @@ -2192,6 +2194,10 @@ public function importAuthResource(Resource $resource): Resource
/** @var AuthMethods $resource */
$this->createAuthMethods($resource);
break;
case Resource::TYPE_OAUTH_PROVIDERS:
/** @var OAuthProviders $resource */
$this->createOAuthProviders($resource);
break;
case Resource::TYPE_POLICIES:
/** @var Policies $resource */
$this->createPolicies($resource);
Expand Down Expand Up @@ -3551,6 +3557,36 @@ protected function createAuthMethods(AuthMethods $resource): bool
return true;
}

/**
* Read-then-merge the project's `oAuthProviders` map. Each provider expands
* into `{key}Enabled` and `{key}Appid` flat entries; the `{key}Secret`
* entry is left untouched so the destination user can re-enter it post-migration.
*/
protected function createOAuthProviders(OAuthProviders $resource): bool
{
$project = $this->dbForPlatform->getDocument('projects', $this->projectId);
$oAuthProviders = $project->getAttribute('oAuthProviders', []);

foreach ($resource->getProviders() as $provider) {
$key = $provider['key'];
if ($key === '') {
continue;
}
$oAuthProviders[$key . 'Enabled'] = $provider['enabled'];
$oAuthProviders[$key . 'Appid'] = $provider['appId'];
}

$this->dbForPlatform->getAuthorization()->skip(fn () => $this->dbForPlatform->updateDocument(
'projects',
$this->projectId,
new UtopiaDocument(['oAuthProviders' => $oAuthProviders]),
));

$this->dbForPlatform->purgeCachedDocument('projects', $this->projectId);

return true;
}

/**
* Direct DB write — SDK policy setters reject `total: 0` but `0` is the
* storage value for "disabled". Shares the `auths` map with
Expand Down
3 changes: 3 additions & 0 deletions src/Migration/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ abstract class Resource implements \JsonSerializable

public const TYPE_AUTH_METHODS = 'auth-methods';

public const TYPE_OAUTH_PROVIDERS = 'oauth-providers';

public const TYPE_POLICIES = 'policies';

public const TYPE_ENVIRONMENT_VARIABLE = 'environment-variable';
Expand Down Expand Up @@ -130,6 +132,7 @@ abstract class Resource implements \JsonSerializable
self::TYPE_TEAM,
self::TYPE_MEMBERSHIP,
self::TYPE_AUTH_METHODS,
self::TYPE_OAUTH_PROVIDERS,
self::TYPE_POLICIES,
self::TYPE_PLATFORM,
self::TYPE_API_KEY,
Expand Down
82 changes: 82 additions & 0 deletions src/Migration/Resources/Auth/OAuthProviders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Utopia\Migration\Resources\Auth;

use Utopia\Migration\Resource;
use Utopia\Migration\Transfer;

/**
* Singleton resource representing the project's OAuth2 provider configurations.
* One per project; destination merges entries into the project doc's `oAuthProviders`
* map. Secrets are never migrated — the source API returns them empty, and the
* destination user must re-enter them post-migration.
*/
class OAuthProviders extends Resource
{
/**
* @param array<array{key: string, enabled: bool, appId: string}> $providers
*/
public function __construct(
string $id,
private readonly array $providers = [],
string $createdAt = '',
string $updatedAt = '',
) {
$this->id = $id;
$this->createdAt = $createdAt;
$this->updatedAt = $updatedAt;
}

/**
* @param array<string, mixed> $array
*/
public static function fromArray(array $array): self
{
$providers = [];
foreach ($array['providers'] ?? [] as $provider) {
$providers[] = [
'key' => (string) ($provider['key'] ?? ''),
'enabled' => (bool) ($provider['enabled'] ?? false),
'appId' => (string) ($provider['appId'] ?? ''),
];
}

return new self(
$array['id'],
$providers,
createdAt: $array['createdAt'] ?? '',
updatedAt: $array['updatedAt'] ?? '',
);
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'providers' => $this->providers,
'createdAt' => $this->createdAt,
'updatedAt' => $this->updatedAt,
];
}

public static function getName(): string
{
return Resource::TYPE_OAUTH_PROVIDERS;
}

public function getGroup(): string
{
return Transfer::GROUP_AUTH;
}

/**
* @return array<array{key: string, enabled: bool, appId: string}>
*/
public function getProviders(): array
{
return $this->providers;
}
}
48 changes: 48 additions & 0 deletions src/Migration/Sources/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Utopia\Migration\Resources\Auth\AuthMethods;
use Utopia\Migration\Resources\Auth\Hash;
use Utopia\Migration\Resources\Auth\Membership;
use Utopia\Migration\Resources\Auth\OAuthProviders;
use Utopia\Migration\Resources\Auth\Policies;
use Utopia\Migration\Resources\Auth\Team;
use Utopia\Migration\Resources\Auth\User;
Expand Down Expand Up @@ -190,6 +191,7 @@ public static function getSupportedResources(): array
Resource::TYPE_TEAM,
Resource::TYPE_MEMBERSHIP,
Resource::TYPE_AUTH_METHODS,
Resource::TYPE_OAUTH_PROVIDERS,
Resource::TYPE_POLICIES,

// Database
Expand Down Expand Up @@ -391,6 +393,11 @@ private function reportAuth(array $resources, array &$report, array $resourceIds
$report[Resource::TYPE_AUTH_METHODS] = 1;
}

if (\in_array(Resource::TYPE_OAUTH_PROVIDERS, $resources)) {
// Singleton — one OAuth providers config map per project.
$report[Resource::TYPE_OAUTH_PROVIDERS] = 1;
}

if (\in_array(Resource::TYPE_POLICIES, $resources)) {
// Singleton — one policies config per project.
$report[Resource::TYPE_POLICIES] = 1;
Expand Down Expand Up @@ -663,6 +670,20 @@ protected function exportGroupAuth(int $batchSize, array $resources): void
previous: $e
));
}

try {
if (\in_array(Resource::TYPE_OAUTH_PROVIDERS, $resources)) {
$this->exportOAuthProviders();
}
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_OAUTH_PROVIDERS,
Transfer::GROUP_AUTH,
message: $e->getMessage(),
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
previous: $e
));
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
}

private function exportPolicies(): void
Expand Down Expand Up @@ -722,6 +743,33 @@ private function exportAuthMethods(): void
$this->callback([$authMethods]);
}

private function exportOAuthProviders(): void
{
// listOAuth2Providers returns a heterogeneous list — each entry is a typed
// OAuth2{Provider} payload with provider-specific field names (Google's
// `clientId` vs Apple's `serviceId`). We only migrate the `enabled` toggle;
// credential fields (clientId/secret/serviceId/keyId/...) are intentionally
// not migrated — destination user must re-register the OAuth app and
// re-enter credentials, same caveat as the SMTP password.
$response = $this->project->listOAuth2Providers();

$providers = [];
foreach ($response->providers as $provider) {
$providers[] = [
'key' => (string) ($provider['$id'] ?? ''),
'enabled' => (bool) ($provider['enabled'] ?? false),
'appId' => '',
];
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Outdated

$oAuthProviders = new OAuthProviders(
$this->projectId,
$providers,
);

$this->callback([$oAuthProviders]);
}

/**
* @throws AppwriteException
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Migration/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Transfer
Resource::TYPE_MEMBERSHIP,
Resource::TYPE_HASH,
Resource::TYPE_AUTH_METHODS,
Resource::TYPE_OAUTH_PROVIDERS,
Resource::TYPE_POLICIES,
];

Expand Down Expand Up @@ -128,6 +129,7 @@ class Transfer
Resource::TYPE_TEAM,
Resource::TYPE_MEMBERSHIP,
Resource::TYPE_AUTH_METHODS,
Resource::TYPE_OAUTH_PROVIDERS,
Resource::TYPE_POLICIES,
Resource::TYPE_FILE,
Resource::TYPE_BUCKET,
Expand Down
Loading