Skip to content
Closed
Changes from all commits
Commits
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
58 changes: 50 additions & 8 deletions src/licensing/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
rc.apiKey = apiKey;
try {
await activateInstance(rc);
} catch (err) {
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
rc.apiKey = '';
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
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 });
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
} catch (err) {
logger.warn(`Could not save license: ${readErrorMessage(err)}`);
}
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved

markActivated(rc, apiKey, rc.tier);
return true;
}

function markActivated(rc: RuntimeContext, apiKey: string, tier: string): void {
rc.recomputeContextHash();
rc.setActive(true);
activateIntegrity(rc);
Expand Down Expand Up @@ -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<void> {
const exchangeResp = await postUnsigned<RegisterExchangeResponse>('/v1/register/exchange', {
authorization_code: code,
instance_id: rc.instanceId,
});
let result: RegisterExchangeResponse | undefined;
let exchangeErr: unknown;
try {
const exchangeResp = await postUnsigned<RegisterExchangeResponse>('/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;
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved

throw exchangeErr ?? new Error('Invalid or expired code');
}

/** Convenience helper for routes that need a public-safe view of the runtime. */
Expand Down
Loading