@dbx-tools/tunnel 0.9.132 → 0.9.136

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/login-page.ts CHANGED
@@ -14,6 +14,8 @@ import { LOGIN_CLIENT_SOURCE } from "./generated/_login-client.ts";
14
14
  export interface LoginPageOptions {
15
15
  /** Product/brand name shown in the heading. */
16
16
  brandName: string;
17
+ /** Prefix added before the humanized tunnel subdomain in a new passkey name. */
18
+ passkeyNamePrefix?: string;
17
19
  /** Validated same-origin path loaded after authentication succeeds. */
18
20
  returnTo: string;
19
21
  }
@@ -21,6 +23,7 @@ export interface LoginPageOptions {
21
23
  /** The login page HTML for a `text/html` request to a gated path with no session. */
22
24
  export function loginPageHtml(options: LoginPageOptions): string {
23
25
  const brand = stringUtils.escapeHtml(options.brandName);
26
+ const passkeyNamePrefix = stringUtils.escapeHtml(options.passkeyNamePrefix ?? "");
24
27
  const returnTo = stringUtils.escapeHtml(options.returnTo);
25
28
  // prettier-ignore
26
29
  return stringUtils.dedent(
@@ -63,6 +66,13 @@ export function loginPageHtml(options: LoginPageOptions): string {
63
66
  border: 1px solid var(--border); background: var(--field); color: var(--fg);
64
67
  }
65
68
  input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
69
+ .choice {
70
+ display: flex; align-items: center; gap: 8px; margin: 12px 0 0;
71
+ color: var(--fg); cursor: pointer;
72
+ }
73
+ .choice[hidden] { display: none; }
74
+ .choice input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); }
75
+ .choice span { font-size: 13px; }
66
76
  button {
67
77
  width: 100%; margin-top: 16px; padding: 10px 12px; font-size: 15px;
68
78
  font-weight: 600; border: 0; border-radius: 9px; background: var(--accent);
@@ -77,7 +87,7 @@ export function loginPageHtml(options: LoginPageOptions): string {
77
87
  .back { background: none; color: var(--muted); font-weight: 400; margin-top: 8px; }
78
88
  </style>
79
89
  </head>
80
- <body data-return-to="${returnTo}" data-auth-base="${AUTH_BASE_PATH}">
90
+ <body data-return-to="${returnTo}" data-auth-base="${AUTH_BASE_PATH}" data-passkey-name-prefix="${passkeyNamePrefix}">
81
91
  <main class="card">
82
92
  <svg class="logo" viewBox="0 0 64 64" role="img" aria-label="${brand}" shape-rendering="crispEdges">
83
93
  <rect x="19" y="20" width="8" height="8" rx="1" fill="#FF3621"/><rect x="37" y="20" width="8" height="8" rx="1" fill="#FF3621"/>
@@ -97,6 +107,10 @@ export function loginPageHtml(options: LoginPageOptions): string {
97
107
  <form id="code-form" class="hidden">
98
108
  <label for="code">Verification code</label>
99
109
  <input id="code" name="code" inputmode="numeric" autocomplete="one-time-code" required>
110
+ <label id="create-passkey-option" class="choice" for="create-passkey" hidden>
111
+ <input id="create-passkey" name="create-passkey" type="checkbox" checked>
112
+ <span>Create a passkey for this tunnel host</span>
113
+ </label>
100
114
  <button id="code-submit" type="submit">Verify</button>
101
115
  <button id="back" type="button" class="back">Use a different email</button>
102
116
  </form>
package/src/plugin.ts CHANGED
@@ -33,7 +33,7 @@ import {
33
33
  type AuthStorageMode,
34
34
  type PasswordlessAuthRuntime,
35
35
  } from "@dbx-tools/auth-gate";
36
- import { configUtils } from "@dbx-tools/core";
36
+ import { configUtils, projectUtils } from "@dbx-tools/core";
37
37
  import { AUTH_BASE_PATH } from "@dbx-tools/shared-auth";
38
38
  import { brandUtils, log, stringUtils } from "@dbx-tools/shared-core";
39
39
  import type { RequestHandler } from "express";
@@ -99,6 +99,7 @@ export const AuthGateConfigSchema = authStorage.AuthStorageConfigSchema.extend({
99
99
  allow: textOrList("Email allow-list patterns."),
100
100
  subject: text("Verification email subject."),
101
101
  brandName: text("Verification email brand name."),
102
+ passkeyNamePrefix: text("Prefix for humanized tunnel passkey names."),
102
103
  message: text("Verification email message."),
103
104
  sessionTtlSeconds: z.coerce
104
105
  .number<number>()
@@ -147,6 +148,7 @@ export interface ResolvedAuthGateConfig {
147
148
  allow: string[];
148
149
  subject: string;
149
150
  brandName: string;
151
+ passkeyNamePrefix: string;
150
152
  message: string;
151
153
  sessionTtlSeconds: number;
152
154
  codeTtlSeconds: number;
@@ -199,6 +201,9 @@ export function resolveAuthGateConfig(config: AuthGateConfig): ResolvedAuthGateC
199
201
  TUNNEL_CONFIG,
200
202
  );
201
203
  const frpPublicDomain = configUtils.text("TUNNEL_FRP_PUBLIC_DOMAIN", TUNNEL_CONFIG);
204
+ const brandName =
205
+ configUtils.string(config.brandName, "TUNNEL_AUTH_BRAND_NAME", TUNNEL_CONFIG) ??
206
+ DEFAULTS.brandName;
202
207
  return {
203
208
  // Both sources are unioned rather than one overriding: a deployment-wide
204
209
  // TUNNEL_AUTH_ALLOW and a per-invocation `--allow` should both grant access.
@@ -210,9 +215,13 @@ export function resolveAuthGateConfig(config: AuthGateConfig): ResolvedAuthGateC
210
215
  ],
211
216
  subject:
212
217
  configUtils.string(config.subject, "TUNNEL_AUTH_SUBJECT", TUNNEL_CONFIG) ?? DEFAULTS.subject,
213
- brandName:
214
- configUtils.string(config.brandName, "TUNNEL_AUTH_BRAND_NAME", TUNNEL_CONFIG) ??
215
- DEFAULTS.brandName,
218
+ brandName,
219
+ passkeyNamePrefix:
220
+ configUtils.string(
221
+ config.passkeyNamePrefix,
222
+ "TUNNEL_AUTH_PASSKEY_NAME_PREFIX",
223
+ TUNNEL_CONFIG,
224
+ ) ?? projectUtils.name(),
216
225
  message:
217
226
  configUtils.string(config.message, "TUNNEL_AUTH_MESSAGE", TUNNEL_CONFIG) ?? DEFAULTS.message,
218
227
  sessionTtlSeconds: configUtils.positiveInt(
@@ -335,13 +344,14 @@ export class AuthGatePlugin extends Plugin<AuthGateConfig> {
335
344
  for (const origin of new Set(origins)) {
336
345
  const storage = await authStorage.createAuthStorage(this.resolved, pool);
337
346
  this.runtimes.set(
338
- new URL(origin).host.toLowerCase(),
347
+ new URL(origin).hostname.toLowerCase(),
339
348
  await passwordlessAuth.createPasswordlessAuth({
340
349
  storage,
341
350
  baseURL: origin,
342
351
  trustedOrigins: [origin],
343
352
  basePath: AUTH_BASE_PATH,
344
353
  appName: this.resolved.brandName,
354
+ passkeyNamePrefix: this.resolved.passkeyNamePrefix,
345
355
  secret: Buffer.from(key).toString("base64url"),
346
356
  sessionTtlSeconds: this.resolved.sessionTtlSeconds,
347
357
  sessionCutoffMs: this.resolved.sessionCutoffMs,
@@ -400,6 +410,7 @@ export class AuthGatePlugin extends Plugin<AuthGateConfig> {
400
410
  forwardHeaders: this.resolved.forwardHeaders,
401
411
  gatePaths: this.resolved.gatePaths,
402
412
  brandName: this.resolved.brandName,
413
+ passkeyNamePrefix: this.resolved.passkeyNamePrefix,
403
414
  });
404
415
  }
405
416
 
@@ -415,15 +426,16 @@ export class AuthGatePlugin extends Plugin<AuthGateConfig> {
415
426
 
416
427
  override exports(): AuthGateApi {
417
428
  const runtime = (headers?: Headers, request?: Request) => {
418
- const host = (
419
- headers?.get("host") ?? (request ? new URL(request.url).host : "")
420
- ).toLowerCase();
421
- return this.runtimes.get(host) ?? this.runtimes.values().next().value;
429
+ const hostname = _authHostname(headers, request);
430
+ return hostname ? this.runtimes.get(hostname) : this.runtimes.values().next().value;
422
431
  };
423
432
  return {
424
433
  basePath: AUTH_BASE_PATH,
425
434
  passkeysEnabled: [...this.runtimes.values()].some((entry) => entry.passkeysEnabled),
426
- isProtectedHost: (headers) => this.runtimes.has((headers.get("host") ?? "").toLowerCase()),
435
+ isProtectedHost: (headers) => {
436
+ const hostname = _authHostname(headers);
437
+ return hostname ? this.runtimes.has(hostname) : false;
438
+ },
427
439
  handler: (request) =>
428
440
  runtime(request.headers, request)?.handler(request) ??
429
441
  Promise.resolve(new Response("Not Found", { status: 404 })),
@@ -436,6 +448,17 @@ export class AuthGatePlugin extends Plugin<AuthGateConfig> {
436
448
  }
437
449
  }
438
450
 
451
+ /** Resolve an exact request hostname without widening it to a parent domain. */
452
+ function _authHostname(headers?: Headers, request?: Request): string | undefined {
453
+ const host = headers?.get("host")?.trim();
454
+ try {
455
+ if (host) return new URL(`https://${host}`).hostname.toLowerCase();
456
+ return request ? new URL(request.url).hostname.toLowerCase() : undefined;
457
+ } catch {
458
+ return undefined;
459
+ }
460
+ }
461
+
439
462
  function authOrigin(publicDomain?: string): string {
440
463
  const value = stringUtils.trimToNull(publicDomain);
441
464
  if (!value) return "http://localhost";