Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/project/servers/secret-token.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Generate the "secret_token" file if it does not already exist.7*/89import { callback } from "awaiting";10import { randomBytes } from "crypto";11import { chmod, readFile, writeFile } from "node:fs/promises";1213import { secretToken as secretTokenPath } from "@cocalc/project/data";1415import { getLogger } from "@cocalc/project/logger";16const winston = getLogger("secret-token");1718// We use an n-character cryptographic random token, where n19// is given below. If you want to change this, changing only20// the following line should be safe.21const LENGTH = 128;2223let secretToken: string = ""; // not yet initialized2425async function createSecretToken(): Promise<string> {26winston.info(`creating '${secretTokenPath}'`);2728secretToken = (await callback(randomBytes, LENGTH)).toString("base64");29await writeFile(secretTokenPath, secretToken);30// set restrictive permissions; shouldn't be necessary31await chmod(secretTokenPath, 0o600);32return secretToken;33}3435export default async function init(): Promise<string> {36try {37winston.info(`checking for secret token in "${secretTokenPath}"`);38secretToken = (await readFile(secretTokenPath)).toString();39return secretToken;40} catch (err) {41return await createSecretToken();42}43}4445export function getSecretToken(): string {46if (secretToken == "") {47throw Error("secret token not yet initialized");48}49return secretToken;50}515253