Files
paseo/packages/server/src/server/auth.ts
T
Mohamed BoudraGitHubgithub-actions[bot] <github-actions[bot]@users.noreply.github.com>
088a6c14c0 feat: direct TCP URI with SSL toggle and optional password auth (#635)
* feat: direct TCP URI with SSL toggle and optional password auth

Replaces the heuristic-driven direct-connection model with a user-controlled
one. The Add Host dialog now exposes structured Host, Port, "Use SSL", and
masked Password fields that compose the canonical
`tcp://host:port?ssl=true&password=xxx` URI used as the storage form.

The daemon gains optional shared-secret auth: `Authorization: Bearer <pw>` on
HTTP and `Sec-WebSocket-Protocol: paseo.bearer.<pw>` on the WS upgrade
(browser WebSocket can't set custom headers). Configured via config.json
`auth.password` or `PASEO_PASSWORD` env. Off by default — old clients keep
working unchanged.

The `port === 443` heuristic for ws/wss is gone; the explicit `useTls` flag
drives scheme selection at every call site.

* fix: stabilize direct tcp auth ci checks

* fix: restore fetch stub in bootstrap smoke test

* fix(app): collapse Advanced section in add-host modal

* feat(server): hash daemon password in config

* fix: update lockfile signatures and Nix hash

* Improve direct TCP auth failures

* Fix workspace cwd updates after rebase

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2026-04-30 22:05:49 +08:00

128 lines
3.1 KiB
TypeScript

import { compare, compareSync, hashSync } from "bcryptjs";
import type { RequestHandler } from "express";
export const DAEMON_PASSWORD_BCRYPT_COST = 12;
export interface DaemonAuthConfig {
password?: string;
}
export interface BearerAuthRejectContext {
path: string;
method: string;
hasToken: boolean;
}
interface BearerValidationInput {
password: string | undefined;
token: string | null;
}
export function isBearerTokenValid(input: BearerValidationInput): boolean {
return isBearerTokenValidSync(input);
}
export async function isBearerTokenValidAsync(input: BearerValidationInput): Promise<boolean> {
if (!input.password) {
return true;
}
if (input.token === null) {
return false;
}
return compare(input.token, input.password);
}
export function isBearerTokenValidSync(input: BearerValidationInput): boolean {
if (!input.password) {
return true;
}
if (input.token === null) {
return false;
}
return compareSync(input.token, input.password);
}
export function hashDaemonPassword(password: string): string {
return hashSync(password, DAEMON_PASSWORD_BCRYPT_COST);
}
export function extractHttpBearerToken(value: string | undefined): string | null {
if (!value) {
return null;
}
const [scheme, ...tokenParts] = value.trim().split(/\s+/);
if (scheme !== "Bearer" || tokenParts.length !== 1) {
return null;
}
return tokenParts[0] ?? null;
}
export function extractWsBearerProtocol(value: string | undefined): string | null {
if (!value) {
return null;
}
for (const protocol of value.split(",")) {
const trimmed = protocol.trim();
const segments = trimmed.split(".");
if (segments[0] === "paseo" && segments[1] === "bearer" && segments.length >= 3) {
return trimmed;
}
}
return null;
}
export function extractWsBearerToken(protocol: string | null): string | null {
if (!protocol) {
return null;
}
const segments = protocol.split(".");
if (segments[0] !== "paseo" || segments[1] !== "bearer" || segments.length < 3) {
return null;
}
return segments.slice(2).join(".");
}
export function createRequireBearerMiddleware(
auth: DaemonAuthConfig | undefined,
onReject?: (context: BearerAuthRejectContext) => void,
): RequestHandler {
const password = auth?.password;
return (req, res, next) => {
if (!password || shouldBypassBearerAuth(req.method, req.path)) {
next();
return;
}
void (async () => {
try {
const token = extractHttpBearerToken(req.header("authorization"));
if (!(await isBearerTokenValidAsync({ password, token }))) {
onReject?.({
path: req.path,
method: req.method,
hasToken: token !== null,
});
res.status(401).json({ error: "Unauthorized" });
return;
}
next();
} catch (error) {
next(error);
}
})();
};
}
export function shouldBypassBearerAuth(method: string, path: string): boolean {
if (method === "OPTIONS") {
return true;
}
// Public liveness/version endpoints used by local supervisors and health probes.
return path === "/api/health" || path === "/api/status";
}