Skip to content

Commit 7603a36

Browse files
authored
Merge pull request #644 from MasterKale/fix/example-conformance
fix/example-conformance
2 parents 2338747 + 1c37694 commit 7603a36

File tree

1 file changed

+29
-43
lines changed

1 file changed

+29
-43
lines changed

example/fido-conformance.ts

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
verifyAuthenticationResponse,
1212
verifyRegistrationResponse,
1313
} from '@simplewebauthn/server';
14+
import { isoBase64URL } from '@simplewebauthn/server/helpers';
1415
import { AuthenticationResponseJSON, RegistrationResponseJSON } from '@simplewebauthn/types';
1516

1617
import { expectedOrigin, rpID } from './index';
@@ -77,23 +78,8 @@ fetch('https://mds3.fido.tools/getEndpoints', {
7778
console.log('🔐 FIDO Conformance routes ready');
7879
});
7980

80-
const inMemoryUserDeviceDB: { [username: string]: LoggedInFIDOUser } = {
81-
// [username]: string: {
82-
// id: loggedInUserId,
83-
// username: '[email protected]',
84-
// devices: [
85-
// /**
86-
// * {
87-
// * credentialID: string,
88-
// * publicKey: string,
89-
// * counter: number,
90-
// * }
91-
// */
92-
// ],
93-
// currentChallenge: undefined,
94-
// currentAuthenticationUserVerification: undefined,
95-
// },
96-
};
81+
const inMemoryUserDB: { [username: string]: LoggedInFIDOUser } = {};
82+
9783
// A cheap way of remembering who's "logged in" between the request for options and the response
9884
let loggedInUsername: string | undefined = undefined;
9985

@@ -126,31 +112,31 @@ fidoConformanceRouter.post('/attestation/options', async (req, res) => {
126112

127113
loggedInUsername = username;
128114

129-
let user = inMemoryUserDeviceDB[username];
115+
let user = inMemoryUserDB[username];
130116
if (!user) {
131117
const newUser = {
132118
id: username,
133119
username,
134-
devices: [],
120+
credentials: [],
135121
};
136122

137-
inMemoryUserDeviceDB[username] = newUser;
123+
inMemoryUserDB[username] = newUser;
138124
user = newUser;
139125
}
140126

141-
const { devices } = user;
127+
const { credentials } = user;
142128

143129
const opts = await generateRegistrationOptions({
144130
rpName,
145131
rpID,
146-
userID: username,
132+
userID: isoBase64URL.toBuffer(username),
147133
userName: username,
148134
userDisplayName: displayName,
149135
attestationType: attestation,
150136
authenticatorSelection,
151137
extensions,
152-
excludeCredentials: devices.map((dev) => ({
153-
id: dev.credentialID,
138+
excludeCredentials: credentials.map((cred) => ({
139+
id: cred.id,
154140
type: 'public-key',
155141
transports: ['usb', 'ble', 'nfc', 'internal'],
156142
})),
@@ -175,7 +161,7 @@ fidoConformanceRouter.post('/attestation/options', async (req, res) => {
175161
fidoConformanceRouter.post('/attestation/result', async (req, res) => {
176162
const body: RegistrationResponseJSON = req.body;
177163

178-
const user = inMemoryUserDeviceDB[`${loggedInUsername}`];
164+
const user = inMemoryUserDB[`${loggedInUsername}`];
179165

180166
const expectedChallenge = req.session.currentChallenge;
181167

@@ -197,18 +183,18 @@ fidoConformanceRouter.post('/attestation/result', async (req, res) => {
197183
const { verified, registrationInfo } = verification;
198184

199185
if (verified && registrationInfo) {
200-
const { credentialPublicKey, credentialID, counter } = registrationInfo;
186+
const { credential } = registrationInfo;
201187

202-
const existingDevice = user.devices.find((device) => device.credentialID === credentialID);
188+
const existingCredential = user.credentials.find((cred) => cred.id === credential.id);
203189

204-
if (!existingDevice) {
190+
if (!existingCredential) {
205191
/**
206-
* Add the returned device to the user's list of devices
192+
* Add the returned credential to the user's list of credentials
207193
*/
208-
user.devices.push({
209-
credentialPublicKey,
210-
credentialID,
211-
counter,
194+
user.credentials.push({
195+
id: credential.id,
196+
publicKey: credential.publicKey,
197+
counter: credential.counter,
212198
});
213199
}
214200
}
@@ -228,16 +214,16 @@ fidoConformanceRouter.post('/assertion/options', async (req, res) => {
228214

229215
loggedInUsername = username;
230216

231-
const user = inMemoryUserDeviceDB[username];
217+
const user = inMemoryUserDB[username];
232218

233-
const { devices } = user;
219+
const { credentials } = user;
234220

235221
const opts = await generateAuthenticationOptions({
236222
rpID,
237223
extensions,
238224
userVerification,
239-
allowCredentials: devices.map((dev) => ({
240-
id: dev.credentialID,
225+
allowCredentials: credentials.map((cred) => ({
226+
id: cred.id,
241227
type: 'public-key',
242228
transports: ['usb', 'ble', 'nfc', 'internal'],
243229
})),
@@ -257,7 +243,7 @@ fidoConformanceRouter.post('/assertion/result', async (req, res) => {
257243
const body: AuthenticationResponseJSON = req.body;
258244
const { id } = body;
259245

260-
const user = inMemoryUserDeviceDB[`${loggedInUsername}`];
246+
const user = inMemoryUserDB[`${loggedInUsername}`];
261247

262248
// Pull up values specified when generation authentication options
263249
const expectedChallenge = req.session.currentChallenge;
@@ -269,10 +255,10 @@ fidoConformanceRouter.post('/assertion/result', async (req, res) => {
269255
return res.status(400).send({ errorMessage: msg });
270256
}
271257

272-
const existingDevice = user.devices.find((device) => device.credentialID === id);
258+
const existingCredential = user.credentials.find((cred) => cred.id === id);
273259

274-
if (!existingDevice) {
275-
const msg = `Could not find device matching ${id}`;
260+
if (!existingCredential) {
261+
const msg = `Could not find credential matching ${id}`;
276262
console.error(`RP - authentication: ${msg}`);
277263
return res.status(400).send({ errorMessage: msg });
278264
}
@@ -284,7 +270,7 @@ fidoConformanceRouter.post('/assertion/result', async (req, res) => {
284270
expectedChallenge: `${expectedChallenge}`,
285271
expectedOrigin,
286272
expectedRPID: rpID,
287-
authenticator: existingDevice,
273+
credential: existingCredential,
288274
advancedFIDOConfig: { userVerification },
289275
requireUserVerification: false,
290276
});
@@ -297,7 +283,7 @@ fidoConformanceRouter.post('/assertion/result', async (req, res) => {
297283
const { verified, authenticationInfo } = verification;
298284

299285
if (verified) {
300-
existingDevice.counter = authenticationInfo.newCounter;
286+
existingCredential.counter = authenticationInfo.newCounter;
301287
}
302288

303289
return res.send({

0 commit comments

Comments
 (0)