@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/index.d.ts
CHANGED
|
@@ -1,435 +1,173 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { S as SiwxSession, a as SiwxJwtKey, b as SiwxJwks, c as SiwxPublicJwk, d as SiwxJwtPayload, e as SiwxJwtAlgorithm } from './server-b6Tp6Wea.js';
|
|
2
|
+
export { C as CookieOptions, G as GetSiwxServerSessionOptions, M as MemorySiwxNonceStore, f as MemorySiwxSessionStore, g as ServerVerifyOptions, h as ServerVerifyResult, i as SiwxJwtOptions, j as SiwxNonceStore, k as SiwxSessionRecord, l as SiwxSessionStore, m as StatelessDemoLimits, n as StatelessDemoTokenPayload, o as createClearCookie, p as createSessionCookie, q as getSiwxServerSession, r as issueStatelessDemoNonce, s as parseCookie, t as signStatelessDemoSession, u as toSession, v as verifySiwxPayload, w as verifyStatelessDemoNonce, x as verifyStatelessDemoSession } from './server-b6Tp6Wea.js';
|
|
3
3
|
export { ParsedSiwxMessage, SiwxMessageFields, SiwxSessionLike, SiwxVerificationPolicy, ValidateMessageOptions, generateNonce, generateNonce as generateServerNonce, isSessionMatchingTarget, validateMessage, validatePolicy } from '@tuwaio/siwx-core';
|
|
4
|
-
import
|
|
4
|
+
import '@tuwaio/siwx-evm';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* @file
|
|
7
|
+
* @file JWTs for SIWX sessions, so services that accept a sign-in only as a JWT verified with public keys (embedded
|
|
8
|
+
* wallet providers with custom authentication such as Coinbase CDP, identity platforms, your own services) can trust a
|
|
9
|
+
* wallet sign-in. Web Crypto API only.
|
|
8
10
|
*/
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
|
-
*
|
|
12
|
-
|
|
13
|
-
interface ServerVerifyOptions {
|
|
14
|
-
/**
|
|
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.
|
|
18
|
-
*/
|
|
19
|
-
usedNonces?: Set<string>;
|
|
20
|
-
/**
|
|
21
|
-
* Skips the check that the message `expirationTime` has not passed. Not recommended in production.
|
|
22
|
-
* @default false
|
|
23
|
-
*/
|
|
24
|
-
skipExpiration?: boolean;
|
|
25
|
-
/**
|
|
26
|
-
* Verification policy enforced on the message fields (domain, URI, chains, timing). See
|
|
27
|
-
* {@link SiwxVerificationPolicy}.
|
|
28
|
-
*/
|
|
29
|
-
policy?: SiwxVerificationPolicy;
|
|
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.
|
|
33
|
-
*/
|
|
34
|
-
publicClient?: PublicClient;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Result of {@link verifySiwxPayload}.
|
|
38
|
-
*/
|
|
39
|
-
interface ServerVerifyResult extends SiwxVerifyResult {
|
|
40
|
-
/**
|
|
41
|
-
* The CAIP-2 namespace whose verifier checked the signature: `eip155` or `solana`. Absent when verification
|
|
42
|
-
* failed before the signature check.
|
|
43
|
-
*/
|
|
44
|
-
namespace?: 'eip155' | 'solana';
|
|
45
|
-
}
|
|
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`.
|
|
49
|
-
*/
|
|
50
|
-
interface SiwxSession {
|
|
51
|
-
/** The verified CAIP-10 blockchain address. */
|
|
52
|
-
address: string;
|
|
53
|
-
/** The CAIP-2 chain ID the session is bound to. */
|
|
54
|
-
chainId: string;
|
|
55
|
-
/** The domain that issued the session. */
|
|
56
|
-
domain: string;
|
|
57
|
-
/** The nonce of the signed message. It must be single-use: consume it in a {@link SiwxNonceStore}. */
|
|
58
|
-
nonce: string;
|
|
59
|
-
/** ISO 8601 timestamp when the session was issued. */
|
|
60
|
-
issuedAt: string;
|
|
61
|
-
/** ISO 8601 timestamp when the session expires, if set. */
|
|
62
|
-
expirationTime?: string;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* A session stored in a {@link SiwxSessionStore}.
|
|
66
|
-
*/
|
|
67
|
-
interface SiwxSessionRecord {
|
|
68
|
-
/** Opaque, unguessable session ID. It is the value of the session cookie. */
|
|
69
|
-
id: string;
|
|
70
|
-
/** The verified SIWX session data. */
|
|
71
|
-
session: SiwxSession;
|
|
72
|
-
/** Optional ID of your own user record (for example a database user ID), set with `bindSubject`. */
|
|
73
|
-
subjectId?: string;
|
|
74
|
-
/** Timestamp in milliseconds when the session record was created. */
|
|
75
|
-
createdAt: number;
|
|
76
|
-
/** Timestamp in milliseconds when the session record expires. */
|
|
77
|
-
expiresAt: number;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
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.
|
|
83
|
-
*/
|
|
84
|
-
interface SiwxSessionStore {
|
|
85
|
-
/**
|
|
86
|
-
* Creates and stores a new session record.
|
|
87
|
-
* @param input - The session to store.
|
|
88
|
-
* @param input.session - The verified session data.
|
|
89
|
-
* @param input.ttlSeconds - Time-to-live in seconds.
|
|
90
|
-
* @returns The created record. Its `id` must be unguessable, because it becomes the session cookie value.
|
|
91
|
-
*/
|
|
92
|
-
create(input: {
|
|
93
|
-
session: SiwxSession;
|
|
94
|
-
ttlSeconds: number;
|
|
95
|
-
}): Promise<SiwxSessionRecord>;
|
|
96
|
-
/**
|
|
97
|
-
* Retrieves a session record by its ID. Session expiry is enforced here: SIWX does not compare `expiresAt` itself.
|
|
98
|
-
* @param id - The session ID.
|
|
99
|
-
* @returns The session record, or `null` if it does not exist or has expired.
|
|
100
|
-
*/
|
|
101
|
-
get(id: string): Promise<SiwxSessionRecord | null>;
|
|
102
|
-
/**
|
|
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.
|
|
105
|
-
* @param id - The session ID.
|
|
106
|
-
* @param subjectId - The user or subject ID.
|
|
107
|
-
* @returns `true` if the session exists and was updated, `false` otherwise.
|
|
108
|
-
*/
|
|
109
|
-
bindSubject(id: string, subjectId: string): Promise<boolean>;
|
|
110
|
-
/**
|
|
111
|
-
* Revokes a session. Must succeed when the session does not exist.
|
|
112
|
-
* @param id - The session ID.
|
|
113
|
-
* @returns A promise that resolves once the session is removed.
|
|
114
|
-
*/
|
|
115
|
-
revoke(id: string): Promise<void>;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
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.
|
|
121
|
-
*/
|
|
122
|
-
interface SiwxNonceStore {
|
|
123
|
-
/**
|
|
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.
|
|
129
|
-
*/
|
|
130
|
-
issue(input: {
|
|
131
|
-
nonce: string;
|
|
132
|
-
ttlSeconds: number;
|
|
133
|
-
}): Promise<void>;
|
|
134
|
-
/**
|
|
135
|
-
* Atomically removes a nonce, so that each nonce is accepted once.
|
|
136
|
-
* @param input - The nonce to consume.
|
|
137
|
-
* @param input.nonce - The nonce string to consume.
|
|
138
|
-
* @returns `true` if the nonce was issued, not expired and not consumed before; otherwise `false`.
|
|
139
|
-
*/
|
|
140
|
-
consume(input: {
|
|
141
|
-
nonce: string;
|
|
142
|
-
}): Promise<boolean>;
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
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.
|
|
147
|
-
*/
|
|
148
|
-
interface StatelessDemoTokenPayload {
|
|
149
|
-
/** Token format version. */
|
|
150
|
-
version: 1;
|
|
151
|
-
/** CAIP-10 account ID of the session. */
|
|
152
|
-
address: string;
|
|
153
|
-
/** CAIP-2 chain ID of the session. */
|
|
154
|
-
chainId: string;
|
|
155
|
-
/** Domain of the signed message. */
|
|
156
|
-
domain: string;
|
|
157
|
-
/** Nonce of the signed message. */
|
|
158
|
-
nonce: string;
|
|
159
|
-
/** `issuedAt` of the signed message. */
|
|
160
|
-
issuedAt: string;
|
|
161
|
-
/** Expiry of the token: the message `expirationTime`, or the issuing time plus the token TTL. */
|
|
162
|
-
expirationTime?: string;
|
|
163
|
-
/** Random ID of the token. */
|
|
164
|
-
sessionId: string;
|
|
165
|
-
/** Token mode. */
|
|
166
|
-
mode: 'demo';
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Request limits of `createStatelessDemoSiwxHandler`. Only the body size is limited; rate limiting is not part of
|
|
170
|
-
* SIWX.
|
|
171
|
-
*/
|
|
172
|
-
interface StatelessDemoLimits {
|
|
173
|
-
/**
|
|
174
|
-
* Maximum body size of `POST /verify`. Larger requests are rejected with HTTP 413. The durable handler always
|
|
175
|
-
* uses 65536.
|
|
176
|
-
* @default 65536 (64 KB)
|
|
177
|
-
*/
|
|
178
|
-
maxTransactionPayloadBytes?: number;
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
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`.
|
|
183
|
-
*/
|
|
184
|
-
interface CookieOptions {
|
|
185
|
-
/**
|
|
186
|
-
* The name of the cookie.
|
|
187
|
-
* @default "siwx-session-v2"
|
|
188
|
-
*/
|
|
189
|
-
name?: string;
|
|
190
|
-
/**
|
|
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.
|
|
193
|
-
* @default 604800
|
|
194
|
-
*/
|
|
195
|
-
maxAge?: number;
|
|
196
|
-
/**
|
|
197
|
-
* The cookie path.
|
|
198
|
-
* @default "/"
|
|
199
|
-
*/
|
|
200
|
-
path?: string;
|
|
201
|
-
/**
|
|
202
|
-
* The cookie domain.
|
|
203
|
-
*/
|
|
204
|
-
domain?: string;
|
|
205
|
-
/**
|
|
206
|
-
* Whether to set the Secure flag.
|
|
207
|
-
* @default true
|
|
208
|
-
*/
|
|
209
|
-
secure?: boolean;
|
|
210
|
-
/**
|
|
211
|
-
* The SameSite policy.
|
|
212
|
-
* @default "Strict"
|
|
213
|
-
*/
|
|
214
|
-
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Options of {@link getSiwxServerSession}.
|
|
218
|
-
*/
|
|
219
|
-
interface GetSiwxServerSessionOptions {
|
|
220
|
-
/**
|
|
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.
|
|
228
|
-
*/
|
|
229
|
-
cookieSource: string | Request | Headers | {
|
|
230
|
-
get(name: string): {
|
|
231
|
-
value: string;
|
|
232
|
-
} | string | undefined | null;
|
|
233
|
-
} | null | undefined;
|
|
234
|
-
/**
|
|
235
|
-
* The name of the cookie.
|
|
236
|
-
* @default "siwx-session-v2"
|
|
237
|
-
*/
|
|
238
|
-
cookieName?: string;
|
|
239
|
-
/**
|
|
240
|
-
* Store of the durable profile. The cookie value is looked up with `sessionStore.get`. Takes precedence over
|
|
241
|
-
* `signingSecret`.
|
|
242
|
-
*/
|
|
243
|
-
sessionStore?: SiwxSessionStore;
|
|
244
|
-
/**
|
|
245
|
-
* HMAC secret of the stateless demo profile. The cookie value is verified with
|
|
246
|
-
* {@link verifyStatelessDemoSession}.
|
|
247
|
-
*/
|
|
248
|
-
signingSecret?: string;
|
|
249
|
-
/**
|
|
250
|
-
* Policy checked against the stored session. Only a subset applies: `expectedDomain`, `allowedChainIds` (exact
|
|
251
|
-
* match, except that a Solana cluster matches under its name and its genesis-hash chain ID, so sessions signed for
|
|
252
|
-
* `solana:devnet` stay valid) and, for durable sessions, `requireExpirationTime`; for demo tokens, expiry with
|
|
253
|
-
* `clockSkewSeconds`.
|
|
254
|
-
*/
|
|
255
|
-
policy?: SiwxVerificationPolicy;
|
|
256
|
-
}
|
|
257
|
-
/**
|
|
258
|
-
* Converts a verified CAIP-122 message into a {@link SiwxSession}: keeps `address`, `chainId`, `domain`, `nonce`,
|
|
259
|
-
* `issuedAt` and `expirationTime`. Pure function.
|
|
13
|
+
* Returns the default `sub` of the JWT of a session: a stable ID of the user, so that signing in on another network
|
|
14
|
+
* does not look like another user to the service that receives the token.
|
|
260
15
|
*
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
|
|
264
|
-
declare function toSession(parsed: ParsedSiwxMessage): SiwxSession;
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Verifies a signed CAIP-122 message on the server, for any supported chain.
|
|
16
|
+
* - `subjectId`, when set and not empty (your own user ID, bound with `SiwxSessionStore.bindSubject`);
|
|
17
|
+
* - otherwise the account without its chain reference: `eip155:<address in lowercase>` for EVM,
|
|
18
|
+
* `solana:<address>` for Solana (base58 is case-sensitive and kept as is), `<namespace>:<address>` otherwise.
|
|
268
19
|
*
|
|
269
|
-
*
|
|
270
|
-
* `clockSkewSeconds`, `notBefore` and the policy rules), reject nonces listed in `options.usedNonces`, then route by the `chainId` namespace to `verifyEvmSignature`
|
|
271
|
-
* (`@tuwaio/siwx-evm`, with the EIP-1271 fallback when `options.publicClient` is set) or `verifyEd25519`
|
|
272
|
-
* (`@tuwaio/siwx-solana`).
|
|
20
|
+
* Pure function.
|
|
273
21
|
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
22
|
+
* @param session - The session of the signed-in wallet.
|
|
23
|
+
* @param subjectId - The user ID bound to the session, if any.
|
|
24
|
+
* @returns The subject.
|
|
25
|
+
* @throws {TypeError} If `subjectId` is not set and the session address is not a CAIP-10 account ID.
|
|
278
26
|
*
|
|
279
|
-
* @
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* siwxJwtSubject({ ...session, address: 'eip155:8453:0xAbC…' }); // "eip155:0xabc…"
|
|
30
|
+
* ```
|
|
283
31
|
*/
|
|
284
|
-
declare function
|
|
285
|
-
/**
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
32
|
+
declare function siwxJwtSubject(session: SiwxSession, subjectId?: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* Signs a JWT for a SIWX session with ES256 or RS256 (Web Crypto API).
|
|
35
|
+
*
|
|
36
|
+
* The header is `{ alg, kid, typ: "JWT" }`. The claims are `iss`, `sub`, `aud` (when set), `iat`, `exp`, `jti` (16
|
|
37
|
+
* random bytes), `caip10` and `chain_id` (with Solana chain IDs in their genesis-hash form), plus `claims`. `exp` is
|
|
38
|
+
* the earliest of `now + ttlSeconds`, `notAfter` and the `expirationTime` of the signed message, so a token never
|
|
39
|
+
* outlives its session.
|
|
40
|
+
*
|
|
41
|
+
* Side effects: reads the clock (unless `now` is set) and the random number generator.
|
|
42
|
+
*
|
|
43
|
+
* @param params - The session, the key and the claims.
|
|
44
|
+
* @param params.session - The verified session of the signed-in wallet.
|
|
45
|
+
* @param params.key - The signing key, from `importSiwxJwtKey`.
|
|
46
|
+
* @param params.issuer - The `iss` claim: the URL of your app. The receiving service checks it.
|
|
47
|
+
* @param params.audience - The `aud` claim, when the receiving service expects one.
|
|
48
|
+
* @param params.ttlSeconds - Token lifetime in seconds, from 1 to 604800 (7 days). Defaults to 600.
|
|
49
|
+
* @param params.subject - The `sub` claim. Defaults to {@link siwxJwtSubject} of the session.
|
|
50
|
+
* @param params.claims - Extra claims. `iss`, `sub`, `aud`, `iat`, `exp`, `nbf`, `jti`, `caip10` and `chain_id` are
|
|
51
|
+
* reserved.
|
|
52
|
+
* @param params.notAfter - Latest expiry in milliseconds since the epoch, for example the expiry of the session record.
|
|
53
|
+
* @param params.now - Current time in milliseconds since the epoch. Defaults to `Date.now()`.
|
|
54
|
+
* @returns The compact JWT and its expiry in milliseconds since the epoch.
|
|
55
|
+
* @throws {TypeError} If `issuer` is empty, `claims` sets a reserved claim or the session address is not a CAIP-10
|
|
56
|
+
* account ID.
|
|
57
|
+
* @throws {RangeError} If `ttlSeconds` is not an integer from 1 to 604800, or the session expires within the current
|
|
58
|
+
* second.
|
|
289
59
|
*
|
|
290
|
-
* @
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
* @returns The token, formatted as `{payload}.{signature}`.
|
|
295
|
-
* @throws {Error} If `secret` is shorter than 32 characters.
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* const { token } = await signSiwxJwt({ session, key, issuer: 'https://app.example.com' });
|
|
63
|
+
* ```
|
|
296
64
|
*/
|
|
297
|
-
declare function
|
|
65
|
+
declare function signSiwxJwt(params: {
|
|
66
|
+
session: SiwxSession;
|
|
67
|
+
key: SiwxJwtKey;
|
|
68
|
+
issuer: string;
|
|
69
|
+
audience?: string | string[];
|
|
70
|
+
ttlSeconds?: number;
|
|
71
|
+
subject?: string;
|
|
72
|
+
claims?: Record<string, unknown>;
|
|
73
|
+
notAfter?: number;
|
|
74
|
+
now?: number;
|
|
75
|
+
}): Promise<{
|
|
76
|
+
token: string;
|
|
77
|
+
expiresAt: number;
|
|
78
|
+
}>;
|
|
298
79
|
/**
|
|
299
|
-
* Verifies a
|
|
300
|
-
* token version and mode, and its expiry (with `policy.clockSkewSeconds`, 60 seconds by default).
|
|
80
|
+
* Verifies a JWT issued by {@link signSiwxJwt}, for services that receive the token instead of the session cookie.
|
|
301
81
|
*
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
82
|
+
* Checks, in order: three base64url parts with JSON header and payload; header `alg` is `ES256` or `RS256` (`none` is
|
|
83
|
+
* rejected), `typ` is `JWT` when present and `kid` is present; a key with that `kid` exists in `jwks` and has the
|
|
84
|
+
* same algorithm; the signature; `iss` equals `issuer`; `aud` contains one of `audience` when `audience` is set;
|
|
85
|
+
* `exp` has not passed and `iat` is not in the future, both with `clockSkewSeconds`; `sub` is a non-empty string.
|
|
305
86
|
*
|
|
306
|
-
*
|
|
307
|
-
* @param secret - The secret the token was signed with.
|
|
308
|
-
* @param policy - Optional policy to check against the token.
|
|
309
|
-
* @returns The session, or `null` if the token is missing, malformed, tampered, expired or rejected by the policy.
|
|
310
|
-
* Never throws.
|
|
311
|
-
*/
|
|
312
|
-
declare function verifyStatelessDemoSession(token: string | null | undefined, secret: string, policy?: _tuwaio_siwx_core.SiwxVerificationPolicy): Promise<SiwxSession | null>;
|
|
313
|
-
/**
|
|
314
|
-
* Issues a challenge nonce for the stateless demo profile: a random value and an expiry time, signed with
|
|
315
|
-
* HMAC-SHA256 (Web Crypto), encoded as 106 lowercase hex characters so that it is a valid CAIP-122 nonce.
|
|
316
|
-
* {@link verifyStatelessDemoNonce} can then check, without any storage, that the server issued the nonce and that it
|
|
317
|
-
* has not expired.
|
|
87
|
+
* Never throws: every failure returns `null`. Side effects: reads the clock unless `now` is set.
|
|
318
88
|
*
|
|
319
|
-
* @param
|
|
320
|
-
* @param
|
|
321
|
-
* @
|
|
322
|
-
*
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
* single use must remember consumed nonces (`createStatelessDemoSiwxHandler` does so per server instance).
|
|
89
|
+
* @param token - The compact JWT.
|
|
90
|
+
* @param params - The keys and the expected claims.
|
|
91
|
+
* @param params.jwks - The JWKS (as served by `/jwks` or built with `createSiwxJwks`), or an array of signing keys
|
|
92
|
+
* and/or public JWKs.
|
|
93
|
+
* @param params.issuer - The expected `iss`.
|
|
94
|
+
* @param params.audience - The expected audience. When set, the token must carry an `aud` containing one of them.
|
|
95
|
+
* @param params.clockSkewSeconds - Tolerance for `exp` and `iat` in seconds. Defaults to 60.
|
|
96
|
+
* @param params.now - Current time in milliseconds since the epoch. Defaults to `Date.now()`.
|
|
97
|
+
* @returns The claims of a valid token, or `null`.
|
|
329
98
|
*
|
|
330
|
-
* @
|
|
331
|
-
*
|
|
332
|
-
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const jwks = await (await fetch('https://app.example.com/api/siwx/jwks')).json();
|
|
102
|
+
* const claims = await verifySiwxJwt(token, { jwks, issuer: 'https://app.example.com' });
|
|
103
|
+
* if (!claims) return new Response('Unauthorized', { status: 401 });
|
|
104
|
+
* ```
|
|
333
105
|
*/
|
|
334
|
-
declare function
|
|
106
|
+
declare function verifySiwxJwt(token: string, params: {
|
|
107
|
+
jwks: SiwxJwks | {
|
|
108
|
+
keys: ReadonlyArray<JsonWebKey>;
|
|
109
|
+
} | ReadonlyArray<SiwxJwtKey | SiwxPublicJwk>;
|
|
110
|
+
issuer: string;
|
|
111
|
+
audience?: string | string[];
|
|
112
|
+
clockSkewSeconds?: number;
|
|
113
|
+
now?: number;
|
|
114
|
+
}): Promise<SiwxJwtPayload | null>;
|
|
115
|
+
|
|
335
116
|
/**
|
|
336
|
-
*
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
* @param value - The cookie value (session ID or demo token). Written as-is, without encoding.
|
|
340
|
-
* @param opts - Cookie attributes. Defaults: name `siwx-session-v2`, `Max-Age` 604800, path `/`.
|
|
341
|
-
* @returns The header value, e.g. `siwx-session-v2=…; Max-Age=604800; Path=/; HttpOnly; SameSite=Strict; Secure`.
|
|
117
|
+
* @file JWT signing keys for SIWX sessions: generation, import, RFC 7638 thumbprints and the JWKS. Web Crypto API
|
|
118
|
+
* only, so it runs on Node.js 20+, Edge runtimes, Bun and Deno.
|
|
342
119
|
*/
|
|
343
|
-
|
|
120
|
+
|
|
344
121
|
/**
|
|
345
|
-
*
|
|
346
|
-
*
|
|
122
|
+
* Computes the RFC 7638 thumbprint of a key: SHA-256 over the required public members in lexicographic order
|
|
123
|
+
* (`crv`, `kty`, `x`, `y` for EC; `e`, `kty`, `n` for RSA), base64url. SIWX uses it as the `kid` of a key.
|
|
347
124
|
*
|
|
348
|
-
* @param
|
|
349
|
-
* @returns The
|
|
125
|
+
* @param jwk - A public or private JWK of an EC or RSA key.
|
|
126
|
+
* @returns The thumbprint, 43 base64url characters.
|
|
127
|
+
* @throws {TypeError} If the key is neither EC nor RSA, or a required member is missing.
|
|
350
128
|
*/
|
|
351
|
-
declare function
|
|
129
|
+
declare function siwxJwkThumbprint(jwk: SiwxPublicJwk | JsonWebKey): Promise<string>;
|
|
352
130
|
/**
|
|
353
|
-
*
|
|
354
|
-
*
|
|
131
|
+
* Generates a new JWT signing key. Run it once, store `privateJwk` as a secret (for example the environment variable
|
|
132
|
+
* read by {@link importSiwxJwtKey}) and never expose it to the browser.
|
|
355
133
|
*
|
|
356
|
-
* @param
|
|
357
|
-
* @
|
|
358
|
-
* @returns The cookie value, or `null` when the header is empty or has no such cookie.
|
|
134
|
+
* @param alg - `ES256` (P-256, default) or `RS256` (2048-bit modulus, exponent 65537).
|
|
135
|
+
* @returns The private JWK to store, the public JWK and its `kid`.
|
|
359
136
|
*/
|
|
360
|
-
declare function
|
|
137
|
+
declare function generateSiwxJwtKey(alg?: SiwxJwtAlgorithm): Promise<{
|
|
138
|
+
privateJwk: JsonWebKey;
|
|
139
|
+
publicJwk: SiwxPublicJwk;
|
|
140
|
+
kid: string;
|
|
141
|
+
}>;
|
|
361
142
|
/**
|
|
362
|
-
*
|
|
363
|
-
* process: they are lost on restart and not shared between instances or serverless invocations. Session IDs are
|
|
364
|
-
* 32-character hex strings from {@link generateNonce}.
|
|
365
|
-
*/
|
|
366
|
-
declare class MemorySiwxSessionStore implements SiwxSessionStore {
|
|
367
|
-
private records;
|
|
368
|
-
/**
|
|
369
|
-
* @param options - Store options.
|
|
370
|
-
* @param options.allowInProduction - Allows the store when `NODE_ENV` is `production`.
|
|
371
|
-
* @throws {Error} When `process.env.NODE_ENV` is `production` and `allowInProduction` is not `true`.
|
|
372
|
-
*/
|
|
373
|
-
constructor(options?: {
|
|
374
|
-
allowInProduction?: boolean;
|
|
375
|
-
});
|
|
376
|
-
create(input: {
|
|
377
|
-
session: SiwxSession;
|
|
378
|
-
ttlSeconds: number;
|
|
379
|
-
}): Promise<SiwxSessionRecord>;
|
|
380
|
-
get(id: string): Promise<SiwxSessionRecord | null>;
|
|
381
|
-
bindSubject(id: string, subjectId: string): Promise<boolean>;
|
|
382
|
-
revoke(id: string): Promise<void>;
|
|
383
|
-
}
|
|
384
|
-
/**
|
|
385
|
-
* In-memory {@link SiwxNonceStore} for local development and tests. Nonces live in a `Map` of the current process:
|
|
386
|
-
* they are lost on restart and not shared between instances or serverless invocations, so a nonce issued by one
|
|
387
|
-
* instance is rejected by another.
|
|
388
|
-
*/
|
|
389
|
-
declare class MemorySiwxNonceStore implements SiwxNonceStore {
|
|
390
|
-
private nonces;
|
|
391
|
-
/**
|
|
392
|
-
* @param options - Store options.
|
|
393
|
-
* @param options.allowInProduction - Allows the store when `NODE_ENV` is `production`.
|
|
394
|
-
* @throws {Error} When `process.env.NODE_ENV` is `production` and `allowInProduction` is not `true`.
|
|
395
|
-
*/
|
|
396
|
-
constructor(options?: {
|
|
397
|
-
allowInProduction?: boolean;
|
|
398
|
-
});
|
|
399
|
-
issue(input: {
|
|
400
|
-
nonce: string;
|
|
401
|
-
ttlSeconds: number;
|
|
402
|
-
}): Promise<void>;
|
|
403
|
-
consume(input: {
|
|
404
|
-
nonce: string;
|
|
405
|
-
}): Promise<boolean>;
|
|
406
|
-
}
|
|
407
|
-
/**
|
|
408
|
-
* Reads the SIWX session of the current request from its session cookie. Use it in Server Actions, Route Handlers
|
|
409
|
-
* or any server code instead of trusting session data sent by the client.
|
|
143
|
+
* Imports the private key that signs the JWTs of SIWX sessions.
|
|
410
144
|
*
|
|
411
|
-
*
|
|
412
|
-
*
|
|
413
|
-
*
|
|
145
|
+
* Accepted forms of `privateKey`:
|
|
146
|
+
* - a JWK object, or its JSON string (as stored by {@link generateSiwxJwtKey});
|
|
147
|
+
* - a PKCS#8 PEM (`-----BEGIN PRIVATE KEY-----`). Line breaks may be real, `\r\n` or the two characters `\n`, as
|
|
148
|
+
* environment variables often store them. Without `alg`, an EC P-256 key is tried first, then RSA.
|
|
414
149
|
*
|
|
415
|
-
*
|
|
416
|
-
* @returns The session, or `null` when there is no cookie, no store or secret, or the session is unknown, expired or
|
|
417
|
-
* rejected by the policy.
|
|
418
|
-
* @throws Errors thrown by `sessionStore.get` are not caught.
|
|
150
|
+
* The `use`, `key_ops` and `ext` members of a JWK are ignored. The imported private key is not extractable.
|
|
419
151
|
*
|
|
420
|
-
* @
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
*
|
|
152
|
+
* @param params - The key and, optionally, the algorithm it must have.
|
|
153
|
+
* @param params.privateKey - The private key as a JWK object, a JWK JSON string or a PKCS#8 PEM.
|
|
154
|
+
* @param params.alg - The expected algorithm. Fails when the key has another one.
|
|
155
|
+
* @returns The signing key with its public JWK and `kid` (RFC 7638 thumbprint).
|
|
156
|
+
* @throws {TypeError} If the input is not a private key, has an unsupported type or curve, does not match `alg` or
|
|
157
|
+
* the JWK `alg` member, is an RSA key shorter than 2048 bits, or is a PEM other than PKCS#8.
|
|
158
|
+
*/
|
|
159
|
+
declare function importSiwxJwtKey(params: {
|
|
160
|
+
privateKey: string | JsonWebKey;
|
|
161
|
+
alg?: SiwxJwtAlgorithm;
|
|
162
|
+
}): Promise<SiwxJwtKey>;
|
|
163
|
+
/**
|
|
164
|
+
* Builds the JWKS to publish: the public JWK of every key, in order, without duplicate `kid`s. Pass the current
|
|
165
|
+
* signing key first and the keys it replaced after it, so tokens signed before a rotation keep verifying until they
|
|
166
|
+
* expire.
|
|
426
167
|
*
|
|
427
|
-
*
|
|
428
|
-
*
|
|
429
|
-
* sessionStore,
|
|
430
|
-
* });
|
|
431
|
-
* ```
|
|
168
|
+
* @param keys - Signing keys (their public JWK is used) and/or public JWKs.
|
|
169
|
+
* @returns The JWKS, safe to serve publicly.
|
|
432
170
|
*/
|
|
433
|
-
declare function
|
|
171
|
+
declare function createSiwxJwks(keys: ReadonlyArray<SiwxJwtKey | SiwxPublicJwk>): SiwxJwks;
|
|
434
172
|
|
|
435
|
-
export {
|
|
173
|
+
export { SiwxJwks, SiwxJwtAlgorithm, SiwxJwtKey, SiwxJwtPayload, SiwxPublicJwk, SiwxSession, createSiwxJwks, generateSiwxJwtKey, importSiwxJwtKey, signSiwxJwt, siwxJwkThumbprint, siwxJwtSubject, verifySiwxJwt };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var chunk455KLGU5_js=require('./chunk-455KLGU5.js'),siwxCore=require('@tuwaio/siwx-core');Object.defineProperty(exports,"MemorySiwxNonceStore",{enumerable:true,get:function(){return chunk455KLGU5_js.t}});Object.defineProperty(exports,"MemorySiwxSessionStore",{enumerable:true,get:function(){return chunk455KLGU5_js.s}});Object.defineProperty(exports,"createClearCookie",{enumerable:true,get:function(){return chunk455KLGU5_js.q}});Object.defineProperty(exports,"createSessionCookie",{enumerable:true,get:function(){return chunk455KLGU5_js.p}});Object.defineProperty(exports,"createSiwxJwks",{enumerable:true,get:function(){return chunk455KLGU5_js.d}});Object.defineProperty(exports,"generateServerNonce",{enumerable:true,get:function(){return chunk455KLGU5_js.j}});Object.defineProperty(exports,"generateSiwxJwtKey",{enumerable:true,get:function(){return chunk455KLGU5_js.b}});Object.defineProperty(exports,"getSiwxServerSession",{enumerable:true,get:function(){return chunk455KLGU5_js.u}});Object.defineProperty(exports,"importSiwxJwtKey",{enumerable:true,get:function(){return chunk455KLGU5_js.c}});Object.defineProperty(exports,"issueStatelessDemoNonce",{enumerable:true,get:function(){return chunk455KLGU5_js.n}});Object.defineProperty(exports,"parseCookie",{enumerable:true,get:function(){return chunk455KLGU5_js.r}});Object.defineProperty(exports,"signSiwxJwt",{enumerable:true,get:function(){return chunk455KLGU5_js.f}});Object.defineProperty(exports,"signStatelessDemoSession",{enumerable:true,get:function(){return chunk455KLGU5_js.l}});Object.defineProperty(exports,"siwxJwkThumbprint",{enumerable:true,get:function(){return chunk455KLGU5_js.a}});Object.defineProperty(exports,"siwxJwtSubject",{enumerable:true,get:function(){return chunk455KLGU5_js.e}});Object.defineProperty(exports,"toSession",{enumerable:true,get:function(){return chunk455KLGU5_js.i}});Object.defineProperty(exports,"verifySiwxJwt",{enumerable:true,get:function(){return chunk455KLGU5_js.g}});Object.defineProperty(exports,"verifySiwxPayload",{enumerable:true,get:function(){return chunk455KLGU5_js.k}});Object.defineProperty(exports,"verifyStatelessDemoNonce",{enumerable:true,get:function(){return chunk455KLGU5_js.o}});Object.defineProperty(exports,"verifyStatelessDemoSession",{enumerable:true,get:function(){return chunk455KLGU5_js.m}});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{t as MemorySiwxNonceStore,s as MemorySiwxSessionStore,q as createClearCookie,p as createSessionCookie,d as createSiwxJwks,j as generateServerNonce,b as generateSiwxJwtKey,u as getSiwxServerSession,c as importSiwxJwtKey,n as issueStatelessDemoNonce,r as parseCookie,f as signSiwxJwt,l as signStatelessDemoSession,a as siwxJwkThumbprint,e as siwxJwtSubject,i as toSession,g as verifySiwxJwt,k as verifySiwxPayload,o as verifyStatelessDemoNonce,m as verifyStatelessDemoSession}from'./chunk-PZHK7SYL.mjs';export{generateNonce,isSessionMatchingTarget,validateMessage,validatePolicy}from'@tuwaio/siwx-core';
|