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