@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
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
import * as _tuwaio_siwx_core from '@tuwaio/siwx-core';
|
|
2
|
+
import { SiwxVerificationPolicy, SiwxVerifyResult, ParsedSiwxMessage, SiwxVerifyPayload } from '@tuwaio/siwx-core';
|
|
3
|
+
import { EvmPublicClientSource } from '@tuwaio/siwx-evm';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @file Server-side types for the @tuwaio/siwx-server package.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Options of {@link verifySiwxPayload}.
|
|
11
|
+
*/
|
|
12
|
+
interface ServerVerifyOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Nonces that must be rejected. Verification fails when the message nonce is in the set. The set is only read:
|
|
15
|
+
* record used nonces yourself, or use a {@link SiwxNonceStore} (as `createSiwxApiHandler` does) for
|
|
16
|
+
* multi-instance deployments.
|
|
17
|
+
*/
|
|
18
|
+
usedNonces?: Set<string>;
|
|
19
|
+
/**
|
|
20
|
+
* Skips the check that the message `expirationTime` has not passed. Not recommended in production.
|
|
21
|
+
* @default false
|
|
22
|
+
*/
|
|
23
|
+
skipExpiration?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Verification policy enforced on the message fields (domain, URI, chains, timing). See
|
|
26
|
+
* {@link SiwxVerificationPolicy}.
|
|
27
|
+
*/
|
|
28
|
+
policy?: SiwxVerificationPolicy;
|
|
29
|
+
/**
|
|
30
|
+
* Enables the smart contract wallet fallback for `eip155` messages (deployed wallets through EIP-1271, wallets not
|
|
31
|
+
* deployed yet through ERC-6492): a viem client, used only for messages of its own chain, or a function that
|
|
32
|
+
* returns the client of a chain number, for sign-ins on several chains. See `EvmPublicClientSource` from
|
|
33
|
+
* `@tuwaio/siwx-evm`. Ignored for Solana.
|
|
34
|
+
*/
|
|
35
|
+
publicClient?: EvmPublicClientSource;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Result of {@link verifySiwxPayload}.
|
|
39
|
+
*/
|
|
40
|
+
interface ServerVerifyResult extends SiwxVerifyResult {
|
|
41
|
+
/**
|
|
42
|
+
* The CAIP-2 namespace whose verifier checked the signature: `eip155` or `solana`. Absent when verification
|
|
43
|
+
* failed before the signature check.
|
|
44
|
+
*/
|
|
45
|
+
namespace?: 'eip155' | 'solana';
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Serializable session derived from a verified CAIP-122 message (see {@link toSession}). Returned as JSON by the
|
|
49
|
+
* `verify` and `session` endpoints of `@tuwaio/siwx-server/next`.
|
|
50
|
+
*/
|
|
51
|
+
interface SiwxSession {
|
|
52
|
+
/** The verified CAIP-10 blockchain address. */
|
|
53
|
+
address: string;
|
|
54
|
+
/** The CAIP-2 chain ID the session is bound to. */
|
|
55
|
+
chainId: string;
|
|
56
|
+
/** The domain that issued the session. */
|
|
57
|
+
domain: string;
|
|
58
|
+
/** The nonce of the signed message. It must be single-use: consume it in a {@link SiwxNonceStore}. */
|
|
59
|
+
nonce: string;
|
|
60
|
+
/** ISO 8601 timestamp when the session was issued. */
|
|
61
|
+
issuedAt: string;
|
|
62
|
+
/** ISO 8601 timestamp when the session expires, if set. */
|
|
63
|
+
expirationTime?: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A session stored in a {@link SiwxSessionStore}.
|
|
67
|
+
*/
|
|
68
|
+
interface SiwxSessionRecord {
|
|
69
|
+
/** Opaque, unguessable session ID. It is the value of the session cookie. */
|
|
70
|
+
id: string;
|
|
71
|
+
/** The verified SIWX session data. */
|
|
72
|
+
session: SiwxSession;
|
|
73
|
+
/** Optional ID of your own user record (for example a database user ID), set with `bindSubject`. */
|
|
74
|
+
subjectId?: string;
|
|
75
|
+
/** Timestamp in milliseconds when the session record was created. */
|
|
76
|
+
createdAt: number;
|
|
77
|
+
/** Timestamp in milliseconds when the session record expires. */
|
|
78
|
+
expiresAt: number;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Storage contract for durable sessions (Redis, SQL, KV…), used by `createSiwxApiHandler` and
|
|
82
|
+
* {@link getSiwxServerSession}. Implementations must be shared by every server instance.
|
|
83
|
+
* {@link MemorySiwxSessionStore} is an in-memory implementation for development and tests.
|
|
84
|
+
*/
|
|
85
|
+
interface SiwxSessionStore {
|
|
86
|
+
/**
|
|
87
|
+
* Creates and stores a new session record.
|
|
88
|
+
* @param input - The session to store.
|
|
89
|
+
* @param input.session - The verified session data.
|
|
90
|
+
* @param input.ttlSeconds - Time-to-live in seconds.
|
|
91
|
+
* @returns The created record. Its `id` must be unguessable, because it becomes the session cookie value.
|
|
92
|
+
*/
|
|
93
|
+
create(input: {
|
|
94
|
+
session: SiwxSession;
|
|
95
|
+
ttlSeconds: number;
|
|
96
|
+
}): Promise<SiwxSessionRecord>;
|
|
97
|
+
/**
|
|
98
|
+
* Retrieves a session record by its ID. Session expiry is enforced here: SIWX does not compare `expiresAt` itself.
|
|
99
|
+
* @param id - The session ID.
|
|
100
|
+
* @returns The session record, or `null` if it does not exist or has expired.
|
|
101
|
+
*/
|
|
102
|
+
get(id: string): Promise<SiwxSessionRecord | null>;
|
|
103
|
+
/**
|
|
104
|
+
* Binds one of your user IDs to the session. Not called by SIWX; use it after sign-in to link the wallet session to
|
|
105
|
+
* your own user record.
|
|
106
|
+
* @param id - The session ID.
|
|
107
|
+
* @param subjectId - The user or subject ID.
|
|
108
|
+
* @returns `true` if the session exists and was updated, `false` otherwise.
|
|
109
|
+
*/
|
|
110
|
+
bindSubject(id: string, subjectId: string): Promise<boolean>;
|
|
111
|
+
/**
|
|
112
|
+
* Revokes a session. Must succeed when the session does not exist.
|
|
113
|
+
* @param id - The session ID.
|
|
114
|
+
* @returns A promise that resolves once the session is removed.
|
|
115
|
+
*/
|
|
116
|
+
revoke(id: string): Promise<void>;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Storage contract for single-use challenge nonces, used by `createSiwxApiHandler`. Implementations must be shared
|
|
120
|
+
* by every server instance and `consume` must be atomic (for example Redis `GETDEL`).
|
|
121
|
+
* {@link MemorySiwxNonceStore} is an in-memory implementation for development and tests.
|
|
122
|
+
*/
|
|
123
|
+
interface SiwxNonceStore {
|
|
124
|
+
/**
|
|
125
|
+
* Stores a newly issued nonce.
|
|
126
|
+
* @param input - The nonce to store.
|
|
127
|
+
* @param input.nonce - The nonce string.
|
|
128
|
+
* @param input.ttlSeconds - Time-to-live in seconds. `createSiwxApiHandler` uses 300.
|
|
129
|
+
* @returns A promise that resolves once the nonce is stored.
|
|
130
|
+
*/
|
|
131
|
+
issue(input: {
|
|
132
|
+
nonce: string;
|
|
133
|
+
ttlSeconds: number;
|
|
134
|
+
}): Promise<void>;
|
|
135
|
+
/**
|
|
136
|
+
* Atomically removes a nonce, so that each nonce is accepted once.
|
|
137
|
+
* @param input - The nonce to consume.
|
|
138
|
+
* @param input.nonce - The nonce string to consume.
|
|
139
|
+
* @returns `true` if the nonce was issued, not expired and not consumed before; otherwise `false`.
|
|
140
|
+
*/
|
|
141
|
+
consume(input: {
|
|
142
|
+
nonce: string;
|
|
143
|
+
}): Promise<boolean>;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* JSON payload of a stateless demo session token (see {@link signStatelessDemoSession}). The token is signed, not
|
|
147
|
+
* encrypted: anyone holding it can read these fields.
|
|
148
|
+
*/
|
|
149
|
+
interface StatelessDemoTokenPayload {
|
|
150
|
+
/** Token format version. */
|
|
151
|
+
version: 1;
|
|
152
|
+
/** CAIP-10 account ID of the session. */
|
|
153
|
+
address: string;
|
|
154
|
+
/** CAIP-2 chain ID of the session. */
|
|
155
|
+
chainId: string;
|
|
156
|
+
/** Domain of the signed message. */
|
|
157
|
+
domain: string;
|
|
158
|
+
/** Nonce of the signed message. */
|
|
159
|
+
nonce: string;
|
|
160
|
+
/** `issuedAt` of the signed message. */
|
|
161
|
+
issuedAt: string;
|
|
162
|
+
/** Expiry of the token: the message `expirationTime`, or the issuing time plus the token TTL. */
|
|
163
|
+
expirationTime?: string;
|
|
164
|
+
/** Random ID of the token. */
|
|
165
|
+
sessionId: string;
|
|
166
|
+
/** Token mode. */
|
|
167
|
+
mode: 'demo';
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Request limits of `createStatelessDemoSiwxHandler`. Only the body size is limited; rate limiting is not part of
|
|
171
|
+
* SIWX.
|
|
172
|
+
*/
|
|
173
|
+
interface StatelessDemoLimits {
|
|
174
|
+
/**
|
|
175
|
+
* Maximum body size of `POST /verify`. Larger requests are rejected with HTTP 413. The durable handler always
|
|
176
|
+
* uses 65536.
|
|
177
|
+
* @default 65536 (64 KB)
|
|
178
|
+
*/
|
|
179
|
+
maxTransactionPayloadBytes?: number;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Attributes of the session cookie, used by {@link createSessionCookie}, {@link createClearCookie} and the
|
|
183
|
+
* `@tuwaio/siwx-server/next` handlers. The cookie is always `HttpOnly`.
|
|
184
|
+
*/
|
|
185
|
+
interface CookieOptions {
|
|
186
|
+
/**
|
|
187
|
+
* The name of the cookie.
|
|
188
|
+
* @default "siwx-session-v2"
|
|
189
|
+
*/
|
|
190
|
+
name?: string;
|
|
191
|
+
/**
|
|
192
|
+
* `Max-Age` in seconds. {@link createSessionCookie} defaults to 604800 (7 days). The handlers use their
|
|
193
|
+
* `ttlSeconds` instead, and fall back to this value when `ttlSeconds` is not set.
|
|
194
|
+
* @default 604800
|
|
195
|
+
*/
|
|
196
|
+
maxAge?: number;
|
|
197
|
+
/**
|
|
198
|
+
* The cookie path.
|
|
199
|
+
* @default "/"
|
|
200
|
+
*/
|
|
201
|
+
path?: string;
|
|
202
|
+
/**
|
|
203
|
+
* The cookie domain.
|
|
204
|
+
*/
|
|
205
|
+
domain?: string;
|
|
206
|
+
/**
|
|
207
|
+
* Whether to set the Secure flag.
|
|
208
|
+
* @default true
|
|
209
|
+
*/
|
|
210
|
+
secure?: boolean;
|
|
211
|
+
/**
|
|
212
|
+
* The SameSite policy.
|
|
213
|
+
* @default "Strict"
|
|
214
|
+
*/
|
|
215
|
+
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Options of {@link getSiwxServerSession}.
|
|
219
|
+
*/
|
|
220
|
+
interface GetSiwxServerSessionOptions {
|
|
221
|
+
/**
|
|
222
|
+
* Where to read the session cookie from:
|
|
223
|
+
* - a `Cookie` header string (`"siwx-session-v2=…; other=…"`) or the bare cookie value;
|
|
224
|
+
* - a Web API `Request`;
|
|
225
|
+
* - the Next.js cookie store (`await cookies()`) or any object with `get(name)` returning `{ value }` or a string;
|
|
226
|
+
* - a Web API `Headers` object.
|
|
227
|
+
*
|
|
228
|
+
* `null` and `undefined` resolve to no session.
|
|
229
|
+
*/
|
|
230
|
+
cookieSource: string | Request | Headers | {
|
|
231
|
+
get(name: string): {
|
|
232
|
+
value: string;
|
|
233
|
+
} | string | undefined | null;
|
|
234
|
+
} | null | undefined;
|
|
235
|
+
/**
|
|
236
|
+
* The name of the cookie.
|
|
237
|
+
* @default "siwx-session-v2"
|
|
238
|
+
*/
|
|
239
|
+
cookieName?: string;
|
|
240
|
+
/**
|
|
241
|
+
* Store of the durable profile. The cookie value is looked up with `sessionStore.get`. Takes precedence over
|
|
242
|
+
* `signingSecret`.
|
|
243
|
+
*/
|
|
244
|
+
sessionStore?: SiwxSessionStore;
|
|
245
|
+
/**
|
|
246
|
+
* HMAC secret of the stateless demo profile. The cookie value is verified with
|
|
247
|
+
* {@link verifyStatelessDemoSession}.
|
|
248
|
+
*/
|
|
249
|
+
signingSecret?: string;
|
|
250
|
+
/**
|
|
251
|
+
* Policy checked against the stored session. Only a subset applies: `expectedDomain`, `allowedChainIds` (exact
|
|
252
|
+
* match, except that a Solana cluster matches under its name and its genesis-hash chain ID, so sessions signed for
|
|
253
|
+
* `solana:devnet` stay valid) and, for durable sessions, `requireExpirationTime`; for demo tokens, expiry with
|
|
254
|
+
* `clockSkewSeconds`.
|
|
255
|
+
*/
|
|
256
|
+
policy?: SiwxVerificationPolicy;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* JWS algorithm of the JWTs issued for SIWX sessions: `ES256` (ECDSA with P-256 and SHA-256) or `RS256`
|
|
260
|
+
* (RSASSA-PKCS1-v1_5 with SHA-256). Both are accepted by embedded wallet providers with custom authentication, such
|
|
261
|
+
* as Coinbase CDP.
|
|
262
|
+
*/
|
|
263
|
+
type SiwxJwtAlgorithm = 'ES256' | 'RS256';
|
|
264
|
+
/**
|
|
265
|
+
* Public JSON Web Key of a JWT signing key, as published in a JWKS (see {@link SiwxJwks}). Holds no private members.
|
|
266
|
+
*/
|
|
267
|
+
interface SiwxPublicJwk {
|
|
268
|
+
/** Key type: `EC` for ES256, `RSA` for RS256. */
|
|
269
|
+
kty: 'EC' | 'RSA';
|
|
270
|
+
/** Curve of an EC key. Always `P-256`. */
|
|
271
|
+
crv?: 'P-256';
|
|
272
|
+
/** x coordinate of an EC key, base64url. */
|
|
273
|
+
x?: string;
|
|
274
|
+
/** y coordinate of an EC key, base64url. */
|
|
275
|
+
y?: string;
|
|
276
|
+
/** Modulus of an RSA key, base64url. */
|
|
277
|
+
n?: string;
|
|
278
|
+
/** Public exponent of an RSA key, base64url. */
|
|
279
|
+
e?: string;
|
|
280
|
+
/** The algorithm the key signs with. */
|
|
281
|
+
alg: SiwxJwtAlgorithm;
|
|
282
|
+
/** Key ID: the RFC 7638 thumbprint of the key. Written into the `kid` header of every token the key signs. */
|
|
283
|
+
kid: string;
|
|
284
|
+
/** Public key use. Always `sig`. */
|
|
285
|
+
use: 'sig';
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* A JWT signing key, created by `importSiwxJwtKey`.
|
|
289
|
+
*/
|
|
290
|
+
interface SiwxJwtKey {
|
|
291
|
+
/** The algorithm the key signs with. */
|
|
292
|
+
alg: SiwxJwtAlgorithm;
|
|
293
|
+
/** Key ID: the RFC 7638 thumbprint of the key. */
|
|
294
|
+
kid: string;
|
|
295
|
+
/** The private key. Not extractable, usable only to sign. */
|
|
296
|
+
privateKey: CryptoKey;
|
|
297
|
+
/** The public key to publish in the JWKS. */
|
|
298
|
+
publicJwk: SiwxPublicJwk;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* JSON Web Key Set (RFC 7517) with the public keys that verify the JWTs of SIWX sessions. Serve it at a public HTTPS
|
|
302
|
+
* URL and give that URL to the services that accept the tokens.
|
|
303
|
+
*/
|
|
304
|
+
interface SiwxJwks {
|
|
305
|
+
/** The public keys. */
|
|
306
|
+
keys: SiwxPublicJwk[];
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* The `jwt` option of `createSiwxApiHandler` (`@tuwaio/siwx-server/next`): enables `GET …/token` (a fresh JWT for
|
|
310
|
+
* the session cookie) and `GET …/jwks` (the public keys).
|
|
311
|
+
*/
|
|
312
|
+
interface SiwxJwtOptions {
|
|
313
|
+
/**
|
|
314
|
+
* The key that signs the tokens, from `importSiwxJwtKey`. A promise is accepted, so the route file needs no
|
|
315
|
+
* top-level `await`; an import error then surfaces as a 500 on the first `/token` or `/jwks` request.
|
|
316
|
+
*/
|
|
317
|
+
signingKey: SiwxJwtKey | Promise<SiwxJwtKey>;
|
|
318
|
+
/**
|
|
319
|
+
* Keys replaced by `signingKey`, published in the JWKS so that tokens they signed keep verifying until they
|
|
320
|
+
* expire. Signing keys or public JWKs.
|
|
321
|
+
*/
|
|
322
|
+
previousKeys?: ReadonlyArray<SiwxJwtKey | SiwxPublicJwk>;
|
|
323
|
+
/** The `iss` claim: the URL of your app. */
|
|
324
|
+
issuer: string;
|
|
325
|
+
/** The `aud` claim, when the receiving service expects one. */
|
|
326
|
+
audience?: string | string[];
|
|
327
|
+
/**
|
|
328
|
+
* Token lifetime in seconds, from 1 to 604800 (7 days). A token never outlives its session.
|
|
329
|
+
* @default 600
|
|
330
|
+
*/
|
|
331
|
+
ttlSeconds?: number;
|
|
332
|
+
/**
|
|
333
|
+
* Builds the `sub` claim from the session record. Defaults to the bound `subjectId`, otherwise the account without
|
|
334
|
+
* its chain (see `siwxJwtSubject`).
|
|
335
|
+
*/
|
|
336
|
+
subject?: (record: SiwxSessionRecord) => string | Promise<string>;
|
|
337
|
+
/** Extra claims from the session record. Reserved claims cannot be set. */
|
|
338
|
+
claims?: (record: SiwxSessionRecord) => Record<string, unknown> | Promise<Record<string, unknown>>;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Claims of a JWT issued for a SIWX session. Times are Unix seconds.
|
|
342
|
+
*/
|
|
343
|
+
interface SiwxJwtPayload {
|
|
344
|
+
/** Issuer: the URL of your app. */
|
|
345
|
+
iss: string;
|
|
346
|
+
/**
|
|
347
|
+
* Subject: a stable ID of the user. By default the user ID bound with `bindSubject`, otherwise the account without
|
|
348
|
+
* its chain (`eip155:0x…` in lowercase, `solana:<address>`).
|
|
349
|
+
*/
|
|
350
|
+
sub: string;
|
|
351
|
+
/** Audience, when one was set. */
|
|
352
|
+
aud?: string | string[];
|
|
353
|
+
/** Issued at. */
|
|
354
|
+
iat: number;
|
|
355
|
+
/** Expiration time. */
|
|
356
|
+
exp: number;
|
|
357
|
+
/** Unique token ID: 16 random bytes, base64url. */
|
|
358
|
+
jti: string;
|
|
359
|
+
/** The signed-in CAIP-10 account as it was signed, with Solana chain IDs in their genesis-hash form. */
|
|
360
|
+
caip10: string;
|
|
361
|
+
/** The CAIP-2 chain ID of the sign-in, with Solana chain IDs in their genesis-hash form. */
|
|
362
|
+
chain_id: string;
|
|
363
|
+
/** Custom claims added when the token was signed. */
|
|
364
|
+
[claim: string]: unknown;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Converts a verified CAIP-122 message into a {@link SiwxSession}: keeps `address`, `chainId`, `domain`, `nonce`,
|
|
368
|
+
* `issuedAt` and `expirationTime`. Pure function.
|
|
369
|
+
*
|
|
370
|
+
* @param parsed - The verified message, for example `result.data` of {@link verifySiwxPayload}.
|
|
371
|
+
* @returns The session object to store or sign.
|
|
372
|
+
*/
|
|
373
|
+
declare function toSession(parsed: ParsedSiwxMessage): SiwxSession;
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Verifies a signed CAIP-122 message on the server, for any supported chain.
|
|
377
|
+
*
|
|
378
|
+
* Steps: parse the message, run {@link validateMessage} with `options.policy` (format, expiration with
|
|
379
|
+
* `clockSkewSeconds`, `notBefore` and the policy rules), reject nonces listed in `options.usedNonces`, then route by the `chainId` namespace to `verifyEvmSignature`
|
|
380
|
+
* (`@tuwaio/siwx-evm`, with the smart contract wallet fallback, EIP-1271 and ERC-6492, when `options.publicClient`
|
|
381
|
+
* gives a client for the chain of the message) or `verifyEd25519`
|
|
382
|
+
* (`@tuwaio/siwx-solana`).
|
|
383
|
+
*
|
|
384
|
+
* Side effects: dynamically imports the chain package of the namespace, so `@tuwaio/siwx-evm` and/or
|
|
385
|
+
* `@tuwaio/siwx-solana` must be installed for the chains you accept; the smart contract wallet fallback makes one RPC call.
|
|
386
|
+
* It does not consume nonces or create sessions: pair it with a {@link SiwxNonceStore} and a
|
|
387
|
+
* {@link SiwxSessionStore}, or use the handlers of `@tuwaio/siwx-server/next`.
|
|
388
|
+
*
|
|
389
|
+
* @param payload - The `{ message, signature }` sent by the client.
|
|
390
|
+
* @param options - Policy, replay protection and chain options.
|
|
391
|
+
* @returns `{ success: true, data, namespace }` with the parsed message, or `{ success: false, error }`. Never
|
|
392
|
+
* throws: every failure is returned as `error`.
|
|
393
|
+
*/
|
|
394
|
+
declare function verifySiwxPayload(payload: SiwxVerifyPayload, options?: ServerVerifyOptions): Promise<ServerVerifyResult>;
|
|
395
|
+
/**
|
|
396
|
+
* Creates a stateless demo session token: the session as base64url JSON ({@link StatelessDemoTokenPayload}) plus
|
|
397
|
+
* an HMAC-SHA256 signature (Web Crypto). The token is signed, not encrypted, and cannot be revoked before it
|
|
398
|
+
* expires.
|
|
399
|
+
*
|
|
400
|
+
* @param session - The verified session to sign.
|
|
401
|
+
* @param secret - Server-only signing secret of at least 32 characters.
|
|
402
|
+
* @param ttlSeconds - Token lifetime in seconds, used only when the session has no `expirationTime`; otherwise the
|
|
403
|
+
* token expires with the message.
|
|
404
|
+
* @returns The token, formatted as `{payload}.{signature}`.
|
|
405
|
+
* @throws {Error} If `secret` is shorter than 32 characters.
|
|
406
|
+
*/
|
|
407
|
+
declare function signStatelessDemoSession(session: SiwxSession, secret: string, ttlSeconds?: number): Promise<string>;
|
|
408
|
+
/**
|
|
409
|
+
* Verifies a token created by {@link signStatelessDemoSession}: checks the HMAC signature with Web Crypto, the
|
|
410
|
+
* token version and mode, and its expiry (with `policy.clockSkewSeconds`, 60 seconds by default).
|
|
411
|
+
*
|
|
412
|
+
* Only `expectedDomain` and `allowedChainIds` (exact match, except that a Solana cluster matches under its name and its
|
|
413
|
+
* genesis-hash chain ID) of the policy are applied; other policy fields are ignored because they were checked when the
|
|
414
|
+
* token was issued.
|
|
415
|
+
*
|
|
416
|
+
* @param token - The token from the session cookie.
|
|
417
|
+
* @param secret - The secret the token was signed with.
|
|
418
|
+
* @param policy - Optional policy to check against the token.
|
|
419
|
+
* @returns The session, or `null` if the token is missing, malformed, tampered, expired or rejected by the policy.
|
|
420
|
+
* Never throws.
|
|
421
|
+
*/
|
|
422
|
+
declare function verifyStatelessDemoSession(token: string | null | undefined, secret: string, policy?: _tuwaio_siwx_core.SiwxVerificationPolicy): Promise<SiwxSession | null>;
|
|
423
|
+
/**
|
|
424
|
+
* Issues a challenge nonce for the stateless demo profile: a random value and an expiry time, signed with
|
|
425
|
+
* HMAC-SHA256 (Web Crypto), encoded as 106 lowercase hex characters so that it is a valid CAIP-122 nonce.
|
|
426
|
+
* {@link verifyStatelessDemoNonce} can then check, without any storage, that the server issued the nonce and that it
|
|
427
|
+
* has not expired.
|
|
428
|
+
*
|
|
429
|
+
* @param secret - Server-only signing secret of at least 32 characters.
|
|
430
|
+
* @param ttlSeconds - How long the nonce can be used, in seconds.
|
|
431
|
+
* @returns The signed nonce.
|
|
432
|
+
* @throws {Error} If `secret` is shorter than 32 characters.
|
|
433
|
+
*/
|
|
434
|
+
declare function issueStatelessDemoNonce(secret: string, ttlSeconds?: number): Promise<string>;
|
|
435
|
+
/**
|
|
436
|
+
* Checks a nonce issued by {@link issueStatelessDemoNonce}: its format, its HMAC signature (constant-time, Web
|
|
437
|
+
* Crypto) and its expiry. It does not track usage, so the same nonce passes until it expires; callers that need
|
|
438
|
+
* single use must remember consumed nonces (`createStatelessDemoSiwxHandler` does so per server instance).
|
|
439
|
+
*
|
|
440
|
+
* @param nonce - The nonce of the signed message.
|
|
441
|
+
* @param secret - The secret the nonce was issued with.
|
|
442
|
+
* @returns `true` if the nonce was issued with `secret` and has not expired; otherwise `false`. Never throws.
|
|
443
|
+
*/
|
|
444
|
+
declare function verifyStatelessDemoNonce(nonce: string, secret: string): Promise<boolean>;
|
|
445
|
+
/**
|
|
446
|
+
* Formats a `Set-Cookie` header value for the session cookie: `HttpOnly`, `Secure` and `SameSite=Strict` by
|
|
447
|
+
* default.
|
|
448
|
+
*
|
|
449
|
+
* @param value - The cookie value (session ID or demo token). Written as-is, without encoding.
|
|
450
|
+
* @param opts - Cookie attributes. Defaults: name `siwx-session-v2`, `Max-Age` 604800, path `/`.
|
|
451
|
+
* @returns The header value, e.g. `siwx-session-v2=…; Max-Age=604800; Path=/; HttpOnly; SameSite=Strict; Secure`.
|
|
452
|
+
*/
|
|
453
|
+
declare function createSessionCookie(value: string, opts?: CookieOptions): string;
|
|
454
|
+
/**
|
|
455
|
+
* Formats a `Set-Cookie` header value that deletes the session cookie (`Max-Age=0` and an expiry date in the
|
|
456
|
+
* past). Use the same `name`, `path` and `domain` as when the cookie was set.
|
|
457
|
+
*
|
|
458
|
+
* @param opts - Cookie attributes. `maxAge` is ignored.
|
|
459
|
+
* @returns The header value.
|
|
460
|
+
*/
|
|
461
|
+
declare function createClearCookie(opts?: CookieOptions): string;
|
|
462
|
+
/**
|
|
463
|
+
* Reads one cookie from a `Cookie` request header. Pairs must be separated by `"; "`; values are returned as-is,
|
|
464
|
+
* without URL-decoding.
|
|
465
|
+
*
|
|
466
|
+
* @param cookieHeader - The `Cookie` header value.
|
|
467
|
+
* @param name - The cookie name.
|
|
468
|
+
* @returns The cookie value, or `null` when the header is empty or has no such cookie.
|
|
469
|
+
*/
|
|
470
|
+
declare function parseCookie(cookieHeader: string | null | undefined, name: string): string | null;
|
|
471
|
+
/**
|
|
472
|
+
* In-memory {@link SiwxSessionStore} for local development and tests. Sessions live in a `Map` of the current
|
|
473
|
+
* process: they are lost on restart and not shared between instances or serverless invocations. Session IDs are
|
|
474
|
+
* 32-character hex strings from {@link generateNonce}.
|
|
475
|
+
*/
|
|
476
|
+
declare class MemorySiwxSessionStore implements SiwxSessionStore {
|
|
477
|
+
private records;
|
|
478
|
+
/**
|
|
479
|
+
* @param options - Store options.
|
|
480
|
+
* @param options.allowInProduction - Allows the store when `NODE_ENV` is `production`.
|
|
481
|
+
* @throws {Error} When `process.env.NODE_ENV` is `production` and `allowInProduction` is not `true`.
|
|
482
|
+
*/
|
|
483
|
+
constructor(options?: {
|
|
484
|
+
allowInProduction?: boolean;
|
|
485
|
+
});
|
|
486
|
+
create(input: {
|
|
487
|
+
session: SiwxSession;
|
|
488
|
+
ttlSeconds: number;
|
|
489
|
+
}): Promise<SiwxSessionRecord>;
|
|
490
|
+
get(id: string): Promise<SiwxSessionRecord | null>;
|
|
491
|
+
bindSubject(id: string, subjectId: string): Promise<boolean>;
|
|
492
|
+
revoke(id: string): Promise<void>;
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* In-memory {@link SiwxNonceStore} for local development and tests. Nonces live in a `Map` of the current process:
|
|
496
|
+
* they are lost on restart and not shared between instances or serverless invocations, so a nonce issued by one
|
|
497
|
+
* instance is rejected by another.
|
|
498
|
+
*/
|
|
499
|
+
declare class MemorySiwxNonceStore implements SiwxNonceStore {
|
|
500
|
+
private nonces;
|
|
501
|
+
/**
|
|
502
|
+
* @param options - Store options.
|
|
503
|
+
* @param options.allowInProduction - Allows the store when `NODE_ENV` is `production`.
|
|
504
|
+
* @throws {Error} When `process.env.NODE_ENV` is `production` and `allowInProduction` is not `true`.
|
|
505
|
+
*/
|
|
506
|
+
constructor(options?: {
|
|
507
|
+
allowInProduction?: boolean;
|
|
508
|
+
});
|
|
509
|
+
issue(input: {
|
|
510
|
+
nonce: string;
|
|
511
|
+
ttlSeconds: number;
|
|
512
|
+
}): Promise<void>;
|
|
513
|
+
consume(input: {
|
|
514
|
+
nonce: string;
|
|
515
|
+
}): Promise<boolean>;
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Reads the SIWX session of the current request from its session cookie. Use it in Server Actions, Route Handlers
|
|
519
|
+
* or any server code instead of trusting session data sent by the client.
|
|
520
|
+
*
|
|
521
|
+
* With `sessionStore`, the cookie value is a session ID looked up with `sessionStore.get` (which enforces
|
|
522
|
+
* expiry). Otherwise, with `signingSecret`, it is a demo token verified with {@link verifyStatelessDemoSession}.
|
|
523
|
+
* See {@link GetSiwxServerSessionOptions} for the accepted cookie sources and the policy checks.
|
|
524
|
+
*
|
|
525
|
+
* @param options - Cookie source, store or secret, and optional policy.
|
|
526
|
+
* @returns The session, or `null` when there is no cookie, no store or secret, or the session is unknown, expired or
|
|
527
|
+
* rejected by the policy.
|
|
528
|
+
* @throws Errors thrown by `sessionStore.get` are not caught.
|
|
529
|
+
*
|
|
530
|
+
* @example
|
|
531
|
+
* ```ts
|
|
532
|
+
* // In Next.js Server Actions:
|
|
533
|
+
* import { cookies } from 'next/headers';
|
|
534
|
+
* import { getSiwxServerSession } from '@tuwaio/siwx-server';
|
|
535
|
+
* import { sessionStore } from '@/lib/authStores';
|
|
536
|
+
*
|
|
537
|
+
* const session = await getSiwxServerSession({
|
|
538
|
+
* cookieSource: await cookies(),
|
|
539
|
+
* sessionStore,
|
|
540
|
+
* });
|
|
541
|
+
* ```
|
|
542
|
+
*/
|
|
543
|
+
declare function getSiwxServerSession(options: GetSiwxServerSessionOptions): Promise<SiwxSession | null>;
|
|
544
|
+
|
|
545
|
+
export { type CookieOptions as C, type GetSiwxServerSessionOptions as G, MemorySiwxNonceStore as M, type SiwxSession as S, type SiwxJwtKey as a, type SiwxJwks as b, type SiwxPublicJwk as c, type SiwxJwtPayload as d, type SiwxJwtAlgorithm as e, MemorySiwxSessionStore as f, type ServerVerifyOptions as g, type ServerVerifyResult as h, type SiwxJwtOptions as i, type SiwxNonceStore as j, type SiwxSessionRecord as k, type SiwxSessionStore as l, type StatelessDemoLimits as m, type StatelessDemoTokenPayload as n, createClearCookie as o, createSessionCookie as p, getSiwxServerSession as q, issueStatelessDemoNonce as r, parseCookie as s, signStatelessDemoSession as t, toSession as u, verifySiwxPayload as v, verifyStatelessDemoNonce as w, verifyStatelessDemoSession as x };
|