@zitadel/sdk-nuxt 0.0.0 → 0.1.0-alpha.2

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.
@@ -64,6 +64,7 @@ function createNextgenMiddleware(options = {}) {
64
64
  allowedTokenTypes = ["JWT", "at+JWT"],
65
65
  jwksTimeoutMs,
66
66
  proxyTimeoutMs = 5e3,
67
+ opaqueTokenTimeoutMs = 5e3,
67
68
  onExchangeResponse
68
69
  } = options;
69
70
  if (!loginPath.startsWith("/") || loginPath.startsWith("//")) {
@@ -97,6 +98,7 @@ function createNextgenMiddleware(options = {}) {
97
98
  audience,
98
99
  allowedTokenTypes,
99
100
  jwksTimeoutMs,
101
+ opaqueTokenTimeoutMs,
100
102
  pathname
101
103
  });
102
104
  });
@@ -108,9 +110,17 @@ async function proxyRequest(event, authUrl, proxyPath, url, proxyTimeoutMs, onEx
108
110
  const hasBody = !["GET", "HEAD"].includes(method);
109
111
  const rawBody = hasBody ? await readRawBody(event, false) : void 0;
110
112
  const body = rawBody != null ? new Uint8Array(rawBody) : void 0;
113
+ const upstreamHeaders = buildUpstreamHeaders(event);
114
+ const isExchangeRequest = method === "POST" && suffix.startsWith("/sessions/exchange");
115
+ if (isExchangeRequest && !upstreamHeaders.has("authorization")) {
116
+ const projectId = url.searchParams.get("project_id");
117
+ if (projectId) {
118
+ upstreamHeaders.set("authorization", `Bearer sk_${projectId}`);
119
+ }
120
+ }
111
121
  const upstream = await fetch(target, {
112
122
  method,
113
- headers: buildUpstreamHeaders(event),
123
+ headers: upstreamHeaders,
114
124
  body,
115
125
  redirect: "manual",
116
126
  signal: AbortSignal.timeout(proxyTimeoutMs)
@@ -139,6 +149,33 @@ async function proxyRequest(event, authUrl, proxyPath, url, proxyTimeoutMs, onEx
139
149
  }
140
150
  return response.body;
141
151
  }
152
+ var DECODER = new TextDecoder();
153
+ function isJwtShaped(token) {
154
+ const parts = token.split(".");
155
+ if (parts.length < 3 || !parts[0]) return false;
156
+ try {
157
+ const header = JSON.parse(
158
+ DECODER.decode(base64UrlDecode(parts[0]))
159
+ );
160
+ return typeof header?.alg === "string" && !("enc" in header);
161
+ } catch {
162
+ return false;
163
+ }
164
+ }
165
+ async function validateOpaqueSessionToken(token, issuerUrl, timeoutMs) {
166
+ try {
167
+ const res = await fetch(`${issuerUrl}/sessions/me`, {
168
+ method: "GET",
169
+ headers: { cookie: `__nextgen_session=${token}` },
170
+ signal: AbortSignal.timeout(timeoutMs)
171
+ });
172
+ if (!res.ok) return null;
173
+ const body = await res.json();
174
+ return { userId: body.user_id };
175
+ } catch {
176
+ return null;
177
+ }
178
+ }
142
179
  async function handleAuth(event, opts) {
143
180
  const {
144
181
  url,
@@ -149,6 +186,7 @@ async function handleAuth(event, opts) {
149
186
  audience,
150
187
  allowedTokenTypes,
151
188
  jwksTimeoutMs,
189
+ opaqueTokenTimeoutMs,
152
190
  pathname
153
191
  } = opts;
154
192
  delete event.node.req.headers["x-nextgen-auth-token"];
@@ -176,6 +214,25 @@ async function handleAuth(event, opts) {
176
214
  };
177
215
  return;
178
216
  }
217
+ if (!payload && cookieToken && !isJwtShaped(cookieToken)) {
218
+ const opaqueResult = await validateOpaqueSessionToken(
219
+ cookieToken,
220
+ url,
221
+ opaqueTokenTimeoutMs
222
+ );
223
+ if (opaqueResult) {
224
+ event.context.nextgenAuth = {
225
+ isAuthenticated: true,
226
+ session: {
227
+ userId: opaqueResult.userId ?? "unknown",
228
+ email: null,
229
+ name: null,
230
+ token: cookieToken
231
+ }
232
+ };
233
+ return;
234
+ }
235
+ }
179
236
  event.context.nextgenAuth = { isAuthenticated: false, session: null };
180
237
  for (const name of Object.keys(parseCookies(event))) {
181
238
  if (name.startsWith("__nextgen")) {
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import "./chunk-ZNXAFGLP.js";
2
2
  import {
3
3
  createNextgenMiddleware,
4
4
  getAuth
5
- } from "./chunk-J2P7YNGG.js";
5
+ } from "./chunk-E7GMYXBM.js";
6
6
 
7
7
  // src/index.ts
8
8
  import { ZitadelLogin, ZitadelLogout } from "@zitadel/components";
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  createNextgenMiddleware
3
- } from "../../chunk-J2P7YNGG.js";
3
+ } from "../../chunk-E7GMYXBM.js";
4
4
 
5
5
  // src/runtime/server/handler.ts
6
6
  var config = useRuntimeConfig();
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  createNextgenMiddleware,
3
3
  getAuth
4
- } from "../../chunk-J2P7YNGG.js";
4
+ } from "../../chunk-E7GMYXBM.js";
5
5
  export {
6
6
  createNextgenMiddleware,
7
7
  getAuth
package/dist/server.js CHANGED
@@ -2,7 +2,7 @@ import "./chunk-ZNXAFGLP.js";
2
2
  import {
3
3
  createNextgenMiddleware,
4
4
  getAuth
5
- } from "./chunk-J2P7YNGG.js";
5
+ } from "./chunk-E7GMYXBM.js";
6
6
  export {
7
7
  createNextgenMiddleware,
8
8
  getAuth
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zitadel/sdk-nuxt",
3
- "version": "0.0.0",
3
+ "version": "0.1.0-alpha.2",
4
4
  "description": "Nuxt proxy middleware and helpers for Nextgen Auth",
5
5
  "homepage": "https://github.com/zitadel/nextgen/tree/main/packages/sdk-nuxt#readme",
6
6
  "bugs": {
@@ -33,9 +33,9 @@
33
33
  "access": "public"
34
34
  },
35
35
  "dependencies": {
36
- "@zitadel/api": "0.0.0",
37
- "@zitadel/components": "0.0.0",
38
- "@zitadel/sdk-core": "0.0.0"
36
+ "@zitadel/api": "0.1.0-alpha.2",
37
+ "@zitadel/components": "0.1.0-alpha.2",
38
+ "@zitadel/sdk-core": "0.1.0-alpha.2"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "h3": ">=1",