From 06c3ec1e930f2f0d4ea0e5d193fe39698199725a Mon Sep 17 00:00:00 2001 From: Danilo Leone Date: Wed, 2 Dec 2026 13:23:21 -0300 Subject: [PATCH] fix(licensing): accept an already exchanged api_key on /license/activate The licensing success page lets the operator trade the authorization code for an api_key by hand, which burns the code. /license/activate then failed with "authorization code expired or already used" for both the code and the api_key. When the exchange fails, try the value as an api_key and store it only after the licensing server accepts it. --- src/licensing/runtime.ts | 58 ++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/src/licensing/runtime.ts b/src/licensing/runtime.ts index f99d026bc0..09150b9314 100644 --- a/src/licensing/runtime.ts +++ b/src/licensing/runtime.ts @@ -390,6 +390,37 @@ export async function completeActivation( await activateInstance(rc); + markActivated(rc, apiKey, tier); +} + +/** + * Activates with a value that may already be an api_key. The value is stored + * only after the licensing server accepts it, so a wrong code never ends up as + * the saved license. + */ +async function activateWithApiKey(rc: RuntimeContext, apiKey: string): Promise { + rc.apiKey = apiKey; + try { + await activateInstance(rc); + } catch (err) { + rc.apiKey = ''; + logger.debug(`Value is not a valid api_key either: ${readErrorMessage(err)}`); + return false; + } + + rc.registerUrl = ''; + rc.registerToken = ''; + try { + await saveRuntimeData({ apiKey, tier: rc.tier, customerId: 0 }); + } catch (err) { + logger.warn(`Could not save license: ${readErrorMessage(err)}`); + } + + markActivated(rc, apiKey, rc.tier); + return true; +} + +function markActivated(rc: RuntimeContext, apiKey: string, tier: string): void { rc.recomputeContextHash(); rc.setActive(true); activateIntegrity(rc); @@ -480,17 +511,28 @@ export async function initRegistration(rc: RuntimeContext, redirectUri?: string) /** Handles the activation callback — exchanges code, saves and activates. */ export async function activateWithCode(rc: RuntimeContext, code: string): Promise { - const exchangeResp = await postUnsigned('/v1/register/exchange', { - authorization_code: code, - instance_id: rc.instanceId, - }); + let result: RegisterExchangeResponse | undefined; + let exchangeErr: unknown; + try { + const exchangeResp = await postUnsigned('/v1/register/exchange', { + authorization_code: code, + instance_id: rc.instanceId, + }); + result = exchangeResp.data; + } catch (err) { + exchangeErr = err; + } - const result = exchangeResp.data; - if (!result?.api_key) { - throw new Error('Invalid or expired code'); + if (result?.api_key) { + await completeActivation(rc, result.api_key, result.tier, result.customer_id); + return; } - await completeActivation(rc, result.api_key, result.tier, result.customer_id); + // The code may already have been traded for an api_key outside the instance, + // which burns it; accept that api_key as well. + if (await activateWithApiKey(rc, code)) return; + + throw exchangeErr ?? new Error('Invalid or expired code'); } /** Convenience helper for routes that need a public-safe view of the runtime. */