Files
paseo/packages/server/src/server/auth.test.ts
T
Mohamed Boudraandgithub-actions[bot] 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

56 lines
2.0 KiB
TypeScript

import { describe, expect, test } from "vitest";
import {
extractHttpBearerToken,
extractWsBearerProtocol,
extractWsBearerToken,
hashDaemonPassword,
isBearerTokenValidAsync,
isBearerTokenValid,
} from "./auth.js";
const CORRECT_PASSWORD_HASH = "$2b$12$OLxyuuP9uLK30Uzc4wQX0O6liuU/Q1t5P2b0Ebf36mULvpVK3DRZW";
describe("daemon bearer validator", () => {
test("allows any token when no password is configured", () => {
expect(isBearerTokenValid({ password: undefined, token: null })).toBe(true);
expect(isBearerTokenValid({ password: undefined, token: "anything" })).toBe(true);
});
test("accepts the plaintext token against the bcrypt hash and rejects missing or wrong tokens", async () => {
expect(
await isBearerTokenValidAsync({ password: CORRECT_PASSWORD_HASH, token: "correct-password" }),
).toBe(true);
expect(isBearerTokenValid({ password: CORRECT_PASSWORD_HASH, token: "correct-password" })).toBe(
true,
);
expect(await isBearerTokenValidAsync({ password: CORRECT_PASSWORD_HASH, token: null })).toBe(
false,
);
expect(await isBearerTokenValidAsync({ password: CORRECT_PASSWORD_HASH, token: "wrong" })).toBe(
false,
);
});
test("hashes a password into a bcrypt value", () => {
const hash = hashDaemonPassword("correct-password");
expect(hash).toMatch(/^\$2[aby]\$12\$/);
expect(isBearerTokenValid({ password: hash, token: "correct-password" })).toBe(true);
});
test("extracts HTTP bearer tokens", () => {
expect(extractHttpBearerToken("Bearer secret")).toBe("secret");
expect(extractHttpBearerToken("Basic secret")).toBeNull();
expect(extractHttpBearerToken(undefined)).toBeNull();
});
test("extracts WebSocket paseo bearer subprotocol tokens", () => {
const protocol = extractWsBearerProtocol("chat, paseo.bearer.secret.with.dots");
expect(protocol).toBe("paseo.bearer.secret.with.dots");
expect(extractWsBearerToken(protocol)).toBe("secret.with.dots");
expect(extractWsBearerToken("paseo.other.secret")).toBeNull();
});
});