@zitadel/sdk-nuxt 0.1.0-alpha.10 → 0.1.0-alpha.12

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,4 +1,5 @@
1
1
  // src/runtime/server/middleware.ts
2
+ import { useRuntimeConfig } from "#imports";
2
3
  import {
3
4
  HOP_BY_HOP,
4
5
  INTERNAL_HEADERS,
@@ -17,12 +18,7 @@ import {
17
18
  } from "h3";
18
19
 
19
20
  // src/runtime/lib/jwt.ts
20
- import {
21
- JWKS_TTL_MS,
22
- base64UrlDecode,
23
- decodeJwt,
24
- verifyJwt
25
- } from "@zitadel/sdk-core/jwt";
21
+ import { JWKS_TTL_MS, base64UrlDecode, decodeJwt, verifyJwt } from "@zitadel/sdk-core/jwt";
26
22
 
27
23
  // src/runtime/server/middleware.ts
28
24
  function buildUpstreamHeaders(event) {
@@ -38,10 +34,7 @@ function buildUpstreamHeaders(event) {
38
34
  const socketIp = event.node.req.socket?.remoteAddress;
39
35
  if (socketIp) {
40
36
  const existingXff = headers.get("x-forwarded-for");
41
- headers.set(
42
- "x-forwarded-for",
43
- existingXff ? `${existingXff}, ${socketIp}` : socketIp
44
- );
37
+ headers.set("x-forwarded-for", existingXff ? `${existingXff}, ${socketIp}` : socketIp);
45
38
  }
46
39
  if (!headers.has("x-forwarded-host")) {
47
40
  headers.set("x-forwarded-host", url.host);
@@ -64,8 +57,7 @@ function createNextgenMiddleware(options = {}) {
64
57
  allowedTokenTypes = ["JWT", "at+JWT"],
65
58
  jwksTimeoutMs,
66
59
  proxyTimeoutMs = 5e3,
67
- opaqueTokenTimeoutMs = 5e3,
68
- onExchangeResponse
60
+ opaqueTokenTimeoutMs = 5e3
69
61
  } = options;
70
62
  if (!loginPath.startsWith("/") || loginPath.startsWith("//")) {
71
63
  throw new Error(
@@ -80,14 +72,7 @@ function createNextgenMiddleware(options = {}) {
80
72
  return;
81
73
  }
82
74
  if (pathname === proxyPath || pathname.startsWith(`${proxyPath}/`)) {
83
- return proxyRequest(
84
- event,
85
- url,
86
- proxyPath,
87
- urlObj,
88
- proxyTimeoutMs,
89
- onExchangeResponse
90
- );
75
+ return proxyRequest(event, url, proxyPath, urlObj, proxyTimeoutMs);
91
76
  }
92
77
  return handleAuth(event, {
93
78
  url,
@@ -103,7 +88,7 @@ function createNextgenMiddleware(options = {}) {
103
88
  });
104
89
  });
105
90
  }
106
- async function proxyRequest(event, authUrl, proxyPath, url, proxyTimeoutMs, onExchangeResponse) {
91
+ async function proxyRequest(event, authUrl, proxyPath, url, proxyTimeoutMs) {
107
92
  const suffix = url.pathname.slice(proxyPath.length);
108
93
  const target = `${authUrl}${suffix}${url.search}`;
109
94
  const method = event.node.req.method ?? "GET";
@@ -111,11 +96,11 @@ async function proxyRequest(event, authUrl, proxyPath, url, proxyTimeoutMs, onEx
111
96
  const rawBody = hasBody ? await readRawBody(event, false) : void 0;
112
97
  const body = rawBody != null ? new Uint8Array(rawBody) : void 0;
113
98
  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}`);
99
+ if (!upstreamHeaders.has("authorization")) {
100
+ const config = useRuntimeConfig();
101
+ const projectSecret = config.nextgen?.projectSecret;
102
+ if (projectSecret) {
103
+ upstreamHeaders.set("authorization", `Bearer ${projectSecret}`);
119
104
  }
120
105
  }
121
106
  const upstream = await fetch(target, {
@@ -130,14 +115,10 @@ async function proxyRequest(event, authUrl, proxyPath, url, proxyTimeoutMs, onEx
130
115
  for (const cookie of setCookieHeaders) {
131
116
  responseHeaders.append("set-cookie", cookie);
132
117
  }
133
- let response = new Response(upstream.body, {
118
+ const response = new Response(upstream.body, {
134
119
  status: upstream.status,
135
120
  headers: responseHeaders
136
121
  });
137
- const isExchange = method === "POST" && suffix.startsWith("/sessions/exchange");
138
- if (isExchange && onExchangeResponse) {
139
- response = await onExchangeResponse(response);
140
- }
141
122
  event.node.res.statusCode = response.status;
142
123
  for (const [key, value] of response.headers.entries()) {
143
124
  if (key.toLowerCase() === "set-cookie") continue;
@@ -154,9 +135,7 @@ function isJwtShaped(token) {
154
135
  const parts = token.split(".");
155
136
  if (parts.length < 3 || !parts[0]) return false;
156
137
  try {
157
- const header = JSON.parse(
158
- DECODER.decode(base64UrlDecode(parts[0]))
159
- );
138
+ const header = JSON.parse(DECODER.decode(base64UrlDecode(parts[0])));
160
139
  return typeof header?.alg === "string" && !("enc" in header);
161
140
  } catch {
162
141
  return false;
@@ -215,11 +194,7 @@ async function handleAuth(event, opts) {
215
194
  return;
216
195
  }
217
196
  if (!payload && cookieToken && !isJwtShaped(cookieToken)) {
218
- const opaqueResult = await validateOpaqueSessionToken(
219
- cookieToken,
220
- url,
221
- opaqueTokenTimeoutMs
222
- );
197
+ const opaqueResult = await validateOpaqueSessionToken(cookieToken, url, opaqueTokenTimeoutMs);
223
198
  if (opaqueResult) {
224
199
  event.context.nextgenAuth = {
225
200
  isAuthenticated: true,
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export { ZitadelLogin, ZitadelLogout } from '@zitadel/components';
2
2
  export { createNextgenMiddleware, getAuth } from './runtime/server/middleware.js';
3
3
  import { ZitadelProject } from '@zitadel/api/config';
4
4
  export { ClientAuthResult } from './runtime/types.js';
5
- export { AuthResult, NextgenMiddlewareOptions } from '@zitadel/sdk-core/types';
5
+ export { AuthResult, NextgenMiddlewareOptions } from '@zitadel/sdk-core/middleware';
6
6
  import 'h3';
7
7
 
8
8
  /**
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-E7GMYXBM.js";
5
+ } from "./chunk-AQZKZRKQ.js";
6
6
 
7
7
  // src/index.ts
8
8
  import { ZitadelLogin, ZitadelLogout } from "@zitadel/components";
package/dist/module.js CHANGED
@@ -6,6 +6,27 @@ import {
6
6
  addImportsDir,
7
7
  createResolver
8
8
  } from "@nuxt/kit";
9
+ import { existsSync, readFileSync } from "fs";
10
+ import { resolve as resolvePath } from "path";
11
+ function loadProjectSecret(rootDir) {
12
+ if (process.env.ZITADEL_PROJECT_SECRET) {
13
+ return process.env.ZITADEL_PROJECT_SECRET;
14
+ }
15
+ const path = resolvePath(rootDir, ".env.local");
16
+ if (!existsSync(path)) {
17
+ return void 0;
18
+ }
19
+ for (const line of readFileSync(path, "utf8").split(/\r?\n/)) {
20
+ const match = line.match(/^\s*(?:export\s+)?ZITADEL_PROJECT_SECRET\s*=\s*(.*)$/);
21
+ if (match && match[1] !== void 0) {
22
+ const raw = match[1].trim();
23
+ const quoted = raw.match(/^(['"])(.*)\1\s*(?:#.*)?$/);
24
+ const value = quoted ? quoted[2] : raw.replace(/\s+#.*$/, "").trim();
25
+ return value === "" ? void 0 : value;
26
+ }
27
+ }
28
+ return void 0;
29
+ }
9
30
  var module_default = defineNuxtModule({
10
31
  meta: {
11
32
  name: "@zitadel/sdk-nuxt",
@@ -23,7 +44,12 @@ var module_default = defineNuxtModule({
23
44
  url: options.url ?? "http://localhost:4000",
24
45
  loginPath: options.loginPath ?? "/login",
25
46
  protectedRoutes: options.protectedRoutes ?? [],
26
- jwtKey: options.jwtKey
47
+ jwtKey: options.jwtKey,
48
+ // Server-only — never exposed to the client. Override at deploy time via
49
+ // `NUXT_NEXTGEN_PROJECT_SECRET`. Left undefined when the env var (and
50
+ // `.env.local`) provide no value, so the middleware surfaces a missing
51
+ // bearer instead of silently sending an empty one.
52
+ projectSecret: loadProjectSecret(nuxt.options.rootDir)
27
53
  };
28
54
  nuxt.options.runtimeConfig.public.zitadelProxyPath = options.proxyPath ?? "/__nextgen";
29
55
  nuxt.options.runtimeConfig.public.nextgenProxyPath = options.proxyPath ?? "/__nextgen";
@@ -1,10 +1,5 @@
1
1
  // src/runtime/plugin.ts
2
- import {
3
- defineNuxtPlugin,
4
- useRequestEvent,
5
- useRuntimeConfig,
6
- useState
7
- } from "#imports";
2
+ import { defineNuxtPlugin, useRequestEvent, useRuntimeConfig, useState } from "#imports";
8
3
  import { configureZitadel } from "@zitadel/api/config";
9
4
  var plugin_default = defineNuxtPlugin(() => {
10
5
  const event = useRequestEvent();
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  createNextgenMiddleware
3
- } from "../../chunk-E7GMYXBM.js";
3
+ } from "../../chunk-AQZKZRKQ.js";
4
4
 
5
5
  // src/runtime/server/handler.ts
6
6
  import { useRuntimeConfig } from "#imports";
@@ -1,7 +1,7 @@
1
- import { AuthResult, NextgenMiddlewareOptions } from '@zitadel/sdk-core/types';
1
+ import { AuthResult, NextgenMiddlewareOptions } from '@zitadel/sdk-core/middleware';
2
2
  import { EventHandler, H3Event } from 'h3';
3
3
 
4
- declare module 'h3' {
4
+ declare module "h3" {
5
5
  interface H3EventContext {
6
6
  nextgenAuth: AuthResult;
7
7
  }
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  createNextgenMiddleware,
3
3
  getAuth
4
- } from "../../chunk-E7GMYXBM.js";
4
+ } from "../../chunk-AQZKZRKQ.js";
5
5
  export {
6
6
  createNextgenMiddleware,
7
7
  getAuth
@@ -1,5 +1,5 @@
1
- import { UnauthState } from '@zitadel/sdk-core/types';
2
- export { AuthResult, AuthState, NextgenMiddlewareOptions, NextgenSession, UnauthState } from '@zitadel/sdk-core/types';
1
+ import { UnauthState } from '@zitadel/sdk-core/middleware';
2
+ export { AuthResult, AuthState, NextgenMiddlewareOptions, NextgenSession, UnauthState } from '@zitadel/sdk-core/middleware';
3
3
 
4
4
  /**
5
5
  * Re-exports shared SDK types from `@zitadel/sdk-core`.
package/dist/server.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export { createNextgenMiddleware, getAuth } from './runtime/server/middleware.js';
2
- import '@zitadel/sdk-core/types';
2
+ import '@zitadel/sdk-core/middleware';
3
3
  import 'h3';
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-E7GMYXBM.js";
5
+ } from "./chunk-AQZKZRKQ.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.1.0-alpha.10",
3
+ "version": "0.1.0-alpha.12",
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.1.0-alpha.10",
37
- "@zitadel/components": "0.1.0-alpha.10",
38
- "@zitadel/sdk-core": "0.1.0-alpha.10"
36
+ "@zitadel/api": "0.1.0-alpha.12",
37
+ "@zitadel/sdk-core": "0.1.0-alpha.12",
38
+ "@zitadel/components": "0.1.0-alpha.12"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "h3": ">=1",