mirror of
https://gitee.com/mateos/mateclaw.git
synced 2026-11-02 14:11:49 +08:00
36 lines
958 B
JavaScript
36 lines
958 B
JavaScript
import { randomBytes } from 'node:crypto'
|
|
import { chmodSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
|
|
const SECRET_FILE = 'jwt-secret'
|
|
|
|
function readSecret(path) {
|
|
const secret = readFileSync(path, 'utf8')
|
|
chmodSync(path, 0o600)
|
|
return secret
|
|
}
|
|
|
|
export function getLocalJwtSecret(userDataPath, explicitSecret) {
|
|
if (explicitSecret !== undefined) return explicitSecret
|
|
|
|
mkdirSync(userDataPath, { recursive: true, mode: 0o700 })
|
|
const secretPath = join(userDataPath, SECRET_FILE)
|
|
|
|
try {
|
|
return readSecret(secretPath)
|
|
} catch (error) {
|
|
if (error.code !== 'ENOENT') throw error
|
|
}
|
|
|
|
const secret = randomBytes(48).toString('base64url')
|
|
|
|
try {
|
|
writeFileSync(secretPath, secret, { encoding: 'utf8', flag: 'wx', mode: 0o600 })
|
|
chmodSync(secretPath, 0o600)
|
|
return secret
|
|
} catch (error) {
|
|
if (error.code !== 'EEXIST') throw error
|
|
return readSecret(secretPath)
|
|
}
|
|
}
|