@tuwaio/siwx-server 0.1.0 → 0.2.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 +201 -97
- package/dist/chunk-4HK2SXOC.js +2 -0
- package/dist/chunk-GQ7NU6NM.mjs +2 -0
- package/dist/index.d.mts +68 -50
- package/dist/index.d.ts +68 -50
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/next.d.mts +92 -15
- package/dist/next.d.ts +92 -15
- package/dist/next.js +1 -1
- package/dist/next.mjs +1 -1
- package/dist/types-Dz3SGS59.d.mts +193 -0
- package/dist/types-Dz3SGS59.d.ts +193 -0
- package/package.json +5 -5
- package/dist/chunk-47HTX7ZQ.js +0 -2
- package/dist/chunk-JVIBIICZ.mjs +0 -2
- package/dist/types-CvnkVdoo.d.mts +0 -108
- package/dist/types-CvnkVdoo.d.ts +0 -108
package/dist/index.d.ts
CHANGED
|
@@ -1,67 +1,85 @@
|
|
|
1
|
+
import * as _tuwaio_siwx_core from '@tuwaio/siwx-core';
|
|
1
2
|
import { SiwxVerifyPayload } from '@tuwaio/siwx-core';
|
|
2
|
-
export { SiwxSessionLike, generateNonce as generateServerNonce, isSessionMatchingTarget } from '@tuwaio/siwx-core';
|
|
3
|
-
import { S as SiwxSession,
|
|
4
|
-
export { t as toSession } from './types-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @fileoverview Server-side CAIP-122 payload verification and session utilities.
|
|
8
|
-
* Backend-agnostic — compatible with any Node.js or Edge runtime (Next.js, NestJS, Hono, etc.).
|
|
9
|
-
*/
|
|
3
|
+
export { ParsedSiwxMessage, SiwxMessageFields, SiwxSessionLike, SiwxVerificationPolicy, ValidateMessageOptions, generateNonce, generateNonce as generateServerNonce, isSessionMatchingTarget, validateMessage, validatePolicy } from '@tuwaio/siwx-core';
|
|
4
|
+
import { S as SiwxNonceStore, a as SiwxSessionStore, b as SiwxSession, c as SiwxSessionRecord, C as CookieOptions, d as ServerVerifyOptions, e as ServerVerifyResult } from './types-Dz3SGS59.js';
|
|
5
|
+
export { f as StatelessDemoLimits, g as StatelessDemoTokenPayload, t as toSession } from './types-Dz3SGS59.js';
|
|
10
6
|
|
|
11
7
|
/**
|
|
12
8
|
* Parses and validates a raw CAIP-122 payload (message + signature) on the server side.
|
|
13
9
|
* Dynamically routes verification to the correct chain adapter based on the CAIP-2 namespace.
|
|
14
10
|
*
|
|
15
|
-
* This function is the primary entry point for server-side authentication.
|
|
16
|
-
* It handles EVM (eip155) and Solana chains.
|
|
17
|
-
*
|
|
18
11
|
* @param payload - The `{ message, signature }` payload sent by the client.
|
|
19
|
-
* @param options -
|
|
12
|
+
* @param options - Server-side verification options (policy, nonce replay protection, etc.).
|
|
20
13
|
* @returns A `ServerVerifyResult` with `success: true` and the parsed session data, or an error.
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* ```ts
|
|
24
|
-
* // In a Next.js API route or NestJS controller:
|
|
25
|
-
* const result = await verifySiwxPayload({ message, signature }, {
|
|
26
|
-
* usedNonces: await redis.smembers('used_nonces'),
|
|
27
|
-
* });
|
|
28
|
-
* if (result.success) {
|
|
29
|
-
* // Session is valid — issue a cookie or JWT
|
|
30
|
-
* }
|
|
31
|
-
* ```
|
|
32
14
|
*/
|
|
33
15
|
declare function verifySiwxPayload(payload: SiwxVerifyPayload, options?: ServerVerifyOptions): Promise<ServerVerifyResult>;
|
|
34
16
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
17
|
+
* Signs a stateless demo session into an authenticated compact token.
|
|
18
|
+
* Uses Web Crypto HMAC-SHA256.
|
|
37
19
|
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* @
|
|
42
|
-
* @param opts - Cookie options (name, maxAge, secure, SameSite, etc.).
|
|
43
|
-
* @returns A `SerializedCookieSession` containing the `Set-Cookie` header value and session data.
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* ```ts
|
|
47
|
-
* const { cookieHeader } = serializeCookieSession(session);
|
|
48
|
-
* return new Response(null, { headers: { 'Set-Cookie': cookieHeader } });
|
|
49
|
-
* ```
|
|
20
|
+
* @param session - The verified session to sign.
|
|
21
|
+
* @param secret - Server-only signing secret (minimum 32 bytes).
|
|
22
|
+
* @param ttlSeconds - Maximum session validity in seconds (default 1800 = 30m).
|
|
23
|
+
* @returns Authenticated compact token in `${payload}.${signature}` format.
|
|
50
24
|
*/
|
|
51
|
-
declare function
|
|
25
|
+
declare function signStatelessDemoSession(session: SiwxSession, secret: string, ttlSeconds?: number): Promise<string>;
|
|
52
26
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
27
|
+
* Verifies an authenticated stateless demo session token.
|
|
28
|
+
* Performs constant-time cryptographic verification and validates expiration and policy.
|
|
55
29
|
*
|
|
56
|
-
* @param
|
|
57
|
-
* @
|
|
58
|
-
*
|
|
59
|
-
* @
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
*
|
|
30
|
+
* @param token - Compact token from cookie (`${payload}.${signature}`).
|
|
31
|
+
* @param secret - Server-only signing secret.
|
|
32
|
+
* @param policy - Optional verification policy to enforce.
|
|
33
|
+
* @returns The verified SiwxSession, or null if invalid, expired, or tampered.
|
|
34
|
+
*/
|
|
35
|
+
declare function verifyStatelessDemoSession(token: string | null | undefined, secret: string, policy?: _tuwaio_siwx_core.SiwxVerificationPolicy): Promise<SiwxSession | null>;
|
|
36
|
+
/**
|
|
37
|
+
* Creates an HttpOnly Set-Cookie header value for a session.
|
|
38
|
+
*/
|
|
39
|
+
declare function createSessionCookie(value: string, opts?: CookieOptions): string;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a clear/destroy Set-Cookie header value.
|
|
42
|
+
*/
|
|
43
|
+
declare function createClearCookie(opts?: CookieOptions): string;
|
|
44
|
+
/**
|
|
45
|
+
* Extracts a cookie value by name from a raw Cookie header string.
|
|
46
|
+
*/
|
|
47
|
+
declare function parseCookie(cookieHeader: string | null | undefined, name: string): string | null;
|
|
48
|
+
/**
|
|
49
|
+
* In-memory implementation of SiwxSessionStore.
|
|
50
|
+
* STRICTLY for local development, prototyping, and unit testing.
|
|
51
|
+
* Fails closed in production environments.
|
|
52
|
+
*/
|
|
53
|
+
declare class MemorySiwxSessionStore implements SiwxSessionStore {
|
|
54
|
+
private records;
|
|
55
|
+
constructor(options?: {
|
|
56
|
+
allowInProduction?: boolean;
|
|
57
|
+
});
|
|
58
|
+
create(input: {
|
|
59
|
+
session: SiwxSession;
|
|
60
|
+
ttlSeconds: number;
|
|
61
|
+
}): Promise<SiwxSessionRecord>;
|
|
62
|
+
get(id: string): Promise<SiwxSessionRecord | null>;
|
|
63
|
+
bindSubject(id: string, subjectId: string): Promise<boolean>;
|
|
64
|
+
revoke(id: string): Promise<void>;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* In-memory implementation of SiwxNonceStore.
|
|
68
|
+
* STRICTLY for local development, prototyping, and unit testing.
|
|
69
|
+
* Fails closed in production environments.
|
|
64
70
|
*/
|
|
65
|
-
declare
|
|
71
|
+
declare class MemorySiwxNonceStore implements SiwxNonceStore {
|
|
72
|
+
private nonces;
|
|
73
|
+
constructor(options?: {
|
|
74
|
+
allowInProduction?: boolean;
|
|
75
|
+
});
|
|
76
|
+
issue(input: {
|
|
77
|
+
nonce: string;
|
|
78
|
+
ttlSeconds: number;
|
|
79
|
+
}): Promise<void>;
|
|
80
|
+
consume(input: {
|
|
81
|
+
nonce: string;
|
|
82
|
+
}): Promise<boolean>;
|
|
83
|
+
}
|
|
66
84
|
|
|
67
|
-
export { CookieOptions,
|
|
85
|
+
export { CookieOptions, MemorySiwxNonceStore, MemorySiwxSessionStore, ServerVerifyOptions, ServerVerifyResult, SiwxNonceStore, SiwxSession, SiwxSessionRecord, SiwxSessionStore, createClearCookie, createSessionCookie, parseCookie, signStatelessDemoSession, verifySiwxPayload, verifyStatelessDemoSession };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var chunk4HK2SXOC_js=require('./chunk-4HK2SXOC.js'),siwxCore=require('@tuwaio/siwx-core');Object.defineProperty(exports,"MemorySiwxNonceStore",{enumerable:true,get:function(){return chunk4HK2SXOC_js.j}});Object.defineProperty(exports,"MemorySiwxSessionStore",{enumerable:true,get:function(){return chunk4HK2SXOC_js.i}});Object.defineProperty(exports,"createClearCookie",{enumerable:true,get:function(){return chunk4HK2SXOC_js.g}});Object.defineProperty(exports,"createSessionCookie",{enumerable:true,get:function(){return chunk4HK2SXOC_js.f}});Object.defineProperty(exports,"generateServerNonce",{enumerable:true,get:function(){return chunk4HK2SXOC_js.b}});Object.defineProperty(exports,"parseCookie",{enumerable:true,get:function(){return chunk4HK2SXOC_js.h}});Object.defineProperty(exports,"signStatelessDemoSession",{enumerable:true,get:function(){return chunk4HK2SXOC_js.d}});Object.defineProperty(exports,"toSession",{enumerable:true,get:function(){return chunk4HK2SXOC_js.a}});Object.defineProperty(exports,"verifySiwxPayload",{enumerable:true,get:function(){return chunk4HK2SXOC_js.c}});Object.defineProperty(exports,"verifyStatelessDemoSession",{enumerable:true,get:function(){return chunk4HK2SXOC_js.e}});Object.defineProperty(exports,"generateNonce",{enumerable:true,get:function(){return siwxCore.generateNonce}});Object.defineProperty(exports,"isSessionMatchingTarget",{enumerable:true,get:function(){return siwxCore.isSessionMatchingTarget}});Object.defineProperty(exports,"validateMessage",{enumerable:true,get:function(){return siwxCore.validateMessage}});Object.defineProperty(exports,"validatePolicy",{enumerable:true,get:function(){return siwxCore.validatePolicy}});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{
|
|
1
|
+
export{j as MemorySiwxNonceStore,i as MemorySiwxSessionStore,g as createClearCookie,f as createSessionCookie,b as generateServerNonce,h as parseCookie,d as signStatelessDemoSession,a as toSession,c as verifySiwxPayload,e as verifyStatelessDemoSession}from'./chunk-GQ7NU6NM.mjs';export{generateNonce,isSessionMatchingTarget,validateMessage,validatePolicy}from'@tuwaio/siwx-core';
|
package/dist/next.d.mts
CHANGED
|
@@ -1,43 +1,120 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import '
|
|
1
|
+
import { SiwxVerificationPolicy } from '@tuwaio/siwx-core';
|
|
2
|
+
import { a as SiwxSessionStore, S as SiwxNonceStore, C as CookieOptions, d as ServerVerifyOptions, f as StatelessDemoLimits } from './types-Dz3SGS59.mjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @fileoverview Next.js App Router (Route Handlers) API helper for SIWX.
|
|
6
|
+
* Provides durable production handler and stateless demo handler.
|
|
6
7
|
*/
|
|
7
8
|
|
|
8
9
|
interface SiwxApiHandlerOptions {
|
|
9
10
|
/**
|
|
10
|
-
*
|
|
11
|
+
* Durable session store instance (e.g. RedisSiwxSessionStore or MemorySiwxSessionStore for tests).
|
|
12
|
+
*/
|
|
13
|
+
sessionStore: SiwxSessionStore;
|
|
14
|
+
/**
|
|
15
|
+
* Durable single-use nonce store instance (e.g. RedisSiwxNonceStore or MemorySiwxNonceStore for tests).
|
|
16
|
+
*/
|
|
17
|
+
nonceStore: SiwxNonceStore;
|
|
18
|
+
/**
|
|
19
|
+
* Verification policy to enforce (expected domains, URIs, allowed chains, expiration limits).
|
|
20
|
+
*/
|
|
21
|
+
policy?: SiwxVerificationPolicy;
|
|
22
|
+
/**
|
|
23
|
+
* Cookie configuration options (name, secure, path, domain, maxAge).
|
|
24
|
+
*/
|
|
25
|
+
cookieOptions?: CookieOptions;
|
|
26
|
+
/**
|
|
27
|
+
* Additional verification options (e.g. custom public client).
|
|
28
|
+
*/
|
|
29
|
+
verifyOptions?: Omit<ServerVerifyOptions, 'policy' | 'usedNonces'>;
|
|
30
|
+
/**
|
|
31
|
+
* Session time-to-live in seconds (defaults to 7 days = 604800s).
|
|
32
|
+
*/
|
|
33
|
+
ttlSeconds?: number;
|
|
34
|
+
}
|
|
35
|
+
interface StatelessDemoSiwxHandlerOptions {
|
|
36
|
+
/**
|
|
37
|
+
* Server-only cryptographic secret for HMAC signing (minimum 32 characters).
|
|
38
|
+
* MUST NEVER be exposed to the browser or client-side bundles.
|
|
39
|
+
*/
|
|
40
|
+
signingSecret: string;
|
|
41
|
+
/**
|
|
42
|
+
* Verification policy to enforce.
|
|
43
|
+
*/
|
|
44
|
+
policy?: SiwxVerificationPolicy;
|
|
45
|
+
/**
|
|
46
|
+
* Cookie configuration options.
|
|
11
47
|
*/
|
|
12
48
|
cookieOptions?: CookieOptions;
|
|
13
49
|
/**
|
|
14
|
-
*
|
|
50
|
+
* Demo limits (payload size, max requests).
|
|
51
|
+
*/
|
|
52
|
+
demoLimits?: StatelessDemoLimits;
|
|
53
|
+
/**
|
|
54
|
+
* Additional verification options.
|
|
15
55
|
*/
|
|
16
|
-
verifyOptions?: ServerVerifyOptions
|
|
56
|
+
verifyOptions?: Omit<ServerVerifyOptions, 'policy'>;
|
|
57
|
+
/**
|
|
58
|
+
* Session TTL in seconds for demo profile (defaults to 1800s = 30 minutes).
|
|
59
|
+
*/
|
|
60
|
+
ttlSeconds?: number;
|
|
17
61
|
}
|
|
18
62
|
/**
|
|
19
|
-
* Creates a
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* Works purely with the standard Web `Request` and `Response` objects natively
|
|
23
|
-
* supported by Next.js Route Handlers.
|
|
63
|
+
* Creates a standard production Next.js App Router route handler for SIWX with durable storage.
|
|
64
|
+
* Requires persistent session and nonce stores (Redis, PostgreSQL, etc.).
|
|
24
65
|
*
|
|
25
|
-
* @param options -
|
|
26
|
-
* @returns
|
|
66
|
+
* @param options - Configuration including sessionStore, nonceStore, and policy.
|
|
67
|
+
* @returns Object with `GET`, `POST`, and `DELETE` HTTP route handlers.
|
|
27
68
|
*
|
|
28
69
|
* @example
|
|
29
70
|
* ```ts
|
|
30
71
|
* // app/api/siwx/[...siwx]/route.ts
|
|
31
72
|
* import { createSiwxApiHandler } from '@tuwaio/siwx-server/next';
|
|
73
|
+
* import { sessionStore, nonceStore } from '@/lib/authStores';
|
|
74
|
+
*
|
|
75
|
+
* const handler = createSiwxApiHandler({
|
|
76
|
+
* sessionStore,
|
|
77
|
+
* nonceStore,
|
|
78
|
+
* policy: { expectedDomain: 'app.tuwa.io' },
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* export const { GET, POST, DELETE } = handler;
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
declare function createSiwxApiHandler(options: SiwxApiHandlerOptions): {
|
|
85
|
+
GET: (req: Request) => Promise<Response>;
|
|
86
|
+
POST: (req: Request) => Promise<Response>;
|
|
87
|
+
DELETE: (req: Request) => Promise<Response>;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Creates a stateless demo Next.js App Router route handler for SIWX.
|
|
91
|
+
* Uses authenticated HMAC-SHA256 tokens in HttpOnly cookies without requiring Redis or a database.
|
|
92
|
+
*
|
|
93
|
+
* Intended STRICTLY for zero-infrastructure demonstration apps and website prototypes.
|
|
94
|
+
*
|
|
95
|
+
* @param options - Configuration including server signingSecret, policy, and demoLimits.
|
|
96
|
+
* @returns Object with `GET`, `POST`, and `DELETE` HTTP route handlers.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* // app/api/siwx/[...siwx]/route.ts
|
|
101
|
+
* import { createStatelessDemoSiwxHandler } from '@tuwaio/siwx-server/next';
|
|
102
|
+
*
|
|
103
|
+
* const handler = createStatelessDemoSiwxHandler({
|
|
104
|
+
* signingSecret: process.env.SIWX_DEMO_SIGNING_SECRET!,
|
|
105
|
+
* policy: {
|
|
106
|
+
* expectedDomain: 'tuwa.io',
|
|
107
|
+
* requireExpirationTime: true,
|
|
108
|
+
* },
|
|
109
|
+
* });
|
|
32
110
|
*
|
|
33
|
-
* const handler = createSiwxApiHandler();
|
|
34
111
|
* export const { GET, POST, DELETE } = handler;
|
|
35
112
|
* ```
|
|
36
113
|
*/
|
|
37
|
-
declare function
|
|
114
|
+
declare function createStatelessDemoSiwxHandler(options: StatelessDemoSiwxHandlerOptions): {
|
|
38
115
|
GET: (req: Request) => Promise<Response>;
|
|
39
116
|
POST: (req: Request) => Promise<Response>;
|
|
40
117
|
DELETE: (req: Request) => Promise<Response>;
|
|
41
118
|
};
|
|
42
119
|
|
|
43
|
-
export { type SiwxApiHandlerOptions, createSiwxApiHandler };
|
|
120
|
+
export { type SiwxApiHandlerOptions, type StatelessDemoSiwxHandlerOptions, createSiwxApiHandler, createStatelessDemoSiwxHandler };
|
package/dist/next.d.ts
CHANGED
|
@@ -1,43 +1,120 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import '
|
|
1
|
+
import { SiwxVerificationPolicy } from '@tuwaio/siwx-core';
|
|
2
|
+
import { a as SiwxSessionStore, S as SiwxNonceStore, C as CookieOptions, d as ServerVerifyOptions, f as StatelessDemoLimits } from './types-Dz3SGS59.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @fileoverview Next.js App Router (Route Handlers) API helper for SIWX.
|
|
6
|
+
* Provides durable production handler and stateless demo handler.
|
|
6
7
|
*/
|
|
7
8
|
|
|
8
9
|
interface SiwxApiHandlerOptions {
|
|
9
10
|
/**
|
|
10
|
-
*
|
|
11
|
+
* Durable session store instance (e.g. RedisSiwxSessionStore or MemorySiwxSessionStore for tests).
|
|
12
|
+
*/
|
|
13
|
+
sessionStore: SiwxSessionStore;
|
|
14
|
+
/**
|
|
15
|
+
* Durable single-use nonce store instance (e.g. RedisSiwxNonceStore or MemorySiwxNonceStore for tests).
|
|
16
|
+
*/
|
|
17
|
+
nonceStore: SiwxNonceStore;
|
|
18
|
+
/**
|
|
19
|
+
* Verification policy to enforce (expected domains, URIs, allowed chains, expiration limits).
|
|
20
|
+
*/
|
|
21
|
+
policy?: SiwxVerificationPolicy;
|
|
22
|
+
/**
|
|
23
|
+
* Cookie configuration options (name, secure, path, domain, maxAge).
|
|
24
|
+
*/
|
|
25
|
+
cookieOptions?: CookieOptions;
|
|
26
|
+
/**
|
|
27
|
+
* Additional verification options (e.g. custom public client).
|
|
28
|
+
*/
|
|
29
|
+
verifyOptions?: Omit<ServerVerifyOptions, 'policy' | 'usedNonces'>;
|
|
30
|
+
/**
|
|
31
|
+
* Session time-to-live in seconds (defaults to 7 days = 604800s).
|
|
32
|
+
*/
|
|
33
|
+
ttlSeconds?: number;
|
|
34
|
+
}
|
|
35
|
+
interface StatelessDemoSiwxHandlerOptions {
|
|
36
|
+
/**
|
|
37
|
+
* Server-only cryptographic secret for HMAC signing (minimum 32 characters).
|
|
38
|
+
* MUST NEVER be exposed to the browser or client-side bundles.
|
|
39
|
+
*/
|
|
40
|
+
signingSecret: string;
|
|
41
|
+
/**
|
|
42
|
+
* Verification policy to enforce.
|
|
43
|
+
*/
|
|
44
|
+
policy?: SiwxVerificationPolicy;
|
|
45
|
+
/**
|
|
46
|
+
* Cookie configuration options.
|
|
11
47
|
*/
|
|
12
48
|
cookieOptions?: CookieOptions;
|
|
13
49
|
/**
|
|
14
|
-
*
|
|
50
|
+
* Demo limits (payload size, max requests).
|
|
51
|
+
*/
|
|
52
|
+
demoLimits?: StatelessDemoLimits;
|
|
53
|
+
/**
|
|
54
|
+
* Additional verification options.
|
|
15
55
|
*/
|
|
16
|
-
verifyOptions?: ServerVerifyOptions
|
|
56
|
+
verifyOptions?: Omit<ServerVerifyOptions, 'policy'>;
|
|
57
|
+
/**
|
|
58
|
+
* Session TTL in seconds for demo profile (defaults to 1800s = 30 minutes).
|
|
59
|
+
*/
|
|
60
|
+
ttlSeconds?: number;
|
|
17
61
|
}
|
|
18
62
|
/**
|
|
19
|
-
* Creates a
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* Works purely with the standard Web `Request` and `Response` objects natively
|
|
23
|
-
* supported by Next.js Route Handlers.
|
|
63
|
+
* Creates a standard production Next.js App Router route handler for SIWX with durable storage.
|
|
64
|
+
* Requires persistent session and nonce stores (Redis, PostgreSQL, etc.).
|
|
24
65
|
*
|
|
25
|
-
* @param options -
|
|
26
|
-
* @returns
|
|
66
|
+
* @param options - Configuration including sessionStore, nonceStore, and policy.
|
|
67
|
+
* @returns Object with `GET`, `POST`, and `DELETE` HTTP route handlers.
|
|
27
68
|
*
|
|
28
69
|
* @example
|
|
29
70
|
* ```ts
|
|
30
71
|
* // app/api/siwx/[...siwx]/route.ts
|
|
31
72
|
* import { createSiwxApiHandler } from '@tuwaio/siwx-server/next';
|
|
73
|
+
* import { sessionStore, nonceStore } from '@/lib/authStores';
|
|
74
|
+
*
|
|
75
|
+
* const handler = createSiwxApiHandler({
|
|
76
|
+
* sessionStore,
|
|
77
|
+
* nonceStore,
|
|
78
|
+
* policy: { expectedDomain: 'app.tuwa.io' },
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* export const { GET, POST, DELETE } = handler;
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
declare function createSiwxApiHandler(options: SiwxApiHandlerOptions): {
|
|
85
|
+
GET: (req: Request) => Promise<Response>;
|
|
86
|
+
POST: (req: Request) => Promise<Response>;
|
|
87
|
+
DELETE: (req: Request) => Promise<Response>;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Creates a stateless demo Next.js App Router route handler for SIWX.
|
|
91
|
+
* Uses authenticated HMAC-SHA256 tokens in HttpOnly cookies without requiring Redis or a database.
|
|
92
|
+
*
|
|
93
|
+
* Intended STRICTLY for zero-infrastructure demonstration apps and website prototypes.
|
|
94
|
+
*
|
|
95
|
+
* @param options - Configuration including server signingSecret, policy, and demoLimits.
|
|
96
|
+
* @returns Object with `GET`, `POST`, and `DELETE` HTTP route handlers.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* // app/api/siwx/[...siwx]/route.ts
|
|
101
|
+
* import { createStatelessDemoSiwxHandler } from '@tuwaio/siwx-server/next';
|
|
102
|
+
*
|
|
103
|
+
* const handler = createStatelessDemoSiwxHandler({
|
|
104
|
+
* signingSecret: process.env.SIWX_DEMO_SIGNING_SECRET!,
|
|
105
|
+
* policy: {
|
|
106
|
+
* expectedDomain: 'tuwa.io',
|
|
107
|
+
* requireExpirationTime: true,
|
|
108
|
+
* },
|
|
109
|
+
* });
|
|
32
110
|
*
|
|
33
|
-
* const handler = createSiwxApiHandler();
|
|
34
111
|
* export const { GET, POST, DELETE } = handler;
|
|
35
112
|
* ```
|
|
36
113
|
*/
|
|
37
|
-
declare function
|
|
114
|
+
declare function createStatelessDemoSiwxHandler(options: StatelessDemoSiwxHandlerOptions): {
|
|
38
115
|
GET: (req: Request) => Promise<Response>;
|
|
39
116
|
POST: (req: Request) => Promise<Response>;
|
|
40
117
|
DELETE: (req: Request) => Promise<Response>;
|
|
41
118
|
};
|
|
42
119
|
|
|
43
|
-
export { type SiwxApiHandlerOptions, createSiwxApiHandler };
|
|
120
|
+
export { type SiwxApiHandlerOptions, type StatelessDemoSiwxHandlerOptions, createSiwxApiHandler, createStatelessDemoSiwxHandler };
|
package/dist/next.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var chunk4HK2SXOC_js=require('./chunk-4HK2SXOC.js'),siwxCore=require('@tuwaio/siwx-core');function k(e){if(!e?.sessionStore||!e?.nonceStore)throw new Error("[SIWX-SERVER] createSiwxApiHandler requires both `sessionStore` and `nonceStore`. For zero-infrastructure demos without storage, use `createStatelessDemoSiwxHandler`.");let i=e.cookieOptions?.name||"siwx-session-v2",a=e.ttlSeconds??(e.cookieOptions?.maxAge||3600*24*7),r=async n=>{try{let p=new URL(n.url).pathname.split("/").filter(Boolean),o=p[p.length-1]||"";if(n.method==="GET"&&o==="session"){let t=chunk4HK2SXOC_js.h(n.headers.get("cookie"),i);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((n.method==="GET"||n.method==="POST")&&o==="nonce"){let t=siwxCore.generateNonce();return await e.nonceStore.issue({nonce:t,ttlSeconds:300}),new Response(JSON.stringify({nonce:t}),{status:200,headers:{"Content-Type":"application/json"}})}if(n.method==="POST"&&o==="verify"){let t=await n.json();if(!t.message||!t.signature)return new Response(JSON.stringify({error:"Missing message or signature"}),{status:400,headers:{"Content-Type":"application/json"}});let s=await chunk4HK2SXOC_js.c(t,{...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 S=chunk4HK2SXOC_js.a(s.data),d=await e.sessionStore.create({session:S,ttlSeconds:a}),T=chunk4HK2SXOC_js.f(d.id,{...e.cookieOptions,name:i,maxAge:a});return new Response(JSON.stringify(S),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":T}})}if(n.method==="DELETE"&&o==="session"||n.method==="POST"&&o==="logout"){let t=chunk4HK2SXOC_js.h(n.headers.get("cookie"),i);return t&&await e.sessionStore.revoke(t),new Response(JSON.stringify({success:!0}),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":chunk4HK2SXOC_js.g({...e.cookieOptions,name:i})}})}return new Response(JSON.stringify({error:"Not Found"}),{status:404,headers:{"Content-Type":"application/json"}})}catch(c){return console.error("[SIWX-SERVER] Durable Handler error:",c),new Response(JSON.stringify({error:"Internal Server Error"}),{status:500,headers:{"Content-Type":"application/json"}})}};return {GET:r,POST:r,DELETE:r}}function E(e){if(!e?.signingSecret||e.signingSecret.length<32)throw new Error("[SIWX-SERVER] createStatelessDemoSiwxHandler requires a `signingSecret` of at least 32 characters.");let i=e.cookieOptions?.name||"siwx-session-v2",a=e.ttlSeconds??(e.cookieOptions?.maxAge||1800),r=async n=>{try{let p=new URL(n.url).pathname.split("/").filter(Boolean),o=p[p.length-1]||"";if(n.method==="GET"&&o==="session"){let t=chunk4HK2SXOC_js.h(n.headers.get("cookie"),i);if(!t)return new Response(JSON.stringify(null),{status:200,headers:{"Content-Type":"application/json"}});let s=await chunk4HK2SXOC_js.e(t,e.signingSecret,e.policy);return new Response(JSON.stringify(s),{status:200,headers:{"Content-Type":"application/json"}})}if((n.method==="GET"||n.method==="POST")&&o==="nonce"){let t=siwxCore.generateNonce();return new Response(JSON.stringify({nonce:t}),{status:200,headers:{"Content-Type":"application/json"}})}if(n.method==="POST"&&o==="verify"){let t=await n.json();if(!t.message||!t.signature)return new Response(JSON.stringify({error:"Missing message or signature"}),{status:400,headers:{"Content-Type":"application/json"}});let s=await chunk4HK2SXOC_js.c(t,{...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"}});let u=chunk4HK2SXOC_js.a(s.data),S=await chunk4HK2SXOC_js.d(u,e.signingSecret,a),d=chunk4HK2SXOC_js.f(S,{...e.cookieOptions,name:i,maxAge:a});return new Response(JSON.stringify(u),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":d}})}return n.method==="DELETE"&&o==="session"||n.method==="POST"&&o==="logout"?new Response(JSON.stringify({success:!0}),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":chunk4HK2SXOC_js.g({...e.cookieOptions,name:i})}}):new Response(JSON.stringify({error:"Not Found"}),{status:404,headers:{"Content-Type":"application/json"}})}catch(c){return console.error("[SIWX-SERVER] Stateless Demo Handler error:",c),new Response(JSON.stringify({error:"Internal Server Error"}),{status:500,headers:{"Content-Type":"application/json"}})}};return {GET:r,POST:r,DELETE:r}}exports.createSiwxApiHandler=k;exports.createStatelessDemoSiwxHandler=E;
|
package/dist/next.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {h,c,a,f,g,e,d}from'./chunk-GQ7NU6NM.mjs';import {generateNonce}from'@tuwaio/siwx-core';function k(e){if(!e?.sessionStore||!e?.nonceStore)throw new Error("[SIWX-SERVER] createSiwxApiHandler requires both `sessionStore` and `nonceStore`. For zero-infrastructure demos without storage, use `createStatelessDemoSiwxHandler`.");let i=e.cookieOptions?.name||"siwx-session-v2",a$1=e.ttlSeconds??(e.cookieOptions?.maxAge||3600*24*7),r=async n=>{try{let p=new URL(n.url).pathname.split("/").filter(Boolean),o=p[p.length-1]||"";if(n.method==="GET"&&o==="session"){let t=h(n.headers.get("cookie"),i);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((n.method==="GET"||n.method==="POST")&&o==="nonce"){let t=generateNonce();return await e.nonceStore.issue({nonce:t,ttlSeconds:300}),new Response(JSON.stringify({nonce:t}),{status:200,headers:{"Content-Type":"application/json"}})}if(n.method==="POST"&&o==="verify"){let t=await n.json();if(!t.message||!t.signature)return new Response(JSON.stringify({error:"Missing message or signature"}),{status:400,headers:{"Content-Type":"application/json"}});let s=await c(t,{...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 S=a(s.data),d=await e.sessionStore.create({session:S,ttlSeconds:a$1}),T=f(d.id,{...e.cookieOptions,name:i,maxAge:a$1});return new Response(JSON.stringify(S),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":T}})}if(n.method==="DELETE"&&o==="session"||n.method==="POST"&&o==="logout"){let t=h(n.headers.get("cookie"),i);return t&&await e.sessionStore.revoke(t),new Response(JSON.stringify({success:!0}),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":g({...e.cookieOptions,name:i})}})}return new Response(JSON.stringify({error:"Not Found"}),{status:404,headers:{"Content-Type":"application/json"}})}catch(c){return console.error("[SIWX-SERVER] Durable Handler error:",c),new Response(JSON.stringify({error:"Internal Server Error"}),{status:500,headers:{"Content-Type":"application/json"}})}};return {GET:r,POST:r,DELETE:r}}function E(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 i=e$1.cookieOptions?.name||"siwx-session-v2",a$1=e$1.ttlSeconds??(e$1.cookieOptions?.maxAge||1800),r=async n=>{try{let p=new URL(n.url).pathname.split("/").filter(Boolean),o=p[p.length-1]||"";if(n.method==="GET"&&o==="session"){let t=h(n.headers.get("cookie"),i);if(!t)return new Response(JSON.stringify(null),{status:200,headers:{"Content-Type":"application/json"}});let s=await e(t,e$1.signingSecret,e$1.policy);return new Response(JSON.stringify(s),{status:200,headers:{"Content-Type":"application/json"}})}if((n.method==="GET"||n.method==="POST")&&o==="nonce"){let t=generateNonce();return new Response(JSON.stringify({nonce:t}),{status:200,headers:{"Content-Type":"application/json"}})}if(n.method==="POST"&&o==="verify"){let t=await n.json();if(!t.message||!t.signature)return new Response(JSON.stringify({error:"Missing message or signature"}),{status:400,headers:{"Content-Type":"application/json"}});let s=await c(t,{...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"}});let u=a(s.data),S=await d(u,e$1.signingSecret,a$1),d$1=f(S,{...e$1.cookieOptions,name:i,maxAge:a$1});return new Response(JSON.stringify(u),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":d$1}})}return n.method==="DELETE"&&o==="session"||n.method==="POST"&&o==="logout"?new Response(JSON.stringify({success:!0}),{status:200,headers:{"Content-Type":"application/json","Set-Cookie":g({...e$1.cookieOptions,name:i})}}):new Response(JSON.stringify({error:"Not Found"}),{status:404,headers:{"Content-Type":"application/json"}})}catch(c){return console.error("[SIWX-SERVER] Stateless Demo Handler error:",c),new Response(JSON.stringify({error:"Internal Server Error"}),{status:500,headers:{"Content-Type":"application/json"}})}};return {GET:r,POST:r,DELETE:r}}export{k as createSiwxApiHandler,E as createStatelessDemoSiwxHandler};
|