mirror of
https://github.com/makecindy/cindy.git
synced 2026-12-02 06:09:35 +08:00
Expose the real session list and the adopted-account projection over the loopback listener behind a per-profile bearer token stored 0600 next to the profile. The token never appears in URLs, logs or responses. Verified with a live host: /sessions returns 401 without the bearer and the real (empty) list with it; /me reports the bound owner. Signed-off-by: CHEYAN <me@iamcheyan.com>
37 lines
1.4 KiB
JavaScript
37 lines
1.4 KiB
JavaScript
/**
|
|
* Host API token: one random bearer per profile, stored 0600 next to the profile.
|
|
*
|
|
* Never put it in a URL, log line, or HTTP body. Clients read the file (same OS
|
|
* user) or receive it once from an explicit pairing step in a later stage.
|
|
*/
|
|
|
|
import crypto from 'node:crypto';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
const TOKEN_FILE = 'host-api-token';
|
|
|
|
export function readOrCreateApiToken(userDataDir) {
|
|
const file = path.join(userDataDir, TOKEN_FILE);
|
|
try {
|
|
const existing = fs.readFileSync(file, 'utf8').trim();
|
|
if (existing.length >= 32) return existing;
|
|
} catch (error) {
|
|
if (error?.code !== 'ENOENT') throw error;
|
|
}
|
|
const token = crypto.randomBytes(32).toString('base64url');
|
|
fs.mkdirSync(userDataDir, { recursive: true, mode: 0o700 });
|
|
const temporary = `${file}.tmp-${process.pid}`;
|
|
fs.writeFileSync(temporary, `${token}\n`, { encoding: 'utf8', mode: 0o600 });
|
|
fs.renameSync(temporary, file);
|
|
return token;
|
|
}
|
|
|
|
/** Constant-time comparison; a missing header never matches. */
|
|
export function isAuthorized(header, expected) {
|
|
if (typeof header !== 'string' || !header.startsWith('Bearer ') || !expected) return false;
|
|
const provided = Buffer.from(header.slice('Bearer '.length), 'utf8');
|
|
const wanted = Buffer.from(expected, 'utf8');
|
|
return provided.length === wanted.length && crypto.timingSafeEqual(provided, wanted);
|
|
}
|