openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
50 lines (49 loc) • 2.21 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { h as resolveDefaultSecretProviderAlias } from "./ref-contract-b8L-9DrQ.mjs";
import { r as assertSecretStoreValue } from "./secret-store-validation-CU_ws9sE.mjs";
import { d as writeSecretStoreEntry, o as readSecretStoreValue } from "./secret-store-BZfjrX_2.mjs";
import { t as randomToken } from "./random-token-Sl4a6KEQ.mjs";
//#region src/gateway/auth-token-store-ref.ts
/** Store entry name for the gateway token; mirrors the documented env-var contract. */
const GATEWAY_AUTH_TOKEN_STORE_NAME = "OPENCLAW_GATEWAY_TOKEN";
const GATEWAY_AUTH_TOKEN_STORE_SCOPE = { kind: "team" };
async function readStoredGatewayToken() {
const e = await readSecretStoreValue({
scope: GATEWAY_AUTH_TOKEN_STORE_SCOPE,
name: GATEWAY_AUTH_TOKEN_STORE_NAME
});
return e.ok ? normalizeOptionalString(e.value) : void 0;
}
/**
* Provisions the gateway token in the secret store and returns the ref config points at.
*
* Omit `token` when setup has no value of its own: an existing store entry then wins so
* reruns never rotate a token already paired with clients or a running service, and a
* fresh one is minted otherwise. A supplied token always wins, which also migrates a
* previously persisted plaintext token without invalidating it. The store write stays
* ahead of the config write on purpose — a ref persisted without its value would leave
* the gateway unauthenticatable, while an entry whose config write later fails is simply
* picked up by the next run.
*/
async function provisionGatewayTokenStoreRef(e) {
const t = e.token ? void 0 : await readStoredGatewayToken();
const n = e.token ?? t ?? randomToken();
assertSecretStoreValue(n, "secret", GATEWAY_AUTH_TOKEN_STORE_NAME);
if (n !== t) await writeSecretStoreEntry({
scope: GATEWAY_AUTH_TOKEN_STORE_SCOPE,
name: GATEWAY_AUTH_TOKEN_STORE_NAME,
value: n,
kind: "secret",
updatedBy: "setup"
});
return {
ref: {
source: "store",
provider: resolveDefaultSecretProviderAlias(e.config, "store", { preferFirstProviderForSource: true }),
id: GATEWAY_AUTH_TOKEN_STORE_NAME
},
token: n
};
}
//#endregion
export { provisionGatewayTokenStoreRef as t };