@tuwaio/siwx-server 0.5.1 → 0.7.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/README.md +84 -5
- package/dist/chunk-455KLGU5.js +4 -0
- package/dist/chunk-PZHK7SYL.mjs +4 -0
- package/dist/index.d.mts +139 -401
- package/dist/index.d.ts +139 -401
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/next.d.mts +20 -6
- package/dist/next.d.ts +20 -6
- package/dist/next.js +1 -1
- package/dist/next.mjs +1 -1
- package/dist/server-b6Tp6Wea.d.mts +545 -0
- package/dist/server-b6Tp6Wea.d.ts +545 -0
- package/package.json +10 -6
- package/dist/chunk-NXEEBKUG.js +0 -2
- package/dist/chunk-VTRW27M2.mjs +0 -2
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 './
|
|
3
|
-
export { GetSiwxServerSessionOptions, getSiwxServerSession } from './
|
|
4
|
-
import '
|
|
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`
|
|
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
|
|
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 './
|
|
3
|
-
export { GetSiwxServerSessionOptions, getSiwxServerSession } from './
|
|
4
|
-
import '
|
|
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`
|
|
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
|
|
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
|
|
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 {
|
|
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};
|