@zitadel/sdk-core 1.0.0-alpha.24 → 1.0.0-alpha.28

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.
@@ -1,202 +0,0 @@
1
- /**
2
- * Shared constants and utility functions for the SDK middleware proxy layer.
3
- *
4
- * These are consumed by both `sdk-next` and `sdk-nuxt` to avoid duplicating
5
- * pure, framework-agnostic logic.
6
- */
7
- /**
8
- * Headers that must never be forwarded to an upstream service.
9
- * These are connection-level headers that are meaningful only between
10
- * two directly connected peers and become invalid when proxied.
11
- */
12
- export declare const HOP_BY_HOP: ReadonlySet<string>;
13
- /**
14
- * SDK-internal headers that must never be forwarded to the upstream backend.
15
- * These headers carry session data between the middleware and server components;
16
- * forwarding them upstream would expose internal state and allow header injection.
17
- */
18
- export declare const INTERNAL_HEADERS: ReadonlySet<string>;
19
- /**
20
- * Returns `true` when `pathname` matches at least one entry in `routes`.
21
- *
22
- * An entry ending with `*` matches any path that starts with the prefix
23
- * before the `*`. All other entries require an exact match.
24
- *
25
- * @param pathname - The URL pathname to test.
26
- * @param routes - The list of route patterns to match against.
27
- * @returns `true` if at least one pattern matches.
28
- */
29
- export declare function matchesRoutes(pathname: string, routes: readonly string[]): boolean;
30
- /**
31
- * Filters upstream response headers for proxying: strips hop-by-hop headers
32
- * and `set-cookie` (handled separately via `getSetCookie()`), and reduces
33
- * `location` to a redirect that can only land on this app.
34
- *
35
- * `location` was stripped outright until the identity-provider callback
36
- * needed it: the provider returns the browser to `/__nextgen/idp/callback`,
37
- * and the engine answers `302` back to the page the sign-in started on. That
38
- * is a top-level navigation with no JavaScript in the loop, so a dropped
39
- * `Location` leaves the browser on an empty page. Outbound redirects to a
40
- * provider do not travel this way -- they arrive as `step.redirect_url` in a
41
- * JSON body and the widget navigates -- so nothing legitimate points off-origin.
42
- *
43
- * @param upstream - The upstream response headers.
44
- * @param selfUrl - The app's own request URL, which is both the origin a
45
- * redirect must land on and the base a relative one resolves against.
46
- * Omitting it keeps the old behaviour of dropping every redirect.
47
- * @returns A new `Headers` object with filtered headers.
48
- */
49
- export declare function filterResponseHeaders(upstream: Headers, selfUrl?: string): Headers;
50
- /**
51
- * The authenticated session for a signed-in user. Identity follows the
52
- * user-ref vocabulary (ADR 058): render `display`, falling back to
53
- * `identifier`, then `userId`.
54
- */
55
- export type NextgenSession = {
56
- /** The user's unique identifier (`sub` claim). */
57
- userId: string;
58
- /**
59
- * The user schema's designated identifier value (the login identifier,
60
- * e.g. an email address or username), or `null` when unresolved. On the
61
- * JWT path this carries the `email` claim.
62
- */
63
- identifier: string | null;
64
- /**
65
- * The schema property `identifier` came from (e.g. `"email"`), or `null`
66
- * when unknown — JWT-claim identities carry no property attribution.
67
- */
68
- identifierProperty: string | null;
69
- /** The user's display name rendering, or `null` when the schema designates none. */
70
- display: string | null;
71
- /** The raw verified JWT. */
72
- token: string;
73
- };
74
- /** Auth state when the user is signed in. */
75
- export type AuthState = {
76
- isAuthenticated: true;
77
- session: NextgenSession;
78
- };
79
- /** Auth state when the user is not signed in. */
80
- export type UnauthState = {
81
- isAuthenticated: false;
82
- session: null;
83
- };
84
- /** Union of all possible auth states. */
85
- export type AuthResult = AuthState | UnauthState;
86
- /**
87
- * The client-safe session exposed to app UI (headers, account menus).
88
- * Identical to {@link NextgenSession} but omits `token` — the raw session
89
- * token must never reach client-side JavaScript, whether through an SSR
90
- * payload or a client-side fetch result.
91
- */
92
- export type ClientSession = {
93
- /** The user's unique identifier (`sub` claim). */
94
- userId: string;
95
- /** The designated identifier value (login identifier), or `null`. */
96
- identifier: string | null;
97
- /** The schema property the identifier came from, or `null`. */
98
- identifierProperty: string | null;
99
- /** The display name rendering, or `null`. */
100
- display: string | null;
101
- };
102
- /** Client-safe auth state when the user is signed in. */
103
- export type ClientAuthState = {
104
- isAuthenticated: true;
105
- session: ClientSession;
106
- };
107
- /**
108
- * Union of all possible client-safe auth states. Returned by the client
109
- * session reads (`useAuth()` in sdk-nuxt, `getSession()` in sdk-next).
110
- * Token is intentionally absent — use the server-side helpers when the raw
111
- * token is needed.
112
- */
113
- export type ClientAuthResult = ClientAuthState | UnauthState;
114
- /**
115
- * Options passed to the SDK middleware factory (`nextgenMiddleware` in
116
- * sdk-next, `createNextgenMiddleware` in sdk-nuxt).
117
- */
118
- export type NextgenMiddlewareOptions = {
119
- /**
120
- * Full URL of the Zitadel auth backend.
121
- * @default process.env.ZITADEL_URL ?? "http://localhost:8080"
122
- */
123
- url?: string;
124
- /**
125
- * URL path prefix that is reverse-proxied to the auth backend.
126
- * @default "/__nextgen"
127
- */
128
- proxyPath?: string;
129
- /**
130
- * Pathnames that require a valid session. Requests to these routes without
131
- * a valid token are redirected to {@link loginPath}.
132
- * Entries ending with `*` match any sub-path (e.g. `"/admin*"`).
133
- * @default []
134
- */
135
- protectedRoutes?: string[];
136
- /**
137
- * Pathnames that are completely skipped by the middleware — no JWT
138
- * verification, no protection check, no header tunnelling.
139
- * Useful for webhooks, health-check endpoints, or any route where
140
- * running auth logic is undesirable.
141
- * Entries ending with `*` match any sub-path (e.g. `"/public*"`).
142
- * @default []
143
- */
144
- ignoredRoutes?: string[];
145
- /**
146
- * Pathname to redirect unauthenticated users to.
147
- * A `next` query parameter is appended with the originally requested path.
148
- * @default "/login"
149
- */
150
- loginPath?: string;
151
- /**
152
- * Reserved for future use. Local PEM public key for offline JWT verification.
153
- */
154
- jwtKey?: string;
155
- /**
156
- * Restrict accepted JWT `alg` header values. Tokens whose algorithm is not
157
- * in this list are rejected before JWKS is fetched.
158
- * @default ["RS256", "ES256"]
159
- */
160
- allowedAlgorithms?: string[];
161
- /**
162
- * Clock skew tolerance in milliseconds applied to `exp`, `nbf`, and `iat`
163
- * claim validation.
164
- * @default 5000
165
- */
166
- clockSkewMs?: number;
167
- /**
168
- * Expected value(s) of the `aud` claim. When omitted, audience is not
169
- * validated. Pass a single string or an array when a token may carry
170
- * multiple audiences.
171
- */
172
- audience?: string | string[];
173
- /**
174
- * Accepted values for the JWT header `typ` claim (case-insensitive).
175
- * Tokens whose `typ` is not in this list are rejected.
176
- * Set to `[]` to disable token-type checking entirely.
177
- * @default ["JWT", "at+JWT"]
178
- */
179
- allowedTokenTypes?: string[];
180
- /**
181
- * Timeout in milliseconds for JWKS endpoint requests.
182
- * Requests that do not complete within this window are aborted and the
183
- * token is rejected, treating the request as unauthenticated.
184
- * @default 5000
185
- */
186
- jwksTimeoutMs?: number;
187
- /**
188
- * Timeout in milliseconds for upstream proxy requests.
189
- * Requests that do not complete within this window are aborted with a
190
- * network error. Defaults to 5 000 ms.
191
- * @default 5000
192
- */
193
- proxyTimeoutMs?: number;
194
- /**
195
- * Timeout in milliseconds for opaque (non-JWT) session token validation
196
- * via the backend's `GET /sessions/me` endpoint. Tokens that cannot be
197
- * validated within this window are treated as invalid.
198
- * @default 5000
199
- */
200
- opaqueTokenTimeoutMs?: number;
201
- };
202
- //# sourceMappingURL=middleware.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH;;;;GAIG;AACH,eAAO,MAAM,UAAU,EAAE,WAAW,CAAC,MAAM,CAazC,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,EAAE,WAAW,CAAC,MAAM,CAAqC,CAAC;AAIvF;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAUlF;AA2CD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAiBlF;AAID;;;;GAIG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,kDAAkD;IAClD,MAAM,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IA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SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;IAE7B;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAE7B;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;IAE7B;;;;;OAKG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;;;;OAKG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;CAC/B,CAAC"}
@@ -1,131 +0,0 @@
1
- /**
2
- * Shared constants and utility functions for the SDK middleware proxy layer.
3
- *
4
- * These are consumed by both `sdk-next` and `sdk-nuxt` to avoid duplicating
5
- * pure, framework-agnostic logic.
6
- */
7
- // ─── Constants ────────────────────────────────────────────────────────────────
8
- /**
9
- * Headers that must never be forwarded to an upstream service.
10
- * These are connection-level headers that are meaningful only between
11
- * two directly connected peers and become invalid when proxied.
12
- */
13
- export const HOP_BY_HOP = new Set([
14
- "connection",
15
- // host is not a hop-by-hop header per RFC 7230, but it must be stripped so
16
- // the fetch implementation derives the correct Host from the upstream URL
17
- // rather than forwarding the client's Host and causing SNI/vhost mismatches.
18
- "host",
19
- "keep-alive",
20
- "proxy-authenticate",
21
- "proxy-authorization",
22
- "te",
23
- "trailer",
24
- "transfer-encoding",
25
- "upgrade",
26
- ]);
27
- /**
28
- * SDK-internal headers that must never be forwarded to the upstream backend.
29
- * These headers carry session data between the middleware and server components;
30
- * forwarding them upstream would expose internal state and allow header injection.
31
- */
32
- export const INTERNAL_HEADERS = new Set(["x-nextgen-auth-token"]);
33
- // ─── Route matching ──────────────────────────────────────────────────────────
34
- /**
35
- * Returns `true` when `pathname` matches at least one entry in `routes`.
36
- *
37
- * An entry ending with `*` matches any path that starts with the prefix
38
- * before the `*`. All other entries require an exact match.
39
- *
40
- * @param pathname - The URL pathname to test.
41
- * @param routes - The list of route patterns to match against.
42
- * @returns `true` if at least one pattern matches.
43
- */
44
- export function matchesRoutes(pathname, routes) {
45
- if (routes.length === 0) {
46
- return false;
47
- }
48
- return routes.some((pattern) => {
49
- if (pattern.endsWith("*")) {
50
- return pathname.startsWith(pattern.slice(0, -1));
51
- }
52
- return pathname === pattern;
53
- });
54
- }
55
- // ─── Response header filtering ───────────────────────────────────────────────
56
- /**
57
- * An upstream `Location`, pointed back at this app.
58
- *
59
- * Only the path, query and fragment survive; whatever origin the upstream
60
- * named is discarded and the result is re-based onto the request's own URL.
61
- * That keeps both properties the blanket strip was protecting -- an internal
62
- * hostname can never reach the browser, and the redirect can only ever land on
63
- * this app -- without needing to know what this app's public origin is.
64
- *
65
- * Comparing origins instead would have required that knowledge, and there is
66
- * no reliable source for it: `X-Forwarded-Host` is attacker-settable on a
67
- * direct request, and the request's own host is wrong behind a proxy that
68
- * terminates TLS or rewrites it. Discarding the origin sidesteps the question
69
- * -- a redirect to another origin becomes the same path on this one rather
70
- * than being dropped, so a deployment behind such a proxy works instead of
71
- * landing on a blank page.
72
- *
73
- * Nothing legitimate on this path points off-origin anyway: an outbound
74
- * redirect to a provider travels as `step.redirect_url` in a JSON body and the
75
- * widget performs it, never as a proxied header.
76
- *
77
- * A relative reference resolves against the path it came from (RFC 3986 §5),
78
- * so `next?flow=x` answered to `/__nextgen/idp/callback` means
79
- * `/__nextgen/idp/next?flow=x`. An unparseable value is the one case with
80
- * nothing to forward.
81
- */
82
- function ontoThisApp(location, selfUrl) {
83
- if (selfUrl === undefined || selfUrl === "") {
84
- return undefined;
85
- }
86
- try {
87
- const base = new URL(selfUrl);
88
- const target = new URL(location, base);
89
- return new URL(`${target.pathname}${target.search}${target.hash}`, base).toString();
90
- }
91
- catch {
92
- return undefined;
93
- }
94
- }
95
- /**
96
- * Filters upstream response headers for proxying: strips hop-by-hop headers
97
- * and `set-cookie` (handled separately via `getSetCookie()`), and reduces
98
- * `location` to a redirect that can only land on this app.
99
- *
100
- * `location` was stripped outright until the identity-provider callback
101
- * needed it: the provider returns the browser to `/__nextgen/idp/callback`,
102
- * and the engine answers `302` back to the page the sign-in started on. That
103
- * is a top-level navigation with no JavaScript in the loop, so a dropped
104
- * `Location` leaves the browser on an empty page. Outbound redirects to a
105
- * provider do not travel this way -- they arrive as `step.redirect_url` in a
106
- * JSON body and the widget navigates -- so nothing legitimate points off-origin.
107
- *
108
- * @param upstream - The upstream response headers.
109
- * @param selfUrl - The app's own request URL, which is both the origin a
110
- * redirect must land on and the base a relative one resolves against.
111
- * Omitting it keeps the old behaviour of dropping every redirect.
112
- * @returns A new `Headers` object with filtered headers.
113
- */
114
- export function filterResponseHeaders(upstream, selfUrl) {
115
- const filtered = new Headers();
116
- upstream.forEach((value, key) => {
117
- const name = key.toLowerCase();
118
- if (HOP_BY_HOP.has(name) || name === "set-cookie") {
119
- return;
120
- }
121
- if (name === "location") {
122
- const onThisApp = ontoThisApp(value, selfUrl);
123
- if (onThisApp !== undefined) {
124
- filtered.set("location", onThisApp);
125
- }
126
- return;
127
- }
128
- filtered.set(key, value);
129
- });
130
- return filtered;
131
- }