nid-webauthn-emulator 0.1.3

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.
Files changed (53) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +185 -0
  3. package/dist/spec/authenticator/authenticator-emulator.spec.d.ts +1 -0
  4. package/dist/spec/authenticator/authenticator-emulator.spec.js +18 -0
  5. package/dist/spec/authenticator/ctap-model.spec.d.ts +1 -0
  6. package/dist/spec/authenticator/ctap-model.spec.js +95 -0
  7. package/dist/spec/integration/integration.spec.d.ts +1 -0
  8. package/dist/spec/integration/integration.spec.js +32 -0
  9. package/dist/spec/integration/passkeys-api-client.d.ts +11 -0
  10. package/dist/spec/integration/passkeys-api-client.js +2 -0
  11. package/dist/spec/integration/webauthn-io.d.ts +32 -0
  12. package/dist/spec/integration/webauthn-io.js +100 -0
  13. package/dist/spec/libs/browser-injection.spec.d.ts +1 -0
  14. package/dist/spec/libs/browser-injection.spec.js +15 -0
  15. package/dist/spec/libs/cose-key.spec.d.ts +1 -0
  16. package/dist/spec/libs/cose-key.spec.js +47 -0
  17. package/dist/spec/repository/credential-repository.spec.d.ts +1 -0
  18. package/dist/spec/repository/credential-repository.spec.js +96 -0
  19. package/dist/spec/webauthn/cose-key.spec.d.ts +1 -0
  20. package/dist/spec/webauthn/cose-key.spec.js +74 -0
  21. package/dist/spec/webauthn/webauthn-emulator.spec.d.ts +1 -0
  22. package/dist/spec/webauthn/webauthn-emulator.spec.js +222 -0
  23. package/dist/spec/webauthn/webauthn-model-json.spec.d.ts +1 -0
  24. package/dist/spec/webauthn/webauthn-model-json.spec.js +103 -0
  25. package/dist/spec/webauthn/webauthn-model.spec.d.ts +1 -0
  26. package/dist/spec/webauthn/webauthn-model.spec.js +97 -0
  27. package/dist/spec/webauthn/webauthn-test-server.d.ts +15 -0
  28. package/dist/spec/webauthn/webauthn-test-server.js +87 -0
  29. package/dist/src/authenticator/authenticator-emulator.d.ts +68 -0
  30. package/dist/src/authenticator/authenticator-emulator.js +304 -0
  31. package/dist/src/authenticator/ctap-model.d.ts +107 -0
  32. package/dist/src/authenticator/ctap-model.js +202 -0
  33. package/dist/src/index.d.ts +16 -0
  34. package/dist/src/index.js +34 -0
  35. package/dist/src/libs/encode-utils.d.ts +15 -0
  36. package/dist/src/libs/encode-utils.js +92 -0
  37. package/dist/src/repository/credentials-file-repository.d.ts +8 -0
  38. package/dist/src/repository/credentials-file-repository.js +62 -0
  39. package/dist/src/repository/credentials-memory-repository.d.ts +7 -0
  40. package/dist/src/repository/credentials-memory-repository.js +20 -0
  41. package/dist/src/repository/credentials-repository.d.ts +39 -0
  42. package/dist/src/repository/credentials-repository.js +46 -0
  43. package/dist/src/test-utils/browser-injection.d.ts +3 -0
  44. package/dist/src/test-utils/browser-injection.js +52 -0
  45. package/dist/src/webauthn/cose-key.d.ts +16 -0
  46. package/dist/src/webauthn/cose-key.js +151 -0
  47. package/dist/src/webauthn/webauthn-emulator.d.ts +30 -0
  48. package/dist/src/webauthn/webauthn-emulator.js +142 -0
  49. package/dist/src/webauthn/webauthn-model-json.d.ts +101 -0
  50. package/dist/src/webauthn/webauthn-model-json.js +187 -0
  51. package/dist/src/webauthn/webauthn-model.d.ts +68 -0
  52. package/dist/src/webauthn/webauthn-model.js +186 -0
  53. package/package.json +37 -0
@@ -0,0 +1,186 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.RpId = void 0;
7
+ exports.packAttestationObject = packAttestationObject;
8
+ exports.packAuthenticatorData = packAuthenticatorData;
9
+ exports.unpackAttestationObject = unpackAttestationObject;
10
+ exports.unpackAuthenticatorData = unpackAuthenticatorData;
11
+ exports.toPublickeyCredentialSourceJSON = toPublickeyCredentialSourceJSON;
12
+ exports.parsePublicKeyCredentialSourceFromJSON = parsePublicKeyCredentialSourceFromJSON;
13
+ exports.toFido2CreateOptions = toFido2CreateOptions;
14
+ exports.toFido2RequestOptions = toFido2RequestOptions;
15
+ const node_crypto_1 = require("node:crypto");
16
+ const tldts_1 = require("tldts");
17
+ const encode_utils_1 = __importDefault(require("../libs/encode-utils"));
18
+ const cose_key_1 = require("./cose-key");
19
+ class RpId {
20
+ value;
21
+ constructor(value) {
22
+ this.value = value;
23
+ }
24
+ get hash() {
25
+ return new Uint8Array((0, node_crypto_1.createHash)("sha256").update(this.value).digest());
26
+ }
27
+ /** @see https://www.w3.org/TR/webauthn-3/#sctn-validating-origin */
28
+ validate(origin) {
29
+ const parsedOrigin = (0, tldts_1.parse)(origin);
30
+ const parsedRpId = (0, tldts_1.parse)(this.value);
31
+ return Boolean(parsedOrigin.domain &&
32
+ parsedOrigin.subdomain !== null &&
33
+ parsedRpId.subdomain !== null &&
34
+ parsedOrigin.domain === parsedRpId.domain &&
35
+ parsedOrigin.subdomain.endsWith(parsedRpId.subdomain));
36
+ }
37
+ }
38
+ exports.RpId = RpId;
39
+ function packAttestationObject(attestationObject) {
40
+ return encode_utils_1.default.encodeCbor({
41
+ fmt: attestationObject.fmt,
42
+ attStmt: attestationObject.attStmt,
43
+ authData: packAuthenticatorData(attestationObject.authData),
44
+ });
45
+ }
46
+ function packAuthenticatorData(authData) {
47
+ const ret = [];
48
+ const cred = authData.attestedCredentialData;
49
+ ret.push(...authData.rpIdHash);
50
+ ret.push(packAuthenticatorDataFlags({ ...authData.flags, attestedCredentialData: Boolean(cred) }));
51
+ ret.push(...packSignCount(authData.signCount));
52
+ if (cred)
53
+ ret.push(...packAttestedCredentialData(cred));
54
+ return new Uint8Array(ret);
55
+ }
56
+ function packSignCount(signCount) {
57
+ const ret = new ArrayBuffer(4);
58
+ const view = new DataView(ret);
59
+ view.setUint32(0, signCount, false);
60
+ return new Uint8Array(ret);
61
+ }
62
+ function packAttestedCredentialData(attestedCredentialData) {
63
+ const ret = [];
64
+ const rawId = attestedCredentialData.credentialId;
65
+ const credentialIdLength = [rawId.length >> 8, rawId.length & 0xff];
66
+ ret.push(...attestedCredentialData.aaguid);
67
+ ret.push(...credentialIdLength);
68
+ ret.push(...rawId);
69
+ ret.push(...attestedCredentialData.credentialPublicKey.toBytes());
70
+ return new Uint8Array(ret);
71
+ }
72
+ function packAuthenticatorDataFlags(flags) {
73
+ return ((flags.userPresent ? 1 << 0 : 0) |
74
+ (flags.userVerified ? 1 << 2 : 0) |
75
+ (flags.backupEligibility ? 1 << 3 : 0) |
76
+ (flags.backupState ? 1 << 4 : 0) |
77
+ (flags.attestedCredentialData ? 1 << 6 : 0) |
78
+ (flags.extensionData ? 1 << 7 : 0));
79
+ }
80
+ function unpackAttestationObject(attestationObject) {
81
+ const { fmt, attStmt, authData } = encode_utils_1.default.decodeCbor(attestationObject);
82
+ return {
83
+ fmt,
84
+ attStmt,
85
+ authData: unpackAuthenticatorData(authData),
86
+ };
87
+ }
88
+ function unpackAuthenticatorData(authData) {
89
+ const rpIdHash = authData.slice(0, 32);
90
+ const flags = unpackAuthenticatorDataFlags(authData[32]);
91
+ const signCount = (authData[33] << 24) | (authData[34] << 16) | (authData[35] << 8) | authData[36];
92
+ const attestedCredentialData = flags.attestedCredentialData
93
+ ? unpackAttestedCredentialData(authData.slice(37))
94
+ : undefined;
95
+ return { rpIdHash, flags, signCount, attestedCredentialData };
96
+ }
97
+ function unpackAttestedCredentialData(data) {
98
+ const aaguid = data.slice(0, 16);
99
+ const credentialIdLength = (data[16] << 8) | data[17];
100
+ const credentialId = data.slice(18, 18 + credentialIdLength);
101
+ const publicKey = cose_key_1.CoseKey.fromBytes(data.slice(18 + credentialIdLength));
102
+ return { aaguid, credentialId, credentialPublicKey: publicKey };
103
+ }
104
+ function unpackAuthenticatorDataFlags(flags) {
105
+ return {
106
+ userPresent: Boolean(flags & (1 << 0)),
107
+ userVerified: Boolean(flags & (1 << 2)),
108
+ backupEligibility: Boolean(flags & (1 << 3)),
109
+ backupState: Boolean(flags & (1 << 4)),
110
+ attestedCredentialData: Boolean(flags & (1 << 6)),
111
+ extensionData: Boolean(flags & (1 << 7)),
112
+ };
113
+ }
114
+ function toPublickeyCredentialSourceJSON(credentialSource) {
115
+ return {
116
+ type: "public-key",
117
+ id: encode_utils_1.default.encodeBase64Url(credentialSource.id),
118
+ privateKey: encode_utils_1.default.encodeBase64Url(credentialSource.privateKey),
119
+ rpId: credentialSource.rpId.value,
120
+ userHandle: credentialSource.userHandle ? encode_utils_1.default.encodeBase64Url(credentialSource.userHandle) : undefined,
121
+ };
122
+ }
123
+ function parsePublicKeyCredentialSourceFromJSON(json) {
124
+ return {
125
+ type: "public-key",
126
+ id: encode_utils_1.default.decodeBase64Url(json.id),
127
+ privateKey: encode_utils_1.default.decodeBase64Url(json.privateKey),
128
+ rpId: new RpId(json.rpId),
129
+ userHandle: json.userHandle ? encode_utils_1.default.decodeBase64Url(json.userHandle) : undefined,
130
+ };
131
+ }
132
+ /** @see https://www.w3.org/TR/webauthn-3/#CreateCred-async-loop */
133
+ function toFido2CreateOptions(criteria) {
134
+ let requireResidentKey = false;
135
+ let userVerification = false;
136
+ if (criteria) {
137
+ if (criteria.residentKey) {
138
+ if (criteria.residentKey === "required") {
139
+ requireResidentKey = true;
140
+ }
141
+ else if (criteria.residentKey === "preferred") {
142
+ requireResidentKey = true;
143
+ }
144
+ else if (criteria.residentKey === "discouraged") {
145
+ requireResidentKey = false;
146
+ }
147
+ }
148
+ else {
149
+ requireResidentKey = !!criteria.requireResidentKey;
150
+ }
151
+ if (criteria.userVerification === "required") {
152
+ userVerification = true;
153
+ }
154
+ else if (criteria.userVerification === "preferred") {
155
+ userVerification = true;
156
+ }
157
+ else if (criteria.userVerification === "discouraged") {
158
+ userVerification = false;
159
+ }
160
+ }
161
+ return {
162
+ rk: requireResidentKey,
163
+ uv: userVerification,
164
+ up: true,
165
+ };
166
+ }
167
+ /** @see https://www.w3.org/TR/webauthn-3/#sctn-issuing-cred-request-to-authenticator */
168
+ function toFido2RequestOptions(require) {
169
+ let userVerification;
170
+ if (require === "required") {
171
+ userVerification = true;
172
+ }
173
+ else if (require === "preferred") {
174
+ userVerification = true;
175
+ }
176
+ else if (require === "discouraged") {
177
+ userVerification = false;
178
+ }
179
+ else {
180
+ userVerification = false;
181
+ }
182
+ return {
183
+ uv: userVerification,
184
+ up: true,
185
+ };
186
+ }
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "nid-webauthn-emulator",
3
+ "version": "0.1.3",
4
+ "keywords": [
5
+ "test",
6
+ "e2e",
7
+ "passkeys"
8
+ ],
9
+ "repository": "https://github.com/Nikkei/nid-webauthn-emulator.git",
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "author": "Nikkei Inc.",
14
+ "license": "MIT",
15
+ "main": "dist/src/index.js",
16
+ "types": "dist/src/index.d.ts",
17
+ "devDependencies": {
18
+ "@biomejs/biome": "^1.8.3",
19
+ "@jest/globals": "^29.7.0",
20
+ "@simplewebauthn/server": "^10.0.1",
21
+ "@tsconfig/recommended": "^1.0.7",
22
+ "@types/node": "^22.4.1",
23
+ "jest": "^29.7.0",
24
+ "ts-jest": "^29.2.4",
25
+ "ts-node": "^10.9.2",
26
+ "typescript": "^5.5.4"
27
+ },
28
+ "dependencies": {
29
+ "cbor": "^9.0.2",
30
+ "tldts": "^6.1.40"
31
+ },
32
+ "scripts": {
33
+ "check": "biome check .",
34
+ "test": "jest",
35
+ "build": "tsc"
36
+ }
37
+ }