Replace NodeJS dict aliases with maps and named field reads
Co-authored-by: Danny Kim <temeddix@gmail.com> Co-committed-by: Danny Kim <temeddix@gmail.com>
This commit was merged in pull request #24.
This commit is contained in:
+28
-16
@@ -10,9 +10,9 @@ import {
|
||||
rm,
|
||||
writeFile,
|
||||
} from "node:fs/promises";
|
||||
import { dirname, join } from "node:path";
|
||||
import nodePath from "node:path";
|
||||
|
||||
import { isNotFound, isObject } from "#common";
|
||||
import { field, isNotFound, isObject } from "#common";
|
||||
|
||||
interface CodexApiKey {
|
||||
kind: "api-key";
|
||||
@@ -26,7 +26,9 @@ interface CodexPersistent {
|
||||
export type CodexAuthentication = CodexApiKey | CodexPersistent;
|
||||
|
||||
export function codexAuthentication(token?: string): CodexAuthentication {
|
||||
return token === undefined ? { kind: "persistent" } : { kind: "api-key", token };
|
||||
return token === undefined
|
||||
? { kind: "persistent" }
|
||||
: { kind: "api-key", token };
|
||||
}
|
||||
|
||||
// Codex logs revoked, expired, or reused ChatGPT sign-ins and rejected API keys
|
||||
@@ -83,20 +85,23 @@ async function readCodexAuth(path: string): Promise<string> {
|
||||
throw new Error("Codex login must be private");
|
||||
}
|
||||
const text = await readFile(path, "utf8");
|
||||
let value: unknown;
|
||||
let value: unknown = null;
|
||||
try {
|
||||
value = JSON.parse(text);
|
||||
} catch {
|
||||
throw new Error("invalid Codex login");
|
||||
}
|
||||
const tokens = isObject(value) ? value["tokens"] : undefined;
|
||||
const authMode = field(value, "auth_mode");
|
||||
const tokens = field(value, "tokens");
|
||||
if (
|
||||
!isObject(value) ||
|
||||
typeof value["auth_mode"] !== "string" ||
|
||||
value["auth_mode"] === "" ||
|
||||
typeof authMode !== "string" ||
|
||||
authMode === "" ||
|
||||
!isObject(tokens) ||
|
||||
!["access_token", "refresh_token", "id_token", "account_id"].every(
|
||||
(name) => typeof tokens[name] === "string" && tokens[name] !== "",
|
||||
(name) => {
|
||||
const token = field(tokens, name);
|
||||
return typeof token === "string" && token !== "";
|
||||
},
|
||||
)
|
||||
) {
|
||||
throw new Error("invalid Codex login");
|
||||
@@ -110,32 +115,39 @@ export async function stageCodexAuth(
|
||||
uid: number,
|
||||
gid: number,
|
||||
): Promise<string> {
|
||||
const codexHome = join(profileHome, ".codex");
|
||||
const codexHome = nodePath.join(profileHome, ".codex");
|
||||
await mkdir(codexHome, { recursive: true, mode: 0o700 });
|
||||
await chmod(codexHome, 0o700);
|
||||
await chown(codexHome, uid, gid);
|
||||
const stagedPath = join(codexHome, "auth.json");
|
||||
const stagedPath = nodePath.join(codexHome, "auth.json");
|
||||
try {
|
||||
await writeFile(stagedPath, await readCodexAuth(canonicalPath), { mode: 0o600 });
|
||||
await writeFile(stagedPath, await readCodexAuth(canonicalPath), {
|
||||
mode: 0o600,
|
||||
});
|
||||
await chmod(stagedPath, 0o600);
|
||||
await chown(stagedPath, uid, gid);
|
||||
} catch (error) {
|
||||
if (!isNotFound(error)) throw error;
|
||||
if (!isNotFound(error)) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
return stagedPath;
|
||||
}
|
||||
|
||||
// Creates an empty private file under a fresh name.
|
||||
async function makeTempFile(directory: string): Promise<string> {
|
||||
const path = join(directory, `.tmp-${randomUUID()}`);
|
||||
const path = nodePath.join(directory, `.tmp-${randomUUID()}`);
|
||||
const handle = await open(path, "wx", 0o600);
|
||||
await handle.close();
|
||||
return path;
|
||||
}
|
||||
|
||||
export async function persistCodexAuth(stagedPath: string, canonicalPath: string): Promise<void> {
|
||||
export async function persistCodexAuth(
|
||||
stagedPath: string,
|
||||
canonicalPath: string,
|
||||
): Promise<void> {
|
||||
const text = await readCodexAuth(stagedPath);
|
||||
const directory = dirname(canonicalPath);
|
||||
const directory = nodePath.dirname(canonicalPath);
|
||||
await mkdir(directory, { recursive: true, mode: 0o700 });
|
||||
const temporary = await makeTempFile(directory);
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user