Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions apps/web/src/lib/oauth/secret-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
randomBytes,
} from "node:crypto";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
import {
deleteSecret,
Expand Down Expand Up @@ -43,13 +44,33 @@ function parseConfiguredKey(value: string): Buffer | null {
return createHash("sha256").update(trimmed).digest();
}

function isLocalInstall(): boolean {
return process.env.SECOND_LOCAL_INSTALL === "1";
}

function getGeneratedLocalSecretDir(): string {
const configured = process.env.SECOND_LOCAL_SECRET_DIR?.trim();
if (configured) return configured;

if (isLocalInstall()) {
return join(homedir(), ".second", "secrets");
}

return join(process.cwd(), LOCAL_SECRET_DIR);
}

function getGeneratedLocalSecretKey(): Buffer {
const path = join(process.cwd(), LOCAL_SECRET_DIR, LOCAL_SECRET_KEY_FILE);
const secretDir = getGeneratedLocalSecretDir();
const path = join(secretDir, LOCAL_SECRET_KEY_FILE);
if (existsSync(path)) {
return Buffer.from(readFileSync(path, "utf-8").trim(), "base64");
const key = Buffer.from(readFileSync(path, "utf-8").trim(), "base64");
if (key.length !== 32) {
throw new Error("Invalid local OAuth secret-store key.");
}
return key;
}

mkdirSync(join(process.cwd(), LOCAL_SECRET_DIR), { recursive: true });
mkdirSync(secretDir, { recursive: true, mode: 0o700 });
const key = randomBytes(32);
writeFileSync(path, `${key.toString("base64")}\n`, { mode: 0o600 });
return key;
Expand All @@ -60,7 +81,7 @@ function getLocalEncryptionKey(): Buffer {
const configuredKey = configured ? parseConfiguredKey(configured) : null;
if (configuredKey) return configuredKey;

if (process.env.NODE_ENV === "production") {
if (process.env.NODE_ENV === "production" && !isLocalInstall()) {
throw new Error(
"SECOND_TOKEN_ENCRYPTION_KEY is required when WorkOS Vault is not configured in production.",
);
Expand Down
1 change: 1 addition & 0 deletions packages/cli-local-darwin-arm64/bin/second-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ async function startWeb({
WORKER_URL: workerUrl,
REDIS_URL: redisUrl,
SECOND_LOCAL_INSTALL: "1",
SECOND_LOCAL_SECRET_DIR: SECRETS_DIR,
SECOND_RELEASE_RUNTIME: "native",
SECOND_RELEASE_VERSION: packageMetadata.version,
SECOND_RELEASE_PACKAGE: packageMetadata.name,
Expand Down
Loading