-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
passkeys-utils.js
150 lines (124 loc) · 5.53 KB
/
passkeys-utils.js
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
'use strict';
const MINIMUM_TIMEOUT = 15000;
const DEFAULT_TIMEOUT = 30000;
const DISCOURAGED_TIMEOUT = 120000;
// From ArrayBuffer to URL encoded base64 string
const arrayBufferToBase64 = function(buf) {
const str = [ ...new Uint8Array(buf) ].map(c => String.fromCharCode(c)).join('');
return window.btoa(str).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '');
};
const checkErrors = function(pkOptions, sameOriginWithAncestors) {
if (!pkOptions) {
throw new Error('No publicKey configuration options were provided');
}
if (pkOptions.signal && pkOptions.signal.aborted) {
throw new DOMException('Abort signalled', DOMException.AbortError);
}
if (!sameOriginWithAncestors) {
throw new DOMException('Cross-origin register or authentication is not allowed.', DOMException.NotAllowedError);
}
if (pkOptions.challenge.length < 16) {
throw new TypeError('challenge is shorter than required minimum length.');
}
};
const getTimeout = function(userVerification, timeout) {
if (!timeout || Number(timeout) === 0 || isNaN(Number(timeout))) {
return userVerification === 'discouraged' ? DISCOURAGED_TIMEOUT : DEFAULT_TIMEOUT;
}
// Note: A suggested reasonable range for the timeout member of options is 15 seconds to 120 seconds.
if (Number(timeout) < MINIMUM_TIMEOUT || Number(timeout) > DISCOURAGED_TIMEOUT) {
return DEFAULT_TIMEOUT;
}
return Number(timeout);
};
const kpxcPasskeysUtils = {};
// Sends response from KeePassXC back to the injected script
kpxcPasskeysUtils.sendPasskeysResponse = function(publicKey, errorCode, errorMessage) {
const response = errorCode
? { errorCode: errorCode, errorMessage: errorMessage, fallback: kpxc.settings.passkeysFallback }
: { publicKey: publicKey, fallback: kpxc.settings.passkeysFallback };
const details = isFirefox() ? cloneInto(response, document.defaultView) : response;
document.dispatchEvent(new CustomEvent('kpxc-passkeys-response', { detail: details }));
};
// Create a new object with base64 strings for KeePassXC
kpxcPasskeysUtils.buildCredentialCreationOptions = function(pkOptions, sameOriginWithAncestors) {
try {
checkErrors(pkOptions, sameOriginWithAncestors);
const publicKey = {};
publicKey.attestation = pkOptions?.attestation;
publicKey.authenticatorSelection = pkOptions?.authenticatorSelection;
publicKey.challenge = arrayBufferToBase64(pkOptions.challenge);
publicKey.extensions = pkOptions?.extensions;
// Make sure integers are used for "alg". Set to reserved if not found.
// https://www.iana.org/assignments/cose/cose.xhtml#algorithms
publicKey.pubKeyCredParams = [];
if (pkOptions.pubKeyCredParams) {
for (const credParam of pkOptions.pubKeyCredParams) {
publicKey.pubKeyCredParams.push({
type: credParam?.type,
alg: credParam.alg ? Number(credParam.alg) : 0
});
}
}
publicKey.rp = pkOptions?.rp;
publicKey.timeout = getTimeout(publicKey?.authenticatorSelection?.userVerification, pkOptions?.timeout);
publicKey.excludeCredentials = [];
if (pkOptions.excludeCredentials && pkOptions.excludeCredentials.length > 0) {
for (const cred of pkOptions.excludeCredentials) {
publicKey.excludeCredentials.push({
id: arrayBufferToBase64(cred.id),
transports: cred.transports,
type: cred.type
});
}
}
publicKey.user = {};
publicKey.user.displayName = pkOptions.user.displayName;
publicKey.user.id = arrayBufferToBase64(pkOptions.user.id);
publicKey.user.name = pkOptions.user.name;
// TODO: Disable after fixed in KeePassXC side
if (!publicKey.rp.id) {
publicKey.rp.id = window.location.hostname;
}
return publicKey;
} catch (e) {
console.log(e);
}
};
// Create a new object with base64 strings for KeePassXC
kpxcPasskeysUtils.buildCredentialRequestOptions = function(pkOptions, sameOriginWithAncestors) {
try {
checkErrors(pkOptions, sameOriginWithAncestors);
const publicKey = {};
publicKey.challenge = arrayBufferToBase64(pkOptions.challenge);
publicKey.enterpriseAttestationPossible = false;
publicKey.extensions = pkOptions?.extensions;
publicKey.rpId = pkOptions?.rpId;
publicKey.timeout = getTimeout(publicKey?.userVerification, pkOptions?.timeout);
publicKey.userVerification = pkOptions?.userVerification;
publicKey.allowCredentials = [];
if (pkOptions.allowCredentials && pkOptions.allowCredentials.length > 0) {
for (const cred of pkOptions.allowCredentials) {
const transports = [];
if (cred.transports) {
for (const tp of cred.transports) {
transports.push(tp);
}
}
const arr = {
id: arrayBufferToBase64(cred.id),
transports: [ ...transports, 'internal' ],
type: cred.type
};
publicKey.allowCredentials.push(arr);
}
}
// TODO: Disable after fixed in KeePassXC side
if (!publicKey.rpId) {
publicKey.rpId = window.location.hostname;
}
return publicKey;
} catch (e) {
console.log(e);
}
};