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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import {
isLocale,
localeCookieName,
localeCookieOptions,
requestHost,
} from "@/lib/localization";
import { authorizeRoute, consoleRoute, issuerRoute } from "@/lib/credentials";
import { catch3Route, idesRoute, productRoute } from "@/lib/sites";
import {
contentSecurityPolicy,
createNonce,
nonceHeader,
} from "@/lib/content-security-policy";
const accountHosts = new Set(["account.progmasoft.com", "account.localhost"]);
const vigetHosts = new Set(["viget.progmasoft.com", "viget.localhost"]);
interface OwnSite {
hosts: Set<string>;
/** The internal tree of the site's pages. */
root: string;
route: (pathname: string) => string | null;
/** Whether /api reaches the account API on this host. */
api: boolean;
}
function hostsOf(name: string): Set<string> {
return new Set([`${name}.progmasoft.com`, `${name}.localhost`]);
}
const ownSites: OwnSite[] = [
{
hosts: hostsOf("authorize"),
root: "/authorize",
route: authorizeRoute,
api: true,
},
{
hosts: hostsOf("credentials"),
root: "/issuer",
route: issuerRoute,
api: true,
},
{
hosts: hostsOf("console"),
root: "/console",
route: consoleRoute,
api: true,
},
{
hosts: hostsOf("product"),
root: "/products",
route: productRoute,
api: false,
},
{ hosts: hostsOf("ides"), root: "/ides", route: idesRoute, api: false },
{ hosts: hostsOf("catch3"), root: "/catch3", route: catch3Route, api: false },
];
export function proxy(request: NextRequest) {
// Every page gets its own nonce. Next reads the policy from the request to
// mark its scripts, and the layout reads the nonce for the theme bootstrap.
const nonce = createNonce();
const policy = contentSecurityPolicy(
nonce,
process.env.NODE_ENV === "development",
);
const headers = new Headers(request.headers);
headers.set(nonceHeader, nonce);
headers.set("Content-Security-Policy", policy);
const response = route(request, headers);
response.headers.set("Content-Security-Policy", policy);
return response;
}
/**
* Where a request goes on its host.
*
* @param request The request.
* @param headers The request headers the page renders with.
* @returns The response.
*/
function route(request: NextRequest, headers: Headers): NextResponse {
const host = requestHost(
request.headers.get("x-forwarded-host"),
request.headers.get("host"),
);
const pathname = request.nextUrl.pathname;
// Language selection is a user preference, not a second canonical URL.
// Consume the query parameter once, persist it across Progmasoft hosts, and
// redirect to the clean URL so crawlers never index preference variants.
// The header control switches in place through a server action; this
// parameter remains for links and for browsers that run no script.
const requestedLocale = request.nextUrl.searchParams.get("lang") ?? undefined;
if (isLocale(requestedLocale)) {
const destination = request.nextUrl.clone();
destination.searchParams.delete("lang");
const forwardedProtocol = request.headers.get("x-forwarded-proto");
const isProductionHost =
host === "progmasoft.com" || host.endsWith(".progmasoft.com");
destination.protocol = isProductionHost
? "https:"
: request.nextUrl.protocol;
const response = NextResponse.redirect(destination);
response.cookies.set(
localeCookieName,
requestedLocale,
localeCookieOptions(
host,
forwardedProtocol === "https" || request.nextUrl.protocol === "https:",
),
);
return response;
}
// Hosts with pages of their own: Progmasoft OAuth, the Progmasoft Console,
// the product catalog and the product sites. Each serves its own tree and
// nothing else of the site, so the account pages keep their own host and
// cookies.
const site = ownSites.find((candidate) => candidate.hosts.has(host));
if (site) {
if (site.api && pathname.startsWith("/api/")) {
return NextResponse.next({ request: { headers } });
}
// The standalone server runs this proxy a second time for the rewritten
// path, with the same host: a path already in the site's tree is final.
if (pathname === site.root || pathname.startsWith(`${site.root}/`)) {
return NextResponse.next({ request: { headers } });
}
const destination = internalRoute(
request,
site.route(pathname) ?? `${site.root}/not-found`,
);
// The sign-in page reads ReturnUrl and the consent page every parameter
// of the authorization request.
destination.search = request.nextUrl.search;
return rewrite(headers, destination);
}
// The internal trees belong to their hosts only.
for (const { root } of ownSites) {
if (pathname === root || pathname.startsWith(`${root}/`)) {
return rewrite(headers, internalRoute(request, "/not-found"));
}
}
// The account root has its own landing page, but authentication and dashboard URLs remain stable and readable.
if (accountHosts.has(host) && pathname === "/") {
const destination = new URL("/account", request.url);
// TLS terminates at Nginx. Next derives the public scheme from X-Forwarded-Proto,
// so an unmodified absolute rewrite would attempt TLS against its HTTP-only
// loopback listener and fail before the account page can render.
destination.protocol = "http:";
return rewrite(headers, destination);
}
// ViGet is deployed by Progmaweb but keeps short public catalog URLs. The
// internal /viget tree prevents those routes from colliding with the apex
// organization page and the account surface.
if (vigetHosts.has(host)) {
if (pathname === "/") {
return rewrite(headers, internalRoute(request, "/viget"));
}
// Packages are grouped by the language they are written in and DSL
// plugins by the build system they extend. A group without a catalog of
// its own leads to the one catalog there is.
if (pathname === "/langs/tala" || pathname === "/langs/tala/") {
return rewrite(headers, internalRoute(request, "/viget/langs/tala"));
}
if (pathname === "/dslplugins/tazel" || pathname === "/dslplugins/tazel/") {
return rewrite(
headers,
internalRoute(request, "/viget/dslplugins/tazel"),
);
}
// The page of one plugin. Its index.json and JARs below it are served by
// Nginx, from the API and from the files of ViGet.
const plugin =
/^\/dslplugins\/tazel\/([A-Za-z_][A-Za-z0-9_]*)\/([A-Za-z_][A-Za-z0-9_]*)\/?$/.exec(
pathname,
);
if (plugin) {
return rewrite(
headers,
internalRoute(
request,
`/viget/dslplugins/tazel/${plugin[1]}/${plugin[2]}`,
),
);
}
if (
pathname === "/dslplugins/gradle" ||
pathname === "/dslplugins/gradle/"
) {
return rewrite(
headers,
internalRoute(request, "/viget/dslplugins/gradle"),
);
}
if (pathname === "/langs" || pathname === "/langs/") {
return NextResponse.redirect(
new URL("https://viget.progmasoft.com/langs/tala"),
308,
);
}
if (pathname === "/dslplugins" || pathname === "/dslplugins/") {
return NextResponse.redirect(
new URL("https://viget.progmasoft.com/dslplugins/tazel"),
308,
);
}
if (pathname === "/login" || pathname === "/login/") {
return NextResponse.redirect(
new URL("https://account.progmasoft.com/login"),
);
}
if (pathname === "/register" || pathname === "/register/") {
return NextResponse.redirect(
new URL("https://account.progmasoft.com/register"),
);
}
if (pathname === "/recover" || pathname === "/recover/") {
return NextResponse.redirect(
new URL("https://account.progmasoft.com/recover"),
);
}
}
if (
!vigetHosts.has(host) &&
(pathname === "/viget" || pathname.startsWith("/viget/"))
) {
const publicPath = pathname.replace(/^\/viget/, "") || "/";
return NextResponse.redirect(
new URL(publicPath, "https://viget.progmasoft.com"),
);
}
// Corporate hosts must not accidentally expose the account-only landing page as a public navigation destination.
if (!accountHosts.has(host) && pathname === "/account") {
return NextResponse.redirect(new URL("https://account.progmasoft.com/"));
}
return NextResponse.next({ request: { headers } });
}
function rewrite(headers: Headers, destination: URL): NextResponse {
return NextResponse.rewrite(destination, { request: { headers } });
}
function internalRoute(request: NextRequest, pathname: string): URL {
const destination = new URL(pathname, request.url);
destination.protocol = "http:";
return destination;
}
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico|icon.svg|apple-icon.png|robots.txt|sitemap.xml).*)",
],
};
|