-
Notifications
You must be signed in to change notification settings - Fork 0
/
fido-conformance.ts
333 lines (284 loc) · 9.53 KB
/
fido-conformance.ts
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* eslint-disable @typescript-eslint/no-var-requires */
import fs from 'fs';
import express from 'express';
import fetch from 'node-fetch';
import base64url from 'base64url';
import {
generateRegistrationOptions,
verifyRegistrationResponse,
generateAuthenticationOptions,
verifyAuthenticationResponse,
MetadataService,
MetadataStatement,
SettingsService,
} from '@simplewebauthn/server';
import { isoUint8Array } from '@simplewebauthn/server/helpers';
import {
RegistrationResponseJSON,
AuthenticationResponseJSON,
} from '@simplewebauthn/typescript-types';
import { rpID, expectedOrigin } from './index';
import { LoggedInUser } from './example-server';
interface LoggedInFIDOUser extends LoggedInUser {
currentAuthenticationUserVerification?: UserVerificationRequirement;
}
/**
* Create paths specifically for testing with the FIDO Conformance Tools
*/
export const fidoConformanceRouter = express.Router();
export const fidoRouteSuffix = '/fido';
const rpName = 'FIDO Conformance Test';
/**
* Load JSON metadata statements provided by the Conformance Tools
*
* FIDO2 > TESTS CONFIGURATION > DOWNLOAD SERVER METADATA (button)
*/
const statements: MetadataStatement[] = [];
try {
// Update this to whatever folder you extracted the statements to
const conformanceMetadataPath = './fido-conformance-mds';
const conformanceMetadataFilenames = fs.readdirSync(conformanceMetadataPath);
for (const statementPath of conformanceMetadataFilenames) {
if (statementPath.endsWith('.json')) {
const contents = fs.readFileSync(`${conformanceMetadataPath}/${statementPath}`, 'utf-8');
statements.push(JSON.parse(contents));
}
}
} catch (err) {
// pass
}
/**
* Initialize MetadataService with Conformance Testing-specific statements.
*/
fetch('https://mds3.certinfra.fidoalliance.org/getEndpoints', {
method: 'POST',
body: JSON.stringify({ endpoint: `${expectedOrigin}${fidoRouteSuffix}` }),
headers: { 'Content-Type': 'application/json' },
})
.then(resp => resp.json())
.then(json => {
const mdsServers: string[] = json.result;
return MetadataService.initialize({
statements,
mdsServers,
verificationMode: 'strict',
});
})
.catch(console.error)
.finally(() => {
console.log('🔐 FIDO Conformance routes ready');
});
const inMemoryUserDeviceDB: { [username: string]: LoggedInFIDOUser } = {
// [username]: string: {
// id: loggedInUserId,
// username: '[email protected]',
// devices: [
// /**
// * {
// * credentialID: string,
// * publicKey: string,
// * counter: number,
// * }
// */
// ],
// currentChallenge: undefined,
// currentAuthenticationUserVerification: undefined,
// },
};
// A cheap way of remembering who's "logged in" between the request for options and the response
let loggedInUsername: string | undefined = undefined;
const supportedAlgorithmIDs = [-7, -8, -35, -36, -37, -38, -39, -257, -258, -259, -65535];
/**
* [FIDO2] Server Tests > MakeCredential Request
*/
fidoConformanceRouter.post('/attestation/options', (req, res) => {
const { body } = req;
const { username, displayName, authenticatorSelection, attestation, extensions } = body;
loggedInUsername = username;
let user = inMemoryUserDeviceDB[username];
if (!user) {
const newUser = {
id: username,
username,
devices: [],
};
inMemoryUserDeviceDB[username] = newUser;
user = newUser;
}
const { devices } = user;
const opts = generateRegistrationOptions({
rpName,
rpID,
userID: username,
userName: username,
userDisplayName: displayName,
attestationType: attestation,
authenticatorSelection,
extensions,
excludeCredentials: devices.map(dev => ({
id: dev.credentialID,
type: 'public-key',
transports: ['usb', 'ble', 'nfc', 'internal'],
})),
supportedAlgorithmIDs,
});
user.currentChallenge = opts.challenge;
return res.send({
...opts,
status: 'ok',
errorMessage: '',
});
});
/**
* [FIDO2] Server Tests > MakeCredential Response
*/
fidoConformanceRouter.post('/attestation/result', async (req, res) => {
const body: RegistrationResponseJSON = req.body;
const user = inMemoryUserDeviceDB[`${loggedInUsername}`];
const expectedChallenge = user.currentChallenge;
let verification;
try {
verification = await verifyRegistrationResponse({
response: body,
expectedChallenge: `${expectedChallenge}`,
expectedOrigin,
supportedAlgorithmIDs,
requireUserVerification: false,
});
} catch (error) {
const _error: Error = error as Error;
console.error(`RP - registration: ${_error.message}`);
return res.status(400).send({ errorMessage: _error.message });
}
const { verified, registrationInfo } = verification;
if (verified && registrationInfo) {
const { credentialPublicKey, credentialID, counter } = registrationInfo;
const existingDevice = user.devices.find(device => device.credentialID === credentialID);
if (!existingDevice) {
/**
* Add the returned device to the user's list of devices
*/
user.devices.push({
credentialPublicKey,
credentialID,
counter,
});
}
}
return res.send({
status: verified ? 'ok' : '',
errorMessage: '',
});
});
/**
* [FIDO2] Server Tests > GetAuthentication Request
*/
fidoConformanceRouter.post('/assertion/options', (req, res) => {
const { body } = req;
const { username, userVerification, extensions } = body;
loggedInUsername = username;
const user = inMemoryUserDeviceDB[username];
const { devices } = user;
const opts = generateAuthenticationOptions({
extensions,
userVerification,
allowCredentials: devices.map(dev => ({
id: dev.credentialID,
type: 'public-key',
transports: ['usb', 'ble', 'nfc', 'internal'],
})),
});
user.currentChallenge = opts.challenge;
user.currentAuthenticationUserVerification = userVerification;
return res.send({
...opts,
status: 'ok',
errorMessage: '',
});
});
fidoConformanceRouter.post('/assertion/result', async (req, res) => {
const body: AuthenticationResponseJSON = req.body;
const { id } = body;
const user = inMemoryUserDeviceDB[`${loggedInUsername}`];
// Pull up values specified when generation authentication options
const expectedChallenge = user.currentChallenge;
const userVerification = user.currentAuthenticationUserVerification;
if (!id) {
const msg = `Invalid id: ${id}`;
console.error(`RP - authentication: ${msg}`);
return res.status(400).send({ errorMessage: msg });
}
const credIDBuffer = base64url.toBuffer(id);
const existingDevice = user.devices.find(device => isoUint8Array.areEqual(device.credentialID, credIDBuffer));
if (!existingDevice) {
const msg = `Could not find device matching ${id}`;
console.error(`RP - authentication: ${msg}`);
return res.status(400).send({ errorMessage: msg });
}
let verification;
try {
verification = await verifyAuthenticationResponse({
response: body,
expectedChallenge: `${expectedChallenge}`,
expectedOrigin,
expectedRPID: rpID,
authenticator: existingDevice,
advancedFIDOConfig: { userVerification },
requireUserVerification: false,
});
} catch (error) {
const _error = error as Error;
console.error(`RP - authentication: ${_error.message}`);
return res.status(400).send({ errorMessage: _error.message });
}
const { verified, authenticationInfo } = verification;
if (verified) {
existingDevice.counter = authenticationInfo.newCounter;
}
return res.send({
status: verified ? 'ok' : '',
errorMessage: '',
});
});
/**
* A catch-all for future test routes we might need to support but haven't yet defined (helps with
* discovering which routes, what methods, and what data need to be defined)
*/
fidoConformanceRouter.all('*', (req, res, next) => {
console.log(req.url);
console.log(req.method);
console.log(req.body);
next();
});
/**
* MDS3ROOT
*
* Downloaded from https://mds3.certinfra.fidoalliance.org/
*
* Valid until 2045-01-31 @ 00:00 PST
*
* SHA256 Fingerprint
* 66:D9:77:0B:57:71:10:9B:8D:83:55:7B:A2:7D:58:9B:56:BD:B3:BF:DB:DE:A2:D2:42:C4:CA:0D:57:70:A4:7C
*/
export const MDS3ROOT = `-----BEGIN CERTIFICATE-----
MIICaDCCAe6gAwIBAgIPBCqih0DiJLW7+UHXx/o1MAoGCCqGSM49BAMDMGcxCzAJ
BgNVBAYTAlVTMRYwFAYDVQQKDA1GSURPIEFsbGlhbmNlMScwJQYDVQQLDB5GQUtF
IE1ldGFkYXRhIDMgQkxPQiBST09UIEZBS0UxFzAVBgNVBAMMDkZBS0UgUm9vdCBG
QUtFMB4XDTE3MDIwMTAwMDAwMFoXDTQ1MDEzMTIzNTk1OVowZzELMAkGA1UEBhMC
VVMxFjAUBgNVBAoMDUZJRE8gQWxsaWFuY2UxJzAlBgNVBAsMHkZBS0UgTWV0YWRh
dGEgMyBCTE9CIFJPT1QgRkFLRTEXMBUGA1UEAwwORkFLRSBSb290IEZBS0UwdjAQ
BgcqhkjOPQIBBgUrgQQAIgNiAASKYiz3YltC6+lmxhPKwA1WFZlIqnX8yL5RybSL
TKFAPEQeTD9O6mOz+tg8wcSdnVxHzwnXiQKJwhrav70rKc2ierQi/4QUrdsPes8T
EirZOkCVJurpDFbXZOgs++pa4XmjYDBeMAsGA1UdDwQEAwIBBjAPBgNVHRMBAf8E
BTADAQH/MB0GA1UdDgQWBBQGcfeCs0Y8D+lh6U5B2xSrR74eHTAfBgNVHSMEGDAW
gBQGcfeCs0Y8D+lh6U5B2xSrR74eHTAKBggqhkjOPQQDAwNoADBlAjEA/xFsgri0
xubSa3y3v5ormpPqCwfqn9s0MLBAtzCIgxQ/zkzPKctkiwoPtDzI51KnAjAmeMyg
X2S5Ht8+e+EQnezLJBJXtnkRWY+Zt491wgt/AwSs5PHHMv5QgjELOuMxQBc=
-----END CERTIFICATE-----
`;
// Set above root cert for use by MetadataService
SettingsService.setRootCertificates({ identifier: 'mds', certificates: [MDS3ROOT] });
// Reset preset root certificates
SettingsService.setRootCertificates({ identifier: 'apple', certificates: [] });
SettingsService.setRootCertificates({ identifier: 'android-key', certificates: [] });
SettingsService.setRootCertificates({ identifier: 'android-safetynet', certificates: [] });