(file: AuthFile)
| 59 | } |
| 60 | |
| 61 | async function writeAuthFile(file: AuthFile): Promise<void> { |
| 62 | const path = AUTH_FILE() |
| 63 | await mkdir(dirname(path), { recursive: true }) |
| 64 | const data = JSON.stringify(file, null, 2) + '\n' |
| 65 | // Tight permissions: only owner can read. Important because the hash, if |
| 66 | // leaked alongside the salt, could in principle be brute-forced. 256-bit |
| 67 | // input makes that infeasible, but defense in depth still wants chmod 600. |
| 68 | await writeFile(path, data, { mode: 0o600 }) |
| 69 | // Some platforms ignore the `mode` option on writeFile (windows, certain |
| 70 | // Docker images with restrictive umask). Force a second chmod to be sure |
| 71 | // — best-effort, ignore failures on platforms that don't support it. |
| 72 | await chmod(path, 0o600).catch(() => { /* noop */ }) |
| 73 | } |
| 74 | |
| 75 | function deriveHash(token: string, salt: Buffer): Buffer { |
| 76 | return scryptSync(token, salt, KEY_LEN, { N: SCRYPT_N, r: SCRYPT_R, p: SCRYPT_P }) |
no test coverage detected