Repository navigation
Expand file tree
/
Copy pathtest_passkeys.js
More file actions
151 lines (144 loc) · 5.26 KB
/
Copy pathtest_passkeys.js
File metadata and controls
151 lines (144 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const assert = require("node:assert/strict");
const { createPasskeyService, PasskeyServiceError } = require("./server/core/passkeyService");
async function run() {
const challenges = new Map();
const credentials = new Map();
let counterUpdates = 0;
let registrationOptions;
let authenticationOptions;
const passkeyStore = {
async getUserHandle(userId) {
return `stable-handle-${userId}`;
},
async create(credential) {
credentials.set(credential.id, credential);
},
async getByCredentialId(id) {
return credentials.get(id) || null;
},
async listByUserId() {
return [];
},
async updateCounter(id, expectedCounter, newCounter) {
const credential = credentials.get(id);
if (!credential || credential.counter !== expectedCounter) return false;
credential.counter = newCounter;
counterUpdates++;
return true;
},
async deleteByCredentialId() {
return false;
}
};
const passkeyChallengeStore = {
async put(challenge, record) {
challenges.set(challenge, record);
},
async consume(challenge, purpose) {
const record = challenges.get(challenge);
if (!record || record.purpose !== purpose || record.expiresAt <= Date.now()) return null;
challenges.delete(challenge);
return record;
}
};
const webAuthn = {
isoBase64URL: {
toBuffer: (value) => new TextEncoder().encode(value)
},
async generateRegistrationOptions(options) {
registrationOptions = options;
return { challenge: "registration-challenge", ...options };
},
async verifyRegistrationResponse({ expectedChallenge }) {
const valid = await expectedChallenge("registration-challenge");
return valid ? {
verified: true,
registrationInfo: {
credential: {
id: "test-credential",
publicKey: new Uint8Array([1, 2, 3]),
counter: 4,
transports: ["internal"]
},
credentialDeviceType: "singleDevice",
credentialBackedUp: false
}
} : { verified: false };
},
async generateAuthenticationOptions(options) {
authenticationOptions = options;
return { challenge: "authentication-challenge", ...options };
},
async verifyAuthenticationResponse({ expectedChallenge }) {
const valid = await expectedChallenge("authentication-challenge");
return valid ? {
verified: true,
authenticationInfo: {
newCounter: 5,
credentialDeviceType: "singleDevice",
credentialBackedUp: false
}
} : { verified: false };
}
};
const configValues = {
passkeys: {
rpName: "DevAuthKit test",
rpID: "example.com",
origins: ["https://example.com"]
},
passkeyStore,
passkeyChallengeStore
};
const service = createPasskeyService({
config: { get: (key) => configValues[key] },
authService: {
async resolveUserPayload(email) {
return { id: "user-1", email };
}
},
loadWebAuthn: () => webAuthn
});
const user = { id: "user-1", email: "[email protected]", name: "Test User" };
const registration = await service.registrationOptions(user);
assert.equal(registration.challenge, "registration-challenge");
assert.equal(registrationOptions.authenticatorSelection.residentKey, "required");
assert.equal(registrationOptions.authenticatorSelection.userVerification, "required");
assert.deepEqual(
Array.from(registrationOptions.userID),
Array.from(new TextEncoder().encode("stable-handle-user-1"))
);
const registrationResult = await service.verifyRegistration(user, { id: "registration-response" });
assert.equal(registrationResult.verified, true);
assert.equal(credentials.get("test-credential").email, "[email protected]");
assert.equal(await service.verifyRegistration(user, { id: "registration-response" }).then(
() => false,
(error) => error instanceof PasskeyServiceError && error.status === 400
), true);
const loginOptions = await service.authenticationOptions("[email protected]");
assert.equal(loginOptions.challenge, "authentication-challenge");
assert.equal(authenticationOptions.allowCredentials, undefined);
assert.equal((await passkeyChallengeStore.consume("authentication-challenge", "authentication")).email,
const loginOptionsAgain = await service.authenticationOptions();
assert.equal(loginOptionsAgain.challenge, "authentication-challenge");
const credential = credentials.get("test-credential");
credential.userHandle = "stable-handle-user-1";
credential.userId = "user-1";
const authenticatedUser = await service.verifyAuthentication({
id: "test-credential",
response: { userHandle: "stable-handle-user-1" }
});
assert.deepEqual(authenticatedUser, { id: "user-1", email: "[email protected]" });
assert.equal(counterUpdates, 1);
assert.equal(credential.counter, 5);
assert.equal(await service.verifyAuthentication({
id: "test-credential",
response: { userHandle: "stable-handle-user-1" }
}).then(
() => false,
(error) => error instanceof PasskeyServiceError && error.status === 401
), true);
console.log("Passkey service tests passed");
}
run().then(() => process.exit(0)).catch((err) => { console.error(err); process.exit(1); });