@tuwaio/siwx-server 0.2.2 → 0.4.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 +144 -219
- package/dist/chunk-AEFOZH2W.mjs +2 -0
- package/dist/chunk-SS5KJYWU.js +2 -0
- package/dist/index.d.mts +181 -82
- package/dist/index.d.ts +181 -82
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/next.d.mts +61 -24
- package/dist/next.d.ts +61 -24
- package/dist/next.js +1 -1
- package/dist/next.mjs +1 -1
- package/package.json +29 -16
- package/dist/chunk-PBR725EL.mjs +0 -2
- package/dist/chunk-WPKBWXVR.js +0 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,49 +1,51 @@
|
|
|
1
1
|
import * as _tuwaio_siwx_core from '@tuwaio/siwx-core';
|
|
2
2
|
import { SiwxVerificationPolicy, SiwxVerifyResult, ParsedSiwxMessage, SiwxVerifyPayload } from '@tuwaio/siwx-core';
|
|
3
3
|
export { ParsedSiwxMessage, SiwxMessageFields, SiwxSessionLike, SiwxVerificationPolicy, ValidateMessageOptions, generateNonce, generateNonce as generateServerNonce, isSessionMatchingTarget, validateMessage, validatePolicy } from '@tuwaio/siwx-core';
|
|
4
|
+
import { PublicClient } from 'viem';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
|
-
* @
|
|
7
|
+
* @file Server-side types for the @tuwaio/siwx-server package.
|
|
7
8
|
*/
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
|
-
* Options
|
|
11
|
+
* Options of {@link verifySiwxPayload}.
|
|
11
12
|
*/
|
|
12
13
|
interface ServerVerifyOptions {
|
|
13
14
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
15
|
+
* Nonces that must be rejected. Verification fails when the message nonce is in the set. The set is only read:
|
|
16
|
+
* record used nonces yourself, or use a {@link SiwxNonceStore} (as `createSiwxApiHandler` does) for
|
|
17
|
+
* multi-instance deployments.
|
|
17
18
|
*/
|
|
18
19
|
usedNonces?: Set<string>;
|
|
19
20
|
/**
|
|
20
|
-
*
|
|
21
|
-
* Not recommended for production use.
|
|
21
|
+
* Skips the check that the message `expirationTime` has not passed. Not recommended in production.
|
|
22
22
|
* @default false
|
|
23
23
|
*/
|
|
24
24
|
skipExpiration?: boolean;
|
|
25
25
|
/**
|
|
26
|
-
*
|
|
26
|
+
* Verification policy enforced on the message fields (domain, URI, chains, timing). See
|
|
27
|
+
* {@link SiwxVerificationPolicy}.
|
|
27
28
|
*/
|
|
28
29
|
policy?: SiwxVerificationPolicy;
|
|
29
30
|
/**
|
|
30
|
-
*
|
|
31
|
+
* viem `PublicClient` for the chain of the message. Enables the EIP-1271 (smart contract wallet) fallback for
|
|
32
|
+
* `eip155` messages; ignored for Solana.
|
|
31
33
|
*/
|
|
32
|
-
publicClient?:
|
|
34
|
+
publicClient?: PublicClient;
|
|
33
35
|
}
|
|
34
36
|
/**
|
|
35
|
-
*
|
|
36
|
-
* with the verification method used.
|
|
37
|
+
* Result of {@link verifySiwxPayload}.
|
|
37
38
|
*/
|
|
38
39
|
interface ServerVerifyResult extends SiwxVerifyResult {
|
|
39
40
|
/**
|
|
40
|
-
* The CAIP-2 namespace
|
|
41
|
-
*
|
|
41
|
+
* The CAIP-2 namespace whose verifier checked the signature: `eip155` or `solana`. Absent when verification
|
|
42
|
+
* failed before the signature check.
|
|
42
43
|
*/
|
|
43
44
|
namespace?: 'eip155' | 'solana';
|
|
44
45
|
}
|
|
45
46
|
/**
|
|
46
|
-
*
|
|
47
|
+
* Serializable session derived from a verified CAIP-122 message (see {@link toSession}). Returned as JSON by the
|
|
48
|
+
* `verify` and `session` endpoints of `@tuwaio/siwx-server/next`.
|
|
47
49
|
*/
|
|
48
50
|
interface SiwxSession {
|
|
49
51
|
/** The verified CAIP-10 blockchain address. */
|
|
@@ -52,7 +54,7 @@ interface SiwxSession {
|
|
|
52
54
|
chainId: string;
|
|
53
55
|
/** The domain that issued the session. */
|
|
54
56
|
domain: string;
|
|
55
|
-
/** The nonce
|
|
57
|
+
/** The nonce of the signed message. It must be single-use: consume it in a {@link SiwxNonceStore}. */
|
|
56
58
|
nonce: string;
|
|
57
59
|
/** ISO 8601 timestamp when the session was issued. */
|
|
58
60
|
issuedAt: string;
|
|
@@ -60,14 +62,14 @@ interface SiwxSession {
|
|
|
60
62
|
expirationTime?: string;
|
|
61
63
|
}
|
|
62
64
|
/**
|
|
63
|
-
*
|
|
65
|
+
* A session stored in a {@link SiwxSessionStore}.
|
|
64
66
|
*/
|
|
65
67
|
interface SiwxSessionRecord {
|
|
66
|
-
/**
|
|
68
|
+
/** Opaque, unguessable session ID. It is the value of the session cookie. */
|
|
67
69
|
id: string;
|
|
68
70
|
/** The verified SIWX session data. */
|
|
69
71
|
session: SiwxSession;
|
|
70
|
-
/** Optional
|
|
72
|
+
/** Optional ID of your own user record (for example a database user ID), set with `bindSubject`. */
|
|
71
73
|
subjectId?: string;
|
|
72
74
|
/** Timestamp in milliseconds when the session record was created. */
|
|
73
75
|
createdAt: number;
|
|
@@ -75,90 +77,109 @@ interface SiwxSessionRecord {
|
|
|
75
77
|
expiresAt: number;
|
|
76
78
|
}
|
|
77
79
|
/**
|
|
78
|
-
*
|
|
80
|
+
* Storage contract for durable sessions (Redis, SQL, KV…), used by `createSiwxApiHandler` and
|
|
81
|
+
* {@link getSiwxServerSession}. Implementations must be shared by every server instance.
|
|
82
|
+
* {@link MemorySiwxSessionStore} is an in-memory implementation for development and tests.
|
|
79
83
|
*/
|
|
80
84
|
interface SiwxSessionStore {
|
|
81
85
|
/**
|
|
82
86
|
* Creates and stores a new session record.
|
|
87
|
+
* @param input - The session to store.
|
|
83
88
|
* @param input.session - The verified session data.
|
|
84
89
|
* @param input.ttlSeconds - Time-to-live in seconds.
|
|
85
|
-
* @returns The created
|
|
90
|
+
* @returns The created record. Its `id` must be unguessable, because it becomes the session cookie value.
|
|
86
91
|
*/
|
|
87
92
|
create(input: {
|
|
88
93
|
session: SiwxSession;
|
|
89
94
|
ttlSeconds: number;
|
|
90
95
|
}): Promise<SiwxSessionRecord>;
|
|
91
96
|
/**
|
|
92
|
-
* Retrieves a session record by its
|
|
97
|
+
* Retrieves a session record by its ID. Session expiry is enforced here: SIWX does not compare `expiresAt` itself.
|
|
93
98
|
* @param id - The session ID.
|
|
94
|
-
* @returns The session record, or null if not
|
|
99
|
+
* @returns The session record, or `null` if it does not exist or has expired.
|
|
95
100
|
*/
|
|
96
101
|
get(id: string): Promise<SiwxSessionRecord | null>;
|
|
97
102
|
/**
|
|
98
|
-
*
|
|
103
|
+
* Binds one of your user IDs to the session. Not called by SIWX; use it after sign-in to link the wallet session to
|
|
104
|
+
* your own user record.
|
|
99
105
|
* @param id - The session ID.
|
|
100
106
|
* @param subjectId - The user or subject ID.
|
|
101
|
-
* @returns
|
|
107
|
+
* @returns `true` if the session exists and was updated, `false` otherwise.
|
|
102
108
|
*/
|
|
103
109
|
bindSubject(id: string, subjectId: string): Promise<boolean>;
|
|
104
110
|
/**
|
|
105
|
-
* Revokes
|
|
111
|
+
* Revokes a session. Must succeed when the session does not exist.
|
|
106
112
|
* @param id - The session ID.
|
|
113
|
+
* @returns A promise that resolves once the session is removed.
|
|
107
114
|
*/
|
|
108
115
|
revoke(id: string): Promise<void>;
|
|
109
116
|
}
|
|
110
117
|
/**
|
|
111
|
-
*
|
|
118
|
+
* Storage contract for single-use challenge nonces, used by `createSiwxApiHandler`. Implementations must be shared
|
|
119
|
+
* by every server instance and `consume` must be atomic (for example Redis `GETDEL`).
|
|
120
|
+
* {@link MemorySiwxNonceStore} is an in-memory implementation for development and tests.
|
|
112
121
|
*/
|
|
113
122
|
interface SiwxNonceStore {
|
|
114
123
|
/**
|
|
115
|
-
*
|
|
116
|
-
* @param input
|
|
117
|
-
* @param input.
|
|
124
|
+
* Stores a newly issued nonce.
|
|
125
|
+
* @param input - The nonce to store.
|
|
126
|
+
* @param input.nonce - The nonce string.
|
|
127
|
+
* @param input.ttlSeconds - Time-to-live in seconds. `createSiwxApiHandler` uses 300.
|
|
128
|
+
* @returns A promise that resolves once the nonce is stored.
|
|
118
129
|
*/
|
|
119
130
|
issue(input: {
|
|
120
131
|
nonce: string;
|
|
121
132
|
ttlSeconds: number;
|
|
122
133
|
}): Promise<void>;
|
|
123
134
|
/**
|
|
124
|
-
* Atomically
|
|
135
|
+
* Atomically removes a nonce, so that each nonce is accepted once.
|
|
136
|
+
* @param input - The nonce to consume.
|
|
125
137
|
* @param input.nonce - The nonce string to consume.
|
|
126
|
-
* @returns
|
|
138
|
+
* @returns `true` if the nonce was issued, not expired and not consumed before; otherwise `false`.
|
|
127
139
|
*/
|
|
128
140
|
consume(input: {
|
|
129
141
|
nonce: string;
|
|
130
142
|
}): Promise<boolean>;
|
|
131
143
|
}
|
|
132
144
|
/**
|
|
133
|
-
*
|
|
145
|
+
* JSON payload of a stateless demo session token (see {@link signStatelessDemoSession}). The token is signed, not
|
|
146
|
+
* encrypted: anyone holding it can read these fields.
|
|
134
147
|
*/
|
|
135
148
|
interface StatelessDemoTokenPayload {
|
|
149
|
+
/** Token format version. */
|
|
136
150
|
version: 1;
|
|
151
|
+
/** CAIP-10 account ID of the session. */
|
|
137
152
|
address: string;
|
|
153
|
+
/** CAIP-2 chain ID of the session. */
|
|
138
154
|
chainId: string;
|
|
155
|
+
/** Domain of the signed message. */
|
|
139
156
|
domain: string;
|
|
157
|
+
/** Nonce of the signed message. */
|
|
140
158
|
nonce: string;
|
|
159
|
+
/** `issuedAt` of the signed message. */
|
|
141
160
|
issuedAt: string;
|
|
161
|
+
/** Expiry of the token: the message `expirationTime`, or the issuing time plus the token TTL. */
|
|
142
162
|
expirationTime?: string;
|
|
163
|
+
/** Random ID of the token. */
|
|
143
164
|
sessionId: string;
|
|
165
|
+
/** Token mode. */
|
|
144
166
|
mode: 'demo';
|
|
145
167
|
}
|
|
146
168
|
/**
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
* Note: Shared rate limits and request quotas across multiple replicas require
|
|
150
|
-
* a durable store or are authoritative at the Quasar App RPS / Quota layer.
|
|
169
|
+
* Request limits of `createStatelessDemoSiwxHandler`. Only the body size is limited; rate limiting is not part of
|
|
170
|
+
* SIWX.
|
|
151
171
|
*/
|
|
152
172
|
interface StatelessDemoLimits {
|
|
153
173
|
/**
|
|
154
|
-
* Maximum
|
|
155
|
-
*
|
|
174
|
+
* Maximum body size of `POST /verify`. Larger requests are rejected with HTTP 413. The durable handler always
|
|
175
|
+
* uses 65536.
|
|
156
176
|
* @default 65536 (64 KB)
|
|
157
177
|
*/
|
|
158
178
|
maxTransactionPayloadBytes?: number;
|
|
159
179
|
}
|
|
160
180
|
/**
|
|
161
|
-
*
|
|
181
|
+
* Attributes of the session cookie, used by {@link createSessionCookie}, {@link createClearCookie} and the
|
|
182
|
+
* `@tuwaio/siwx-server/next` handlers. The cookie is always `HttpOnly`.
|
|
162
183
|
*/
|
|
163
184
|
interface CookieOptions {
|
|
164
185
|
/**
|
|
@@ -167,7 +188,8 @@ interface CookieOptions {
|
|
|
167
188
|
*/
|
|
168
189
|
name?: string;
|
|
169
190
|
/**
|
|
170
|
-
* Max
|
|
191
|
+
* `Max-Age` in seconds. {@link createSessionCookie} defaults to 604800 (7 days). The handlers use their
|
|
192
|
+
* `ttlSeconds` instead, and fall back to this value when `ttlSeconds` is not set.
|
|
171
193
|
* @default 604800
|
|
172
194
|
*/
|
|
173
195
|
maxAge?: number;
|
|
@@ -192,15 +214,17 @@ interface CookieOptions {
|
|
|
192
214
|
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
193
215
|
}
|
|
194
216
|
/**
|
|
195
|
-
* Options
|
|
217
|
+
* Options of {@link getSiwxServerSession}.
|
|
196
218
|
*/
|
|
197
219
|
interface GetSiwxServerSessionOptions {
|
|
198
220
|
/**
|
|
199
|
-
*
|
|
200
|
-
* -
|
|
201
|
-
* -
|
|
202
|
-
* -
|
|
203
|
-
* -
|
|
221
|
+
* Where to read the session cookie from:
|
|
222
|
+
* - a `Cookie` header string (`"siwx-session-v2=…; other=…"`) or the bare cookie value;
|
|
223
|
+
* - a Web API `Request`;
|
|
224
|
+
* - the Next.js cookie store (`await cookies()`) or any object with `get(name)` returning `{ value }` or a string;
|
|
225
|
+
* - a Web API `Headers` object.
|
|
226
|
+
*
|
|
227
|
+
* `null` and `undefined` resolve to no session.
|
|
204
228
|
*/
|
|
205
229
|
cookieSource: string | Request | Headers | {
|
|
206
230
|
get(name: string): {
|
|
@@ -213,72 +237,136 @@ interface GetSiwxServerSessionOptions {
|
|
|
213
237
|
*/
|
|
214
238
|
cookieName?: string;
|
|
215
239
|
/**
|
|
216
|
-
*
|
|
240
|
+
* Store of the durable profile. The cookie value is looked up with `sessionStore.get`. Takes precedence over
|
|
241
|
+
* `signingSecret`.
|
|
217
242
|
*/
|
|
218
243
|
sessionStore?: SiwxSessionStore;
|
|
219
244
|
/**
|
|
220
|
-
*
|
|
245
|
+
* HMAC secret of the stateless demo profile. The cookie value is verified with
|
|
246
|
+
* {@link verifyStatelessDemoSession}.
|
|
221
247
|
*/
|
|
222
248
|
signingSecret?: string;
|
|
223
249
|
/**
|
|
224
|
-
*
|
|
250
|
+
* Policy checked against the stored session. Only a subset applies: `expectedDomain`, `allowedChainIds` (exact
|
|
251
|
+
* match) and, for durable sessions, `requireExpirationTime`; for demo tokens, expiry with `clockSkewSeconds`.
|
|
225
252
|
*/
|
|
226
253
|
policy?: SiwxVerificationPolicy;
|
|
227
254
|
}
|
|
228
255
|
/**
|
|
229
|
-
* Converts a
|
|
230
|
-
*
|
|
256
|
+
* Converts a verified CAIP-122 message into a {@link SiwxSession}: keeps `address`, `chainId`, `domain`, `nonce`,
|
|
257
|
+
* `issuedAt` and `expirationTime`. Pure function.
|
|
258
|
+
*
|
|
259
|
+
* @param parsed - The verified message, for example `result.data` of {@link verifySiwxPayload}.
|
|
260
|
+
* @returns The session object to store or sign.
|
|
231
261
|
*/
|
|
232
262
|
declare function toSession(parsed: ParsedSiwxMessage): SiwxSession;
|
|
233
263
|
|
|
234
264
|
/**
|
|
235
|
-
*
|
|
236
|
-
*
|
|
265
|
+
* Verifies a signed CAIP-122 message on the server, for any supported chain.
|
|
266
|
+
*
|
|
267
|
+
* Steps: parse the message, run {@link validateMessage} with `options.policy` (format, expiration with
|
|
268
|
+
* `clockSkewSeconds`, `notBefore` and the policy rules), reject nonces listed in `options.usedNonces`, then route by the `chainId` namespace to `verifyEvmSignature`
|
|
269
|
+
* (`@tuwaio/siwx-evm`, with the EIP-1271 fallback when `options.publicClient` is set) or `verifyEd25519`
|
|
270
|
+
* (`@tuwaio/siwx-solana`).
|
|
237
271
|
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
*
|
|
272
|
+
* Side effects: dynamically imports the chain package of the namespace, so `@tuwaio/siwx-evm` and/or
|
|
273
|
+
* `@tuwaio/siwx-solana` must be installed for the chains you accept; the EIP-1271 fallback makes one RPC call.
|
|
274
|
+
* It does not consume nonces or create sessions: pair it with a {@link SiwxNonceStore} and a
|
|
275
|
+
* {@link SiwxSessionStore}, or use the handlers of `@tuwaio/siwx-server/next`.
|
|
276
|
+
*
|
|
277
|
+
* @param payload - The `{ message, signature }` sent by the client.
|
|
278
|
+
* @param options - Policy, replay protection and chain options.
|
|
279
|
+
* @returns `{ success: true, data, namespace }` with the parsed message, or `{ success: false, error }`. Never
|
|
280
|
+
* throws: every failure is returned as `error`.
|
|
241
281
|
*/
|
|
242
282
|
declare function verifySiwxPayload(payload: SiwxVerifyPayload, options?: ServerVerifyOptions): Promise<ServerVerifyResult>;
|
|
243
283
|
/**
|
|
244
|
-
*
|
|
245
|
-
*
|
|
284
|
+
* Creates a stateless demo session token: the session as base64url JSON ({@link StatelessDemoTokenPayload}) plus
|
|
285
|
+
* an HMAC-SHA256 signature (Web Crypto). The token is signed, not encrypted, and cannot be revoked before it
|
|
286
|
+
* expires.
|
|
246
287
|
*
|
|
247
288
|
* @param session - The verified session to sign.
|
|
248
|
-
* @param secret - Server-only signing secret
|
|
249
|
-
* @param ttlSeconds -
|
|
250
|
-
*
|
|
289
|
+
* @param secret - Server-only signing secret of at least 32 characters.
|
|
290
|
+
* @param ttlSeconds - Token lifetime in seconds, used only when the session has no `expirationTime`; otherwise the
|
|
291
|
+
* token expires with the message.
|
|
292
|
+
* @returns The token, formatted as `{payload}.{signature}`.
|
|
293
|
+
* @throws {Error} If `secret` is shorter than 32 characters.
|
|
251
294
|
*/
|
|
252
295
|
declare function signStatelessDemoSession(session: SiwxSession, secret: string, ttlSeconds?: number): Promise<string>;
|
|
253
296
|
/**
|
|
254
|
-
* Verifies
|
|
255
|
-
*
|
|
297
|
+
* Verifies a token created by {@link signStatelessDemoSession}: checks the HMAC signature with Web Crypto, the
|
|
298
|
+
* token version and mode, and its expiry (with `policy.clockSkewSeconds`, 60 seconds by default).
|
|
299
|
+
*
|
|
300
|
+
* Only `expectedDomain` and `allowedChainIds` (exact match) of the policy are applied; other policy fields are
|
|
301
|
+
* ignored because they were checked when the token was issued.
|
|
256
302
|
*
|
|
257
|
-
* @param token -
|
|
258
|
-
* @param secret -
|
|
259
|
-
* @param policy - Optional
|
|
260
|
-
* @returns The
|
|
303
|
+
* @param token - The token from the session cookie.
|
|
304
|
+
* @param secret - The secret the token was signed with.
|
|
305
|
+
* @param policy - Optional policy to check against the token.
|
|
306
|
+
* @returns The session, or `null` if the token is missing, malformed, tampered, expired or rejected by the policy.
|
|
307
|
+
* Never throws.
|
|
261
308
|
*/
|
|
262
309
|
declare function verifyStatelessDemoSession(token: string | null | undefined, secret: string, policy?: _tuwaio_siwx_core.SiwxVerificationPolicy): Promise<SiwxSession | null>;
|
|
263
310
|
/**
|
|
264
|
-
*
|
|
311
|
+
* Issues a challenge nonce for the stateless demo profile: a random value and an expiry time, signed with
|
|
312
|
+
* HMAC-SHA256 (Web Crypto), encoded as 106 lowercase hex characters so that it is a valid CAIP-122 nonce.
|
|
313
|
+
* {@link verifyStatelessDemoNonce} can then check, without any storage, that the server issued the nonce and that it
|
|
314
|
+
* has not expired.
|
|
315
|
+
*
|
|
316
|
+
* @param secret - Server-only signing secret of at least 32 characters.
|
|
317
|
+
* @param ttlSeconds - How long the nonce can be used, in seconds.
|
|
318
|
+
* @returns The signed nonce.
|
|
319
|
+
* @throws {Error} If `secret` is shorter than 32 characters.
|
|
320
|
+
*/
|
|
321
|
+
declare function issueStatelessDemoNonce(secret: string, ttlSeconds?: number): Promise<string>;
|
|
322
|
+
/**
|
|
323
|
+
* Checks a nonce issued by {@link issueStatelessDemoNonce}: its format, its HMAC signature (constant-time, Web
|
|
324
|
+
* Crypto) and its expiry. It does not track usage, so the same nonce passes until it expires; callers that need
|
|
325
|
+
* single use must remember consumed nonces (`createStatelessDemoSiwxHandler` does so per server instance).
|
|
326
|
+
*
|
|
327
|
+
* @param nonce - The nonce of the signed message.
|
|
328
|
+
* @param secret - The secret the nonce was issued with.
|
|
329
|
+
* @returns `true` if the nonce was issued with `secret` and has not expired; otherwise `false`. Never throws.
|
|
330
|
+
*/
|
|
331
|
+
declare function verifyStatelessDemoNonce(nonce: string, secret: string): Promise<boolean>;
|
|
332
|
+
/**
|
|
333
|
+
* Formats a `Set-Cookie` header value for the session cookie: `HttpOnly`, `Secure` and `SameSite=Strict` by
|
|
334
|
+
* default.
|
|
335
|
+
*
|
|
336
|
+
* @param value - The cookie value (session ID or demo token). Written as-is, without encoding.
|
|
337
|
+
* @param opts - Cookie attributes. Defaults: name `siwx-session-v2`, `Max-Age` 604800, path `/`.
|
|
338
|
+
* @returns The header value, e.g. `siwx-session-v2=…; Max-Age=604800; Path=/; HttpOnly; SameSite=Strict; Secure`.
|
|
265
339
|
*/
|
|
266
340
|
declare function createSessionCookie(value: string, opts?: CookieOptions): string;
|
|
267
341
|
/**
|
|
268
|
-
*
|
|
342
|
+
* Formats a `Set-Cookie` header value that deletes the session cookie (`Max-Age=0` and an expiry date in the
|
|
343
|
+
* past). Use the same `name`, `path` and `domain` as when the cookie was set.
|
|
344
|
+
*
|
|
345
|
+
* @param opts - Cookie attributes. `maxAge` is ignored.
|
|
346
|
+
* @returns The header value.
|
|
269
347
|
*/
|
|
270
348
|
declare function createClearCookie(opts?: CookieOptions): string;
|
|
271
349
|
/**
|
|
272
|
-
*
|
|
350
|
+
* Reads one cookie from a `Cookie` request header. Pairs must be separated by `"; "`; values are returned as-is,
|
|
351
|
+
* without URL-decoding.
|
|
352
|
+
*
|
|
353
|
+
* @param cookieHeader - The `Cookie` header value.
|
|
354
|
+
* @param name - The cookie name.
|
|
355
|
+
* @returns The cookie value, or `null` when the header is empty or has no such cookie.
|
|
273
356
|
*/
|
|
274
357
|
declare function parseCookie(cookieHeader: string | null | undefined, name: string): string | null;
|
|
275
358
|
/**
|
|
276
|
-
* In-memory
|
|
277
|
-
*
|
|
278
|
-
*
|
|
359
|
+
* In-memory {@link SiwxSessionStore} for local development and tests. Sessions live in a `Map` of the current
|
|
360
|
+
* process: they are lost on restart and not shared between instances or serverless invocations. Session IDs are
|
|
361
|
+
* 32-character hex strings from {@link generateNonce}.
|
|
279
362
|
*/
|
|
280
363
|
declare class MemorySiwxSessionStore implements SiwxSessionStore {
|
|
281
364
|
private records;
|
|
365
|
+
/**
|
|
366
|
+
* @param options - Store options.
|
|
367
|
+
* @param options.allowInProduction - Allows the store when `NODE_ENV` is `production`.
|
|
368
|
+
* @throws {Error} When `process.env.NODE_ENV` is `production` and `allowInProduction` is not `true`.
|
|
369
|
+
*/
|
|
282
370
|
constructor(options?: {
|
|
283
371
|
allowInProduction?: boolean;
|
|
284
372
|
});
|
|
@@ -291,12 +379,17 @@ declare class MemorySiwxSessionStore implements SiwxSessionStore {
|
|
|
291
379
|
revoke(id: string): Promise<void>;
|
|
292
380
|
}
|
|
293
381
|
/**
|
|
294
|
-
* In-memory
|
|
295
|
-
*
|
|
296
|
-
*
|
|
382
|
+
* In-memory {@link SiwxNonceStore} for local development and tests. Nonces live in a `Map` of the current process:
|
|
383
|
+
* they are lost on restart and not shared between instances or serverless invocations, so a nonce issued by one
|
|
384
|
+
* instance is rejected by another.
|
|
297
385
|
*/
|
|
298
386
|
declare class MemorySiwxNonceStore implements SiwxNonceStore {
|
|
299
387
|
private nonces;
|
|
388
|
+
/**
|
|
389
|
+
* @param options - Store options.
|
|
390
|
+
* @param options.allowInProduction - Allows the store when `NODE_ENV` is `production`.
|
|
391
|
+
* @throws {Error} When `process.env.NODE_ENV` is `production` and `allowInProduction` is not `true`.
|
|
392
|
+
*/
|
|
300
393
|
constructor(options?: {
|
|
301
394
|
allowInProduction?: boolean;
|
|
302
395
|
});
|
|
@@ -309,11 +402,17 @@ declare class MemorySiwxNonceStore implements SiwxNonceStore {
|
|
|
309
402
|
}): Promise<boolean>;
|
|
310
403
|
}
|
|
311
404
|
/**
|
|
312
|
-
*
|
|
313
|
-
*
|
|
405
|
+
* Reads the SIWX session of the current request from its session cookie. Use it in Server Actions, Route Handlers
|
|
406
|
+
* or any server code instead of trusting session data sent by the client.
|
|
407
|
+
*
|
|
408
|
+
* With `sessionStore`, the cookie value is a session ID looked up with `sessionStore.get` (which enforces
|
|
409
|
+
* expiry). Otherwise, with `signingSecret`, it is a demo token verified with {@link verifyStatelessDemoSession}.
|
|
410
|
+
* See {@link GetSiwxServerSessionOptions} for the accepted cookie sources and the policy checks.
|
|
314
411
|
*
|
|
315
|
-
* @param options -
|
|
316
|
-
* @returns The
|
|
412
|
+
* @param options - Cookie source, store or secret, and optional policy.
|
|
413
|
+
* @returns The session, or `null` when there is no cookie, no store or secret, or the session is unknown, expired or
|
|
414
|
+
* rejected by the policy.
|
|
415
|
+
* @throws Errors thrown by `sessionStore.get` are not caught.
|
|
317
416
|
*
|
|
318
417
|
* @example
|
|
319
418
|
* ```ts
|
|
@@ -330,4 +429,4 @@ declare class MemorySiwxNonceStore implements SiwxNonceStore {
|
|
|
330
429
|
*/
|
|
331
430
|
declare function getSiwxServerSession(options: GetSiwxServerSessionOptions): Promise<SiwxSession | null>;
|
|
332
431
|
|
|
333
|
-
export { type CookieOptions, type GetSiwxServerSessionOptions, MemorySiwxNonceStore, MemorySiwxSessionStore, type ServerVerifyOptions, type ServerVerifyResult, type SiwxNonceStore, type SiwxSession, type SiwxSessionRecord, type SiwxSessionStore, type StatelessDemoLimits, type StatelessDemoTokenPayload, createClearCookie, createSessionCookie, getSiwxServerSession, parseCookie, signStatelessDemoSession, toSession, verifySiwxPayload, verifyStatelessDemoSession };
|
|
432
|
+
export { type CookieOptions, type GetSiwxServerSessionOptions, MemorySiwxNonceStore, MemorySiwxSessionStore, type ServerVerifyOptions, type ServerVerifyResult, type SiwxNonceStore, type SiwxSession, type SiwxSessionRecord, type SiwxSessionStore, type StatelessDemoLimits, type StatelessDemoTokenPayload, createClearCookie, createSessionCookie, getSiwxServerSession, issueStatelessDemoNonce, parseCookie, signStatelessDemoSession, toSession, verifySiwxPayload, verifyStatelessDemoNonce, verifyStatelessDemoSession };
|