import { writeFile , chmod , readFile } from "fs" ; import { randomBytes } from "crypto" ; import { callback } from "awaiting" ; import { getLogger } from "@cocalc/project/logger" ; import { secretToken as secretTokenPath } from "@cocalc/project/data" ; const winston = getLogger ( "secret-token" ) ; const LENGTH = 128 ; let secretToken = "" ; export { secretToken } ; async function createSecretToken ( ) { winston . info ( `creating '${ secretTokenPath }'` ) ; secretToken = ( await callback ( randomBytes , LENGTH ) ) . toString ( "base64" ) ; await callback ( writeFile , secretTokenPath , secretToken ) ; await callback ( chmod , secretTokenPath , 0o600 ) ; return secretToken ; } export default async function init ( ) { try { winston . info ( `checking for secret token in "${ secretTokenPath }"` ) ; secretToken = ( await callback ( readFile , secretTokenPath ) ) . toString ( ) ; return secretToken ; } catch ( err ) { return await createSecretToken ( ) ; } }