1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-11-20 12:02:22 +03:00

syn2mas: accept ULIDs and UUIDs in arguments for upstream IDP mapping

This commit is contained in:
Quentin Gliech
2023-11-02 11:55:50 +01:00
parent 89145d574b
commit 209758db92

View File

@@ -47,6 +47,24 @@ interface MigrationOptions {
help?: boolean; 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> { export async function migrate(): Promise<void> {
const args = parse<MigrationOptions>( const args = parse<MigrationOptions>(
{ {
@@ -135,22 +153,26 @@ export async function migrate(): Promise<void> {
if ( if (
!id128.Uuid.isRaw(masProviderId) && !id128.Uuid.isRaw(masProviderId) &&
!id128.Uuid.isCanonical(masProviderId) !id128.Uuid.isCanonical(masProviderId) &&
!id128.Ulid.isCanonical(masProviderId)
) { ) {
throw new Error( 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( 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") const existingProvider = await mas("upstream_oauth_providers")
.select("*") .select("*")
.where({ upstream_oauth_provider_id: masProviderId }) .where({ upstream_oauth_provider_id: masProviderUuid })
.first(); .first();
if (!existingProvider) { if (!existingProvider) {
throw new Error( 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); upstreamProviders.set(providerId, existingProvider);