Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
syn2mas: accept ULIDs and UUIDs in arguments for upstream IDP mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed Nov 2, 2023
1 parent 89145d5 commit 209758d
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions tools/syn2mas/src/migrate.mts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ interface MigrationOptions {
help?: boolean;
}

// Parses a string that is either a UUID or a ULID
// Returns [uuid, ulid] in canonical format
const parseUuidOrUlid = (input: string): [string, string] => {
let bytes: Uint8Array;
if (id128.Ulid.isCanonical(input)) {
bytes = id128.Ulid.fromCanonicalTrusted(input).bytes;
} else if (id128.Uuid.isCanonical(input)) {
bytes = id128.Uuid.fromCanonicalTrusted(input).bytes;
} else {
bytes = id128.Uuid.fromRaw(input).bytes;
}

return [
id128.Uuid.construct(bytes).toCanonical(),
id128.Ulid.construct(bytes).toCanonical(),
];
};

export async function migrate(): Promise<void> {
const args = parse<MigrationOptions>(
{
Expand Down Expand Up @@ -135,22 +153,26 @@ export async function migrate(): Promise<void> {

if (
!id128.Uuid.isRaw(masProviderId) &&
!id128.Uuid.isCanonical(masProviderId)
!id128.Uuid.isCanonical(masProviderId) &&
!id128.Ulid.isCanonical(masProviderId)
) {
throw new Error(
`Upstream provider mapping UUID is not in correct format. It should be a UUID: ${masProviderId}`,
`Upstream provider mapping is not in correct format. It should be a UUID or a ULID: ${masProviderId}`,
);
}

const [masProviderUuid, masProviderUlid] = parseUuidOrUlid(masProviderId);

log.info(
`Loading existing upstream provider ${masProviderId} from MAS database as ${providerId}`,
`Loading existing upstream provider ${masProviderUlid} from MAS database as ${providerId}`,
);
const existingProvider = await mas("upstream_oauth_providers")
.select("*")
.where({ upstream_oauth_provider_id: masProviderId })
.where({ upstream_oauth_provider_id: masProviderUuid })
.first();
if (!existingProvider) {
throw new Error(
`Could not find upstream provider ${masProviderId} in MAS database`,
`Could not find upstream provider ${masProviderUlid} in MAS database`,
);
}
upstreamProviders.set(providerId, existingProvider);
Expand Down

0 comments on commit 209758d

Please sign in to comment.