fix: paste command

This commit is contained in:
Cristina Poncela Cubeiro 2026-10-28 16:50:44 +01:00
parent 9c76f3370c
commit 7cab68ac68
2 changed files with 33 additions and 2 deletions

View file

@ -85,11 +85,17 @@ function optionalRegistration(value: unknown, field: string): "dcr" | "cimd" | u
return value; return value;
} }
/**
* Accepts the payload alone or the whole command it came in, such as
* `PI_RADIUS_GATEWAY='…' pi auth --continue '<payload>'`: base64url has no spaces, so the payload is
* the last word, without the quotes a shell would remove. Raw JSON is passed through unchanged.
*/
function normalizeContinuationInput(inputValue: string): string { function normalizeContinuationInput(inputValue: string): string {
const trimmed = inputValue.trim(); const trimmed = inputValue.trim();
if (!trimmed) throw new AuthContinueError("Continuation payload is empty"); if (!trimmed) throw new AuthContinueError("Continuation payload is empty");
const commandMatch = /^\s*pi\s+auth\s+--continue\s+(.+)$/su.exec(trimmed); if (trimmed.startsWith("{")) return trimmed;
return commandMatch ? commandMatch[1].trim() : trimmed; const lastWord = trimmed.split(/\s+/u).at(-1)!;
return /^(['"])(.*)\1$/su.exec(lastWord)?.[2] ?? lastWord;
} }
function decodeBase64Json(inputValue: string): unknown { function decodeBase64Json(inputValue: string): unknown {

View file

@ -170,6 +170,31 @@ describe("credential print commands", () => {
expect(decodeAuthContinuationPayload(`pi auth --continue ${encoded}`)).toMatchObject(payload); expect(decodeAuthContinuationPayload(`pi auth --continue ${encoded}`)).toMatchObject(payload);
}); });
test("accepts the payload pasted in any command form", () => {
const payload = {
version: 1,
serviceUrl: "https://auth.example",
providerId: "auth-service",
continuationId: "cont_123",
secret: "one-time-secret",
returnUrl: "https://auth.example/connections",
mcp: { serverUrl: "https://mcp.example/mcp", serverName: "example", authType: "none" },
};
const encoded = Buffer.from(JSON.stringify(payload)).toString("base64url");
for (const input of [
encoded,
` ${encoded}\n`,
`'${encoded}'`,
`"${encoded}"`,
`pi auth --continue '${encoded}'`,
`pi auth continue ${encoded}`,
`PI_RADIUS_GATEWAY='http://localhost:8787' pi auth --continue '${encoded}'`,
JSON.stringify(payload, null, 2),
]) {
expect(decodeAuthContinuationPayload(input)).toMatchObject(payload);
}
});
test("accepts a *.localhost returnUrl for a localhost serviceUrl", () => { test("accepts a *.localhost returnUrl for a localhost serviceUrl", () => {
const payload = { const payload = {
version: 1, version: 1,