forked from MasterKale/webauthn-polyfills
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
229 lines (208 loc) · 7.43 KB
/
index.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
// @deno-types="npm:@types/ua-parser-js@^0.7.39"
import { UAParser } from 'ua-parser-js';
import type {
PublicKeyCredentialCreationOptionsJSON,
PublicKeyCredentialRequestOptionsJSON,
} from '@simplewebauthn/types';
import { Base64URL } from './base64url.ts';
/**
* Make sure at least PublicKeyCredential is defined before trying to polyfill anything on it
*/
if (globalThis.PublicKeyCredential) {
const uap = new UAParser();
const browser = uap.getBrowser();
if (!browser?.major || !browser?.name) {
throw new Error('Browser major version not found.');
}
const browserName = browser.name;
const browserVer = parseInt(browser.major);
const engine = uap.getEngine();
if (!engine?.version || !engine?.name) {
throw new Error('Engine version not found.');
}
const engineName = engine.name;
const engineVer = parseInt(engine.version.replace(/^([0-9]+)\.*$/, '$1'));
const isWebkit = engineName?.indexOf('WebKit') > -1;
/**
* Polyfill `PublicKeyCredential.parseCreationOptionsFromJSON`
*
* See https://w3c.github.io/webauthn/#sctn-parseCreationOptionsFromJSON
*/
// @ts-ignore: We're polyfilling this, so ignore whether TS knows about this or not
if (!PublicKeyCredential.parseCreationOptionsFromJSON) {
Object.defineProperty(PublicKeyCredential, 'parseCreationOptionsFromJSON', {
value: (options: PublicKeyCredentialCreationOptionsJSON) => {
const user = {
...options.user,
id: Base64URL.decode(options.user.id),
};
const challenge = Base64URL.decode(options.challenge);
const excludeCredentials = options.excludeCredentials?.map((cred) => {
return {
...cred,
id: Base64URL.decode(cred.id),
transports: cred.transports as AuthenticatorTransport[] | undefined,
};
}) ?? [];
const toReturn: PublicKeyCredentialCreationOptions = {
...options,
user,
challenge,
excludeCredentials,
};
return toReturn;
},
});
}
/**
* Polyfill `PublicKeyCredential.parseRequestOptionsFromJSON`
*
* See https://w3c.github.io/webauthn/#sctn-parseRequestOptionsFromJSON
*/
// @ts-ignore: We're polyfilling this, so ignore whether TS knows about this or not
if (!PublicKeyCredential.parseRequestOptionsFromJSON) {
Object.defineProperty(PublicKeyCredential, 'parseRequestOptionsFromJSON', {
value: (options: PublicKeyCredentialRequestOptionsJSON) => {
const challenge = Base64URL.decode(options.challenge);
const allowCredentials = options.allowCredentials?.map((cred) => {
return {
...cred,
id: Base64URL.decode(cred.id),
transports: cred.transports as AuthenticatorTransport[] | undefined,
};
}) ?? [];
const toReturn: PublicKeyCredentialRequestOptions = {
...options,
allowCredentials,
challenge,
};
return toReturn;
},
});
}
/**
* Polyfill `PublicKeyCredential.prototype.toJSON`
*
* See https://w3c.github.io/webauthn/#dom-publickeycredential-tojson
*/
// @ts-ignore: We're polyfilling this, so ignore whether TS knows about this or not
if (!PublicKeyCredential.prototype.toJSON) {
Object.defineProperty(PublicKeyCredential.prototype, 'toJSON', {
value: function () {
try {
const id = this.id;
const rawId = Base64URL.encode(this.rawId);
const authenticatorAttachment = this.authenticatorAttachment;
const clientExtensionResults = {};
const type = this.type;
// This is authentication.
if (this.response.signature) {
return {
id,
rawId,
response: {
authenticatorData: Base64URL.encode(
this.response.authenticatorData,
),
clientDataJSON: Base64URL.encode(
this.response.clientDataJSON,
),
signature: Base64URL.encode(this.response.signature),
userHandle: Base64URL.encode(this.response.userHandle),
},
authenticatorAttachment,
clientExtensionResults,
type,
};
} else {
return {
id,
rawId,
response: {
clientDataJSON: Base64URL.encode(
this.response.clientDataJSON,
),
attestationObject: Base64URL.encode(
this.response.attestationObject,
),
transports: this.response?.getTransports() || [],
},
authenticatorAttachment,
clientExtensionResults,
type,
};
}
} catch (error) {
console.error(error);
throw error;
}
},
});
}
/**
* Polyfill `PublicKeyCredential.getClientCapabilities`
*
* See https://w3c.github.io/webauthn/#sctn-getClientCapabilities
*/
if (
// @ts-ignore: We're polyfilling this, so ignore whether TS knows about this or not
!PublicKeyCredential.getClientCapabilities ||
// If this is Safari 17.4+, there's a spec glitch.
(isWebkit && browserVer >= 17.4)
) {
Object.defineProperty(PublicKeyCredential, 'getClientCapabilities', {
value: async () => {
let conditionalCreate = false;
let conditionalGet = false;
let userVerifyingPlatformAuthenticator = false;
let relatedOrigins = false;
let signalAllAcceptedCredentials = false;
let signalCurrentUserDetails = false;
let signalUnknownCredential = false;
if (
PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable &&
PublicKeyCredential.isConditionalMediationAvailable
) {
// Are UVPAA and conditional UI available on this browser?
const results = await Promise.all([
PublicKeyCredential
.isUserVerifyingPlatformAuthenticatorAvailable(),
PublicKeyCredential.isConditionalMediationAvailable(),
]);
userVerifyingPlatformAuthenticator = results[0];
conditionalGet = results[1];
}
// @ts-ignore: It's okay if this doesn't exist
if (typeof PublicKeyCredential.signalAllAcceptedCredentials === 'function') {
signalAllAcceptedCredentials = true;
}
// @ts-ignore: It's okay if this doesn't exist
if (typeof PublicKeyCredential.signalCurrentUserDetails === 'function') {
signalCurrentUserDetails = true;
}
// @ts-ignore: It's okay if this doesn't exist
if (typeof PublicKeyCredential.signalUknownCredential === 'function') {
signalUnknownCredential = true;
}
// `conditionalCreate` is `true` on Safari 15+
if (browserName === 'Safari' && browserVer >= 18) {
conditionalCreate = true;
}
// `relatedOrigins` is `true` on Chromium 128+ or Safari 18+
if ((engineName === 'Blink' && engineVer >= 128) ||
(isWebkit && browserVer >= 18)) {
relatedOrigins = true;
}
return {
conditionalCreate,
conditionalGet,
relatedOrigins,
signalAllAcceptedCredentials,
signalCurrentUserDetails,
signalUnknownCredential,
userVerifyingPlatformAuthenticator,
};
},
});
}
}