@sirosfoundation/wmp-js 0.2.0 → 0.5.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 +1 -0
- package/dist/discovery.d.ts +46 -0
- package/dist/discovery.js +70 -0
- package/dist/evidence.d.ts +92 -0
- package/dist/evidence.js +54 -0
- package/dist/index.d.ts +15 -4
- package/dist/index.js +11 -2
- package/dist/invitation.d.ts +131 -0
- package/dist/invitation.js +210 -0
- package/dist/jsonrpc.d.ts +9 -3
- package/dist/jsonrpc.js +60 -10
- package/dist/mls-provider.d.ts +65 -0
- package/dist/mls-provider.js +216 -0
- package/dist/mls.d.ts +8 -0
- package/dist/mls.js +8 -0
- package/dist/native.d.ts +9 -5
- package/dist/native.js +62 -57
- package/dist/node.d.ts +1 -1
- package/dist/openid4x.d.ts +150 -4
- package/dist/openid4x.js +69 -1
- package/dist/peer.d.ts +27 -1
- package/dist/peer.js +109 -5
- package/dist/schema.d.ts +2 -1
- package/dist/schema.js +12 -5
- package/dist/session-store.d.ts +33 -0
- package/dist/session-store.js +36 -0
- package/dist/transport.d.ts +8 -0
- package/dist/transport.js +28 -3
- package/dist/types.d.ts +63 -5
- package/dist/types.js +25 -0
- package/package.json +9 -5
package/dist/openid4x.d.ts
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* Provides OID4VCI (credential issuance) and OID4VP (credential presentation)
|
|
5
5
|
* flow handling via the WMP profile system.
|
|
6
6
|
*/
|
|
7
|
-
import type { FlowStartParams, FlowStartResult, FlowActionParams, FlowActionResult, FlowProgressParams, FlowCompleteParams, FlowErrorParams, ResolveParams, ResolveResult } from "./types.js";
|
|
8
|
-
import type { Profile, PeerContext, FlowHandler, ResolveHandler } from "./profile.js";
|
|
7
|
+
import type { FlowStartParams, FlowStartResult, FlowActionParams, FlowActionResult, FlowProgressParams, FlowCompleteParams, FlowErrorParams, Metadata, ResolveParams, ResolveResult } from "./types.js";
|
|
8
|
+
import type { Profile, PeerContext, FlowHandler, ResolveHandler, MethodHandler } from "./profile.js";
|
|
9
9
|
export declare const CredentialFormat: {
|
|
10
10
|
readonly VC_SDJWT: "vc+sd-jwt";
|
|
11
11
|
readonly DC_SDJWT: "dc+sd-jwt";
|
|
@@ -98,18 +98,143 @@ export interface CredentialResult {
|
|
|
98
98
|
credential: string;
|
|
99
99
|
vct?: string;
|
|
100
100
|
c_nonce?: string;
|
|
101
|
+
notification_id?: string;
|
|
102
|
+
}
|
|
103
|
+
/** OID4VCI §10 credential lifecycle events. */
|
|
104
|
+
export type CredentialEvent = "credential_accepted" | "credential_failure";
|
|
105
|
+
/** Params for wmp.credential.notification. */
|
|
106
|
+
export interface CredentialNotificationParams {
|
|
107
|
+
wmp: Metadata;
|
|
108
|
+
flow_id: string;
|
|
109
|
+
notification_id: string;
|
|
110
|
+
event: CredentialEvent;
|
|
111
|
+
event_description?: string;
|
|
101
112
|
}
|
|
102
113
|
export interface VPTokenResult {
|
|
103
114
|
vp_token?: string | string[];
|
|
104
115
|
presentation_submission?: unknown;
|
|
105
116
|
response_code?: string;
|
|
106
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* Client attestation credentials (WIA + PoP) for issuer authentication.
|
|
120
|
+
* Produced by a {@link ClientAttestationProvider}.
|
|
121
|
+
*/
|
|
122
|
+
export interface ClientAttestation {
|
|
123
|
+
/** WIA JWT (typ: oauth-client-attestation+jwt) */
|
|
124
|
+
client_attestation: string;
|
|
125
|
+
/** PoP JWT (typ: oauth-client-attestation-pop+jwt) signed by wallet instance key */
|
|
126
|
+
client_attestation_pop: string;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Provider interface for obtaining OAuth client attestation credentials.
|
|
130
|
+
*
|
|
131
|
+
* Callers implement this to decouple wmp-js from any specific wallet backend.
|
|
132
|
+
* The provider is responsible for:
|
|
133
|
+
* 1. Obtaining a WIA JWT from the wallet provider (implementation-defined)
|
|
134
|
+
* 2. Signing the PoP JWT with the wallet instance key (aud = issuer AS URL)
|
|
135
|
+
*
|
|
136
|
+
* Example implementations:
|
|
137
|
+
* - SIROS: calls /wallet-provider/wia/generate, signs PoP with passkey-PRF key
|
|
138
|
+
* - EUDI: calls the PID provider's WIA endpoint, signs PoP with device key
|
|
139
|
+
* - Test: returns static test JWTs
|
|
140
|
+
*/
|
|
141
|
+
export interface ClientAttestationProvider {
|
|
142
|
+
/**
|
|
143
|
+
* Get attestation credentials for a specific issuer.
|
|
144
|
+
* @param audience - The issuer's AS URL (used as PoP aud claim)
|
|
145
|
+
* @returns WIA + PoP JWTs, or null if attestation is not available/required
|
|
146
|
+
*/
|
|
147
|
+
getAttestation(audience: string): Promise<ClientAttestation | null>;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* OID4VCI-specific parameters for wmp.flow.start.
|
|
151
|
+
* Passed as the `params` field of FlowStartParams when flow_type = "oid4vci".
|
|
152
|
+
*
|
|
153
|
+
* Client attestation fields support draft-ietf-oauth-attestation-based-client-auth-04:
|
|
154
|
+
* - `client_attestation`: WIA JWT from the provider
|
|
155
|
+
* - `client_attestation_pop`: PoP JWT signed by the wallet instance key (aud = AS URL)
|
|
156
|
+
*
|
|
157
|
+
* The instance key is held client-side (never on the backend).
|
|
158
|
+
* The backend forwards these as HTTP headers without modification.
|
|
159
|
+
*/
|
|
160
|
+
/** Common fields shared by all OID4VCI flow param variants. */
|
|
161
|
+
interface OID4VCIFlowParamsBase {
|
|
162
|
+
/** OAuth redirect URI for authorization code flow */
|
|
163
|
+
redirect_uri?: string;
|
|
164
|
+
/** WIA JWT (typ: oauth-client-attestation+jwt) obtained via ClientAttestationProvider */
|
|
165
|
+
client_attestation?: string;
|
|
166
|
+
/** PoP JWT (typ: oauth-client-attestation-pop+jwt) signed by wallet instance key */
|
|
167
|
+
client_attestation_pop?: string;
|
|
168
|
+
/** Authorization code from OAuth redirect */
|
|
169
|
+
auth_code?: string;
|
|
170
|
+
/** PKCE code verifier (saved by client before redirect) */
|
|
171
|
+
code_verifier?: string;
|
|
172
|
+
}
|
|
173
|
+
/** OID4VCI params with an inline credential offer. */
|
|
174
|
+
export interface OID4VCIFlowParamsWithOffer extends OID4VCIFlowParamsBase {
|
|
175
|
+
/** Credential offer URI (openid-credential-offer://...) */
|
|
176
|
+
offer: string;
|
|
177
|
+
credential_offer_uri?: never;
|
|
178
|
+
}
|
|
179
|
+
/** OID4VCI params with a credential offer by reference. */
|
|
180
|
+
export interface OID4VCIFlowParamsWithURI extends OID4VCIFlowParamsBase {
|
|
181
|
+
offer?: never;
|
|
182
|
+
/** Credential offer URI by reference (https://...) */
|
|
183
|
+
credential_offer_uri: string;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* OID4VCI-specific parameters for wmp.flow.start.
|
|
187
|
+
* Passed as the `params` field of FlowStartParams when flow_type = "oid4vci".
|
|
188
|
+
*
|
|
189
|
+
* Exactly one of `offer` or `credential_offer_uri` must be provided.
|
|
190
|
+
*/
|
|
191
|
+
export type OID4VCIFlowParams = OID4VCIFlowParamsWithOffer | OID4VCIFlowParamsWithURI;
|
|
192
|
+
/**
|
|
193
|
+
* OID4VP-specific parameters for wmp.flow.start.
|
|
194
|
+
* Passed as the `params` field of FlowStartParams when flow_type = "oid4vp".
|
|
195
|
+
*/
|
|
196
|
+
export interface OID4VPFlowParams {
|
|
197
|
+
/** Request URI (openid4vp://...) */
|
|
198
|
+
request_uri?: string;
|
|
199
|
+
/** Request URI by reference (https://...) */
|
|
200
|
+
request_uri_ref?: string;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Helper to build WMP FlowStartParams for an OID4VCI flow.
|
|
204
|
+
* If attestation is provided, it is included in the params.
|
|
205
|
+
*/
|
|
206
|
+
export declare function buildVCIFlowStart(sessionId: string, flowId: string, params: OID4VCIFlowParams, timeout?: number): FlowStartParams;
|
|
207
|
+
/**
|
|
208
|
+
* Helper to build VCI flow params with attestation from a provider.
|
|
209
|
+
* Calls the provider to obtain WIA + PoP, then merges into the params.
|
|
210
|
+
*
|
|
211
|
+
* @param provider - The attestation provider (caller-supplied)
|
|
212
|
+
* @param audience - The issuer's AS URL for PoP audience binding
|
|
213
|
+
* @param params - Base OID4VCI flow params (offer, redirect_uri, etc.)
|
|
214
|
+
* @returns params with attestation fields populated (or unchanged if provider returns null)
|
|
215
|
+
*/
|
|
216
|
+
export declare function withAttestation(provider: ClientAttestationProvider, audience: string, params: OID4VCIFlowParams): Promise<OID4VCIFlowParams>;
|
|
217
|
+
/**
|
|
218
|
+
* TransactionData represents a single transaction data object from
|
|
219
|
+
* the verifier's OID4VP authorization request (TS12/SCA).
|
|
220
|
+
*/
|
|
221
|
+
export interface TransactionData {
|
|
222
|
+
type: string;
|
|
223
|
+
params?: Record<string, unknown>;
|
|
224
|
+
/** Credential IDs this transaction data applies to */
|
|
225
|
+
credential_ids?: string[];
|
|
226
|
+
/** Hash algorithm for transaction_data_hashes */
|
|
227
|
+
hash_alg?: string;
|
|
228
|
+
/** Algorithm identifier for the hashes claim in KB-JWT */
|
|
229
|
+
transaction_data_hashes_alg?: string;
|
|
230
|
+
}
|
|
107
231
|
export interface SignSubFlowParams {
|
|
108
232
|
action: string;
|
|
109
233
|
nonce: string;
|
|
110
234
|
audience: string;
|
|
111
235
|
proof_type?: string;
|
|
112
236
|
parent_flow_id: string;
|
|
237
|
+
transaction_data?: TransactionData[];
|
|
113
238
|
}
|
|
114
239
|
export interface SelectionAction {
|
|
115
240
|
selected_indices: number[];
|
|
@@ -126,10 +251,11 @@ export interface CredentialSelection {
|
|
|
126
251
|
}
|
|
127
252
|
export type FlowStartHandler = (params: FlowStartParams) => Promise<FlowStartResult>;
|
|
128
253
|
export type ActionHandler = (params: FlowActionParams) => Promise<FlowActionResult>;
|
|
254
|
+
export type CredentialNotificationHandler = (params: CredentialNotificationParams) => void | Promise<void>;
|
|
129
255
|
export interface OpenID4xConfig {
|
|
130
256
|
/** OID4VCI capability. Omit to disable issuance. */
|
|
131
257
|
oid4vci?: OID4VCICapability;
|
|
132
|
-
/**
|
|
258
|
+
/** OID4VCI capability. Omit to disable issuance. */
|
|
133
259
|
oid4vp?: OID4VPCapability;
|
|
134
260
|
/** Called when an OID4VCI flow starts. */
|
|
135
261
|
onVCIStart?: FlowStartHandler;
|
|
@@ -139,6 +265,17 @@ export interface OpenID4xConfig {
|
|
|
139
265
|
onVPStart?: FlowStartHandler;
|
|
140
266
|
/** Called when an action arrives for an OID4VP flow. */
|
|
141
267
|
onVPAction?: ActionHandler;
|
|
268
|
+
/**
|
|
269
|
+
* Called when a validated wmp.credential.notification arrives.
|
|
270
|
+
* The notification_id is guaranteed to have been registered for an
|
|
271
|
+
* outstanding OID4VCI flow (see registerCredentialNotification).
|
|
272
|
+
*/
|
|
273
|
+
onCredentialNotification?: CredentialNotificationHandler;
|
|
274
|
+
/**
|
|
275
|
+
* Require notification_ids to be registered before accepting
|
|
276
|
+
* wmp.credential.notification. Default: true.
|
|
277
|
+
*/
|
|
278
|
+
requireRegisteredNotification?: boolean;
|
|
142
279
|
}
|
|
143
280
|
/**
|
|
144
281
|
* OpenID4xProfile implements Profile, FlowHandler, and ResolveHandler
|
|
@@ -148,14 +285,22 @@ export interface OpenID4xConfig {
|
|
|
148
285
|
* const profile = new OpenID4xProfile({ oid4vci: {...}, oid4vp: {...} });
|
|
149
286
|
* peer.use(profile);
|
|
150
287
|
*/
|
|
151
|
-
export declare class OpenID4xProfile implements Profile, FlowHandler, ResolveHandler {
|
|
288
|
+
export declare class OpenID4xProfile implements Profile, FlowHandler, ResolveHandler, MethodHandler {
|
|
152
289
|
readonly name = "openid4x";
|
|
153
290
|
private config;
|
|
154
291
|
private peer?;
|
|
155
292
|
private flowTypeMap;
|
|
293
|
+
private registeredNotifications;
|
|
156
294
|
constructor(config: OpenID4xConfig);
|
|
157
295
|
capabilities(): string[];
|
|
158
296
|
init(ctx: PeerContext): void;
|
|
297
|
+
methods(): string[];
|
|
298
|
+
handleMethod(method: string, params: unknown): Promise<unknown>;
|
|
299
|
+
/**
|
|
300
|
+
* Register a notification_id that was returned for an OID4VCI flow.
|
|
301
|
+
* This allows wmp.credential.notification messages to be validated.
|
|
302
|
+
*/
|
|
303
|
+
registerCredentialNotification(flowId: string, notificationId: string): void;
|
|
159
304
|
flowTypes(): string[];
|
|
160
305
|
startFlow(params: FlowStartParams): Promise<FlowStartResult>;
|
|
161
306
|
handleAction(params: FlowActionParams): Promise<FlowActionResult>;
|
|
@@ -165,3 +310,4 @@ export declare class OpenID4xProfile implements Profile, FlowHandler, ResolveHan
|
|
|
165
310
|
resolveTypes(): string[];
|
|
166
311
|
handleResolve(_params: ResolveParams): Promise<ResolveResult>;
|
|
167
312
|
}
|
|
313
|
+
export {};
|
package/dist/openid4x.js
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* Provides OID4VCI (credential issuance) and OID4VP (credential presentation)
|
|
5
5
|
* flow handling via the WMP profile system.
|
|
6
6
|
*/
|
|
7
|
+
import { Method, ErrorCode, VERSION } from "./types.js";
|
|
7
8
|
import { WMPError } from "./jsonrpc.js";
|
|
8
|
-
import { ErrorCode } from "./types.js";
|
|
9
9
|
// ---------------------------------------------------------------------------
|
|
10
10
|
// Credential format constants — aligned with wallet-common VerifiableCredentialFormat
|
|
11
11
|
// ---------------------------------------------------------------------------
|
|
@@ -93,6 +93,38 @@ export const OID4Action = {
|
|
|
93
93
|
SelectCredentials: "select_credentials",
|
|
94
94
|
Cancel: "cancel",
|
|
95
95
|
};
|
|
96
|
+
/**
|
|
97
|
+
* Helper to build WMP FlowStartParams for an OID4VCI flow.
|
|
98
|
+
* If attestation is provided, it is included in the params.
|
|
99
|
+
*/
|
|
100
|
+
export function buildVCIFlowStart(sessionId, flowId, params, timeout) {
|
|
101
|
+
return {
|
|
102
|
+
wmp: { version: VERSION, session_id: sessionId },
|
|
103
|
+
flow_type: OID4FlowType.OID4VCI,
|
|
104
|
+
flow_id: flowId,
|
|
105
|
+
params,
|
|
106
|
+
timeout,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Helper to build VCI flow params with attestation from a provider.
|
|
111
|
+
* Calls the provider to obtain WIA + PoP, then merges into the params.
|
|
112
|
+
*
|
|
113
|
+
* @param provider - The attestation provider (caller-supplied)
|
|
114
|
+
* @param audience - The issuer's AS URL for PoP audience binding
|
|
115
|
+
* @param params - Base OID4VCI flow params (offer, redirect_uri, etc.)
|
|
116
|
+
* @returns params with attestation fields populated (or unchanged if provider returns null)
|
|
117
|
+
*/
|
|
118
|
+
export async function withAttestation(provider, audience, params) {
|
|
119
|
+
const attestation = await provider.getAttestation(audience);
|
|
120
|
+
if (!attestation)
|
|
121
|
+
return params;
|
|
122
|
+
return {
|
|
123
|
+
...params,
|
|
124
|
+
client_attestation: attestation.client_attestation,
|
|
125
|
+
client_attestation_pop: attestation.client_attestation_pop,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
96
128
|
// ---------------------------------------------------------------------------
|
|
97
129
|
// OpenID4x Profile
|
|
98
130
|
// ---------------------------------------------------------------------------
|
|
@@ -109,6 +141,7 @@ export class OpenID4xProfile {
|
|
|
109
141
|
config;
|
|
110
142
|
peer;
|
|
111
143
|
flowTypeMap = new Map();
|
|
144
|
+
registeredNotifications = new Map(); // flow_id -> notification_id
|
|
112
145
|
constructor(config) {
|
|
113
146
|
this.config = config;
|
|
114
147
|
}
|
|
@@ -124,6 +157,41 @@ export class OpenID4xProfile {
|
|
|
124
157
|
init(ctx) {
|
|
125
158
|
this.peer = ctx;
|
|
126
159
|
}
|
|
160
|
+
// --- MethodHandler ---
|
|
161
|
+
methods() {
|
|
162
|
+
if (!this.config.oid4vci)
|
|
163
|
+
return [];
|
|
164
|
+
return [Method.CredentialNotification];
|
|
165
|
+
}
|
|
166
|
+
async handleMethod(method, params) {
|
|
167
|
+
if (method !== Method.CredentialNotification) {
|
|
168
|
+
throw new WMPError(ErrorCode.MethodNotFound, `Unknown method: ${method}`);
|
|
169
|
+
}
|
|
170
|
+
const p = params;
|
|
171
|
+
if (!p.flow_id || !p.notification_id) {
|
|
172
|
+
throw new WMPError(ErrorCode.InvalidParams, "Missing flow_id or notification_id");
|
|
173
|
+
}
|
|
174
|
+
const requireRegistered = this.config.requireRegisteredNotification ?? true;
|
|
175
|
+
if (requireRegistered) {
|
|
176
|
+
const expected = this.registeredNotifications.get(p.flow_id);
|
|
177
|
+
if (expected !== p.notification_id) {
|
|
178
|
+
throw new WMPError(ErrorCode.NotAuthorized, "Unknown or unexpected credential notification_id");
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (this.config.onCredentialNotification) {
|
|
182
|
+
await this.config.onCredentialNotification(p);
|
|
183
|
+
}
|
|
184
|
+
// Consume the notification so it cannot be replayed.
|
|
185
|
+
this.registeredNotifications.delete(p.flow_id);
|
|
186
|
+
return { wmp: p.wmp, acknowledged: true };
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Register a notification_id that was returned for an OID4VCI flow.
|
|
190
|
+
* This allows wmp.credential.notification messages to be validated.
|
|
191
|
+
*/
|
|
192
|
+
registerCredentialNotification(flowId, notificationId) {
|
|
193
|
+
this.registeredNotifications.set(flowId, notificationId);
|
|
194
|
+
}
|
|
127
195
|
// --- FlowHandler ---
|
|
128
196
|
flowTypes() {
|
|
129
197
|
const types = [];
|
package/dist/peer.d.ts
CHANGED
|
@@ -7,14 +7,23 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { type Transport } from "./transport.js";
|
|
9
9
|
import { type Profile, type PeerContext } from "./profile.js";
|
|
10
|
-
import { type
|
|
10
|
+
import { type ValidationError } from "./schema.js";
|
|
11
|
+
import { type SessionCreateParams, type SessionCreateResult, type SessionResumeParams, type SessionResumeResult, type SessionCloseParams, type SessionAuthenticateParams, type SessionAuthenticateResult, type MessageDeliverParams, type MessageAckParams, type MessagePollParams, type MessagePollResult, type MessageStatusParams, type CapabilityUpdateParams, type CapabilityUpdateResult, type CapabilityListParams, type CapabilityListResult, type FlowStartParams, type FlowStartResult, type FlowProgressParams, type FlowActionParams, type FlowActionResult, type FlowCompleteParams, type FlowErrorParams, type FlowCancelParams, type FlowCancelResult, type ResolveParams, type ResolveResult, type Capabilities, type SecurityMode } from "./types.js";
|
|
11
12
|
/**
|
|
12
13
|
* Handler processes incoming WMP method calls.
|
|
13
14
|
* All methods are optional — unimplemented methods return "method not found".
|
|
14
15
|
*/
|
|
15
16
|
export interface Handler {
|
|
16
17
|
onSessionCreate?(params: SessionCreateParams): Promise<SessionCreateResult>;
|
|
18
|
+
onSessionResume?(params: SessionResumeParams): Promise<SessionResumeResult>;
|
|
17
19
|
onSessionClose?(params: SessionCloseParams): void;
|
|
20
|
+
onSessionAuthenticate?(params: SessionAuthenticateParams): Promise<SessionAuthenticateResult>;
|
|
21
|
+
onMessageDeliver?(params: MessageDeliverParams): void;
|
|
22
|
+
onMessageAck?(params: MessageAckParams): void;
|
|
23
|
+
onMessagePoll?(params: MessagePollParams): Promise<MessagePollResult>;
|
|
24
|
+
onMessageStatus?(params: MessageStatusParams): void;
|
|
25
|
+
onCapabilityUpdate?(params: CapabilityUpdateParams): Promise<CapabilityUpdateResult>;
|
|
26
|
+
onCapabilityList?(params: CapabilityListParams): Promise<CapabilityListResult>;
|
|
18
27
|
onFlowStart?(params: FlowStartParams): Promise<FlowStartResult>;
|
|
19
28
|
onFlowProgress?(params: FlowProgressParams): void;
|
|
20
29
|
onFlowAction?(params: FlowActionParams): Promise<FlowActionResult>;
|
|
@@ -28,6 +37,12 @@ export interface PeerOptions {
|
|
|
28
37
|
callTimeout?: number;
|
|
29
38
|
/** Handler for incoming method calls. */
|
|
30
39
|
handler?: Handler;
|
|
40
|
+
/** Optional validator for incoming request parameters. */
|
|
41
|
+
validator?: {
|
|
42
|
+
validateMethod(method: string, params: unknown): ValidationError[] | null;
|
|
43
|
+
};
|
|
44
|
+
/** Optional authorization hook called before dispatching incoming requests/notifications. */
|
|
45
|
+
authorize?: (method: string, params: unknown) => boolean | Promise<boolean>;
|
|
31
46
|
}
|
|
32
47
|
export declare class Peer implements PeerContext {
|
|
33
48
|
private transport;
|
|
@@ -36,7 +51,11 @@ export declare class Peer implements PeerContext {
|
|
|
36
51
|
private pending;
|
|
37
52
|
private callTimeout;
|
|
38
53
|
private sessionId?;
|
|
54
|
+
private negotiatedVersion?;
|
|
55
|
+
private resumptionToken?;
|
|
39
56
|
private closed;
|
|
57
|
+
private validator?;
|
|
58
|
+
private authorize?;
|
|
40
59
|
constructor(transport: Transport, opts?: PeerOptions);
|
|
41
60
|
/** Register a profile. Call before starting to receive messages. */
|
|
42
61
|
use(profile: Profile): void;
|
|
@@ -56,7 +75,14 @@ export declare class Peer implements PeerContext {
|
|
|
56
75
|
};
|
|
57
76
|
sender?: string;
|
|
58
77
|
ttl?: number;
|
|
78
|
+
invitationNonce?: string;
|
|
59
79
|
}): Promise<SessionCreateResult>;
|
|
80
|
+
/** Resume a previously established WMP session using a resumption token. */
|
|
81
|
+
resumeSession(opts?: {
|
|
82
|
+
lastReceivedId?: string;
|
|
83
|
+
}): Promise<SessionResumeResult>;
|
|
84
|
+
/** The current resumption token (set after session.create or session.resume). */
|
|
85
|
+
get token(): string | undefined;
|
|
60
86
|
/**
|
|
61
87
|
* Bind a session ID to the underlying transport. For HttpSseTransport,
|
|
62
88
|
* this sets the Wmp-Session-Id header and session_id query parameter.
|
package/dist/peer.js
CHANGED
|
@@ -18,11 +18,17 @@ export class Peer {
|
|
|
18
18
|
pending = new Map();
|
|
19
19
|
callTimeout;
|
|
20
20
|
sessionId;
|
|
21
|
+
negotiatedVersion;
|
|
22
|
+
resumptionToken;
|
|
21
23
|
closed = false;
|
|
24
|
+
validator;
|
|
25
|
+
authorize;
|
|
22
26
|
constructor(transport, opts) {
|
|
23
27
|
this.transport = transport;
|
|
24
28
|
this.handler = opts?.handler ?? {};
|
|
25
29
|
this.callTimeout = opts?.callTimeout ?? 30_000;
|
|
30
|
+
this.validator = opts?.validator;
|
|
31
|
+
this.authorize = opts?.authorize;
|
|
26
32
|
// Wire up transport events.
|
|
27
33
|
this.transport.on("message", (msg) => this.dispatch(msg));
|
|
28
34
|
this.transport.on("error", (err) => this.onTransportError(err));
|
|
@@ -77,15 +83,51 @@ export class Peer {
|
|
|
77
83
|
security: opts.security ?? { mode: "tls" },
|
|
78
84
|
ttl: opts.ttl,
|
|
79
85
|
auth: opts.auth,
|
|
86
|
+
invitation_nonce: opts.invitationNonce,
|
|
80
87
|
};
|
|
81
88
|
const result = await this.call(Method.SessionCreate, params);
|
|
89
|
+
if (!result.wmp?.session_id || typeof result.wmp.session_id !== "string") {
|
|
90
|
+
throw new WMPError(ErrorCode.SessionNotFound, "session.create response missing session_id");
|
|
91
|
+
}
|
|
92
|
+
if (result.wmp.version && result.wmp.version !== VERSION) {
|
|
93
|
+
throw new WMPError(ErrorCode.VersionNotSupported, `negotiated version ${result.wmp.version} is not supported`);
|
|
94
|
+
}
|
|
95
|
+
this.sessionId = result.wmp.session_id;
|
|
96
|
+
this.bindSessionToTransport(result.wmp.session_id);
|
|
97
|
+
this.negotiatedVersion = result.wmp.version ?? VERSION;
|
|
98
|
+
if (result.resumption_token) {
|
|
99
|
+
this.resumptionToken = result.resumption_token;
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
/** Resume a previously established WMP session using a resumption token. */
|
|
104
|
+
async resumeSession(opts) {
|
|
105
|
+
if (!this.sessionId || !this.resumptionToken) {
|
|
106
|
+
throw new Error("no session or resumption token to resume");
|
|
107
|
+
}
|
|
108
|
+
const params = {
|
|
109
|
+
wmp: { version: this.negotiatedVersion ?? VERSION },
|
|
110
|
+
session_id: this.sessionId,
|
|
111
|
+
resumption_token: this.resumptionToken,
|
|
112
|
+
last_received_id: opts?.lastReceivedId,
|
|
113
|
+
};
|
|
114
|
+
const result = await this.call(Method.SessionResume, params);
|
|
82
115
|
if (result.wmp?.session_id) {
|
|
83
116
|
this.sessionId = result.wmp.session_id;
|
|
84
|
-
// Propagate session ID to transport for header/URL binding.
|
|
85
117
|
this.bindSessionToTransport(result.wmp.session_id);
|
|
86
118
|
}
|
|
119
|
+
if (result.wmp?.version) {
|
|
120
|
+
this.negotiatedVersion = result.wmp.version;
|
|
121
|
+
}
|
|
122
|
+
if (result.resumption_token) {
|
|
123
|
+
this.resumptionToken = result.resumption_token;
|
|
124
|
+
}
|
|
87
125
|
return result;
|
|
88
126
|
}
|
|
127
|
+
/** The current resumption token (set after session.create or session.resume). */
|
|
128
|
+
get token() {
|
|
129
|
+
return this.resumptionToken;
|
|
130
|
+
}
|
|
89
131
|
/**
|
|
90
132
|
* Bind a session ID to the underlying transport. For HttpSseTransport,
|
|
91
133
|
* this sets the Wmp-Session-Id header and session_id query parameter.
|
|
@@ -215,13 +257,22 @@ export class Peer {
|
|
|
215
257
|
}
|
|
216
258
|
}
|
|
217
259
|
handleRequest(req) {
|
|
260
|
+
const dispatch = async () => {
|
|
261
|
+
if (this.authorize) {
|
|
262
|
+
const allowed = await this.authorize(req.method, req.params);
|
|
263
|
+
if (!allowed) {
|
|
264
|
+
throw new WMPError(ErrorCode.NotAuthorized, `Not authorized to call ${req.method}`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return this.dispatchMethod(req.method, req.params);
|
|
268
|
+
};
|
|
218
269
|
if (isNotification(req)) {
|
|
219
|
-
|
|
270
|
+
dispatch().catch(() => {
|
|
220
271
|
/* notifications get no error response */
|
|
221
272
|
});
|
|
222
273
|
}
|
|
223
274
|
else {
|
|
224
|
-
|
|
275
|
+
dispatch()
|
|
225
276
|
.then((result) => {
|
|
226
277
|
const resp = createResponse(req.id, result);
|
|
227
278
|
return this.transport.send(resp);
|
|
@@ -234,6 +285,13 @@ export class Peer {
|
|
|
234
285
|
}
|
|
235
286
|
}
|
|
236
287
|
async dispatchMethod(method, params) {
|
|
288
|
+
if (this.validator) {
|
|
289
|
+
const errors = this.validator.validateMethod(method, params);
|
|
290
|
+
if (errors) {
|
|
291
|
+
const detail = errors.map((e) => `${e.path}: ${e.message}`).join("; ");
|
|
292
|
+
throw new WMPError(ErrorCode.InvalidParams, `Invalid params for ${method}: ${detail}`);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
237
295
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- JSON-RPC params are untyped
|
|
238
296
|
const p = params;
|
|
239
297
|
switch (method) {
|
|
@@ -243,11 +301,53 @@ export class Peer {
|
|
|
243
301
|
return this.handler.onSessionCreate(p);
|
|
244
302
|
}
|
|
245
303
|
break;
|
|
304
|
+
case Method.SessionResume:
|
|
305
|
+
if (this.handler.onSessionResume) {
|
|
306
|
+
return this.handler.onSessionResume(p);
|
|
307
|
+
}
|
|
308
|
+
break;
|
|
246
309
|
case Method.SessionClose:
|
|
247
310
|
if (this.handler.onSessionClose) {
|
|
248
311
|
this.handler.onSessionClose(p);
|
|
249
312
|
}
|
|
250
313
|
return undefined;
|
|
314
|
+
case Method.SessionAuthenticate:
|
|
315
|
+
if (this.handler.onSessionAuthenticate) {
|
|
316
|
+
return this.handler.onSessionAuthenticate(p);
|
|
317
|
+
}
|
|
318
|
+
break;
|
|
319
|
+
// Messages
|
|
320
|
+
case Method.MessageDeliver:
|
|
321
|
+
if (this.handler.onMessageDeliver) {
|
|
322
|
+
this.handler.onMessageDeliver(p);
|
|
323
|
+
}
|
|
324
|
+
return undefined;
|
|
325
|
+
case Method.MessageAck:
|
|
326
|
+
if (this.handler.onMessageAck) {
|
|
327
|
+
this.handler.onMessageAck(p);
|
|
328
|
+
}
|
|
329
|
+
return undefined;
|
|
330
|
+
case Method.MessagePoll:
|
|
331
|
+
if (this.handler.onMessagePoll) {
|
|
332
|
+
return this.handler.onMessagePoll(p);
|
|
333
|
+
}
|
|
334
|
+
break;
|
|
335
|
+
case Method.MessageStatus:
|
|
336
|
+
if (this.handler.onMessageStatus) {
|
|
337
|
+
this.handler.onMessageStatus(p);
|
|
338
|
+
}
|
|
339
|
+
return undefined;
|
|
340
|
+
// Capabilities
|
|
341
|
+
case Method.CapabilityUpdate:
|
|
342
|
+
if (this.handler.onCapabilityUpdate) {
|
|
343
|
+
return this.handler.onCapabilityUpdate(p);
|
|
344
|
+
}
|
|
345
|
+
break;
|
|
346
|
+
case Method.CapabilityList:
|
|
347
|
+
if (this.handler.onCapabilityList) {
|
|
348
|
+
return this.handler.onCapabilityList(p);
|
|
349
|
+
}
|
|
350
|
+
break;
|
|
251
351
|
// Flows — delegate to profile FlowHandlers first, then handler.
|
|
252
352
|
case Method.FlowStart: {
|
|
253
353
|
const fh = this.registry.getFlowHandler(p.flow_type);
|
|
@@ -347,7 +447,7 @@ export class Peer {
|
|
|
347
447
|
// -------------------------------------------------------------------------
|
|
348
448
|
wmpMeta() {
|
|
349
449
|
return {
|
|
350
|
-
version: VERSION,
|
|
450
|
+
version: this.negotiatedVersion ?? VERSION,
|
|
351
451
|
session_id: this.sessionId,
|
|
352
452
|
timestamp: new Date().toISOString(),
|
|
353
453
|
};
|
|
@@ -373,8 +473,12 @@ function toRPCError(err) {
|
|
|
373
473
|
if (err instanceof WMPError) {
|
|
374
474
|
return { code: err.code, message: err.message, data: err.data };
|
|
375
475
|
}
|
|
476
|
+
// Sanitize unexpected internal errors to avoid leaking implementation details.
|
|
477
|
+
if (typeof console !== "undefined" && console.error) {
|
|
478
|
+
console.error("Peer handler internal error:", err);
|
|
479
|
+
}
|
|
376
480
|
return {
|
|
377
481
|
code: ErrorCode.InternalError,
|
|
378
|
-
message:
|
|
482
|
+
message: "Internal error",
|
|
379
483
|
};
|
|
380
484
|
}
|
package/dist/schema.d.ts
CHANGED
|
@@ -28,5 +28,6 @@ export interface Validator {
|
|
|
28
28
|
* Create a schema validator by loading schemas from the filesystem.
|
|
29
29
|
*
|
|
30
30
|
* @param schemaDir Path to the wmp/schema directory containing the JSON Schema files.
|
|
31
|
+
* @param strict When true, unknown methods/responses are reported as validation errors.
|
|
31
32
|
*/
|
|
32
|
-
export declare function createValidator(schemaDir: string): Validator;
|
|
33
|
+
export declare function createValidator(schemaDir: string, strict?: boolean): Validator;
|
package/dist/schema.js
CHANGED
|
@@ -38,8 +38,9 @@ const RESPONSE_SCHEMAS = {
|
|
|
38
38
|
* Create a schema validator by loading schemas from the filesystem.
|
|
39
39
|
*
|
|
40
40
|
* @param schemaDir Path to the wmp/schema directory containing the JSON Schema files.
|
|
41
|
+
* @param strict When true, unknown methods/responses are reported as validation errors.
|
|
41
42
|
*/
|
|
42
|
-
export function createValidator(schemaDir) {
|
|
43
|
+
export function createValidator(schemaDir, strict = false) {
|
|
43
44
|
const ajv = new Ajv2020({
|
|
44
45
|
strict: false,
|
|
45
46
|
allErrors: true,
|
|
@@ -89,14 +90,20 @@ export function createValidator(schemaDir) {
|
|
|
89
90
|
return {
|
|
90
91
|
validateMethod(method, message) {
|
|
91
92
|
const schema = METHOD_SCHEMAS[method];
|
|
92
|
-
if (!schema)
|
|
93
|
-
return
|
|
93
|
+
if (!schema) {
|
|
94
|
+
return strict
|
|
95
|
+
? [{ path: "/", message: `No schema defined for method ${method}` }]
|
|
96
|
+
: null;
|
|
97
|
+
}
|
|
94
98
|
return validate(schema, message);
|
|
95
99
|
},
|
|
96
100
|
validateResponse(method, message) {
|
|
97
101
|
const schema = RESPONSE_SCHEMAS[method];
|
|
98
|
-
if (!schema)
|
|
99
|
-
return
|
|
102
|
+
if (!schema) {
|
|
103
|
+
return strict
|
|
104
|
+
? [{ path: "/", message: `No response schema defined for method ${method}` }]
|
|
105
|
+
: null;
|
|
106
|
+
}
|
|
100
107
|
return validate(schema, message);
|
|
101
108
|
},
|
|
102
109
|
validateMetadata(metadata) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SessionStore — server-side session persistence interface.
|
|
3
|
+
* Mirrors go-wmp/pkg/wmp/session.go for alignment.
|
|
4
|
+
*/
|
|
5
|
+
import type { Capabilities, SecurityMode } from "./types.js";
|
|
6
|
+
export interface Session {
|
|
7
|
+
id: string;
|
|
8
|
+
participants: string[];
|
|
9
|
+
capabilities: Capabilities;
|
|
10
|
+
security: SecurityMode;
|
|
11
|
+
createdAt: string;
|
|
12
|
+
expiresAt?: string;
|
|
13
|
+
metadata?: Record<string, unknown>;
|
|
14
|
+
}
|
|
15
|
+
export interface SessionStore {
|
|
16
|
+
create(session: Session): Promise<void>;
|
|
17
|
+
get(id: string): Promise<Session | undefined>;
|
|
18
|
+
update(session: Session): Promise<void>;
|
|
19
|
+
delete(id: string): Promise<void>;
|
|
20
|
+
cleanup(): Promise<number>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* In-memory SessionStore — suitable for development and testing.
|
|
24
|
+
*/
|
|
25
|
+
export declare class MemorySessionStore implements SessionStore {
|
|
26
|
+
private sessions;
|
|
27
|
+
create(session: Session): Promise<void>;
|
|
28
|
+
get(id: string): Promise<Session | undefined>;
|
|
29
|
+
update(session: Session): Promise<void>;
|
|
30
|
+
delete(id: string): Promise<void>;
|
|
31
|
+
cleanup(): Promise<number>;
|
|
32
|
+
get size(): number;
|
|
33
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SessionStore — server-side session persistence interface.
|
|
3
|
+
* Mirrors go-wmp/pkg/wmp/session.go for alignment.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* In-memory SessionStore — suitable for development and testing.
|
|
7
|
+
*/
|
|
8
|
+
export class MemorySessionStore {
|
|
9
|
+
sessions = new Map();
|
|
10
|
+
async create(session) {
|
|
11
|
+
this.sessions.set(session.id, { ...session });
|
|
12
|
+
}
|
|
13
|
+
async get(id) {
|
|
14
|
+
return this.sessions.get(id);
|
|
15
|
+
}
|
|
16
|
+
async update(session) {
|
|
17
|
+
this.sessions.set(session.id, { ...session });
|
|
18
|
+
}
|
|
19
|
+
async delete(id) {
|
|
20
|
+
this.sessions.delete(id);
|
|
21
|
+
}
|
|
22
|
+
async cleanup() {
|
|
23
|
+
const now = new Date().toISOString();
|
|
24
|
+
let count = 0;
|
|
25
|
+
for (const [id, session] of this.sessions) {
|
|
26
|
+
if (session.expiresAt && session.expiresAt < now) {
|
|
27
|
+
this.sessions.delete(id);
|
|
28
|
+
count++;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return count;
|
|
32
|
+
}
|
|
33
|
+
get size() {
|
|
34
|
+
return this.sessions.size;
|
|
35
|
+
}
|
|
36
|
+
}
|