@opendpp/webhooks 0.1.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/LICENSE +176 -0
- package/NOTICE +12 -0
- package/README.md +107 -0
- package/dist/index.d.ts +110 -0
- package/dist/index.js +125 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
package/NOTICE
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
@opendpp/webhooks
|
|
2
|
+
Copyright (c) Opendpp UAB.
|
|
3
|
+
|
|
4
|
+
This product includes software developed by Opendpp UAB.
|
|
5
|
+
Licensed under the Apache License, Version 2.0 (see the LICENSE file).
|
|
6
|
+
|
|
7
|
+
"OpenDPP" is a trademark of Opendpp UAB. Per Section 6 of the Apache License,
|
|
8
|
+
Version 2.0, this license does not grant permission to use the "OpenDPP" trade
|
|
9
|
+
names, trademarks, service marks, or product names. Forks may rebuild and
|
|
10
|
+
redistribute this code but must adopt a different name and may not represent
|
|
11
|
+
themselves as "OpenDPP" or as "OpenDPP-conformant".
|
|
12
|
+
Trademark policy: https://github.com/OpenDPP/opendpp-interop
|
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# @opendpp/webhooks
|
|
2
|
+
|
|
3
|
+
Typed **webhook event names + envelope shape** for the
|
|
4
|
+
[OpenDPP](https://opendpp-node.eu) Digital Product Passport service, plus a
|
|
5
|
+
zero-dependency, **constant-time HMAC-SHA256 signature verifier** so you can
|
|
6
|
+
trust an inbound webhook before acting on it (ESM, Node ≥ 26).
|
|
7
|
+
|
|
8
|
+
> Part of the OpenDPP **open client** surface (Apache-2.0). The hosted node —
|
|
9
|
+
> delivery, retries, the per-subscription secret, resolver, eIDAS sealing, 15-year
|
|
10
|
+
> persistence — stays a service you call. See
|
|
11
|
+
> [`opendpp-interop`](https://github.com/OpenDPP/opendpp-interop).
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install @opendpp/webhooks
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Verify an inbound webhook
|
|
20
|
+
|
|
21
|
+
Verify over the **raw request body** — re-serialising a parsed object changes key
|
|
22
|
+
order/whitespace and breaks the HMAC. Example with Express:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import express from "express";
|
|
26
|
+
import { verifyWebhookRequest, parseWebhookEnvelope } from "@opendpp/webhooks";
|
|
27
|
+
|
|
28
|
+
const app = express();
|
|
29
|
+
|
|
30
|
+
app.post(
|
|
31
|
+
"/webhooks/opendpp",
|
|
32
|
+
express.raw({ type: "application/json" }), // keep the raw body
|
|
33
|
+
(req, res) => {
|
|
34
|
+
const rawBody = req.body.toString("utf8");
|
|
35
|
+
const ok = verifyWebhookRequest(rawBody, {
|
|
36
|
+
get: (name) => req.header(name) ?? null,
|
|
37
|
+
}, process.env.OPENDPP_WEBHOOK_SECRET!);
|
|
38
|
+
|
|
39
|
+
if (!ok) return res.sendStatus(401);
|
|
40
|
+
|
|
41
|
+
const event = parseWebhookEnvelope(rawBody); // typed { id, type, created, data }
|
|
42
|
+
// dedupe on event.id (stable across retries), then handle event.type …
|
|
43
|
+
res.sendStatus(200); // return 2xx within ~5s or it counts as failed
|
|
44
|
+
},
|
|
45
|
+
);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
`verifyWebhookSignature({ payload, signature, secret, timestamp })` is the
|
|
49
|
+
lower-level primitive if you extract the headers yourself.
|
|
50
|
+
|
|
51
|
+
## The signing recipe
|
|
52
|
+
|
|
53
|
+
The signature OpenDPP sends in `X-OpenDPP-Signature` is:
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
HMAC_SHA256( secret, `${timestamp}.${rawBody}` ) // hex, lowercase
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
- `secret` — the full `whsec_…` per-subscription secret (returned once at create /
|
|
60
|
+
rotate-secret; format `whsec_` + 32 lowercase hex chars).
|
|
61
|
+
- `timestamp` — the `X-OpenDPP-Timestamp` header (unix seconds), prefixed into the
|
|
62
|
+
signed content as a replay defence.
|
|
63
|
+
- `rawBody` — the exact JSON request body.
|
|
64
|
+
|
|
65
|
+
Reject a delivery whose timestamp is more than ~5 minutes from now (the verifier
|
|
66
|
+
does this by default, `toleranceSeconds: 300`). Delivery is **at-least-once** with
|
|
67
|
+
up to 5 retries (exponential backoff) — **dedupe on `X-OpenDPP-Delivery`** (the
|
|
68
|
+
envelope `id`), which is stable across retries.
|
|
69
|
+
|
|
70
|
+
## Events
|
|
71
|
+
|
|
72
|
+
`passport.ingested` · `passport.updated` · `passport.sealed` · `passport.recalled`
|
|
73
|
+
· `passport.status_updated`. Envelope: `{ id, type, created, data }`, where `data`
|
|
74
|
+
is the public redacted JSON-LD passport document. Exported as
|
|
75
|
+
`OPENDPP_WEBHOOK_EVENT_TYPES`, `OpenDppWebhookEventType`, and
|
|
76
|
+
`OpenDppWebhookEnvelope<TData>`.
|
|
77
|
+
|
|
78
|
+
## Python reference receiver
|
|
79
|
+
|
|
80
|
+
The recipe is trivially portable — verify with the standard library:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
import hashlib
|
|
84
|
+
import hmac
|
|
85
|
+
import time
|
|
86
|
+
|
|
87
|
+
def verify_opendpp_webhook(raw_body: str, signature: str, timestamp: str,
|
|
88
|
+
secret: str, tolerance_seconds: int = 300) -> bool:
|
|
89
|
+
if tolerance_seconds > 0 and abs(time.time() - int(timestamp)) > tolerance_seconds:
|
|
90
|
+
return False
|
|
91
|
+
expected = hmac.new(
|
|
92
|
+
secret.encode(), f"{timestamp}.{raw_body}".encode(), hashlib.sha256
|
|
93
|
+
).hexdigest()
|
|
94
|
+
return hmac.compare_digest(expected, signature)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
# Flask:
|
|
99
|
+
# raw = request.get_data(as_text=True)
|
|
100
|
+
# ok = verify_opendpp_webhook(raw, request.headers["X-OpenDPP-Signature"],
|
|
101
|
+
# request.headers["X-OpenDPP-Timestamp"], SECRET)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## License
|
|
105
|
+
|
|
106
|
+
[Apache-2.0](./LICENSE) © Opendpp UAB. See [`NOTICE`](./NOTICE). "OpenDPP" is a
|
|
107
|
+
trademark of Opendpp UAB; this license grants no rights to the marks.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @opendpp/webhooks — OpenDPP webhook event types + HMAC-verify reference receiver
|
|
3
|
+
*
|
|
4
|
+
* Typed event names + the signed-envelope shape OpenDPP delivers, plus a constant-time
|
|
5
|
+
* HMAC-SHA256 signature verifier so an integrator can trust an inbound webhook. Pure ESM,
|
|
6
|
+
* Node >=26, zero runtime dependencies (uses only the built-in `node:crypto`).
|
|
7
|
+
*
|
|
8
|
+
* This is a fresh reference implementation derived from the public webhook contract
|
|
9
|
+
* (openapi.json `webhooks` + the documented signing scheme) — NOT a lift of the hosted
|
|
10
|
+
* node's delivery internals. The hosted node remains authoritative for delivery,
|
|
11
|
+
* retries, and the per-subscription secret.
|
|
12
|
+
*
|
|
13
|
+
* SIGNING RECIPE (must match the server byte-for-byte): the signature is
|
|
14
|
+
* `HMAC-SHA256(secret, `${timestamp}.${rawBody}`)` hex-encoded (lowercase), where
|
|
15
|
+
* `secret` is the full `whsec_…` per-subscription value, `timestamp` is the
|
|
16
|
+
* `X-OpenDPP-Timestamp` header (unix seconds), and `rawBody` is the EXACT request body
|
|
17
|
+
* string. Verify over the raw body — re-serialising a parsed object changes key order /
|
|
18
|
+
* whitespace and breaks the HMAC.
|
|
19
|
+
*
|
|
20
|
+
* Copyright (c) Opendpp UAB.
|
|
21
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
22
|
+
*
|
|
23
|
+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
24
|
+
* this file except in compliance with the License. You may obtain a copy of the
|
|
25
|
+
* License at
|
|
26
|
+
*
|
|
27
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
28
|
+
*
|
|
29
|
+
* Unless required by applicable law or agreed to in writing, software distributed
|
|
30
|
+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
31
|
+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
32
|
+
* specific language governing permissions and limitations under the License.
|
|
33
|
+
*
|
|
34
|
+
* "OpenDPP" is a trademark of Opendpp UAB; the Apache-2.0 license grants no rights to the marks.
|
|
35
|
+
*/
|
|
36
|
+
/** The webhook event types OpenDPP delivers (the `type` field + the `X-OpenDPP-Event` header). */
|
|
37
|
+
export declare const OPENDPP_WEBHOOK_EVENT_TYPES: readonly ["passport.ingested", "passport.updated", "passport.sealed", "passport.recalled", "passport.status_updated"];
|
|
38
|
+
export type OpenDppWebhookEventType = (typeof OPENDPP_WEBHOOK_EVENT_TYPES)[number];
|
|
39
|
+
/** A subscription event filter — a specific event type, or `"*"` for all events. */
|
|
40
|
+
export type OpenDppWebhookEventFilter = OpenDppWebhookEventType | "*";
|
|
41
|
+
/**
|
|
42
|
+
* The signed webhook envelope OpenDPP POSTs to a subscription URL. `id` is the stable delivery id
|
|
43
|
+
* (constant across retries — dedupe on it), `type` is the event, `created` is an ISO-8601 timestamp,
|
|
44
|
+
* and `data` is the public redacted JSON-LD passport document.
|
|
45
|
+
*/
|
|
46
|
+
export interface OpenDppWebhookEnvelope<TData = unknown> {
|
|
47
|
+
id: string;
|
|
48
|
+
type: OpenDppWebhookEventType;
|
|
49
|
+
created: string;
|
|
50
|
+
data: TData;
|
|
51
|
+
}
|
|
52
|
+
/** The OpenDPP webhook header names (case-insensitive on the wire). */
|
|
53
|
+
export declare const OPENDPP_WEBHOOK_HEADERS: {
|
|
54
|
+
/** Event type, for routing. */
|
|
55
|
+
readonly event: "X-OpenDPP-Event";
|
|
56
|
+
/** Stable delivery id (= envelope `id`); dedupe on this for at-least-once delivery. */
|
|
57
|
+
readonly delivery: "X-OpenDPP-Delivery";
|
|
58
|
+
/** Unix-seconds timestamp; prefixed into the signed content (replay defence). */
|
|
59
|
+
readonly timestamp: "X-OpenDPP-Timestamp";
|
|
60
|
+
/** Lowercase-hex HMAC-SHA256 signature. */
|
|
61
|
+
readonly signature: "X-OpenDPP-Signature";
|
|
62
|
+
};
|
|
63
|
+
/** The per-subscription secret format: `whsec_` + 32 lowercase hex chars. */
|
|
64
|
+
export declare const WEBHOOK_SECRET_PATTERN: RegExp;
|
|
65
|
+
/** Type guard for a known OpenDPP webhook event type. */
|
|
66
|
+
export declare function isOpenDppWebhookEventType(value: unknown): value is OpenDppWebhookEventType;
|
|
67
|
+
export interface VerifyWebhookSignatureOptions {
|
|
68
|
+
/** The RAW request body string, exactly as received. Do NOT re-stringify a parsed object. */
|
|
69
|
+
payload: string;
|
|
70
|
+
/** The `X-OpenDPP-Signature` header value (lowercase-hex HMAC-SHA256). */
|
|
71
|
+
signature: string;
|
|
72
|
+
/** The full per-subscription secret (the `whsec_…` value returned once at create / rotate). */
|
|
73
|
+
secret: string;
|
|
74
|
+
/** The `X-OpenDPP-Timestamp` header value (unix seconds). */
|
|
75
|
+
timestamp: string | number;
|
|
76
|
+
/** Max accepted clock skew in seconds for the replay check (default 300 = 5 min; 0 disables it). */
|
|
77
|
+
toleranceSeconds?: number;
|
|
78
|
+
/** Current unix-seconds time; injectable for deterministic tests (default `Date.now()/1000`). */
|
|
79
|
+
nowSeconds?: number;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Verifies an OpenDPP webhook signature in constant time, and (unless disabled) rejects a stale
|
|
83
|
+
* timestamp outside the tolerance window. Returns `true` only when the HMAC matches AND the timestamp
|
|
84
|
+
* is fresh. The signed content is `${timestamp}.${payload}` and the HMAC is keyed with the full secret.
|
|
85
|
+
*
|
|
86
|
+
* The hosted node re-mints a fresh `(timestamp, signature)` pair on every retry, so a retried delivery
|
|
87
|
+
* always carries a valid pair; dedupe on `X-OpenDPP-Delivery` (the envelope `id`).
|
|
88
|
+
*/
|
|
89
|
+
export declare function verifyWebhookSignature(options: VerifyWebhookSignatureOptions): boolean;
|
|
90
|
+
/** A minimal headers reader — satisfied by the Fetch `Headers` object and most framework header bags. */
|
|
91
|
+
export interface WebhookHeaderReader {
|
|
92
|
+
get(name: string): string | null | undefined;
|
|
93
|
+
}
|
|
94
|
+
export interface VerifyWebhookRequestOptions {
|
|
95
|
+
/** Max accepted clock skew in seconds (default 300 = 5 min; 0 disables the replay check). */
|
|
96
|
+
toleranceSeconds?: number;
|
|
97
|
+
/** Current unix-seconds time; injectable for deterministic tests. */
|
|
98
|
+
nowSeconds?: number;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Convenience over {@link verifyWebhookSignature}: reads the signature + timestamp headers off a
|
|
102
|
+
* Fetch-style `Headers` object (or anything with a `get(name)` method) and verifies the raw body.
|
|
103
|
+
*/
|
|
104
|
+
export declare function verifyWebhookRequest(rawBody: string, headers: WebhookHeaderReader, secret: string, options?: VerifyWebhookRequestOptions): boolean;
|
|
105
|
+
/**
|
|
106
|
+
* Parses a verified raw body into a typed envelope. Call this AFTER {@link verifyWebhookSignature} /
|
|
107
|
+
* {@link verifyWebhookRequest} — it does no verification itself. Throws if the body is not a
|
|
108
|
+
* well-formed OpenDPP envelope.
|
|
109
|
+
*/
|
|
110
|
+
export declare function parseWebhookEnvelope<TData = unknown>(rawBody: string): OpenDppWebhookEnvelope<TData>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @opendpp/webhooks — OpenDPP webhook event types + HMAC-verify reference receiver
|
|
3
|
+
*
|
|
4
|
+
* Typed event names + the signed-envelope shape OpenDPP delivers, plus a constant-time
|
|
5
|
+
* HMAC-SHA256 signature verifier so an integrator can trust an inbound webhook. Pure ESM,
|
|
6
|
+
* Node >=26, zero runtime dependencies (uses only the built-in `node:crypto`).
|
|
7
|
+
*
|
|
8
|
+
* This is a fresh reference implementation derived from the public webhook contract
|
|
9
|
+
* (openapi.json `webhooks` + the documented signing scheme) — NOT a lift of the hosted
|
|
10
|
+
* node's delivery internals. The hosted node remains authoritative for delivery,
|
|
11
|
+
* retries, and the per-subscription secret.
|
|
12
|
+
*
|
|
13
|
+
* SIGNING RECIPE (must match the server byte-for-byte): the signature is
|
|
14
|
+
* `HMAC-SHA256(secret, `${timestamp}.${rawBody}`)` hex-encoded (lowercase), where
|
|
15
|
+
* `secret` is the full `whsec_…` per-subscription value, `timestamp` is the
|
|
16
|
+
* `X-OpenDPP-Timestamp` header (unix seconds), and `rawBody` is the EXACT request body
|
|
17
|
+
* string. Verify over the raw body — re-serialising a parsed object changes key order /
|
|
18
|
+
* whitespace and breaks the HMAC.
|
|
19
|
+
*
|
|
20
|
+
* Copyright (c) Opendpp UAB.
|
|
21
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
22
|
+
*
|
|
23
|
+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
24
|
+
* this file except in compliance with the License. You may obtain a copy of the
|
|
25
|
+
* License at
|
|
26
|
+
*
|
|
27
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
28
|
+
*
|
|
29
|
+
* Unless required by applicable law or agreed to in writing, software distributed
|
|
30
|
+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
31
|
+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
32
|
+
* specific language governing permissions and limitations under the License.
|
|
33
|
+
*
|
|
34
|
+
* "OpenDPP" is a trademark of Opendpp UAB; the Apache-2.0 license grants no rights to the marks.
|
|
35
|
+
*/
|
|
36
|
+
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
37
|
+
/** The webhook event types OpenDPP delivers (the `type` field + the `X-OpenDPP-Event` header). */
|
|
38
|
+
export const OPENDPP_WEBHOOK_EVENT_TYPES = [
|
|
39
|
+
"passport.ingested",
|
|
40
|
+
"passport.updated",
|
|
41
|
+
"passport.sealed",
|
|
42
|
+
"passport.recalled",
|
|
43
|
+
"passport.status_updated",
|
|
44
|
+
];
|
|
45
|
+
/** The OpenDPP webhook header names (case-insensitive on the wire). */
|
|
46
|
+
export const OPENDPP_WEBHOOK_HEADERS = {
|
|
47
|
+
/** Event type, for routing. */
|
|
48
|
+
event: "X-OpenDPP-Event",
|
|
49
|
+
/** Stable delivery id (= envelope `id`); dedupe on this for at-least-once delivery. */
|
|
50
|
+
delivery: "X-OpenDPP-Delivery",
|
|
51
|
+
/** Unix-seconds timestamp; prefixed into the signed content (replay defence). */
|
|
52
|
+
timestamp: "X-OpenDPP-Timestamp",
|
|
53
|
+
/** Lowercase-hex HMAC-SHA256 signature. */
|
|
54
|
+
signature: "X-OpenDPP-Signature",
|
|
55
|
+
};
|
|
56
|
+
/** The per-subscription secret format: `whsec_` + 32 lowercase hex chars. */
|
|
57
|
+
export const WEBHOOK_SECRET_PATTERN = /^whsec_[0-9a-f]{32}$/;
|
|
58
|
+
/** Type guard for a known OpenDPP webhook event type. */
|
|
59
|
+
export function isOpenDppWebhookEventType(value) {
|
|
60
|
+
return typeof value === "string" && OPENDPP_WEBHOOK_EVENT_TYPES.includes(value);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Verifies an OpenDPP webhook signature in constant time, and (unless disabled) rejects a stale
|
|
64
|
+
* timestamp outside the tolerance window. Returns `true` only when the HMAC matches AND the timestamp
|
|
65
|
+
* is fresh. The signed content is `${timestamp}.${payload}` and the HMAC is keyed with the full secret.
|
|
66
|
+
*
|
|
67
|
+
* The hosted node re-mints a fresh `(timestamp, signature)` pair on every retry, so a retried delivery
|
|
68
|
+
* always carries a valid pair; dedupe on `X-OpenDPP-Delivery` (the envelope `id`).
|
|
69
|
+
*/
|
|
70
|
+
export function verifyWebhookSignature(options) {
|
|
71
|
+
const { payload, signature, secret, timestamp } = options;
|
|
72
|
+
if (typeof payload !== "string" || typeof signature !== "string" || typeof secret !== "string") {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
if (signature.length === 0)
|
|
76
|
+
return false;
|
|
77
|
+
const toleranceSeconds = options.toleranceSeconds ?? 300;
|
|
78
|
+
if (toleranceSeconds > 0) {
|
|
79
|
+
const ts = Number(timestamp);
|
|
80
|
+
if (!Number.isFinite(ts))
|
|
81
|
+
return false;
|
|
82
|
+
const now = options.nowSeconds ?? Math.floor(Date.now() / 1000);
|
|
83
|
+
if (Math.abs(now - ts) > toleranceSeconds)
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
const expected = createHmac("sha256", secret).update(`${timestamp}.${payload}`).digest("hex");
|
|
87
|
+
const provided = Buffer.from(signature, "utf8");
|
|
88
|
+
const computed = Buffer.from(expected, "utf8");
|
|
89
|
+
// timingSafeEqual throws on a length mismatch — guard first; a different length is a non-match anyway.
|
|
90
|
+
if (provided.length !== computed.length)
|
|
91
|
+
return false;
|
|
92
|
+
return timingSafeEqual(provided, computed);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Convenience over {@link verifyWebhookSignature}: reads the signature + timestamp headers off a
|
|
96
|
+
* Fetch-style `Headers` object (or anything with a `get(name)` method) and verifies the raw body.
|
|
97
|
+
*/
|
|
98
|
+
export function verifyWebhookRequest(rawBody, headers, secret, options = {}) {
|
|
99
|
+
const signature = headers.get(OPENDPP_WEBHOOK_HEADERS.signature) ?? "";
|
|
100
|
+
const timestamp = headers.get(OPENDPP_WEBHOOK_HEADERS.timestamp) ?? "";
|
|
101
|
+
return verifyWebhookSignature({
|
|
102
|
+
payload: rawBody,
|
|
103
|
+
signature,
|
|
104
|
+
secret,
|
|
105
|
+
timestamp,
|
|
106
|
+
toleranceSeconds: options.toleranceSeconds,
|
|
107
|
+
nowSeconds: options.nowSeconds,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Parses a verified raw body into a typed envelope. Call this AFTER {@link verifyWebhookSignature} /
|
|
112
|
+
* {@link verifyWebhookRequest} — it does no verification itself. Throws if the body is not a
|
|
113
|
+
* well-formed OpenDPP envelope.
|
|
114
|
+
*/
|
|
115
|
+
export function parseWebhookEnvelope(rawBody) {
|
|
116
|
+
const value = JSON.parse(rawBody);
|
|
117
|
+
if (typeof value !== "object" ||
|
|
118
|
+
value === null ||
|
|
119
|
+
typeof value.id !== "string" ||
|
|
120
|
+
typeof value.created !== "string" ||
|
|
121
|
+
!isOpenDppWebhookEventType(value.type)) {
|
|
122
|
+
throw new Error("Not a well-formed OpenDPP webhook envelope.");
|
|
123
|
+
}
|
|
124
|
+
return value;
|
|
125
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@opendpp/webhooks",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "OpenDPP webhook event types + a zero-dependency, constant-time HMAC-SHA256 signature verifier for inbound OpenDPP webhooks (ESM, Node >=26).",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"opendpp",
|
|
7
|
+
"dpp",
|
|
8
|
+
"digital-product-passport",
|
|
9
|
+
"webhooks",
|
|
10
|
+
"hmac",
|
|
11
|
+
"signature",
|
|
12
|
+
"webhook-verification",
|
|
13
|
+
"esm"
|
|
14
|
+
],
|
|
15
|
+
"license": "Apache-2.0",
|
|
16
|
+
"author": "Giovanni Savastano <giovanni.savastano@opendpp-node.eu>",
|
|
17
|
+
"type": "module",
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"import": "./dist/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"LICENSE",
|
|
30
|
+
"NOTICE",
|
|
31
|
+
"README.md"
|
|
32
|
+
],
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=26"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/OpenDPP/opendpp-interop.git",
|
|
42
|
+
"directory": "packages/webhooks"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/OpenDPP/opendpp-interop#readme",
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/OpenDPP/opendpp-interop/issues"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsc -p tsconfig.json",
|
|
50
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
51
|
+
"test": "node --test --import tsx 'test/*.test.ts'",
|
|
52
|
+
"prepublishOnly": "npm run build"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@types/node": "^26.0.0",
|
|
56
|
+
"tsx": "^4.22.4",
|
|
57
|
+
"typescript": "^6.0.3"
|
|
58
|
+
}
|
|
59
|
+
}
|