@dbsc-toolkit/core 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 +132 -0
- package/README.md +59 -0
- package/dist/crypto/jwk.d.ts +3 -0
- package/dist/crypto/jwk.d.ts.map +1 -0
- package/dist/crypto/jwk.js +36 -0
- package/dist/crypto/jwk.js.map +1 -0
- package/dist/crypto/jws.d.ts +15 -0
- package/dist/crypto/jws.d.ts.map +1 -0
- package/dist/crypto/jws.js +89 -0
- package/dist/crypto/jws.js.map +1 -0
- package/dist/errors.d.ts +27 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +39 -0
- package/dist/errors.js.map +1 -0
- package/dist/fallback/hmac.d.ts +9 -0
- package/dist/fallback/hmac.d.ts.map +1 -0
- package/dist/fallback/hmac.js +37 -0
- package/dist/fallback/hmac.js.map +1 -0
- package/dist/fallback/negotiate.d.ts +9 -0
- package/dist/fallback/negotiate.d.ts.map +1 -0
- package/dist/fallback/negotiate.js +22 -0
- package/dist/fallback/negotiate.js.map +1 -0
- package/dist/fallback/webauthn.d.ts +10 -0
- package/dist/fallback/webauthn.d.ts.map +1 -0
- package/dist/fallback/webauthn.js +41 -0
- package/dist/fallback/webauthn.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol/challenge.d.ts +4 -0
- package/dist/protocol/challenge.d.ts.map +1 -0
- package/dist/protocol/challenge.js +18 -0
- package/dist/protocol/challenge.js.map +1 -0
- package/dist/protocol/headers.d.ts +20 -0
- package/dist/protocol/headers.d.ts.map +1 -0
- package/dist/protocol/headers.js +30 -0
- package/dist/protocol/headers.js.map +1 -0
- package/dist/protocol/refresh.d.ts +8 -0
- package/dist/protocol/refresh.d.ts.map +1 -0
- package/dist/protocol/refresh.js +33 -0
- package/dist/protocol/refresh.js.map +1 -0
- package/dist/protocol/registration.d.ts +11 -0
- package/dist/protocol/registration.d.ts.map +1 -0
- package/dist/protocol/registration.js +35 -0
- package/dist/protocol/registration.js.map +1 -0
- package/dist/ratelimit/interface.d.ts +7 -0
- package/dist/ratelimit/interface.d.ts.map +1 -0
- package/dist/ratelimit/interface.js +11 -0
- package/dist/ratelimit/interface.js.map +1 -0
- package/dist/telemetry/hooks.d.ts +4 -0
- package/dist/telemetry/hooks.d.ts.map +1 -0
- package/dist/telemetry/hooks.js +11 -0
- package/dist/telemetry/hooks.js.map +1 -0
- package/dist/types.d.ts +91 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
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.
|
|
18
|
+
|
|
19
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
20
|
+
exercising permissions granted by this License.
|
|
21
|
+
|
|
22
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
23
|
+
including but not limited to software source code, documentation
|
|
24
|
+
source, and configuration files.
|
|
25
|
+
|
|
26
|
+
"Object" form shall mean any form resulting from mechanical
|
|
27
|
+
transformation or translation of a Source form, including but
|
|
28
|
+
not limited to compiled object code, generated documentation,
|
|
29
|
+
and conversions to other media types.
|
|
30
|
+
|
|
31
|
+
"Work" shall mean the work of authorship made available under
|
|
32
|
+
the License, as indicated by a copyright notice that is included in
|
|
33
|
+
or attached to the work.
|
|
34
|
+
|
|
35
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
36
|
+
form, that is based on (or derived from) the Work and for which the
|
|
37
|
+
editorial revisions, annotations, elaborations, or other transformations
|
|
38
|
+
represent, as a whole, an original work of authorship.
|
|
39
|
+
|
|
40
|
+
"Contribution" shall mean any work of authorship, including
|
|
41
|
+
the original version of the Work and any modifications or additions
|
|
42
|
+
to that Work or Derivative Works of the Work, that is intentionally
|
|
43
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
44
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
45
|
+
the copyright owner.
|
|
46
|
+
|
|
47
|
+
"Contributor" shall mean Licensor and any Legal Entity
|
|
48
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
49
|
+
subsequently incorporated within the Work.
|
|
50
|
+
|
|
51
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
52
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
53
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
54
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
55
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
56
|
+
Work and such Derivative Works in Source or Object form.
|
|
57
|
+
|
|
58
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
59
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
60
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
61
|
+
(except as stated in this section) patent license to make, have made,
|
|
62
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
63
|
+
where such license applies only to those patent claims licensable
|
|
64
|
+
by such Contributor that are necessarily infringed by their
|
|
65
|
+
Contribution(s) alone or by the combined work of their Contribution(s)
|
|
66
|
+
with the Work to which such Contribution(s) was submitted.
|
|
67
|
+
|
|
68
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
69
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
70
|
+
modifications, and in Source or Object form, provided that You
|
|
71
|
+
meet the following conditions:
|
|
72
|
+
|
|
73
|
+
(a) You must give any other recipients of the Work or Derivative Works
|
|
74
|
+
a copy of this License; and
|
|
75
|
+
|
|
76
|
+
(b) You must cause any modified files to carry prominent notices
|
|
77
|
+
stating that You changed the files; and
|
|
78
|
+
|
|
79
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
80
|
+
that You distribute, all copyright, patent, trademark, and
|
|
81
|
+
attribution notices from the Source form of the Work; and
|
|
82
|
+
|
|
83
|
+
(d) If the Work includes a "NOTICE" text file, you must include a
|
|
84
|
+
readable copy of the attribution notices contained within such NOTICE
|
|
85
|
+
file, in at least one of the following places: within a NOTICE text
|
|
86
|
+
file distributed as part of the Derivative Works; or, within the
|
|
87
|
+
Source form or documentation, if provided along with the Derivative
|
|
88
|
+
Works; or, on a display generated by the Derivative Works.
|
|
89
|
+
|
|
90
|
+
The "NOTICE" file referenced above refers to a text file that may
|
|
91
|
+
accompany the source form of the Work.
|
|
92
|
+
|
|
93
|
+
You may add Your own attribution notices within Derivative Works
|
|
94
|
+
that You distribute, alongside or as an addendum to the NOTICE text
|
|
95
|
+
from the Work, provided that such additional attribution notices
|
|
96
|
+
cannot be construed as modifying the License.
|
|
97
|
+
|
|
98
|
+
You may add Your own license statement for Your modifications and
|
|
99
|
+
may provide additional grant of rights to use, reproduce, or
|
|
100
|
+
distribute Derivative Works, subject to the terms and conditions
|
|
101
|
+
for the Work itself.
|
|
102
|
+
|
|
103
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
104
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
105
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
106
|
+
this License, without any additional terms or conditions.
|
|
107
|
+
|
|
108
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
109
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
110
|
+
|
|
111
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
112
|
+
agreed to in writing, Licensor provides the Work (and each Contributor
|
|
113
|
+
provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES
|
|
114
|
+
OR CONDITIONS OF ANY KIND, either express or implied, including,
|
|
115
|
+
without limitation, any warranties or conditions of TITLE,
|
|
116
|
+
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.
|
|
117
|
+
|
|
118
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
119
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
120
|
+
unless required by applicable law (such as deliberate and grossly
|
|
121
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
122
|
+
liable to You for damages, including any direct, indirect, special,
|
|
123
|
+
incidental, or exemplary damages of any character arising as a
|
|
124
|
+
result of this License or out of the use or inability to use the
|
|
125
|
+
Work.
|
|
126
|
+
|
|
127
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
128
|
+
the Work or Derivative Works thereof, You may choose to offer, and
|
|
129
|
+
charge a fee for, acceptance of support, warranty, indemnity,
|
|
130
|
+
or other liability obligations and/or rights consistent with this License.
|
|
131
|
+
|
|
132
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# @dbsc-toolkit/core
|
|
2
|
+
|
|
3
|
+
Framework-agnostic core for the DBSC Toolkit. Pure protocol logic, crypto primitives, type definitions, and a pluggable storage interface. No Express, no Fastify, no HTTP framework dependency.
|
|
4
|
+
|
|
5
|
+
Use this package directly if you are wiring DBSC into Koa, Hapi, AdonisJS, raw `http`, Bun, Deno, or any framework without a shipped adapter. Otherwise use one of the framework adapters built on top of this:
|
|
6
|
+
|
|
7
|
+
- `@dbsc-toolkit/server-express`
|
|
8
|
+
- `@dbsc-toolkit/server-nextjs`
|
|
9
|
+
- `@dbsc-toolkit/server-fastify`
|
|
10
|
+
- `@dbsc-toolkit/server-hono`
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
pnpm add @dbsc-toolkit/core
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## What it exports
|
|
19
|
+
|
|
20
|
+
- `handleRegistration`, `handleRefresh` — verify the JWS proof Chrome posts to your registration / refresh endpoints
|
|
21
|
+
- `issueChallenge`, `generateJti` — issue single-use challenges
|
|
22
|
+
- `buildRegistrationHeader`, `buildChallengeHeader`, `readSessionResponseHeader` — wire-format helpers
|
|
23
|
+
- `REGISTRATION_HEADER`, `RESPONSE_HEADER`, `CHALLENGE_HEADER` plus `LEGACY_*` aliases — current and legacy header names
|
|
24
|
+
- `MemoryStorage`-compatible `StorageAdapter` interface, `RateLimiter` interface, telemetry event types
|
|
25
|
+
- `DbscProtocolError`, `DbscVerificationError`, `ErrorCodes`
|
|
26
|
+
|
|
27
|
+
## Direct use without an adapter
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import {
|
|
31
|
+
handleRefresh,
|
|
32
|
+
issueChallenge,
|
|
33
|
+
readSessionResponseHeader,
|
|
34
|
+
buildChallengeHeader,
|
|
35
|
+
} from "@dbsc-toolkit/core";
|
|
36
|
+
|
|
37
|
+
// inside any framework's POST /dbsc/refresh handler
|
|
38
|
+
const sessionId = getCookie("__Host-dbsc-session");
|
|
39
|
+
const expectedJti = getCookie("__Host-dbsc-challenge");
|
|
40
|
+
const responseHeader = readSessionResponseHeader(req.headers);
|
|
41
|
+
|
|
42
|
+
if (!responseHeader) {
|
|
43
|
+
const challenge = await issueChallenge(sessionId, storage);
|
|
44
|
+
setHeader("Secure-Session-Challenge", buildChallengeHeader(challenge.jti));
|
|
45
|
+
setHeader("Sec-Session-Challenge", buildChallengeHeader(challenge.jti));
|
|
46
|
+
setCookie("__Host-dbsc-challenge", challenge.jti, { maxAge: 5 * 60 * 1000, ... });
|
|
47
|
+
return reply.status(403);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
await handleRefresh({ sessionId, secSessionResponseHeader: responseHeader, expectedJti }, storage);
|
|
51
|
+
setCookie("__Host-dbsc-session", sessionId, { maxAge: 10 * 60 * 1000, ... });
|
|
52
|
+
return reply.status(204);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
See the [main README](https://github.com/SulimanAbdulrazzaq/DBSC#readme) for the full picture.
|
|
56
|
+
|
|
57
|
+
## License
|
|
58
|
+
|
|
59
|
+
Apache-2.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwk.d.ts","sourceRoot":"","sources":["../../src/crypto/jwk.ts"],"names":[],"mappings":"AAKA,wBAAgB,WAAW,CAAC,GAAG,EAAE,UAAU,GAAG,IAAI,CAsCjD;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO,GAAG,OAAO,CAOlE"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { DbscVerificationError, ErrorCodes } from "../errors.js";
|
|
2
|
+
const SUPPORTED_CURVES = new Set(["P-256"]);
|
|
3
|
+
const MIN_RSA_BITS = 2048;
|
|
4
|
+
export function validateJwk(jwk) {
|
|
5
|
+
if (jwk.kty === "EC") {
|
|
6
|
+
if (!SUPPORTED_CURVES.has(jwk.crv ?? "")) {
|
|
7
|
+
throw new DbscVerificationError(ErrorCodes.INVALID_JWK, `unsupported curve: ${jwk.crv}`);
|
|
8
|
+
}
|
|
9
|
+
if (!jwk.x || !jwk.y) {
|
|
10
|
+
throw new DbscVerificationError(ErrorCodes.INVALID_JWK, "EC key missing x or y coordinate");
|
|
11
|
+
}
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if (jwk.kty === "RSA") {
|
|
15
|
+
if (!jwk.n) {
|
|
16
|
+
throw new DbscVerificationError(ErrorCodes.INVALID_JWK, "RSA key missing modulus");
|
|
17
|
+
}
|
|
18
|
+
const bits = base64urlBits(jwk.n);
|
|
19
|
+
if (bits < MIN_RSA_BITS) {
|
|
20
|
+
throw new DbscVerificationError(ErrorCodes.INVALID_JWK, `RSA key too short: ${bits} bits, minimum ${MIN_RSA_BITS}`);
|
|
21
|
+
}
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
throw new DbscVerificationError(ErrorCodes.INVALID_JWK, `unsupported key type: ${jwk.kty}`);
|
|
25
|
+
}
|
|
26
|
+
export function detectAlgorithm(jwk) {
|
|
27
|
+
if (jwk.kty === "EC" && jwk.crv === "P-256")
|
|
28
|
+
return "ES256";
|
|
29
|
+
if (jwk.kty === "RSA")
|
|
30
|
+
return "RS256";
|
|
31
|
+
throw new DbscVerificationError(ErrorCodes.UNKNOWN_ALGORITHM, `cannot determine algorithm for kty=${jwk.kty} crv=${jwk.crv}`);
|
|
32
|
+
}
|
|
33
|
+
function base64urlBits(b64) {
|
|
34
|
+
return (b64.length * 6) / 8 * 8;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=jwk.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwk.js","sourceRoot":"","sources":["../../src/crypto/jwk.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAEjE,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC;AAC5C,MAAM,YAAY,GAAG,IAAI,CAAC;AAE1B,MAAM,UAAU,WAAW,CAAC,GAAe;IACzC,IAAI,GAAG,CAAC,GAAG,KAAK,IAAI,EAAE,CAAC;QACrB,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,CAAC;YACzC,MAAM,IAAI,qBAAqB,CAC7B,UAAU,CAAC,WAAW,EACtB,sBAAsB,GAAG,CAAC,GAAG,EAAE,CAChC,CAAC;QACJ,CAAC;QACD,IAAI,CAA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|
1
|
+
import { type JWTPayload } from "jose";
|
|
2
|
+
export interface DbscJwsClaims extends JWTPayload {
|
|
3
|
+
jti: string;
|
|
4
|
+
}
|
|
5
|
+
export interface ParsedDbscJws {
|
|
6
|
+
claims: DbscJwsClaims;
|
|
7
|
+
jwk?: JsonWebKey;
|
|
8
|
+
}
|
|
9
|
+
export declare function verifyDbscJws(token: string, storedJwk: JsonWebKey, expectedJti: string): Promise<DbscJwsClaims>;
|
|
10
|
+
export declare function parseRegistrationJws(token: string): Promise<{
|
|
11
|
+
claims: JWTPayload;
|
|
12
|
+
jwk: JsonWebKey;
|
|
13
|
+
algorithm: "ES256" | "RS256";
|
|
14
|
+
}>;
|
|
15
|
+
//# sourceMappingURL=jws.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jws.d.ts","sourceRoot":"","sources":["../../src/crypto/jws.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+C,KAAK,UAAU,EAAY,MAAM,MAAM,CAAC;AAO9F,MAAM,WAAW,aAAc,SAAQ,UAAU;IAC/C,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,aAAa,CAAC;IACtB,GAAG,CAAC,EAAE,UAAU,CAAC;CAClB;AAED,wBAAsB,aAAa,CACjC,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,UAAU,EACrB,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,aAAa,CAAC,CAoDxB;AAED,wBAAsB,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IACjE,MAAM,EAAE,UAAU,CAAC;IACnB,GAAG,EAAE,UAAU,CAAC;IAChB,SAAS,EAAE,OAAO,GAAG,OAAO,CAAC;CAC9B,CAAC,CAiDD"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { importJWK, jwtVerify, decodeProtectedHeader } from "jose";
|
|
2
|
+
import { DbscVerificationError, ErrorCodes } from "../errors.js";
|
|
3
|
+
import { validateJwk } from "./jwk.js";
|
|
4
|
+
const DBSC_TYPE = "dbsc+jwt";
|
|
5
|
+
const SUPPORTED_ALGS = ["ES256", "RS256"];
|
|
6
|
+
export async function verifyDbscJws(token, storedJwk, expectedJti) {
|
|
7
|
+
let header;
|
|
8
|
+
try {
|
|
9
|
+
header = decodeProtectedHeader(token);
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
throw new DbscVerificationError(ErrorCodes.MALFORMED_JWS, "failed to decode JWS header");
|
|
13
|
+
}
|
|
14
|
+
if (header["typ"] !== DBSC_TYPE) {
|
|
15
|
+
throw new DbscVerificationError(ErrorCodes.MALFORMED_JWS, `expected typ=${DBSC_TYPE}, got ${header["typ"]}`);
|
|
16
|
+
}
|
|
17
|
+
const alg = header["alg"];
|
|
18
|
+
if (!SUPPORTED_ALGS.includes(alg)) {
|
|
19
|
+
throw new DbscVerificationError(ErrorCodes.UNKNOWN_ALGORITHM, `unsupported algorithm: ${alg}`);
|
|
20
|
+
}
|
|
21
|
+
let key;
|
|
22
|
+
try {
|
|
23
|
+
key = await importJWK(storedJwk, alg);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
throw new DbscVerificationError(ErrorCodes.INVALID_JWK, "failed to import stored JWK");
|
|
27
|
+
}
|
|
28
|
+
let payload;
|
|
29
|
+
try {
|
|
30
|
+
const result = await jwtVerify(token, key, {
|
|
31
|
+
algorithms: [...SUPPORTED_ALGS],
|
|
32
|
+
});
|
|
33
|
+
payload = result.payload;
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
throw new DbscVerificationError(ErrorCodes.SIGNATURE_INVALID, "JWS signature verification failed");
|
|
37
|
+
}
|
|
38
|
+
if (typeof payload.jti !== "string") {
|
|
39
|
+
throw new DbscVerificationError(ErrorCodes.MALFORMED_JWS, "missing jti claim");
|
|
40
|
+
}
|
|
41
|
+
if (payload.jti !== expectedJti) {
|
|
42
|
+
throw new DbscVerificationError(ErrorCodes.JTI_MISMATCH, "jti does not match issued challenge");
|
|
43
|
+
}
|
|
44
|
+
return payload;
|
|
45
|
+
}
|
|
46
|
+
export async function parseRegistrationJws(token) {
|
|
47
|
+
let header;
|
|
48
|
+
try {
|
|
49
|
+
header = decodeProtectedHeader(token);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
throw new DbscVerificationError(ErrorCodes.MALFORMED_JWS, "failed to decode registration JWS header");
|
|
53
|
+
}
|
|
54
|
+
if (header["typ"] !== DBSC_TYPE) {
|
|
55
|
+
throw new DbscVerificationError(ErrorCodes.MALFORMED_JWS, `expected typ=${DBSC_TYPE}, got ${header["typ"]}`);
|
|
56
|
+
}
|
|
57
|
+
const alg = header["alg"];
|
|
58
|
+
if (!SUPPORTED_ALGS.includes(alg)) {
|
|
59
|
+
throw new DbscVerificationError(ErrorCodes.UNKNOWN_ALGORITHM, `unsupported algorithm: ${alg}`);
|
|
60
|
+
}
|
|
61
|
+
const jwk = header["jwk"];
|
|
62
|
+
if (!jwk) {
|
|
63
|
+
throw new DbscVerificationError(ErrorCodes.MALFORMED_JWS, "registration JWS missing jwk in header");
|
|
64
|
+
}
|
|
65
|
+
validateJwk(jwk);
|
|
66
|
+
let key;
|
|
67
|
+
try {
|
|
68
|
+
key = await importJWK(jwk, alg);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
throw new DbscVerificationError(ErrorCodes.INVALID_JWK, "failed to import registration JWK");
|
|
72
|
+
}
|
|
73
|
+
let payload;
|
|
74
|
+
try {
|
|
75
|
+
const result = await jwtVerify(token, key, {
|
|
76
|
+
algorithms: [...SUPPORTED_ALGS],
|
|
77
|
+
});
|
|
78
|
+
payload = result.payload;
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
throw new DbscVerificationError(ErrorCodes.SIGNATURE_INVALID, "registration JWS self-signature invalid");
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
claims: payload,
|
|
85
|
+
jwk,
|
|
86
|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
+
readonly code: string;
|
|
3
|
+
constructor(code: string, message: string);
|
|
4
|
+
}
|
|
5
|
+
export declare class DbscVerificationError extends Error {
|
|
6
|
+
readonly code: string;
|
|
7
|
+
constructor(code: string, message: string);
|
|
8
|
+
}
|
|
9
|
+
export declare class DbscStorageError extends Error {
|
|
10
|
+
readonly code: string;
|
|
11
|
+
constructor(code: string, message: string);
|
|
12
|
+
}
|
|
13
|
+
export declare const ErrorCodes: {
|
|
14
|
+
readonly MISSING_RESPONSE_HEADER: "MISSING_RESPONSE_HEADER";
|
|
15
|
+
readonly MALFORMED_JWS: "MALFORMED_JWS";
|
|
16
|
+
readonly INVALID_JWK: "INVALID_JWK";
|
|
17
|
+
readonly UNKNOWN_ALGORITHM: "UNKNOWN_ALGORITHM";
|
|
18
|
+
readonly CHALLENGE_NOT_FOUND: "CHALLENGE_NOT_FOUND";
|
|
19
|
+
readonly CHALLENGE_EXPIRED: "CHALLENGE_EXPIRED";
|
|
20
|
+
readonly CHALLENGE_CONSUMED: "CHALLENGE_CONSUMED";
|
|
21
|
+
readonly JTI_MISMATCH: "JTI_MISMATCH";
|
|
22
|
+
readonly SIGNATURE_INVALID: "SIGNATURE_INVALID";
|
|
23
|
+
readonly KEY_NOT_FOUND: "KEY_NOT_FOUND";
|
|
24
|
+
readonly SESSION_NOT_FOUND: "SESSION_NOT_FOUND";
|
|
25
|
+
readonly RATE_LIMITED: "RATE_LIMITED";
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;gBAEV,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAK1C;AAED,qBAAa,qBAAsB,SAAQ,KAAK;IAC9C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;gBAEV,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAK1C;AAED,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;gBAEV,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAK1C;AAED,eAAO,MAAM,UAAU;;;;;;;;;;;;;CAab,CAAC"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export class DbscProtocolError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
constructor(code, message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "DbscProtocolError";
|
|
6
|
+
this.code = code;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export class DbscVerificationError extends Error {
|
|
10
|
+
code;
|
|
11
|
+
constructor(code, message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "DbscVerificationError";
|
|
14
|
+
this.code = code;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export class DbscStorageError extends Error {
|
|
18
|
+
code;
|
|
19
|
+
constructor(code, message) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "DbscStorageError";
|
|
22
|
+
this.code = code;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export const ErrorCodes = {
|
|
26
|
+
MISSING_RESPONSE_HEADER: "MISSING_RESPONSE_HEADER",
|
|
27
|
+
MALFORMED_JWS: "MALFORMED_JWS",
|
|
28
|
+
INVALID_JWK: "INVALID_JWK",
|
|
29
|
+
UNKNOWN_ALGORITHM: "UNKNOWN_ALGORITHM",
|
|
30
|
+
CHALLENGE_NOT_FOUND: "CHALLENGE_NOT_FOUND",
|
|
31
|
+
CHALLENGE_EXPIRED: "CHALLENGE_EXPIRED",
|
|
32
|
+
CHALLENGE_CONSUMED: "CHALLENGE_CONSUMED",
|
|
33
|
+
JTI_MISMATCH: "JTI_MISMATCH",
|
|
34
|
+
SIGNATURE_INVALID: "SIGNATURE_INVALID",
|
|
35
|
+
KEY_NOT_FOUND: "KEY_NOT_FOUND",
|
|
36
|
+
SESSION_NOT_FOUND: "SESSION_NOT_FOUND",
|
|
37
|
+
RATE_LIMITED: "RATE_LIMITED",
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IACjC,IAAI,CAAS;IAEtB,YAAY,IAAY,EAAE,OAAe;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;QAChC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AAED,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IACrC,IAAI,CAAS;IAEtB,YAAY,IAAY,EAAE,OAAe;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;QACpC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AAED,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAChC,IAAI,CAAS;IAEtB,YAAY,IAAY,EAAE,OAAe;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;QAC/B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AAED,MAAM,CAAC,MAAM,UAAU,GAAG;IACxB,uBAAuB,EAAE,yBAAyB;IAClD,aAAa,EAAE,eAAe;IAC9B,WAAW,EAAE,aAAa;IAC1B,iBAAiB,EAAE,mBAAmB;IACtC,mBAAmB,EAAE,qBAAqB;IAC1C,iBAAiB,EAAE,mBAAmB;IACtC,kBAAkB,EAAE,oBAAoB;IACxC,YAAY,EAAE,cAAc;IAC5B,iBAAiB,EAAE,mBAAmB;IACtC,aAAa,EAAE,eAAe;IAC9B,iBAAiB,EAAE,mBAAmB;IACtC,YAAY,EAAE,cAAc;CACpB,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface HmacSignalBundle {
|
|
2
|
+
userAgent: string;
|
|
3
|
+
acceptLanguage: string;
|
|
4
|
+
secureContext: boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare function collectSignals(headers: Record<string, string | string[] | undefined>): HmacSignalBundle;
|
|
7
|
+
export declare function generateHmacToken(signals: HmacSignalBundle, secret: Buffer): string;
|
|
8
|
+
export declare function verifyHmacToken(token: string, signals: HmacSignalBundle, secret: Buffer): boolean;
|
|
9
|
+
//# sourceMappingURL=hmac.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hmac.d.ts","sourceRoot":"","sources":["../../src/fallback/hmac.ts"],"names":[],"mappings":"AAQA,MAAM,WAAW,gBAAgB;IAC/B,SAAS,EAAE,MAAM,CAAC;IAClB,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,OAAO,CAAC;CACxB;AAED,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,GAAG,gBAAgB,CAMvG;AAQD,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAKnF;AAED,wBAAgB,eAAe,CAC7B,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,gBAAgB,EACzB,MAAM,EAAE,MAAM,GACb,OAAO,CAeT"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
2
|
+
// This tier binds a cookie to a set of browser signals via HMAC.
|
|
3
|
+
// It does NOT provide hardware-level binding. Use only when DBSC and WebAuthn
|
|
4
|
+
// are both unavailable. Document this limitation to end users.
|
|
5
|
+
const SIGNAL_SEPARATOR = "|";
|
|
6
|
+
export function collectSignals(headers) {
|
|
7
|
+
return {
|
|
8
|
+
userAgent: headers["user-agent"] ?? "",
|
|
9
|
+
acceptLanguage: headers["accept-language"] ?? "",
|
|
10
|
+
secureContext: headers["x-forwarded-proto"] === "https",
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function serializeSignals(bundle) {
|
|
14
|
+
return [bundle.userAgent, bundle.acceptLanguage, String(bundle.secureContext)].join(SIGNAL_SEPARATOR);
|
|
15
|
+
}
|
|
16
|
+
export function generateHmacToken(signals, secret) {
|
|
17
|
+
const nonce = randomBytes(16).toString("base64url");
|
|
18
|
+
const data = `${nonce}${SIGNAL_SEPARATOR}${serializeSignals(signals)}`;
|
|
19
|
+
const mac = createHmac("sha256", secret).update(data).digest("base64url");
|
|
20
|
+
return `${nonce}.${mac}`;
|
|
21
|
+
}
|
|
22
|
+
export function verifyHmacToken(token, signals, secret) {
|
|
23
|
+
const dot = token.indexOf(".");
|
|
24
|
+
if (dot === -1)
|
|
25
|
+
return false;
|
|
26
|
+
const nonce = token.slice(0, dot);
|
|
27
|
+
const providedMac = token.slice(dot + 1);
|
|
28
|
+
const data = `${nonce}${SIGNAL_SEPARATOR}${serializeSignals(signals)}`;
|
|
29
|
+
const expectedMac = createHmac("sha256", secret).update(data).digest("base64url");
|
|
30
|
+
try {
|
|
31
|
+
return timingSafeEqual(Buffer.from(providedMac), Buffer.from(expectedMac));
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=hmac.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hmac.js","sourceRoot":"","sources":["../../src/fallback/hmac.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEvE,i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AC,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;IAEzC,MAAM,IAAI,GAAG,GAAG,KAAK,GAAG,gBAAgB,GAAG,gBAAgB,CAAC,OAAO,CAAC,EAAE,CAAC;IACvE,MAAM,WAAW,GAAG,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IAElF,IAAI,CAAC;QACH,OAAO,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC;IAC7E,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ProtectionTier } from "../types.js";
|
|
2
|
+
export interface NegotiationContext {
|
|
3
|
+
acceptsDbsc: boolean;
|
|
4
|
+
supportsWebAuthn: boolean;
|
|
5
|
+
hmacAllowed: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function negotiateTier(ctx: NegotiationContext): ProtectionTier;
|
|
8
|
+
export declare function detectDbscSupport(headers: Record<string, string | string[] | undefined>): boolean;
|
|
9
|
+
//# sourceMappingURL=negotiate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"negotiate.d.ts","sourceRoot":"","sources":["../../src/fallback/negotiate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD,MAAM,WAAW,kBAAkB;IACjC,WAAW,EAAE,OAAO,CAAC;IACrB,gBAAgB,EAAE,OAAO,CAAC;IAC1B,WAAW,EAAE,OAAO,CAAC;CACtB;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,kBAAkB,GAAG,cAAc,CAKrE;AAED,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,GAAG,OAAO,CAWjG"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function negotiateTier(ctx) {
|
|
2
|
+
if (ctx.acceptsDbsc)
|
|
3
|
+
return "dbsc";
|
|
4
|
+
if (ctx.supportsWebAuthn)
|
|
5
|
+
return "webauthn";
|
|
6
|
+
if (ctx.hmacAllowed)
|
|
7
|
+
return "hmac";
|
|
8
|
+
return "none";
|
|
9
|
+
}
|
|
10
|
+
export function detectDbscSupport(headers) {
|
|
11
|
+
const secFetch = headers["sec-fetch-site"];
|
|
12
|
+
const ua = (headers["user-agent"] ?? "");
|
|
13
|
+
// Chrome 146+ on Windows carries DBSC support
|
|
14
|
+
// The definitive signal is whether the browser responds to Secure-Session-Registration
|
|
15
|
+
// This header is set by the server; we detect support by checking Chrome version
|
|
16
|
+
const chromeMatch = /Chrome\/(\d+)/.exec(ua);
|
|
17
|
+
if (!chromeMatch)
|
|
18
|
+
return false;
|
|
19
|
+
const version = parseInt(chromeMatch[1] ?? "0", 10);
|
|
20
|
+
return version >= 146 && !!secFetch;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=negotiate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"negotiate.js","sourceRoot":"","sources":["../../src/fallback/negotiate.ts"],"names":[],"mappings":"AAQA,MAAM,UAAU,aAAa,CAAC,GAAuB;IACnD,IAAI,GAAG,CAAC,WAAW;QAAE,OAAO,MAAM,CAAC;IACnC,IAAI,GAAG,CAAC,gBAAgB;QAAE,OAAO,UAAU,CAAC;IAC5C,IAAI,GAAG,CAAC,WAAW;QAAE,OAAO,MAAM,CAAC;IACnC,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,OAAsD;IACtF,MAAM,QAAQ,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC3C,MAAM,EAAE,GAAG,CAAC,OAAO,CAAC,YAAY,CAAC,IAAI,EAAE,CAAW,CAAC;IAEnD,8CAA8C;IAC9C,uFAAuF;IACvF,iFAAiF;IACjF,MAAM,WAAW,GAAG,eAAe,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC7C,IAAI,CAAC,WAAW;QAAE,OAAO,KAAK,CAAC;IAC/B,MAAM,OAAO,GAAG,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,GAAG,EAAE,EAAE,CAAC,CAAC;IACpD,OAAO,OAAO,IAAI,GAAG,IAAI,CAAC,CAAC,QAAQ,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { generateRegistrationOptions, verifyRegistrationResponse, generateAuthenticationOptions, verifyAuthenticationResponse, type VerifiedRegistrationResponse, type VerifyAuthenticationResponseOpts } from "@simplewebauthn/server";
|
|
2
|
+
export interface WebAuthnRegistrationChallenge {
|
|
3
|
+
options: Awaited<ReturnType<typeof generateRegistrationOptions>>;
|
|
4
|
+
challenge: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function generateWebAuthnRegistration(rpName: string, rpId: string, userId: string, userName: string): Promise<WebAuthnRegistrationChallenge>;
|
|
7
|
+
export declare function verifyWebAuthnRegistration(response: Parameters<typeof verifyRegistrationResponse>[0]["response"], expectedChallenge: string, expectedOrigin: string, rpId: string): Promise<VerifiedRegistrationResponse>;
|
|
8
|
+
export declare function generateWebAuthnAuthentication(rpId: string, allowedCredentialIds: string[]): Promise<Awaited<ReturnType<typeof generateAuthenticationOptions>>>;
|
|
9
|
+
export declare function verifyWebAuthnAuthentication(opts: VerifyAuthenticationResponseOpts): Promise<Awaited<ReturnType<typeof verifyAuthenticationResponse>>>;
|
|
10
|
+
//# sourceMappingURL=webauthn.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webauthn.d.ts","sourceRoot":"","sources":["../../src/fallback/webauthn.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,2BAA2B,EAC3B,0BAA0B,EAC1B,6BAA6B,EAC7B,4BAA4B,EAE5B,KAAK,4BAA4B,EACjC,KAAK,gCAAgC,EACtC,MAAM,wBAAwB,CAAC;AAEhC,MAAM,WAAW,6BAA6B;IAC5C,OAAO,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO,2BAA2B,CAAC,CAAC,CAAC;IACjE,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,wBAAsB,4BAA4B,CAChD,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,6BAA6B,CAAC,CAkBxC;AAED,wBAAsB,0BAA0B,CAC9C,QAAQ,EAAE,UAAU,CAAC,OAAO,0BAA0B,CAAC,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,EACtE,iBAAiB,EAAE,MAAM,EACzB,cAAc,EAAE,MAAM,EACtB,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,4BAA4B,CAAC,CAOvC;AAED,wBAAsB,8BAA8B,CAClD,IAAI,EAAE,MAAM,EACZ,oBAAoB,EAAE,MAAM,EAAE,GAC7B,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,6BAA6B,CAAC,CAAC,CAAC,CASpE;AAED,wBAAsB,4BAA4B,CAChD,IAAI,EAAE,gCAAgC,GACrC,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,4BAA4B,CAAC,CAAC,CAAC,CAEnE"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { generateRegistrationOptions, verifyRegistrationResponse, generateAuthenticationOptions, verifyAuthenticationResponse, } from "@simplewebauthn/server";
|
|
2
|
+
export async function generateWebAuthnRegistration(rpName, rpId, userId, userName) {
|
|
3
|
+
const opts = {
|
|
4
|
+
rpName,
|
|
5
|
+
rpID: rpId,
|
|
6
|
+
userID: new TextEncoder().encode(userId),
|
|
7
|
+
userName,
|
|
8
|
+
authenticatorSelection: {
|
|
9
|
+
residentKey: "required",
|
|
10
|
+
userVerification: "preferred",
|
|
11
|
+
},
|
|
12
|
+
attestationType: "none",
|
|
13
|
+
};
|
|
14
|
+
const options = await generateRegistrationOptions(opts);
|
|
15
|
+
return {
|
|
16
|
+
options,
|
|
17
|
+
challenge: options.challenge,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export async function verifyWebAuthnRegistration(response, expectedChallenge, expectedOrigin, rpId) {
|
|
21
|
+
return verifyRegistrationResponse({
|
|
22
|
+
response,
|
|
23
|
+
expectedChallenge,
|
|
24
|
+
expectedOrigin,
|
|
25
|
+
expectedRPID: rpId,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
export async function generateWebAuthnAuthentication(rpId, allowedCredentialIds) {
|
|
29
|
+
return generateAuthenticationOptions({
|
|
30
|
+
rpID: rpId,
|
|
31
|
+
allowCredentials: allowedCredentialIds.map((id) => ({
|
|
32
|
+
id,
|
|
33
|
+
transports: ["internal"],
|
|
34
|
+
})),
|
|
35
|
+
userVerification: "preferred",
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
export async function verifyWebAuthnAuthentication(opts) {
|
|
39
|
+
return verifyAuthenticationResponse(opts);
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=webauthn.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webauthn.js","sourceRoot":"","sources":["../../src/fallback/webauthn.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,2BAA2B,EAC3B,0BAA0B,EAC1B,6BAA6B,EAC7B,4BAA4B,GAI7B,MAAM,wBAAwB,CAAC;AAOhC,MAAM,CAAC,KAAK,UAAU,4BAA4B,CAChD,MAAc,EACd,IAAY,EACZ,MAAc,EACd,QAAgB;IAEhB,MAAM,IAAI,GAAoC;QAC5C,MAAM;QACN,IAAI,EAAE,IAAI;QACV,MAAM,EAAE,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC;QACxC,QAAQ;QACR,sBAAsB,EAAE;YACtB,WAAW,EAAE,UAAU;YACvB,gBAAgB,EAAE,WAAW;SAC9B;QACD,eAAe,EAAE,MAAM;KACxB,CAAC;IAEF,MAAM,OAAO,GAAG,MAAM,2BAA2B,CAAC,IAAI,CAAC,CAAC;IACxD,OAAO;QACL,OAAO;QACP,SAAS,EAAE,OAAO,CAAC,SAAS;KAC7B,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAC9C,QAAsE,EACtE,iBAAyB,EACzB,cAAsB,EACtB,IAAY;IAEZ,OAAO,0BAA0B,CAAC;QAChC,QAAQ;QACR,iBAAiB;QACjB,cAAc;QACd,YAAY,EAAE,IAAI;KACnB,CAAC,CAAC;AACL,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,8BAA8B,CAClD,IAAY,EACZ,oBAA8B;IAE9B,OAAO,6BAA6B,CAAC;QACnC,IAAI,EAAE,IAAI;QACV,gBAAgB,EAAE,oBAAoB,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;YAClD,EAAE;YACF,UAAU,EAAE,CAAC,UAAU,CAAC;SACzB,CAAC,CAAC;QACH,gBAAgB,EAAE,WAAW;KAC9B,CAAC,CAAC;AACL,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,4BAA4B,CAChD,IAAsC;IAEtC,OAAO,4BAA4B,CAAC,IAAI,CAAC,CAAC;AAC5C,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type { ProtectionTier, BoundKey, Session, Challenge, RegistrationProof, RefreshProof, StorageAdapter, RateLimiter, DbscOptions, AnyTelemetryEvent, TelemetryEvent, RegistrationEvent, RefreshEvent, VerificationFailureEvent, SessionStolenEvent, FallbackTierEvent, } from "./types.js";
|
|
2
|
+
export { DbscProtocolError, DbscVerificationError, DbscStorageError, ErrorCodes } from "./errors.js";
|
|
3
|
+
export { validateJwk, detectAlgorithm } from "./crypto/jwk.js";
|
|
4
|
+
export { verifyDbscJws, parseRegistrationJws } from "./crypto/jws.js";
|
|
5
|
+
export { generateJti, issueChallenge } from "./protocol/challenge.js";
|
|
6
|
+
export { buildRegistrationHeader, buildChallengeHeader, parseSessionResponseHeader, buildSessionIdCookie, readSessionResponseHeader, REGISTRATION_HEADER, RESPONSE_HEADER, CHALLENGE_HEADER, LEGACY_REGISTRATION_HEADER, LEGACY_RESPONSE_HEADER, LEGACY_CHALLENGE_HEADER, } from "./protocol/headers.js";
|
|
7
|
+
export { handleRegistration } from "./protocol/registration.js";
|
|
8
|
+
export { handleRefresh } from "./protocol/refresh.js";
|
|
9
|
+
export { negotiateTier, detectDbscSupport } from "./fallback/negotiate.js";
|
|
10
|
+
export { generateWebAuthnRegistration, verifyWebAuthnRegistration, generateWebAuthnAuthentication, verifyWebAuthnAuthentication, } from "./fallback/webauthn.js";
|
|
11
|
+
export { collectSignals, generateHmacToken, verifyHmacToken } from "./fallback/hmac.js";
|
|
12
|
+
export { NoopRateLimiter } from "./ratelimit/interface.js";
|
|
13
|
+
export { emit } from "./telemetry/hooks.js";
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,cAAc,EACd,QAAQ,EACR,OAAO,EACP,SAAS,EACT,iBAAiB,EACjB,YAAY,EACZ,cAAc,EACd,WAAW,EACX,WAAW,EACX,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EACjB,YAAY,EACZ,wBAAwB,EACxB,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAErG,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAEtE,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACtE,OAAO,EACL,uBAAuB,EACvB,oBAAoB,EACpB,0BAA0B,EAC1B,oBAAoB,EACpB,yBAAyB,EACzB,mBAAmB,EACnB,eAAe,EACf,gBAAgB,EAChB,0BAA0B,EAC1B,sBAAsB,EACtB,uBAAuB,GACxB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC3E,OAAO,EACL,4BAA4B,EAC5B,0BAA0B,EAC1B,8BAA8B,EAC9B,4BAA4B,GAC7B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAExF,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { DbscProtocolError, DbscVerificationError, DbscStorageError, ErrorCodes } from "./errors.js";
|
|
2
|
+
export { validateJwk, detectAlgorithm } from "./crypto/jwk.js";
|
|
3
|
+
export { verifyDbscJws, parseRegistrationJws } from "./crypto/jws.js";
|
|
4
|
+
export { generateJti, issueChallenge } from "./protocol/challenge.js";
|
|
5
|
+
export { buildRegistrationHeader, buildChallengeHeader, parseSessionResponseHeader, buildSessionIdCookie, readSessionResponseHeader, REGISTRATION_HEADER, RESPONSE_HEADER, CHALLENGE_HEADER, LEGACY_REGISTRATION_HEADER, LEGACY_RESPONSE_HEADER, LEGACY_CHALLENGE_HEADER, } from "./protocol/headers.js";
|
|
6
|
+
export { handleRegistration } from "./protocol/registration.js";
|
|
7
|
+
export { handleRefresh } from "./protocol/refresh.js";
|
|
8
|
+
export { negotiateTier, detectDbscSupport } from "./fallback/negotiate.js";
|
|
9
|
+
export { generateWebAuthnRegistration, verifyWebAuthnRegistration, generateWebAuthnAuthentication, verifyWebAuthnAuthentication, } from "./fallback/webauthn.js";
|
|
10
|
+
export { collectSignals, generateHmacToken, verifyHmacToken } from "./fallback/hmac.js";
|
|
11
|
+
export { NoopRateLimiter } from "./ratelimit/interface.js";
|
|
12
|
+
export { emit } from "./telemetry/hooks.js";
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAmBA,OAAO,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAErG,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAEtE,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACtE,OAAO,EACL,uBAAuB,EACvB,oBAAoB,EACpB,0BAA0B,EAC1B,oBAAoB,EACpB,yBAAyB,EACzB,mBAAmB,EACnB,eAAe,EACf,gBAAgB,EAChB,0BAA0B,EAC1B,sBAAsB,EACtB,uBAAuB,GACxB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC3E,OAAO,EACL,4BAA4B,EAC5B,0BAA0B,EAC1B,8BAA8B,EAC9B,4BAA4B,GAC7B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAExF,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Challenge, StorageAdapter } from "../types.js";
|
|
2
|
+
export declare function generateJti(): string;
|
|
3
|
+
export declare function issueChallenge(sessionId: string, storage: StorageAdapter, ttlMs?: number): Promise<Challenge>;
|
|
4
|
+
//# sourceMappingURL=challenge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"challenge.d.ts","sourceRoot":"","sources":["../../src/protocol/challenge.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAI7D,wBAAgB,WAAW,IAAI,MAAM,CAEpC;AAED,wBAAsB,cAAc,CAClC,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,cAAc,EACvB,KAAK,GAAE,MAAuB,GAC7B,OAAO,CAAC,SAAS,CAAC,CAWpB"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
const DEFAULT_TTL_MS = 5 * 60 * 1000;
|
|
3
|
+
export function generateJti() {
|
|
4
|
+
return randomBytes(32).toString("base64url");
|
|
5
|
+
}
|
|
6
|
+
export async function issueChallenge(sessionId, storage, ttlMs = DEFAULT_TTL_MS) {
|
|
7
|
+
const now = Date.now();
|
|
8
|
+
const challenge = {
|
|
9
|
+
jti: generateJti(),
|
|
10
|
+
sessionId,
|
|
11
|
+
createdAt: now,
|
|
12
|
+
expiresAt: now + ttlMs,
|
|
13
|
+
consumed: false,
|
|
14
|
+
};
|
|
15
|
+
await storage.setChallenge(challenge);
|
|
16
|
+
return challenge;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=challenge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"challenge.js","sourceRoot":"","sources":["../../src/protocol/challenge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAG1C,MAAM,cAAc,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;AAErC,MAAM,UAAU,WAAW;IACzB,OAAO,WAAW,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;AAC/C,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,SAAiB,EACjB,OAAuB,EACvB,QAAgB,cAAc;IAE9B,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IACvB,MAAM,SAAS,GAAc;QAC3B,GAAG,EAAE,WAAW,EAAE;QAClB,SAAS;QACT,SAAS,EAAE,GAAG;QACd,SAAS,EAAE,GAAG,GAAG,KAAK;QACtB,QAAQ,EAAE,KAAK;KAChB,CAAC;IACF,MAAM,OAAO,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;IACtC,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface RegistrationHeaderOptions {
|
|
2
|
+
algorithm?: "ES256" | "RS256";
|
|
3
|
+
refreshPath: string;
|
|
4
|
+
challenge: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function buildRegistrationHeader(opts: RegistrationHeaderOptions): string;
|
|
7
|
+
export declare function buildChallengeHeader(jti: string): string;
|
|
8
|
+
export declare function parseSessionResponseHeader(raw: string): string;
|
|
9
|
+
export declare function buildSessionIdCookie(sessionId: string, opts: {
|
|
10
|
+
secure: boolean;
|
|
11
|
+
sameSite: string;
|
|
12
|
+
}): string;
|
|
13
|
+
export declare const REGISTRATION_HEADER = "Secure-Session-Registration";
|
|
14
|
+
export declare const RESPONSE_HEADER = "Secure-Session-Response";
|
|
15
|
+
export declare const CHALLENGE_HEADER = "Secure-Session-Challenge";
|
|
16
|
+
export declare const LEGACY_REGISTRATION_HEADER = "Sec-Session-Registration";
|
|
17
|
+
export declare const LEGACY_RESPONSE_HEADER = "Sec-Session-Response";
|
|
18
|
+
export declare const LEGACY_CHALLENGE_HEADER = "Sec-Session-Challenge";
|
|
19
|
+
export declare function readSessionResponseHeader(headers: Record<string, string | string[] | undefined>): string | undefined;
|
|
20
|
+
//# sourceMappingURL=headers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"headers.d.ts","sourceRoot":"","sources":["../../src/protocol/headers.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,yBAAyB;IACxC,SAAS,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,yBAAyB,GAAG,MAAM,CAG/E;AAED,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAExD;AAED,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE9D;AAED,wBAAgB,oBAAoB,CAClC,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC1C,MAAM,CAKR;AAED,eAAO,MAAM,mBAAmB,gCAAgC,CAAC;AACjE,eAAO,MAAM,eAAe,4BAA4B,CAAC;AACzD,eAAO,MAAM,gBAAgB,6BAA6B,CAAC;AAE3D,eAAO,MAAM,0BAA0B,6BAA6B,CAAC;AACrE,eAAO,MAAM,sBAAsB,yBAAyB,CAAC;AAC7D,eAAO,MAAM,uBAAuB,0BAA0B,CAAC;AAE/D,wBAAgB,yBAAyB,CACvC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,GACrD,MAAM,GAAG,SAAS,CAIpB"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function buildRegistrationHeader(opts) {
|
|
2
|
+
const alg = opts.algorithm ?? "ES256";
|
|
3
|
+
return `(${alg});path="${opts.refreshPath}";challenge="${opts.challenge}"`;
|
|
4
|
+
}
|
|
5
|
+
export function buildChallengeHeader(jti) {
|
|
6
|
+
return `"${jti}"`;
|
|
7
|
+
}
|
|
8
|
+
export function parseSessionResponseHeader(raw) {
|
|
9
|
+
return raw.trim();
|
|
10
|
+
}
|
|
11
|
+
export function buildSessionIdCookie(sessionId, opts) {
|
|
12
|
+
const parts = [`__Secure-Session-Id=${sessionId}`, "HttpOnly", "Path=/"];
|
|
13
|
+
if (opts.secure)
|
|
14
|
+
parts.push("Secure");
|
|
15
|
+
parts.push(`SameSite=${opts.sameSite}`);
|
|
16
|
+
return parts.join("; ");
|
|
17
|
+
}
|
|
18
|
+
export const REGISTRATION_HEADER = "Secure-Session-Registration";
|
|
19
|
+
export const RESPONSE_HEADER = "Secure-Session-Response";
|
|
20
|
+
export const CHALLENGE_HEADER = "Secure-Session-Challenge";
|
|
21
|
+
export const LEGACY_REGISTRATION_HEADER = "Sec-Session-Registration";
|
|
22
|
+
export const LEGACY_RESPONSE_HEADER = "Sec-Session-Response";
|
|
23
|
+
export const LEGACY_CHALLENGE_HEADER = "Sec-Session-Challenge";
|
|
24
|
+
export function readSessionResponseHeader(headers) {
|
|
25
|
+
const v = headers["secure-session-response"] ?? headers["sec-session-response"];
|
|
26
|
+
if (Array.isArray(v))
|
|
27
|
+
return v[0];
|
|
28
|
+
return v;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=headers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"headers.js","sourceRoot":"","sources":["../../src/protocol/headers.ts"],"names":[],"mappings":"AAMA,MAAM,UAAU,uBAAuB,CAAC,IAA+B;IACrE,MAAM,GAAG,GAAG,IA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C;AACX,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { RefreshProof, StorageAdapter } from "../types.js";
|
|
2
|
+
export interface RefreshRequest {
|
|
3
|
+
sessionId: string;
|
|
4
|
+
secSessionResponseHeader: string | undefined;
|
|
5
|
+
expectedJti: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function handleRefresh(req: RefreshRequest, storage: StorageAdapter): Promise<RefreshProof>;
|
|
8
|
+
//# sourceMappingURL=refresh.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refresh.d.ts","sourceRoot":"","sources":["../../src/protocol/refresh.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAEhE,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,MAAM,CAAC;IAClB,wBAAwB,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7C,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,wBAAsB,aAAa,CACjC,GAAG,EAAE,cAAc,EACnB,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,YAAY,CAAC,CAwCvB"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { verifyDbscJws } from "../crypto/jws.js";
|
|
2
|
+
import { DbscProtocolError, DbscVerificationError, ErrorCodes } from "../errors.js";
|
|
3
|
+
export async function handleRefresh(req, storage) {
|
|
4
|
+
if (!req.secSessionResponseHeader) {
|
|
5
|
+
throw new DbscProtocolError(ErrorCodes.MISSING_RESPONSE_HEADER, "Secure-Session-Response header is required for refresh");
|
|
6
|
+
}
|
|
7
|
+
const key = await storage.getBoundKey(req.sessionId);
|
|
8
|
+
if (!key) {
|
|
9
|
+
throw new DbscVerificationError(ErrorCodes.KEY_NOT_FOUND, "no bound key for session");
|
|
10
|
+
}
|
|
11
|
+
const challenge = await storage.getChallenge(req.expectedJti);
|
|
12
|
+
if (!challenge) {
|
|
13
|
+
throw new DbscVerificationError(ErrorCodes.CHALLENGE_NOT_FOUND, "challenge not found");
|
|
14
|
+
}
|
|
15
|
+
if (challenge.consumed) {
|
|
16
|
+
throw new DbscVerificationError(ErrorCodes.CHALLENGE_CONSUMED, "challenge already consumed");
|
|
17
|
+
}
|
|
18
|
+
if (Date.now() > challenge.expiresAt) {
|
|
19
|
+
throw new DbscVerificationError(ErrorCodes.CHALLENGE_EXPIRED, "challenge expired");
|
|
20
|
+
}
|
|
21
|
+
const token = req.secSessionResponseHeader.trim();
|
|
22
|
+
await verifyDbscJws(token, key.jwk, req.expectedJti);
|
|
23
|
+
const consumed = await storage.consumeChallenge(req.expectedJti);
|
|
24
|
+
if (!consumed) {
|
|
25
|
+
throw new DbscVerificationError(ErrorCodes.CHALLENGE_CONSUMED, "challenge already consumed");
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
sessionId: req.sessionId,
|
|
29
|
+
jti: req.expectedJti,
|
|
30
|
+
verified: true,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=refresh.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refresh.js","sourceRoot":"","sources":["../../src/protocol/refresh.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AASpF,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,GAAmB,EACnB,OAAuB;IAEvB,IAAI,CAAC,GAAG,CAAC,wBAAwB,EAAE,CAAC;QAClC,MAAM,IAAI,iBAAiB,CACzB,UAAU,CAAC,uBAAuB,EAClC,wDAAwD,CACzD,CAAC;IACJ,CAAC;IAED,MAAM,GAAG,GAAG,MAAM,OAAO,CAAC,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IACrD,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,MAAM,IA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ader: string | undefined;
|
|
5
|
+
expectedJti: string;
|
|
6
|
+
}
|
|
7
|
+
export interface RegistrationResult {
|
|
8
|
+
boundKey: BoundKey;
|
|
9
|
+
}
|
|
10
|
+
export declare function handleRegistration(req: RegistrationRequest, storage: StorageAdapter): Promise<RegistrationResult>;
|
|
11
|
+
//# sourceMappingURL=registration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registration.d.ts","sourceRoot":"","sources":["../../src/protocol/registration.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE5D,MAAM,WAAW,mBAAmB;IAClC,SAAS,EAAE,MAAM,CAAC;IAClB,wBAAwB,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7C,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,QAAQ,CAAC;CACpB;AAED,wBAAsB,kBAAkB,CACtC,GAAG,EAAE,mBAAmB,EACxB,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,kBAAkB,CAAC,CAwC7B"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { parseRegistrationJws } from "../crypto/jws.js";
|
|
2
|
+
import { DbscProtocolError, DbscVerificationError, ErrorCodes } from "../errors.js";
|
|
3
|
+
export async function handleRegistration(req, storage) {
|
|
4
|
+
if (!req.secSessionResponseHeader) {
|
|
5
|
+
throw new DbscProtocolError(ErrorCodes.MISSING_RESPONSE_HEADER, "Secure-Session-Response header is required");
|
|
6
|
+
}
|
|
7
|
+
const token = req.secSessionResponseHeader.trim();
|
|
8
|
+
const { jwk, algorithm, claims } = await parseRegistrationJws(token);
|
|
9
|
+
const challenge = await storage.getChallenge(req.expectedJti);
|
|
10
|
+
if (!challenge) {
|
|
11
|
+
throw new DbscVerificationError(ErrorCodes.CHALLENGE_NOT_FOUND, "challenge not found");
|
|
12
|
+
}
|
|
13
|
+
if (challenge.consumed) {
|
|
14
|
+
throw new DbscVerificationError(ErrorCodes.CHALLENGE_CONSUMED, "challenge already consumed");
|
|
15
|
+
}
|
|
16
|
+
if (Date.now() > challenge.expiresAt) {
|
|
17
|
+
throw new DbscVerificationError(ErrorCodes.CHALLENGE_EXPIRED, "challenge expired");
|
|
18
|
+
}
|
|
19
|
+
if (claims.jti !== req.expectedJti) {
|
|
20
|
+
throw new DbscVerificationError(ErrorCodes.JTI_MISMATCH, "jti does not match challenge");
|
|
21
|
+
}
|
|
22
|
+
const consumed = await storage.consumeChallenge(req.expectedJti);
|
|
23
|
+
if (!consumed) {
|
|
24
|
+
throw new DbscVerificationError(ErrorCodes.CHALLENGE_CONSUMED, "challenge already consumed");
|
|
25
|
+
}
|
|
26
|
+
const boundKey = {
|
|
27
|
+
sessionId: req.sessionId,
|
|
28
|
+
jwk,
|
|
29
|
+
createdAt: Date.now(),
|
|
30
|
+
algorithm,
|
|
31
|
+
};
|
|
32
|
+
await storage.setBoundKey(boundKey);
|
|
33
|
+
return { boundKey };
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=registration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registration.js","sourceRoot":"","sources":["../../src/protocol/registration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAapF,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,GAAwB,EACxB,OAAuB;IAEvB,IAAI,CAAC,GAAG,CAAC,wBAAwB,EAAE,CAAC;QAClC,MAAM,IAAI,iBAAiB,CACzB,UAAU,CAAC,uBAAuB,EAClC,4CAA4C,CAC7C,CAAC;IACJ,CAAC;IAED,MAAM,KAAK,GAAG,GAAG,CAAC,wBAAwB,CAAC,IAAI,EAAE,CAAC;IAClD,MAAM,EAAE,GAAG,EA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+
import type { RateLimiter } from "../types.js";
|
|
2
|
+
export declare class NoopRateLimiter implements RateLimiter {
|
|
3
|
+
checkRegistration(_ip: string): Promise<boolean>;
|
|
4
|
+
checkRefresh(_ip: string, _sessionId: string): Promise<boolean>;
|
|
5
|
+
recordFailure(_ip: string, _sessionId?: string): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../src/ratelimit/interface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAG/C,qBAAa,eAAgB,YAAW,WAAW;IAC3C,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAIhD,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAI/D,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CACrE"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Default no-op limiter. Replace with a real implementation in production.
|
|
2
|
+
export class NoopRateLimiter {
|
|
3
|
+
async checkRegistration(_ip) {
|
|
4
|
+
return true;
|
|
5
|
+
}
|
|
6
|
+
async checkRefresh(_ip, _sessionId) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
async recordFailure(_ip, _sessionId) { }
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=interface.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interface.js","sourceRoot":"","sources":["../../src/ratelimit/interface.ts"],"names":[],"mappings":"AAEA,2EAA2E;AAC3E,MAAM,OAAO,eAAe;IAC1B,KAAK,CAAC,iBAAiB,CAAC,GAAW;QACjC,OAAO,IAAI,CAAC;IACd,CAAC;IAED,KAAK,CAAC,YAAY,CAAC,GAAW,EAAE,UAAkB;QAChD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,GAAW,EAAE,UAAmB,IAAkB,CAAC;CACxE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/telemetry/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAErD,MAAM,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;AAE9D,wBAAgB,IAAI,CAAC,OAAO,EAAE,YAAY,GAAG,SAAS,EAAE,KAAK,EAAE,iBAAiB,GAAG,IAAI,CAOtF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.js","sourceRoot":"","sources":["../../src/telemetry/hooks.ts"],"names":[],"mappings":"AAIA,MAAM,UAAU,IAAI,CAAC,OAAiC,EAAE,KAAwB;IAC9E,IAAI,CAAC,OAAO;QAAE,OAAO;IACrB,IAAI,CAAC;QACH,OAAO,CAAC,KAAK,CAAC,CAAC;IACjB,CAAC;IAAC,MAAM,CAAC;QACP,oDAAoD;IACtD,CAAC;AACH,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
export type ProtectionTier = "dbsc" | "webauthn" | "hmac" | "none";
|
|
2
|
+
export interface BoundKey {
|
|
3
|
+
sessionId: string;
|
|
4
|
+
jwk: JsonWebKey;
|
|
5
|
+
createdAt: number;
|
|
6
|
+
algorithm: "ES256" | "RS256";
|
|
7
|
+
}
|
|
8
|
+
export interface Session {
|
|
9
|
+
id: string;
|
|
10
|
+
userId: string;
|
|
11
|
+
tier: ProtectionTier;
|
|
12
|
+
createdAt: number;
|
|
13
|
+
expiresAt: number;
|
|
14
|
+
}
|
|
15
|
+
export interface Challenge {
|
|
16
|
+
jti: string;
|
|
17
|
+
sessionId: string;
|
|
18
|
+
createdAt: number;
|
|
19
|
+
expiresAt: number;
|
|
20
|
+
consumed: boolean;
|
|
21
|
+
}
|
|
22
|
+
export interface RegistrationProof {
|
|
23
|
+
sessionId: string;
|
|
24
|
+
jwk: JsonWebKey;
|
|
25
|
+
algorithm: "ES256" | "RS256";
|
|
26
|
+
jti: string;
|
|
27
|
+
}
|
|
28
|
+
export interface RefreshProof {
|
|
29
|
+
sessionId: string;
|
|
30
|
+
jti: string;
|
|
31
|
+
verified: boolean;
|
|
32
|
+
}
|
|
33
|
+
export interface StorageAdapter {
|
|
34
|
+
getSession(id: string): Promise<Session | null>;
|
|
35
|
+
setSession(session: Session): Promise<void>;
|
|
36
|
+
deleteSession(id: string): Promise<void>;
|
|
37
|
+
getBoundKey(sessionId: string): Promise<BoundKey | null>;
|
|
38
|
+
setBoundKey(key: BoundKey): Promise<void>;
|
|
39
|
+
deleteBoundKey(sessionId: string): Promise<void>;
|
|
40
|
+
getChallenge(jti: string): Promise<Challenge | null>;
|
|
41
|
+
setChallenge(challenge: Challenge): Promise<void>;
|
|
42
|
+
consumeChallenge(jti: string): Promise<boolean>;
|
|
43
|
+
revokeSession(sessionId: string): Promise<void>;
|
|
44
|
+
revokeAllForUser(userId: string): Promise<void>;
|
|
45
|
+
}
|
|
46
|
+
export interface RateLimiter {
|
|
47
|
+
checkRegistration(ip: string): Promise<boolean>;
|
|
48
|
+
checkRefresh(ip: string, sessionId: string): Promise<boolean>;
|
|
49
|
+
recordFailure(ip: string, sessionId?: string): Promise<void>;
|
|
50
|
+
}
|
|
51
|
+
export interface TelemetryEvent {
|
|
52
|
+
sessionId: string;
|
|
53
|
+
tier: ProtectionTier;
|
|
54
|
+
timestamp: number;
|
|
55
|
+
}
|
|
56
|
+
export interface RegistrationEvent extends TelemetryEvent {
|
|
57
|
+
type: "registration";
|
|
58
|
+
algorithm: string;
|
|
59
|
+
ip: string;
|
|
60
|
+
}
|
|
61
|
+
export interface RefreshEvent extends TelemetryEvent {
|
|
62
|
+
type: "refresh";
|
|
63
|
+
ip: string;
|
|
64
|
+
}
|
|
65
|
+
export interface VerificationFailureEvent extends TelemetryEvent {
|
|
66
|
+
type: "verification_failure";
|
|
67
|
+
reason: string;
|
|
68
|
+
ip: string;
|
|
69
|
+
}
|
|
70
|
+
export interface SessionStolenEvent extends TelemetryEvent {
|
|
71
|
+
type: "session_stolen";
|
|
72
|
+
ip: string;
|
|
73
|
+
}
|
|
74
|
+
export interface FallbackTierEvent extends TelemetryEvent {
|
|
75
|
+
type: "fallback_tier";
|
|
76
|
+
from: ProtectionTier;
|
|
77
|
+
to: ProtectionTier;
|
|
78
|
+
reason: string;
|
|
79
|
+
}
|
|
80
|
+
export type AnyTelemetryEvent = RegistrationEvent | RefreshEvent | VerificationFailureEvent | SessionStolenEvent | FallbackTierEvent;
|
|
81
|
+
export interface DbscOptions {
|
|
82
|
+
storage: StorageAdapter;
|
|
83
|
+
fallback?: "webauthn" | "hmac" | "none";
|
|
84
|
+
registrationPath?: string;
|
|
85
|
+
refreshPath?: string;
|
|
86
|
+
boundCookieTtl?: number;
|
|
87
|
+
registrationCookieTtl?: number;
|
|
88
|
+
rateLimiter?: RateLimiter;
|
|
89
|
+
onEvent?: (event: AnyTelemetryEvent) => void;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,UAAU,GAAG,MAAM,GAAG,MAAM,CAAC;AAEnE,MAAM,WAAW,QAAQ;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,UAAU,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,OAAO,GAAG,OAAO,CAAC;CAC9B;AAED,MAAM,WAAW,OAAO;IACtB,EAAE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,IAAI,EAAE,cAAc,CAAC;IACrB,EAAE,EAAE,cAAc,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,MAAM,iBAAiB,GACzB,iBAAiB,GACjB,YAAY,GACZ,wBAAwB,GACxB,kBAAkB,GAClB,iBAAiB,CAAC;AAEtB,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,cAAc,CAAC;IACxB,QAAQ,CAAC,EAAE,UAAU,GAAG,MAAM,GAAG,MAAM,CAAC;IACxC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;CAC9C"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dbsc-toolkit/core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Framework-agnostic DBSC protocol implementation for Node.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@simplewebauthn/server": "^10.0.0",
|
|
16
|
+
"jose": "^5.3.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^20.0.0",
|
|
20
|
+
"typescript": "^5.4.0",
|
|
21
|
+
"vitest": "^1.6.0"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"README.md"
|
|
26
|
+
],
|
|
27
|
+
"license": "Apache-2.0",
|
|
28
|
+
"homepage": "https://github.com/SulimanAbdulrazzaq/dbsc-toolkit#readme",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/SulimanAbdulrazzaq/dbsc-toolkit/issues"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/SulimanAbdulrazzaq/dbsc-toolkit.git",
|
|
35
|
+
"directory": "packages/core"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"dbsc",
|
|
42
|
+
"device-bound-session-credentials",
|
|
43
|
+
"session",
|
|
44
|
+
"security",
|
|
45
|
+
"tpm",
|
|
46
|
+
"auth",
|
|
47
|
+
"cookies",
|
|
48
|
+
"webauthn"
|
|
49
|
+
],
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsc -p tsconfig.json",
|
|
52
|
+
"test": "vitest run",
|
|
53
|
+
"lint": "eslint src",
|
|
54
|
+
"clean": "rm -rf dist"
|
|
55
|
+
}
|
|
56
|
+
}
|