@usearete/sdk 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +111 -0
- package/dist/index.d.ts +913 -0
- package/dist/index.esm.js +3031 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +3074 -0
- package/dist/index.js.map +1 -0
- package/dist/ssr/handlers.d.ts +131 -0
- package/dist/ssr/handlers.esm.js +269 -0
- package/dist/ssr/handlers.esm.js.map +1 -0
- package/dist/ssr/handlers.js +294 -0
- package/dist/ssr/handlers.js.map +1 -0
- package/dist/ssr/index.d.ts +147 -0
- package/dist/ssr/index.esm.js +269 -0
- package/dist/ssr/index.esm.js.map +1 -0
- package/dist/ssr/index.js +295 -0
- package/dist/ssr/index.js.map +1 -0
- package/dist/ssr/nextjs-app.d.ts +143 -0
- package/dist/ssr/nextjs-app.esm.js +336 -0
- package/dist/ssr/nextjs-app.esm.js.map +1 -0
- package/dist/ssr/nextjs-app.js +359 -0
- package/dist/ssr/nextjs-app.js.map +1 -0
- package/dist/ssr/tanstack-start.d.ts +165 -0
- package/dist/ssr/tanstack-start.esm.js +357 -0
- package/dist/ssr/tanstack-start.esm.js.map +1 -0
- package/dist/ssr/tanstack-start.js +381 -0
- package/dist/ssr/tanstack-start.js.map +1 -0
- package/dist/ssr/vite.d.ts +164 -0
- package/dist/ssr/vite.esm.js +364 -0
- package/dist/ssr/vite.esm.js.map +1 -0
- package/dist/ssr/vite.js +387 -0
- package/dist/ssr/vite.js.map +1 -0
- package/package.json +98 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { NextRequest } from 'next/server';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Arete Auth Server - Drop-in Endpoint Handlers
|
|
5
|
+
*
|
|
6
|
+
* These are framework-agnostic API route handlers that users can mount however they like.
|
|
7
|
+
* They handle token minting and JWKS serving directly using Ed25519 signing.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // app/api/arete/sessions/route.ts (Next.js App Router)
|
|
12
|
+
* import { handleSessionRequest, handleJwksRequest } from '@usearete/sdk/ssr/handlers';
|
|
13
|
+
*
|
|
14
|
+
* export async function POST(request: Request) {
|
|
15
|
+
* const user = await getAuthenticatedUser(request);
|
|
16
|
+
* if (!user) return new Response('Unauthorized', { status: 401 });
|
|
17
|
+
* return handleSessionRequest({}, user.id);
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* export async function GET() {
|
|
21
|
+
* return handleJwksRequest();
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
interface AuthHandlerConfig {
|
|
26
|
+
/**
|
|
27
|
+
* Ed25519 signing key seed (base64-encoded, 32 bytes).
|
|
28
|
+
* Set ARETE_SIGNING_KEY env var OR pass here.
|
|
29
|
+
* Generate with: node -e "console.log(require('crypto').randomBytes(32).toString('base64'))"
|
|
30
|
+
*/
|
|
31
|
+
signingKey?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Optional: Pre-derived public key (base64-encoded, 32 bytes).
|
|
34
|
+
* If not provided, will be derived from the signing key.
|
|
35
|
+
*/
|
|
36
|
+
publicKey?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Token issuer (defaults to ARETE_ISSUER env var or 'arete')
|
|
39
|
+
*/
|
|
40
|
+
issuer?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Token audience (defaults to ARETE_AUDIENCE env var)
|
|
43
|
+
*/
|
|
44
|
+
audience?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Token TTL in seconds (defaults to 300 = 5 minutes)
|
|
47
|
+
*/
|
|
48
|
+
ttlSeconds?: number;
|
|
49
|
+
/**
|
|
50
|
+
* Key ID for JWKS (defaults to 'key-1')
|
|
51
|
+
*/
|
|
52
|
+
keyId?: string;
|
|
53
|
+
/**
|
|
54
|
+
* Custom limits for tokens
|
|
55
|
+
*/
|
|
56
|
+
limits?: {
|
|
57
|
+
max_connections?: number;
|
|
58
|
+
max_subscriptions?: number;
|
|
59
|
+
max_snapshot_rows?: number;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
interface TokenResponse {
|
|
63
|
+
token: string;
|
|
64
|
+
expires_at: number;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Authenticated session data resolved by framework adapters.
|
|
68
|
+
* Always derive this from verified server-side auth, never request headers.
|
|
69
|
+
*/
|
|
70
|
+
interface ResolvedSession {
|
|
71
|
+
subject: string;
|
|
72
|
+
scope?: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Next.js App Router integration for Arete Auth
|
|
77
|
+
*
|
|
78
|
+
* Drop-in route handlers for Next.js App Router.
|
|
79
|
+
* `resolveSession` must derive the subject from verified server-side auth.
|
|
80
|
+
* Never trust caller-supplied headers for identity or scope.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* // app/api/arete/sessions/route.ts
|
|
85
|
+
* import { createNextJsSessionRoute, createNextJsJwksRoute } from '@usearete/sdk/ssr/nextjs-app';
|
|
86
|
+
*
|
|
87
|
+
* async function getAuthenticatedUser() {
|
|
88
|
+
* // Return your verified server-side user/session here.
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* export const POST = createNextJsSessionRoute({
|
|
92
|
+
* resolveSession: async () => {
|
|
93
|
+
* const user = await getAuthenticatedUser();
|
|
94
|
+
* if (!user) return null;
|
|
95
|
+
* return { subject: user.id };
|
|
96
|
+
* },
|
|
97
|
+
* });
|
|
98
|
+
* export const GET = createNextJsJwksRoute();
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* // app/api/arete/sessions/route.ts (with custom config)
|
|
104
|
+
* import { createNextJsSessionRoute, createNextJsJwksRoute } from '@usearete/sdk/ssr/nextjs-app';
|
|
105
|
+
*
|
|
106
|
+
* export const POST = createNextJsSessionRoute({
|
|
107
|
+
* signingKey: process.env.ARETE_SIGNING_KEY,
|
|
108
|
+
* resolveSession: async () => {
|
|
109
|
+
* const user = await getAuthenticatedUser();
|
|
110
|
+
* if (!user) return null;
|
|
111
|
+
* return { subject: user.id, scope: 'read' };
|
|
112
|
+
* },
|
|
113
|
+
* ttlSeconds: 600,
|
|
114
|
+
* });
|
|
115
|
+
*
|
|
116
|
+
* export const GET = createNextJsJwksRoute({
|
|
117
|
+
* signingKey: process.env.ARETE_SIGNING_KEY,
|
|
118
|
+
* });
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
|
|
122
|
+
interface NextJsSessionRouteConfig extends AuthHandlerConfig {
|
|
123
|
+
resolveSession?: (request: NextRequest) => Promise<ResolvedSession | null> | ResolvedSession | null;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Create a Next.js App Router POST handler for /ws/sessions
|
|
127
|
+
*/
|
|
128
|
+
declare function createNextJsSessionRoute(config?: NextJsSessionRouteConfig): (request: NextRequest) => Promise<Response>;
|
|
129
|
+
/**
|
|
130
|
+
* Create a Next.js App Router GET handler for /.well-known/jwks.json
|
|
131
|
+
*/
|
|
132
|
+
declare function createNextJsJwksRoute(config?: AuthHandlerConfig): () => Promise<Response>;
|
|
133
|
+
/**
|
|
134
|
+
* Create a combined route handler that supports both POST (sessions) and GET (JWKS)
|
|
135
|
+
* Mount at a single route like /api/arete/auth
|
|
136
|
+
*/
|
|
137
|
+
declare function createNextJsAuthRoute(config?: NextJsSessionRouteConfig): {
|
|
138
|
+
POST: (request: NextRequest) => Promise<Response>;
|
|
139
|
+
GET: () => Promise<Response>;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export { createNextJsAuthRoute, createNextJsJwksRoute, createNextJsSessionRoute };
|
|
143
|
+
export type { AuthHandlerConfig, NextJsSessionRouteConfig, ResolvedSession, TokenResponse };
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import * as ed25519 from '@noble/ed25519';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Base64URL encoding/decoding utilities
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Encode Uint8Array to base64url string (RFC 4648)
|
|
8
|
+
*/
|
|
9
|
+
function encode(bytes) {
|
|
10
|
+
// Convert to regular base64
|
|
11
|
+
const base64 = Buffer.from(bytes).toString('base64');
|
|
12
|
+
// Convert to base64url: replace + with -, / with _, remove padding
|
|
13
|
+
return base64
|
|
14
|
+
.replace(/\+/g, '-')
|
|
15
|
+
.replace(/\//g, '_')
|
|
16
|
+
.replace(/=/g, '');
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Decode base64url string to Uint8Array
|
|
20
|
+
*/
|
|
21
|
+
function decode(base64url) {
|
|
22
|
+
// Convert from base64url to regular base64
|
|
23
|
+
let base64 = base64url
|
|
24
|
+
.replace(/-/g, '+')
|
|
25
|
+
.replace(/_/g, '/');
|
|
26
|
+
// Add padding if needed
|
|
27
|
+
const padding = 4 - (base64.length % 4);
|
|
28
|
+
if (padding !== 4) {
|
|
29
|
+
base64 += '='.repeat(padding);
|
|
30
|
+
}
|
|
31
|
+
return new Uint8Array(Buffer.from(base64, 'base64'));
|
|
32
|
+
}
|
|
33
|
+
const base64url = {
|
|
34
|
+
encode,
|
|
35
|
+
decode,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Arete Auth Server - Drop-in Endpoint Handlers
|
|
40
|
+
*
|
|
41
|
+
* These are framework-agnostic API route handlers that users can mount however they like.
|
|
42
|
+
* They handle token minting and JWKS serving directly using Ed25519 signing.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* // app/api/arete/sessions/route.ts (Next.js App Router)
|
|
47
|
+
* import { handleSessionRequest, handleJwksRequest } from '@usearete/sdk/ssr/handlers';
|
|
48
|
+
*
|
|
49
|
+
* export async function POST(request: Request) {
|
|
50
|
+
* const user = await getAuthenticatedUser(request);
|
|
51
|
+
* if (!user) return new Response('Unauthorized', { status: 401 });
|
|
52
|
+
* return handleSessionRequest({}, user.id);
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* export async function GET() {
|
|
56
|
+
* return handleJwksRequest();
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
/**
|
|
61
|
+
* Decode base64 to Uint8Array
|
|
62
|
+
*/
|
|
63
|
+
function decodeBase64(base64) {
|
|
64
|
+
const binary = Buffer.from(base64, 'base64');
|
|
65
|
+
return new Uint8Array(binary);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Encode Uint8Array to base64url
|
|
69
|
+
*/
|
|
70
|
+
function encodeBase64url(bytes) {
|
|
71
|
+
return base64url.encode(bytes);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Generate a key ID from public key bytes
|
|
75
|
+
*/
|
|
76
|
+
function deriveKeyId(publicKey) {
|
|
77
|
+
// Use first 8 bytes of public key as hex for kid
|
|
78
|
+
return Array.from(publicKey.slice(0, 8))
|
|
79
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
80
|
+
.join('');
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Create JWT header for Ed25519
|
|
84
|
+
*/
|
|
85
|
+
function createJwtHeader(keyId) {
|
|
86
|
+
const header = {
|
|
87
|
+
alg: 'EdDSA',
|
|
88
|
+
typ: 'JWT',
|
|
89
|
+
kid: keyId,
|
|
90
|
+
};
|
|
91
|
+
return encodeBase64url(new TextEncoder().encode(JSON.stringify(header)));
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Create JWT payload from claims
|
|
95
|
+
*/
|
|
96
|
+
function createJwtPayload(claims) {
|
|
97
|
+
return encodeBase64url(new TextEncoder().encode(JSON.stringify(claims)));
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Sign data with Ed25519
|
|
101
|
+
*/
|
|
102
|
+
async function signEd25519(data, privateKey) {
|
|
103
|
+
const messageBytes = new TextEncoder().encode(data);
|
|
104
|
+
return await ed25519.signAsync(messageBytes, privateKey);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Mint a session token using Ed25519 signing
|
|
108
|
+
* `subject` must come from verified server-side auth or trusted service code.
|
|
109
|
+
*/
|
|
110
|
+
async function mintSessionToken(config, subject = 'anonymous', scope = 'read', origin) {
|
|
111
|
+
const signingKeyBase64 = config.signingKey || process.env.ARETE_SIGNING_KEY;
|
|
112
|
+
if (!signingKeyBase64) {
|
|
113
|
+
throw new Error('ARETE_SIGNING_KEY not set. Generate with: node -e "console.log(require(\'crypto\').randomBytes(32).toString(\'base64\'))"');
|
|
114
|
+
}
|
|
115
|
+
const privateKeyBytes = decodeBase64(signingKeyBase64);
|
|
116
|
+
if (privateKeyBytes.length !== 32) {
|
|
117
|
+
throw new Error(`Invalid signing key length: expected 32 bytes, got ${privateKeyBytes.length}. ` +
|
|
118
|
+
'Ed25519 signing key must be 32 bytes (base64-encoded).');
|
|
119
|
+
}
|
|
120
|
+
// Derive public key from private key
|
|
121
|
+
const publicKeyBytes = await ed25519.getPublicKeyAsync(privateKeyBytes);
|
|
122
|
+
const keyId = config.keyId || deriveKeyId(publicKeyBytes);
|
|
123
|
+
const issuer = config.issuer || process.env.ARETE_ISSUER || 'arete';
|
|
124
|
+
const audience = config.audience || process.env.ARETE_AUDIENCE || 'arete';
|
|
125
|
+
const ttlSeconds = config.ttlSeconds || 300;
|
|
126
|
+
const now = Math.floor(Date.now() / 1000);
|
|
127
|
+
const expiresAt = now + ttlSeconds;
|
|
128
|
+
const claims = {
|
|
129
|
+
iss: issuer,
|
|
130
|
+
sub: subject,
|
|
131
|
+
aud: audience,
|
|
132
|
+
iat: now,
|
|
133
|
+
nbf: now,
|
|
134
|
+
exp: expiresAt,
|
|
135
|
+
jti: crypto.randomUUID(),
|
|
136
|
+
scope,
|
|
137
|
+
metering_key: `meter:${subject}`,
|
|
138
|
+
key_class: 'secret',
|
|
139
|
+
limits: config.limits || {
|
|
140
|
+
max_connections: 10,
|
|
141
|
+
max_subscriptions: 100,
|
|
142
|
+
max_snapshot_rows: 1000,
|
|
143
|
+
max_messages_per_minute: 10000,
|
|
144
|
+
max_bytes_per_minute: 100 * 1024 * 1024,
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
// Add origin binding if provided
|
|
148
|
+
if (origin) {
|
|
149
|
+
claims.origin = origin;
|
|
150
|
+
}
|
|
151
|
+
// Create JWT
|
|
152
|
+
const header = createJwtHeader(keyId);
|
|
153
|
+
const payload = createJwtPayload(claims);
|
|
154
|
+
const signingInput = `${header}.${payload}`;
|
|
155
|
+
const signature = await signEd25519(signingInput, privateKeyBytes);
|
|
156
|
+
const signatureBase64 = encodeBase64url(signature);
|
|
157
|
+
const token = `${signingInput}.${signatureBase64}`;
|
|
158
|
+
return {
|
|
159
|
+
token,
|
|
160
|
+
expires_at: expiresAt,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Generate JWKS response from signing key
|
|
165
|
+
*/
|
|
166
|
+
async function generateJwks(config) {
|
|
167
|
+
const signingKeyBase64 = config.signingKey || process.env.ARETE_SIGNING_KEY;
|
|
168
|
+
const publicKeyBase64 = config.publicKey || process.env.ARETE_PUBLIC_KEY;
|
|
169
|
+
if (!signingKeyBase64 && !publicKeyBase64) {
|
|
170
|
+
return { keys: [] };
|
|
171
|
+
}
|
|
172
|
+
let publicKeyBytes;
|
|
173
|
+
if (publicKeyBase64) {
|
|
174
|
+
// Use provided public key
|
|
175
|
+
publicKeyBytes = decodeBase64(publicKeyBase64);
|
|
176
|
+
if (publicKeyBytes.length !== 32) {
|
|
177
|
+
throw new Error(`Invalid public key length: expected 32 bytes, got ${publicKeyBytes.length}`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
// Derive public key from private key
|
|
182
|
+
const privateKeyBytes = decodeBase64(signingKeyBase64);
|
|
183
|
+
if (privateKeyBytes.length !== 32) {
|
|
184
|
+
throw new Error(`Invalid signing key length: expected 32 bytes, got ${privateKeyBytes.length}`);
|
|
185
|
+
}
|
|
186
|
+
publicKeyBytes = await ed25519.getPublicKeyAsync(privateKeyBytes);
|
|
187
|
+
}
|
|
188
|
+
const keyId = config.keyId ||
|
|
189
|
+
(publicKeyBase64 ? 'key-1' : deriveKeyId(publicKeyBytes));
|
|
190
|
+
return {
|
|
191
|
+
keys: [
|
|
192
|
+
{
|
|
193
|
+
kty: 'OKP',
|
|
194
|
+
crv: 'Ed25519',
|
|
195
|
+
kid: keyId,
|
|
196
|
+
use: 'sig',
|
|
197
|
+
alg: 'EdDSA',
|
|
198
|
+
x: encodeBase64url(publicKeyBytes),
|
|
199
|
+
},
|
|
200
|
+
],
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Next.js App Router integration for Arete Auth
|
|
206
|
+
*
|
|
207
|
+
* Drop-in route handlers for Next.js App Router.
|
|
208
|
+
* `resolveSession` must derive the subject from verified server-side auth.
|
|
209
|
+
* Never trust caller-supplied headers for identity or scope.
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```typescript
|
|
213
|
+
* // app/api/arete/sessions/route.ts
|
|
214
|
+
* import { createNextJsSessionRoute, createNextJsJwksRoute } from '@usearete/sdk/ssr/nextjs-app';
|
|
215
|
+
*
|
|
216
|
+
* async function getAuthenticatedUser() {
|
|
217
|
+
* // Return your verified server-side user/session here.
|
|
218
|
+
* }
|
|
219
|
+
*
|
|
220
|
+
* export const POST = createNextJsSessionRoute({
|
|
221
|
+
* resolveSession: async () => {
|
|
222
|
+
* const user = await getAuthenticatedUser();
|
|
223
|
+
* if (!user) return null;
|
|
224
|
+
* return { subject: user.id };
|
|
225
|
+
* },
|
|
226
|
+
* });
|
|
227
|
+
* export const GET = createNextJsJwksRoute();
|
|
228
|
+
* ```
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```typescript
|
|
232
|
+
* // app/api/arete/sessions/route.ts (with custom config)
|
|
233
|
+
* import { createNextJsSessionRoute, createNextJsJwksRoute } from '@usearete/sdk/ssr/nextjs-app';
|
|
234
|
+
*
|
|
235
|
+
* export const POST = createNextJsSessionRoute({
|
|
236
|
+
* signingKey: process.env.ARETE_SIGNING_KEY,
|
|
237
|
+
* resolveSession: async () => {
|
|
238
|
+
* const user = await getAuthenticatedUser();
|
|
239
|
+
* if (!user) return null;
|
|
240
|
+
* return { subject: user.id, scope: 'read' };
|
|
241
|
+
* },
|
|
242
|
+
* ttlSeconds: 600,
|
|
243
|
+
* });
|
|
244
|
+
*
|
|
245
|
+
* export const GET = createNextJsJwksRoute({
|
|
246
|
+
* signingKey: process.env.ARETE_SIGNING_KEY,
|
|
247
|
+
* });
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
/**
|
|
251
|
+
* Create a Next.js App Router POST handler for /ws/sessions
|
|
252
|
+
*/
|
|
253
|
+
function createNextJsSessionRoute(config = {}) {
|
|
254
|
+
return async function POST(request) {
|
|
255
|
+
const origin = request.headers.get('origin') || undefined;
|
|
256
|
+
try {
|
|
257
|
+
if (!config.resolveSession) {
|
|
258
|
+
return new Response(JSON.stringify({
|
|
259
|
+
error: 'createNextJsSessionRoute requires resolveSession to derive the subject from authenticated server-side state',
|
|
260
|
+
}), {
|
|
261
|
+
status: 500,
|
|
262
|
+
headers: {
|
|
263
|
+
'Content-Type': 'application/json',
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
const session = await config.resolveSession(request);
|
|
268
|
+
if (!session) {
|
|
269
|
+
return new Response(JSON.stringify({
|
|
270
|
+
error: 'Unauthorized',
|
|
271
|
+
}), {
|
|
272
|
+
status: 401,
|
|
273
|
+
headers: {
|
|
274
|
+
'Content-Type': 'application/json',
|
|
275
|
+
},
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
const tokenData = await mintSessionToken(config, session.subject, session.scope || 'read', origin);
|
|
279
|
+
return new Response(JSON.stringify(tokenData), {
|
|
280
|
+
status: 200,
|
|
281
|
+
headers: {
|
|
282
|
+
'Content-Type': 'application/json',
|
|
283
|
+
},
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
catch (error) {
|
|
287
|
+
return new Response(JSON.stringify({
|
|
288
|
+
error: error instanceof Error ? error.message : 'Failed to mint token',
|
|
289
|
+
}), {
|
|
290
|
+
status: 500,
|
|
291
|
+
headers: {
|
|
292
|
+
'Content-Type': 'application/json',
|
|
293
|
+
},
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Create a Next.js App Router GET handler for /.well-known/jwks.json
|
|
300
|
+
*/
|
|
301
|
+
function createNextJsJwksRoute(config = {}) {
|
|
302
|
+
return async function GET() {
|
|
303
|
+
try {
|
|
304
|
+
const jwks = await generateJwks(config);
|
|
305
|
+
return new Response(JSON.stringify(jwks), {
|
|
306
|
+
status: 200,
|
|
307
|
+
headers: {
|
|
308
|
+
'Content-Type': 'application/json',
|
|
309
|
+
},
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
return new Response(JSON.stringify({
|
|
314
|
+
error: error instanceof Error ? error.message : 'Failed to generate JWKS',
|
|
315
|
+
}), {
|
|
316
|
+
status: 500,
|
|
317
|
+
headers: {
|
|
318
|
+
'Content-Type': 'application/json',
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Create a combined route handler that supports both POST (sessions) and GET (JWKS)
|
|
326
|
+
* Mount at a single route like /api/arete/auth
|
|
327
|
+
*/
|
|
328
|
+
function createNextJsAuthRoute(config = {}) {
|
|
329
|
+
return {
|
|
330
|
+
POST: createNextJsSessionRoute(config),
|
|
331
|
+
GET: createNextJsJwksRoute(config),
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export { createNextJsAuthRoute, createNextJsJwksRoute, createNextJsSessionRoute };
|
|
336
|
+
//# sourceMappingURL=nextjs-app.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nextjs-app.esm.js","sources":["../../src/ssr/utils.ts","../../src/ssr/handlers.ts","../../src/ssr/nextjs-app.ts"],"sourcesContent":[null,null,null],"names":[],"mappings":";;AAAA;;AAEG;AAEH;;AAEG;AACG,SAAU,MAAM,CAAC,KAAiB,EAAA;;AAEtC,IAAA,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;;AAErD,IAAA,OAAO,MAAM;AACV,SAAA,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC;AACnB,SAAA,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC;AACnB,SAAA,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;AACvB,CAAC;AAED;;AAEG;AACG,SAAU,MAAM,CAAC,SAAiB,EAAA;;IAEtC,IAAI,MAAM,GAAG,SAAS;AACnB,SAAA,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC;AAClB,SAAA,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;;IAGtB,MAAM,OAAO,GAAG,CAAC,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACxC,IAAA,IAAI,OAAO,KAAK,CAAC,EAAE;AACjB,QAAA,MAAM,IAAI,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;KAC/B;AAED,IAAA,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;AACvD,CAAC;AAEM,MAAM,SAAS,GAAG;IACvB,MA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