Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/apps/web/lib/hosts.server.ts
blob: 758f834348764ab895a3d52e9364470485340dab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1

import "server-only";
import { headers } from "next/headers";

/** The hosts of Progmasoft OAuth and the account site they link to. */
export type ProgmasoftSite =
  | "authorize"
  | "credentials"
  | "console"
  | "account";

/** The two sites whose pages share the OAuth frame. */
export type OAuthSite = "authorize" | "console";

/**
 * The origin of a sibling host of the current request.
 *
 * authorize.progmasoft.com, credentials.progmasoft.com and
 * console.progmasoft.com link to each other. On a development host
 * (authorize.localhost:3010) the sibling keeps the scheme and the port, so the
 * sites work side by side locally.
 */
export async function siteOrigin(site: ProgmasoftSite): Promise<string> {
  const requestHeaders = await headers();
  const host =
    requestHeaders.get("x-forwarded-host") ?? requestHeaders.get("host") ?? "";
  const [name = "", port] = host.toLowerCase().split(":", 2);
  if (name.endsWith(".localhost")) {
    const protocol = requestHeaders.get("x-forwarded-proto") ?? "http";
    return `${protocol}://${site}.localhost${port ? `:${port}` : ""}`;
  }
  return `https://${site}.progmasoft.com`;
}