@tuwaio/siwx-server 0.5.0 → 0.6.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/dist/next.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { SiwxVerificationPolicy } from '@tuwaio/siwx-core';
2
- import { SiwxSessionStore, SiwxNonceStore, CookieOptions, ServerVerifyOptions, StatelessDemoLimits } from './index.mjs';
3
- export { GetSiwxServerSessionOptions, getSiwxServerSession } from './index.mjs';
4
- import 'viem';
2
+ import { l as SiwxSessionStore, j as SiwxNonceStore, C as CookieOptions, g as ServerVerifyOptions, i as SiwxJwtOptions, m as StatelessDemoLimits } from './server-b6Tp6Wea.mjs';
3
+ export { G as GetSiwxServerSessionOptions, q as getSiwxServerSession } from './server-b6Tp6Wea.mjs';
4
+ import '@tuwaio/siwx-evm';
5
5
 
6
6
  /**
7
7
  * Next.js App Router route handlers, imported from `@tuwaio/siwx-server/next`.
@@ -30,7 +30,8 @@ interface SiwxApiHandlerOptions {
30
30
  */
31
31
  cookieOptions?: CookieOptions;
32
32
  /**
33
- * Extra options of `verifySiwxPayload`, for example `publicClient` for EIP-1271 wallets.
33
+ * Extra options of `verifySiwxPayload`, for example `publicClient` (a client, or a function of the chain number) for
34
+ * smart contract wallets.
34
35
  */
35
36
  verifyOptions?: Omit<ServerVerifyOptions, 'policy' | 'usedNonces'>;
36
37
  /**
@@ -38,6 +39,12 @@ interface SiwxApiHandlerOptions {
38
39
  * `cookieOptions.maxAge`, then to 604800 (7 days).
39
40
  */
40
41
  ttlSeconds?: number;
42
+ /**
43
+ * Enables `GET …/token` and `GET …/jwks`: a short-lived JWT for the signed-in wallet and the public keys that verify
44
+ * it, for services that accept a sign-in only as a JWT (embedded wallet providers with custom authentication such as
45
+ * Coinbase CDP, identity platforms, your own services). Without it both paths return 404.
46
+ */
47
+ jwt?: SiwxJwtOptions;
41
48
  }
42
49
  /**
43
50
  * Options of {@link createStatelessDemoSiwxHandler}.
@@ -82,12 +89,19 @@ interface StatelessDemoSiwxHandlerOptions {
82
89
  * Responds 400 for a malformed body, 401 for a failed verification or nonce, 413 for a too large body.
83
90
  * - `GET …/session`: returns the stored session of the cookie, or `null`.
84
91
  * - `DELETE …/session` or `POST …/logout`: revokes the session in the store and clears the cookie.
92
+ * - `GET …/token` (with the `jwt` option): signs a JWT for the session of the cookie with `signSiwxJwt` and returns
93
+ * `{ token, expiresAt }` with `Cache-Control: no-store`. Responds 401 when there is no session or it has expired.
94
+ * The token expires after `jwt.ttlSeconds` (600 by default) and never after the session.
95
+ * - `GET …/jwks` (with the `jwt` option): returns the JWKS of `jwt.signingKey` and `jwt.previousKeys` with
96
+ * `Cache-Control: public, max-age=300`. Give its URL to the services that verify the tokens.
85
97
  *
86
98
  * Other paths return 404; unexpected errors are logged with `console.error` and return 500.
87
99
  *
88
- * @param options - Stores, policy, cookie and verification options.
100
+ * @param options - Stores, policy, cookie, verification and JWT options.
89
101
  * @returns Route handlers to export as `GET`, `POST` and `DELETE`.
90
102
  * @throws {Error} If `sessionStore` or `nonceStore` is missing.
103
+ * @throws {TypeError} If `jwt.issuer` is empty.
104
+ * @throws {RangeError} If `jwt.ttlSeconds` is not an integer from 1 to 604800.
91
105
  *
92
106
  * @example
93
107
  * ```ts
@@ -155,4 +169,4 @@ declare function createStatelessDemoSiwxHandler(options: StatelessDemoSiwxHandle
155
169
  DELETE: (req: Request) => Promise<Response>;
156
170
  };
157
171
 
158
- export { type SiwxApiHandlerOptions, type StatelessDemoSiwxHandlerOptions, createSiwxApiHandler, createStatelessDemoSiwxHandler };
172
+ export { type SiwxApiHandlerOptions, SiwxJwtOptions, type StatelessDemoSiwxHandlerOptions, createSiwxApiHandler, createStatelessDemoSiwxHandler };
package/dist/next.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { SiwxVerificationPolicy } from '@tuwaio/siwx-core';
2
- import { SiwxSessionStore, SiwxNonceStore, CookieOptions, ServerVerifyOptions, StatelessDemoLimits } from './index.js';
3
- export { GetSiwxServerSessionOptions, getSiwxServerSession } from './index.js';
4
- import 'viem';
2
+ import { l as SiwxSessionStore, j as SiwxNonceStore, C as CookieOptions, g as ServerVerifyOptions, i as SiwxJwtOptions, m as StatelessDemoLimits } from './server-b6Tp6Wea.js';
3
+ export { G as GetSiwxServerSessionOptions, q as getSiwxServerSession } from './server-b6Tp6Wea.js';
4
+ import '@tuwaio/siwx-evm';
5
5
 
6
6
  /**
7
7
  * Next.js App Router route handlers, imported from `@tuwaio/siwx-server/next`.
@@ -30,7 +30,8 @@ interface SiwxApiHandlerOptions {
30
30
  */
31
31
  cookieOptions?: CookieOptions;
32
32
  /**
33
- * Extra options of `verifySiwxPayload`, for example `publicClient` for EIP-1271 wallets.
33
+ * Extra options of `verifySiwxPayload`, for example `publicClient` (a client, or a function of the chain number) for
34
+ * smart contract wallets.
34
35
  */
35
36
  verifyOptions?: Omit<ServerVerifyOptions, 'policy' | 'usedNonces'>;
36
37
  /**
@@ -38,6 +39,12 @@ interface SiwxApiHandlerOptions {
38
39
  * `cookieOptions.maxAge`, then to 604800 (7 days).
39
40
  */
40
41
  ttlSeconds?: number;
42
+ /**
43
+ * Enables `GET …/token` and `GET …/jwks`: a short-lived JWT for the signed-in wallet and the public keys that verify
44
+ * it, for services that accept a sign-in only as a JWT (embedded wallet providers with custom authentication such as
45
+ * Coinbase CDP, identity platforms, your own services). Without it both paths return 404.
46
+ */
47
+ jwt?: SiwxJwtOptions;
41
48
  }
42
49
  /**
43
50
  * Options of {@link createStatelessDemoSiwxHandler}.
@@ -82,12 +89,19 @@ interface StatelessDemoSiwxHandlerOptions {
82
89
  * Responds 400 for a malformed body, 401 for a failed verification or nonce, 413 for a too large body.
83
90
  * - `GET …/session`: returns the stored session of the cookie, or `null`.
84
91
  * - `DELETE …/session` or `POST …/logout`: revokes the session in the store and clears the cookie.
92
+ * - `GET …/token` (with the `jwt` option): signs a JWT for the session of the cookie with `signSiwxJwt` and returns
93
+ * `{ token, expiresAt }` with `Cache-Control: no-store`. Responds 401 when there is no session or it has expired.
94
+ * The token expires after `jwt.ttlSeconds` (600 by default) and never after the session.
95
+ * - `GET …/jwks` (with the `jwt` option): returns the JWKS of `jwt.signingKey` and `jwt.previousKeys` with
96
+ * `Cache-Control: public, max-age=300`. Give its URL to the services that verify the tokens.
85
97
  *
86
98
  * Other paths return 404; unexpected errors are logged with `console.error` and return 500.
87
99
  *
88
- * @param options - Stores, policy, cookie and verification options.
100
+ * @param options - Stores, policy, cookie, verification and JWT options.
89
101
  * @returns Route handlers to export as `GET`, `POST` and `DELETE`.
90
102
  * @throws {Error} If `sessionStore` or `nonceStore` is missing.
103
+ * @throws {TypeError} If `jwt.issuer` is empty.
104
+ * @throws {RangeError} If `jwt.ttlSeconds` is not an integer from 1 to 604800.
91
105
  *
92
106
  * @example
93
107
  * ```ts
@@ -155,4 +169,4 @@ declare function createStatelessDemoSiwxHandler(options: StatelessDemoSiwxHandle
155
169
  DELETE: (req: Request) => Promise<Response>;
156
170
  };
157
171
 
158
- export { type SiwxApiHandlerOptions, type StatelessDemoSiwxHandlerOptions, createSiwxApiHandler, createStatelessDemoSiwxHandler };
172
+ export { type SiwxApiHandlerOptions, SiwxJwtOptions, type StatelessDemoSiwxHandlerOptions, createSiwxApiHandler, createStatelessDemoSiwxHandler };
package/dist/next.js CHANGED
@@ -1 +1 @@
1
- 'use strict';var chunkNXEEBKUG_js=require('./chunk-NXEEBKUG.js'),siwxCore=require('@tuwaio/siwx-core');var T=300;function H(e){if(!e?.sessionStore||!e?.nonceStore)throw new Error("[SIWX-SERVER] createSiwxApiHandler requires both `sessionStore` and `nonceStore`.");let p=e.cookieOptions?.name||"siwx-session-v2",u=e.ttlSeconds??(e.cookieOptions?.maxAge||3600*24*7),y=65536,l=async o=>{try{let t=new URL(o.url).pathname.split("/").filter(Boolean),a=t[t.length-1]||"";if(o.method==="GET"&&a==="session"){let n=chunkNXEEBKUG_js.j(o.headers.get("cookie"),p);if(!n)return new Response(JSON.stringify(null),{status:200,headers:{"Content-Type":"application/json"}});let r=await e.sessionStore.get(n);return new Response(JSON.stringify(r?.session??null),{status:200,headers:{"Content-Type":"application/json"}})}if((o.method==="GET"||o.method==="POST")&&a==="nonce"){let n=siwxCore.generateNonce();return await e.nonceStore.issue({nonce:n,ttlSeconds:T}),new Response(JSON.stringify({nonce:n}),{status:200,headers:{"Content-Type":"application/json"}})}if(o.method==="POST"&&a==="verify"){let n=o.headers.get("content-length");if(n&&parseInt(n,10)>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let r=await o.text();if(r.length>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let s;try{s=JSON.parse(r);}catch{return new Response(JSON.stringify({error:"Invalid JSON payload"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!s||typeof s.message!="string"||typeof s.signature!="string")return new Response(JSON.stringify({error:"Missing message or signature"}),{status:400,headers:{"Content-Type":"application/json"}});let d={message:s.message,signature:s.signature},i=await chunkNXEEBKUG_js.c(d,{...e.verifyOptions,policy:e.policy});if(!i.success||!i.data)return new Response(JSON.stringify({error:i.error||"Verification failed"}),{status:401,headers:{"Content-Type":"application/json"}});if(!await e.nonceStore.consume({nonce:i.data.nonce}))return new Response(JSON.stringify({error:"Nonce replay detected or nonce expired"}),{status:401,headers:{"Content-Type":"application/json"}});let c=chunkNXEEBKUG_js.a(i.data),C=await e.sessionStore.create({session:c,ttlSeconds:u}),g=chunkNXEEBKUG_js.h(C.id,{...e.cookieOptions,name:p,maxAge:u});return new Response(JSON.stringify(c),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":g}})}if(o.method==="DELETE"&&a==="session"||o.method==="POST"&&a==="logout"){let n=chunkNXEEBKUG_js.j(o.headers.get("cookie"),p);return n&&await e.sessionStore.revoke(n),new Response(JSON.stringify({success:!0}),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":chunkNXEEBKUG_js.i({...e.cookieOptions,name:p})}})}return new Response(JSON.stringify({error:"Not Found"}),{status:404,headers:{"Content-Type":"application/json"}})}catch(S){return console.error("[SIWX-SERVER] Durable Handler error:",S),new Response(JSON.stringify({error:"Internal Server Error"}),{status:500,headers:{"Content-Type":"application/json"}})}};return {GET:l,POST:l,DELETE:l}}function V(e){if(!e?.signingSecret||e.signingSecret.length<32)throw new Error("[SIWX-SERVER] createStatelessDemoSiwxHandler requires a `signingSecret` of at least 32 characters.");let p=e.cookieOptions?.name||"siwx-session-v2",u=e.ttlSeconds??(e.cookieOptions?.maxAge||1800),y=e.demoLimits?.maxTransactionPayloadBytes??65536,l=new Map,o=t=>{let a=Date.now();for(let[n,r]of l)r<a&&l.delete(n);return l.has(t)?false:(l.set(t,a+T*1e3),true)},S=async t=>{try{let n=new URL(t.url).pathname.split("/").filter(Boolean),r=n[n.length-1]||"";if(t.method==="GET"&&r==="session"){let s=chunkNXEEBKUG_js.j(t.headers.get("cookie"),p);if(!s)return new Response(JSON.stringify(null),{status:200,headers:{"Content-Type":"application/json"}});let d=await chunkNXEEBKUG_js.e(s,e.signingSecret,e.policy);return new Response(JSON.stringify(d),{status:200,headers:{"Content-Type":"application/json"}})}if((t.method==="GET"||t.method==="POST")&&r==="nonce"){let s=await chunkNXEEBKUG_js.f(e.signingSecret,T);return new Response(JSON.stringify({nonce:s}),{status:200,headers:{"Content-Type":"application/json"}})}if(t.method==="POST"&&r==="verify"){let s=t.headers.get("content-length");if(s&&parseInt(s,10)>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let d=await t.text();if(d.length>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let i;try{i=JSON.parse(d);}catch{return new Response(JSON.stringify({error:"Invalid JSON payload"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!i||typeof i.message!="string"||typeof i.signature!="string")return new Response(JSON.stringify({error:"Missing message or signature"}),{status:400,headers:{"Content-Type":"application/json"}});let N={message:i.message,signature:i.signature},c=await chunkNXEEBKUG_js.c(N,{...e.verifyOptions,policy:e.policy});if(!c.success||!c.data)return new Response(JSON.stringify({error:c.error||"Verification failed"}),{status:401,headers:{"Content-Type":"application/json"}});if(!await chunkNXEEBKUG_js.g(c.data.nonce,e.signingSecret)||!o(c.data.nonce))return new Response(JSON.stringify({error:"Nonce replay detected or nonce expired"}),{status:401,headers:{"Content-Type":"application/json"}});let g=chunkNXEEBKUG_js.a(c.data),J=await chunkNXEEBKUG_js.d(g,e.signingSecret,u),j=chunkNXEEBKUG_js.h(J,{...e.cookieOptions,name:p,maxAge:u});return new Response(JSON.stringify(g),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":j}})}return t.method==="DELETE"&&r==="session"||t.method==="POST"&&r==="logout"?new Response(JSON.stringify({success:!0}),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":chunkNXEEBKUG_js.i({...e.cookieOptions,name:p})}}):new Response(JSON.stringify({error:"Not Found"}),{status:404,headers:{"Content-Type":"application/json"}})}catch(a){return console.error("[SIWX-SERVER] Stateless Demo Handler error:",a),new Response(JSON.stringify({error:"Internal Server Error"}),{status:500,headers:{"Content-Type":"application/json"}})}};return {GET:S,POST:S,DELETE:S}}Object.defineProperty(exports,"getSiwxServerSession",{enumerable:true,get:function(){return chunkNXEEBKUG_js.m}});exports.createSiwxApiHandler=H;exports.createStatelessDemoSiwxHandler=V;
1
+ 'use strict';var chunk455KLGU5_js=require('./chunk-455KLGU5.js'),siwxCore=require('@tuwaio/siwx-core');var x=300;function B(e){if(!e?.sessionStore||!e?.nonceStore)throw new Error("[SIWX-SERVER] createSiwxApiHandler requires both `sessionStore` and `nonceStore`.");let c=e.cookieOptions?.name||"siwx-session-v2",u=e.ttlSeconds??(e.cookieOptions?.maxAge||3600*24*7),y=65536,n=e.jwt;n&&chunk455KLGU5_js.h(n.issuer,n.ttlSeconds??600);let g=async o=>{try{let p=new URL(o.url).pathname.split("/").filter(Boolean),a=p[p.length-1]||"";if(o.method==="GET"&&a==="session"){let t=chunk455KLGU5_js.r(o.headers.get("cookie"),c);if(!t)return new Response(JSON.stringify(null),{status:200,headers:{"Content-Type":"application/json"}});let s=await e.sessionStore.get(t);return new Response(JSON.stringify(s?.session??null),{status:200,headers:{"Content-Type":"application/json"}})}if((o.method==="GET"||o.method==="POST")&&a==="nonce"){let t=siwxCore.generateNonce();return await e.nonceStore.issue({nonce:t,ttlSeconds:x}),new Response(JSON.stringify({nonce:t}),{status:200,headers:{"Content-Type":"application/json"}})}if(o.method==="POST"&&a==="verify"){let t=o.headers.get("content-length");if(t&&parseInt(t,10)>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let s=await o.text();if(s.length>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let i;try{i=JSON.parse(s);}catch{return new Response(JSON.stringify({error:"Invalid JSON payload"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!i||typeof i.message!="string"||typeof i.signature!="string")return new Response(JSON.stringify({error:"Missing message or signature"}),{status:400,headers:{"Content-Type":"application/json"}});let l={message:i.message,signature:i.signature},S=await chunk455KLGU5_js.k(l,{...e.verifyOptions,policy:e.policy});if(!S.success||!S.data)return new Response(JSON.stringify({error:S.error||"Verification failed"}),{status:401,headers:{"Content-Type":"application/json"}});if(!await e.nonceStore.consume({nonce:S.data.nonce}))return new Response(JSON.stringify({error:"Nonce replay detected or nonce expired"}),{status:401,headers:{"Content-Type":"application/json"}});let m=chunk455KLGU5_js.i(S.data),f=await e.sessionStore.create({session:m,ttlSeconds:u}),h=chunk455KLGU5_js.p(f.id,{...e.cookieOptions,name:c,maxAge:u});return new Response(JSON.stringify(m),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":h}})}if(n&&o.method==="GET"&&a==="token"){let t=chunk455KLGU5_js.r(o.headers.get("cookie"),c),s=t?await e.sessionStore.get(t):null;if(!s)return new Response(JSON.stringify({error:"Not signed in"}),{status:401,headers:{"Content-Type":"application/json","Cache-Control":"no-store"}});try{let i=await chunk455KLGU5_js.f({session:s.session,key:await n.signingKey,issuer:n.issuer,audience:n.audience,ttlSeconds:n.ttlSeconds,subject:n.subject?await n.subject(s):chunk455KLGU5_js.e(s.session,s.subjectId),claims:n.claims?await n.claims(s):void 0,notAfter:s.expiresAt});return new Response(JSON.stringify(i),{status:200,headers:{"Content-Type":"application/json","Cache-Control":"no-store"}})}catch(i){if(!(i instanceof RangeError))throw i;return new Response(JSON.stringify({error:"Session expired"}),{status:401,headers:{"Content-Type":"application/json","Cache-Control":"no-store"}})}}if(n&&o.method==="GET"&&a==="jwks"){let t=chunk455KLGU5_js.d([await n.signingKey,...n.previousKeys??[]]);return new Response(JSON.stringify(t),{status:200,headers:{"Content-Type":"application/json","Cache-Control":"public, max-age=300"}})}if(o.method==="DELETE"&&a==="session"||o.method==="POST"&&a==="logout"){let t=chunk455KLGU5_js.r(o.headers.get("cookie"),c);return t&&await e.sessionStore.revoke(t),new Response(JSON.stringify({success:!0}),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":chunk455KLGU5_js.q({...e.cookieOptions,name:c})}})}return new Response(JSON.stringify({error:"Not Found"}),{status:404,headers:{"Content-Type":"application/json"}})}catch(r){return console.error("[SIWX-SERVER] Durable Handler error:",r),new Response(JSON.stringify({error:"Internal Server Error"}),{status:500,headers:{"Content-Type":"application/json"}})}};return {GET:g,POST:g,DELETE:g}}function W(e){if(!e?.signingSecret||e.signingSecret.length<32)throw new Error("[SIWX-SERVER] createStatelessDemoSiwxHandler requires a `signingSecret` of at least 32 characters.");let c=e.cookieOptions?.name||"siwx-session-v2",u=e.ttlSeconds??(e.cookieOptions?.maxAge||1800),y=e.demoLimits?.maxTransactionPayloadBytes??65536,n=new Map,g=r=>{let p=Date.now();for(let[a,t]of n)t<p&&n.delete(a);return n.has(r)?false:(n.set(r,p+x*1e3),true)},o=async r=>{try{let a=new URL(r.url).pathname.split("/").filter(Boolean),t=a[a.length-1]||"";if(r.method==="GET"&&t==="session"){let s=chunk455KLGU5_js.r(r.headers.get("cookie"),c);if(!s)return new Response(JSON.stringify(null),{status:200,headers:{"Content-Type":"application/json"}});let i=await chunk455KLGU5_js.m(s,e.signingSecret,e.policy);return new Response(JSON.stringify(i),{status:200,headers:{"Content-Type":"application/json"}})}if((r.method==="GET"||r.method==="POST")&&t==="nonce"){let s=await chunk455KLGU5_js.n(e.signingSecret,x);return new Response(JSON.stringify({nonce:s}),{status:200,headers:{"Content-Type":"application/json"}})}if(r.method==="POST"&&t==="verify"){let s=r.headers.get("content-length");if(s&&parseInt(s,10)>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let i=await r.text();if(i.length>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let l;try{l=JSON.parse(i);}catch{return new Response(JSON.stringify({error:"Invalid JSON payload"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!l||typeof l.message!="string"||typeof l.signature!="string")return new Response(JSON.stringify({error:"Missing message or signature"}),{status:400,headers:{"Content-Type":"application/json"}});let S={message:l.message,signature:l.signature},d=await chunk455KLGU5_js.k(S,{...e.verifyOptions,policy:e.policy});if(!d.success||!d.data)return new Response(JSON.stringify({error:d.error||"Verification failed"}),{status:401,headers:{"Content-Type":"application/json"}});if(!await chunk455KLGU5_js.o(d.data.nonce,e.signingSecret)||!g(d.data.nonce))return new Response(JSON.stringify({error:"Nonce replay detected or nonce expired"}),{status:401,headers:{"Content-Type":"application/json"}});let f=chunk455KLGU5_js.i(d.data),h=await chunk455KLGU5_js.l(f,e.signingSecret,u),L=chunk455KLGU5_js.p(h,{...e.cookieOptions,name:c,maxAge:u});return new Response(JSON.stringify(f),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":L}})}return r.method==="DELETE"&&t==="session"||r.method==="POST"&&t==="logout"?new Response(JSON.stringify({success:!0}),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":chunk455KLGU5_js.q({...e.cookieOptions,name:c})}}):new Response(JSON.stringify({error:"Not Found"}),{status:404,headers:{"Content-Type":"application/json"}})}catch(p){return console.error("[SIWX-SERVER] Stateless Demo Handler error:",p),new Response(JSON.stringify({error:"Internal Server Error"}),{status:500,headers:{"Content-Type":"application/json"}})}};return {GET:o,POST:o,DELETE:o}}Object.defineProperty(exports,"getSiwxServerSession",{enumerable:true,get:function(){return chunk455KLGU5_js.u}});exports.createSiwxApiHandler=B;exports.createStatelessDemoSiwxHandler=W;
package/dist/next.mjs CHANGED
@@ -1 +1 @@
1
- import {j,c,a,h,i,e,f,g,d}from'./chunk-VTRW27M2.mjs';export{m as getSiwxServerSession}from'./chunk-VTRW27M2.mjs';import {generateNonce}from'@tuwaio/siwx-core';var T=300;function H(e){if(!e?.sessionStore||!e?.nonceStore)throw new Error("[SIWX-SERVER] createSiwxApiHandler requires both `sessionStore` and `nonceStore`.");let p=e.cookieOptions?.name||"siwx-session-v2",u=e.ttlSeconds??(e.cookieOptions?.maxAge||3600*24*7),y=65536,l=async o=>{try{let t=new URL(o.url).pathname.split("/").filter(Boolean),a$1=t[t.length-1]||"";if(o.method==="GET"&&a$1==="session"){let n=j(o.headers.get("cookie"),p);if(!n)return new Response(JSON.stringify(null),{status:200,headers:{"Content-Type":"application/json"}});let r=await e.sessionStore.get(n);return new Response(JSON.stringify(r?.session??null),{status:200,headers:{"Content-Type":"application/json"}})}if((o.method==="GET"||o.method==="POST")&&a$1==="nonce"){let n=generateNonce();return await e.nonceStore.issue({nonce:n,ttlSeconds:T}),new Response(JSON.stringify({nonce:n}),{status:200,headers:{"Content-Type":"application/json"}})}if(o.method==="POST"&&a$1==="verify"){let n=o.headers.get("content-length");if(n&&parseInt(n,10)>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let r=await o.text();if(r.length>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let s;try{s=JSON.parse(r);}catch{return new Response(JSON.stringify({error:"Invalid JSON payload"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!s||typeof s.message!="string"||typeof s.signature!="string")return new Response(JSON.stringify({error:"Missing message or signature"}),{status:400,headers:{"Content-Type":"application/json"}});let d={message:s.message,signature:s.signature},i=await c(d,{...e.verifyOptions,policy:e.policy});if(!i.success||!i.data)return new Response(JSON.stringify({error:i.error||"Verification failed"}),{status:401,headers:{"Content-Type":"application/json"}});if(!await e.nonceStore.consume({nonce:i.data.nonce}))return new Response(JSON.stringify({error:"Nonce replay detected or nonce expired"}),{status:401,headers:{"Content-Type":"application/json"}});let c$1=a(i.data),C=await e.sessionStore.create({session:c$1,ttlSeconds:u}),g=h(C.id,{...e.cookieOptions,name:p,maxAge:u});return new Response(JSON.stringify(c$1),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":g}})}if(o.method==="DELETE"&&a$1==="session"||o.method==="POST"&&a$1==="logout"){let n=j(o.headers.get("cookie"),p);return n&&await e.sessionStore.revoke(n),new Response(JSON.stringify({success:!0}),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":i({...e.cookieOptions,name:p})}})}return new Response(JSON.stringify({error:"Not Found"}),{status:404,headers:{"Content-Type":"application/json"}})}catch(S){return console.error("[SIWX-SERVER] Durable Handler error:",S),new Response(JSON.stringify({error:"Internal Server Error"}),{status:500,headers:{"Content-Type":"application/json"}})}};return {GET:l,POST:l,DELETE:l}}function V(e$1){if(!e$1?.signingSecret||e$1.signingSecret.length<32)throw new Error("[SIWX-SERVER] createStatelessDemoSiwxHandler requires a `signingSecret` of at least 32 characters.");let p=e$1.cookieOptions?.name||"siwx-session-v2",u=e$1.ttlSeconds??(e$1.cookieOptions?.maxAge||1800),y=e$1.demoLimits?.maxTransactionPayloadBytes??65536,l=new Map,o=t=>{let a=Date.now();for(let[n,r]of l)r<a&&l.delete(n);return l.has(t)?false:(l.set(t,a+T*1e3),true)},S=async t=>{try{let n=new URL(t.url).pathname.split("/").filter(Boolean),r=n[n.length-1]||"";if(t.method==="GET"&&r==="session"){let s=j(t.headers.get("cookie"),p);if(!s)return new Response(JSON.stringify(null),{status:200,headers:{"Content-Type":"application/json"}});let d=await e(s,e$1.signingSecret,e$1.policy);return new Response(JSON.stringify(d),{status:200,headers:{"Content-Type":"application/json"}})}if((t.method==="GET"||t.method==="POST")&&r==="nonce"){let s=await f(e$1.signingSecret,T);return new Response(JSON.stringify({nonce:s}),{status:200,headers:{"Content-Type":"application/json"}})}if(t.method==="POST"&&r==="verify"){let s=t.headers.get("content-length");if(s&&parseInt(s,10)>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let d$1=await t.text();if(d$1.length>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let i;try{i=JSON.parse(d$1);}catch{return new Response(JSON.stringify({error:"Invalid JSON payload"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!i||typeof i.message!="string"||typeof i.signature!="string")return new Response(JSON.stringify({error:"Missing message or signature"}),{status:400,headers:{"Content-Type":"application/json"}});let N={message:i.message,signature:i.signature},c$1=await c(N,{...e$1.verifyOptions,policy:e$1.policy});if(!c$1.success||!c$1.data)return new Response(JSON.stringify({error:c$1.error||"Verification failed"}),{status:401,headers:{"Content-Type":"application/json"}});if(!await g(c$1.data.nonce,e$1.signingSecret)||!o(c$1.data.nonce))return new Response(JSON.stringify({error:"Nonce replay detected or nonce expired"}),{status:401,headers:{"Content-Type":"application/json"}});let g$1=a(c$1.data),J=await d(g$1,e$1.signingSecret,u),j=h(J,{...e$1.cookieOptions,name:p,maxAge:u});return new Response(JSON.stringify(g$1),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":j}})}return t.method==="DELETE"&&r==="session"||t.method==="POST"&&r==="logout"?new Response(JSON.stringify({success:!0}),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":i({...e$1.cookieOptions,name:p})}}):new Response(JSON.stringify({error:"Not Found"}),{status:404,headers:{"Content-Type":"application/json"}})}catch(a){return console.error("[SIWX-SERVER] Stateless Demo Handler error:",a),new Response(JSON.stringify({error:"Internal Server Error"}),{status:500,headers:{"Content-Type":"application/json"}})}};return {GET:S,POST:S,DELETE:S}}export{H as createSiwxApiHandler,V as createStatelessDemoSiwxHandler};
1
+ import {h,r,k,i,p,f,e,d,q,m,n,o,l}from'./chunk-PZHK7SYL.mjs';export{u as getSiwxServerSession}from'./chunk-PZHK7SYL.mjs';import {generateNonce}from'@tuwaio/siwx-core';var x=300;function B(e$1){if(!e$1?.sessionStore||!e$1?.nonceStore)throw new Error("[SIWX-SERVER] createSiwxApiHandler requires both `sessionStore` and `nonceStore`.");let c=e$1.cookieOptions?.name||"siwx-session-v2",u=e$1.ttlSeconds??(e$1.cookieOptions?.maxAge||3600*24*7),y=65536,n=e$1.jwt;n&&h(n.issuer,n.ttlSeconds??600);let g=async o=>{try{let p$1=new URL(o.url).pathname.split("/").filter(Boolean),a=p$1[p$1.length-1]||"";if(o.method==="GET"&&a==="session"){let t=r(o.headers.get("cookie"),c);if(!t)return new Response(JSON.stringify(null),{status:200,headers:{"Content-Type":"application/json"}});let s=await e$1.sessionStore.get(t);return new Response(JSON.stringify(s?.session??null),{status:200,headers:{"Content-Type":"application/json"}})}if((o.method==="GET"||o.method==="POST")&&a==="nonce"){let t=generateNonce();return await e$1.nonceStore.issue({nonce:t,ttlSeconds:x}),new Response(JSON.stringify({nonce:t}),{status:200,headers:{"Content-Type":"application/json"}})}if(o.method==="POST"&&a==="verify"){let t=o.headers.get("content-length");if(t&&parseInt(t,10)>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let s=await o.text();if(s.length>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let i$1;try{i$1=JSON.parse(s);}catch{return new Response(JSON.stringify({error:"Invalid JSON payload"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!i$1||typeof i$1.message!="string"||typeof i$1.signature!="string")return new Response(JSON.stringify({error:"Missing message or signature"}),{status:400,headers:{"Content-Type":"application/json"}});let l={message:i$1.message,signature:i$1.signature},S=await k(l,{...e$1.verifyOptions,policy:e$1.policy});if(!S.success||!S.data)return new Response(JSON.stringify({error:S.error||"Verification failed"}),{status:401,headers:{"Content-Type":"application/json"}});if(!await e$1.nonceStore.consume({nonce:S.data.nonce}))return new Response(JSON.stringify({error:"Nonce replay detected or nonce expired"}),{status:401,headers:{"Content-Type":"application/json"}});let m=i(S.data),f=await e$1.sessionStore.create({session:m,ttlSeconds:u}),h=p(f.id,{...e$1.cookieOptions,name:c,maxAge:u});return new Response(JSON.stringify(m),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":h}})}if(n&&o.method==="GET"&&a==="token"){let t=r(o.headers.get("cookie"),c),s=t?await e$1.sessionStore.get(t):null;if(!s)return new Response(JSON.stringify({error:"Not signed in"}),{status:401,headers:{"Content-Type":"application/json","Cache-Control":"no-store"}});try{let i=await f({session:s.session,key:await n.signingKey,issuer:n.issuer,audience:n.audience,ttlSeconds:n.ttlSeconds,subject:n.subject?await n.subject(s):e(s.session,s.subjectId),claims:n.claims?await n.claims(s):void 0,notAfter:s.expiresAt});return new Response(JSON.stringify(i),{status:200,headers:{"Content-Type":"application/json","Cache-Control":"no-store"}})}catch(i){if(!(i instanceof RangeError))throw i;return new Response(JSON.stringify({error:"Session expired"}),{status:401,headers:{"Content-Type":"application/json","Cache-Control":"no-store"}})}}if(n&&o.method==="GET"&&a==="jwks"){let t=d([await n.signingKey,...n.previousKeys??[]]);return new Response(JSON.stringify(t),{status:200,headers:{"Content-Type":"application/json","Cache-Control":"public, max-age=300"}})}if(o.method==="DELETE"&&a==="session"||o.method==="POST"&&a==="logout"){let t=r(o.headers.get("cookie"),c);return t&&await e$1.sessionStore.revoke(t),new Response(JSON.stringify({success:!0}),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":q({...e$1.cookieOptions,name:c})}})}return new Response(JSON.stringify({error:"Not Found"}),{status:404,headers:{"Content-Type":"application/json"}})}catch(r){return console.error("[SIWX-SERVER] Durable Handler error:",r),new Response(JSON.stringify({error:"Internal Server Error"}),{status:500,headers:{"Content-Type":"application/json"}})}};return {GET:g,POST:g,DELETE:g}}function W(e){if(!e?.signingSecret||e.signingSecret.length<32)throw new Error("[SIWX-SERVER] createStatelessDemoSiwxHandler requires a `signingSecret` of at least 32 characters.");let c=e.cookieOptions?.name||"siwx-session-v2",u=e.ttlSeconds??(e.cookieOptions?.maxAge||1800),y=e.demoLimits?.maxTransactionPayloadBytes??65536,n$1=new Map,g=r=>{let p=Date.now();for(let[a,t]of n$1)t<p&&n$1.delete(a);return n$1.has(r)?false:(n$1.set(r,p+x*1e3),true)},o$1=async r$1=>{try{let a=new URL(r$1.url).pathname.split("/").filter(Boolean),t=a[a.length-1]||"";if(r$1.method==="GET"&&t==="session"){let s=r(r$1.headers.get("cookie"),c);if(!s)return new Response(JSON.stringify(null),{status:200,headers:{"Content-Type":"application/json"}});let i=await m(s,e.signingSecret,e.policy);return new Response(JSON.stringify(i),{status:200,headers:{"Content-Type":"application/json"}})}if((r$1.method==="GET"||r$1.method==="POST")&&t==="nonce"){let s=await n(e.signingSecret,x);return new Response(JSON.stringify({nonce:s}),{status:200,headers:{"Content-Type":"application/json"}})}if(r$1.method==="POST"&&t==="verify"){let s=r$1.headers.get("content-length");if(s&&parseInt(s,10)>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let i$1=await r$1.text();if(i$1.length>y)return new Response(JSON.stringify({error:"Payload Too Large"}),{status:413,headers:{"Content-Type":"application/json"}});let l$1;try{l$1=JSON.parse(i$1);}catch{return new Response(JSON.stringify({error:"Invalid JSON payload"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!l$1||typeof l$1.message!="string"||typeof l$1.signature!="string")return new Response(JSON.stringify({error:"Missing message or signature"}),{status:400,headers:{"Content-Type":"application/json"}});let S={message:l$1.message,signature:l$1.signature},d=await k(S,{...e.verifyOptions,policy:e.policy});if(!d.success||!d.data)return new Response(JSON.stringify({error:d.error||"Verification failed"}),{status:401,headers:{"Content-Type":"application/json"}});if(!await o(d.data.nonce,e.signingSecret)||!g(d.data.nonce))return new Response(JSON.stringify({error:"Nonce replay detected or nonce expired"}),{status:401,headers:{"Content-Type":"application/json"}});let f=i(d.data),h=await l(f,e.signingSecret,u),L=p(h,{...e.cookieOptions,name:c,maxAge:u});return new Response(JSON.stringify(f),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":L}})}return r$1.method==="DELETE"&&t==="session"||r$1.method==="POST"&&t==="logout"?new Response(JSON.stringify({success:!0}),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":q({...e.cookieOptions,name:c})}}):new Response(JSON.stringify({error:"Not Found"}),{status:404,headers:{"Content-Type":"application/json"}})}catch(p){return console.error("[SIWX-SERVER] Stateless Demo Handler error:",p),new Response(JSON.stringify({error:"Internal Server Error"}),{status:500,headers:{"Content-Type":"application/json"}})}};return {GET:o$1,POST:o$1,DELETE:o$1}}export{B as createSiwxApiHandler,W as createStatelessDemoSiwxHandler};