auth-install-policy.tsInstall Policies
Install-Time & Auth-Mode Policies
Install policies enforce security requirements during initial deployment. They determine whether a gateway token is required at install time and prevent ambiguous auth configurations where both token and password are set without an explicit mode declaration.
auth-mode-policy.tsauth-config-utils.tsEXPLICIT_GATEWAY_AUTH_MODE_REQUIRED_ERRORTable of Contents
Overview
Two distinct but related policy systems ensure auth is properly configured:
- Install Policy (
auth-install-policy.ts) — decides whether a gateway token must be generated duringopenclaw install - Auth Mode Policy (
auth-mode-policy.ts) — detects and rejects ambiguous configurations where both token and password exist without an explicit mode
auth.token and auth.password are configured without setting auth.mode explicitly, assertExplicitGatewayAuthModeWhenBothConfigured() will throw EXPLICIT_GATEWAY_AUTH_MODE_REQUIRED_ERROR. This prevents silent fallback to an unintended auth method.
Install Policy Decision Flow
flowchart TB
START["openclaw install"]
CHECK["shouldRequireGatewayTokenForInstall(cfg, env)"]
EXPLICIT["hasExplicitGatewayInstallAuthMode(mode)?"]
PASS_CFG["hasConfiguredGatewayPasswordForInstall(cfg)?"]
PASS_ENV["hasDurableGatewayPasswordEnvForInstall(cfg, env)?"]
TOKEN_REQ["Token REQUIRED
Generate during install"]
TOKEN_SKIP["Token NOT required
Password auth sufficient"]
START --> CHECK
CHECK --> EXPLICIT
EXPLICIT -->|"mode explicitly set"| TOKEN_SKIP
EXPLICIT -->|"no explicit mode"| PASS_CFG
PASS_CFG -->|"password in config"| TOKEN_SKIP
PASS_CFG -->|"no password"| PASS_ENV
PASS_ENV -->|"password in env"| TOKEN_SKIP
PASS_ENV -->|"no password"| TOKEN_REQ
How It Works
Install Policy Check
shouldRequireGatewayTokenForInstall() is called during the installation process. It checks three sources in order: explicit install auth mode, config file password, and environment variable password.
import { shouldRequireGatewayTokenForInstall } from './auth-install-policy';
const needsToken = shouldRequireGatewayTokenForInstall(
openClawConfig,
process.env,
);
if (needsToken) {
const token = generateSecureToken();
console.log(`Gateway token: ${token}`);
// Store token in config
}
Auth Mode Ambiguity Detection
When both token and password are present in config, hasAmbiguousGatewayAuthModeConfig() returns true. The assertion function then throws a descriptive error.
import {
hasAmbiguousGatewayAuthModeConfig,
assertExplicitGatewayAuthModeWhenBothConfigured,
EXPLICIT_GATEWAY_AUTH_MODE_REQUIRED_ERROR,
} from './auth-mode-policy';
if (hasAmbiguousGatewayAuthModeConfig(cfg)) {
// Both token and password configured without explicit mode
// This is dangerous: which one should be used?
}
// Throws EXPLICIT_GATEWAY_AUTH_MODE_REQUIRED_ERROR
// if ambiguous config detected
assertExplicitGatewayAuthModeWhenBothConfigured(cfg);
Config Utilities for Password Management
auth-config-utils.ts provides helpers for setting passwords and resolving secret references from external files.
import {
withGatewayAuthPassword,
resolveGatewayPasswordSecretRef,
} from './auth-config-utils';
// Immutably set a password in config
const updatedCfg = withGatewayAuthPassword(cfg, 'my-secure-password');
// Resolve password from external secret file
const resolvedCfg = await resolveGatewayPasswordSecretRef({
cfg,
env: process.env,
hasPasswordCandidate: true,
hasTokenCandidate: false,
});
Auth Mode Policy State
stateDiagram-v2
[*] --> ConfigLoaded: Load OpenClawConfig
ConfigLoaded --> CheckMode: resolveGatewayAuth()
CheckMode --> TokenOnly: Only token configured
CheckMode --> PasswordOnly: Only password configured
CheckMode --> NoneMode: No credentials configured
CheckMode --> BothConfigured: Both token AND password
TokenOnly --> Resolved: mode = "token"
PasswordOnly --> Resolved: mode = "password"
NoneMode --> Resolved: mode = "none"
BothConfigured --> ExplicitCheck: hasAmbiguousGatewayAuthModeConfig?
ExplicitCheck --> Resolved: auth.mode explicitly set
ExplicitCheck --> Error: No explicit mode
Error --> [*]: EXPLICIT_GATEWAY_AUTH_MODE_REQUIRED_ERROR
Resolved --> [*]: ResolvedGatewayAuth
Function Reference
| Function | File | Returns | Description |
|---|---|---|---|
shouldRequireGatewayTokenForInstall() | auth-install-policy.ts | boolean | Whether token is needed at install time |
hasAmbiguousGatewayAuthModeConfig() | auth-mode-policy.ts | boolean | Detects token+password without explicit mode |
assertExplicitGatewayAuthModeWhenBothConfigured() | auth-mode-policy.ts | void | Throws if ambiguous config detected |
withGatewayAuthPassword() | auth-config-utils.ts | OpenClawConfig | Immutably adds password to config |
resolveGatewayPasswordSecretRef() | auth-config-utils.ts | Promise<OpenClawConfig> | Resolves password from external secret |
Edge Cases
| Scenario | Behavior |
|---|---|
| Only token in config | Install policy: token not required (already exists) |
| Only password in config | Install policy: token not required |
| Password in env only | Install policy: token not required if env is durable |
| No credentials anywhere | Install policy: token required (generate one) |
| Both token + password, no mode | Mode policy: throws error |
| Both token + password, mode set | Mode policy: proceeds with explicit mode |
| Secret ref file missing | Throws GatewaySecretRefUnavailableError |
Related Topics
- Auth Overview — full authentication subsystem
- Device Pairing Flow — device token protocol
- Rate Limiting — brute-force protection
Directory Map
Source files for this topic
src/gateway/
├── auth-install-policy.ts ← shouldRequireGatewayTokenForInstall
├── auth-mode-policy.ts ← hasAmbiguousGatewayAuthModeConfig, assertExplicit...
├── auth-config-utils.ts ← withGatewayAuthPassword, resolveGatewayPasswordSecretRef
└── credentials.ts ← GatewaySecretRefUnavailableError