mirror of
https://github.com/github/awesome-copilot.git
synced 2026-08-13 12:49:49 +00:00
Harden persisted authentication recovery
Treat malformed authentication records as missing so browser sign-in can recover, preserve real file-read failures, and pin the Azure Identity persistence dependencies exactly. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -28,14 +28,23 @@ let legacyAuthCacheRemoved = false;
|
|||||||
|
|
||||||
useIdentityPlugin(cachePersistencePlugin);
|
useIdentityPlugin(cachePersistencePlugin);
|
||||||
|
|
||||||
async function loadAuthenticationRecord() {
|
export async function loadAuthenticationRecord({
|
||||||
|
readFile = fs.readFile,
|
||||||
|
deserialize = deserializeAuthenticationRecord,
|
||||||
|
authRecordFile = AUTH_RECORD_FILE,
|
||||||
|
} = {}) {
|
||||||
|
let serialized;
|
||||||
try {
|
try {
|
||||||
const serialized = await fs.readFile(AUTH_RECORD_FILE, "utf-8");
|
serialized = await readFile(authRecordFile, "utf-8");
|
||||||
return deserializeAuthenticationRecord(serialized);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error?.code === "ENOENT") return undefined;
|
if (error?.code === "ENOENT") return undefined;
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
return deserialize(serialized);
|
||||||
|
} catch {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function saveAuthenticationRecord(record) {
|
async function saveAuthenticationRecord(record) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import assert from "node:assert/strict";
|
|||||||
import {
|
import {
|
||||||
ConnectorAuthenticationRequiredError,
|
ConnectorAuthenticationRequiredError,
|
||||||
InteractiveAuthBroker,
|
InteractiveAuthBroker,
|
||||||
|
loadAuthenticationRecord,
|
||||||
TOKEN_CACHE_NAME,
|
TOKEN_CACHE_NAME,
|
||||||
} from "./auth.mjs";
|
} from "./auth.mjs";
|
||||||
|
|
||||||
@@ -36,6 +37,7 @@ test("ARM token requests require an explicit browser sign-in", async () => {
|
|||||||
},
|
},
|
||||||
loadAuthRecord: async () => undefined,
|
loadAuthRecord: async () => undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
await assert.rejects(
|
await assert.rejects(
|
||||||
broker.getToken(),
|
broker.getToken(),
|
||||||
(error) => error instanceof ConnectorAuthenticationRequiredError
|
(error) => error instanceof ConnectorAuthenticationRequiredError
|
||||||
@@ -47,6 +49,25 @@ test("ARM token requests require an explicit browser sign-in", async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("a malformed authentication record falls back to browser sign-in", async () => {
|
||||||
|
assert.equal(
|
||||||
|
await loadAuthenticationRecord({
|
||||||
|
readFile: async () => "{malformed-json",
|
||||||
|
}),
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("authentication record read failures remain operational errors", async () => {
|
||||||
|
const readError = Object.assign(new Error("authentication record is unreadable"), { code: "EACCES" });
|
||||||
|
await assert.rejects(
|
||||||
|
loadAuthenticationRecord({
|
||||||
|
readFile: async () => { throw readError; },
|
||||||
|
}),
|
||||||
|
(error) => error === readError,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
test("interactive sign-in reports pending then done and caches the ARM token", async () => {
|
test("interactive sign-in reports pending then done and caches the ARM token", async () => {
|
||||||
let credentialOptions;
|
let credentialOptions;
|
||||||
let authenticateOptions;
|
let authenticateOptions;
|
||||||
|
|||||||
@@ -14,8 +14,8 @@
|
|||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@azure/identity": "^4.13.1",
|
"@azure/identity": "4.13.1",
|
||||||
"@azure/identity-cache-persistence": "^1.3.0",
|
"@azure/identity-cache-persistence": "1.3.0",
|
||||||
"@github/copilot-sdk": "1.0.6"
|
"@github/copilot-sdk": "1.0.6"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user