@capgo/capacitor-passkey 8.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/CapgoCapacitorPasskey.podspec +17 -0
  2. package/LICENSE +373 -0
  3. package/Package.swift +28 -0
  4. package/README.md +692 -0
  5. package/android/build.gradle +61 -0
  6. package/android/src/main/AndroidManifest.xml +2 -0
  7. package/android/src/main/java/app/capgo/passkey/CapacitorPasskey.java +21 -0
  8. package/android/src/main/java/app/capgo/passkey/CapacitorPasskeyPlugin.java +299 -0
  9. package/android/src/main/res/.gitkeep +0 -0
  10. package/dist/docs.json +1034 -0
  11. package/dist/esm/definitions.d.ts +531 -0
  12. package/dist/esm/definitions.js +2 -0
  13. package/dist/esm/definitions.js.map +1 -0
  14. package/dist/esm/index.d.ts +4 -0
  15. package/dist/esm/index.js +87 -0
  16. package/dist/esm/index.js.map +1 -0
  17. package/dist/esm/internal.d.ts +14 -0
  18. package/dist/esm/internal.js +2 -0
  19. package/dist/esm/internal.js.map +1 -0
  20. package/dist/esm/shim.d.ts +8 -0
  21. package/dist/esm/shim.js +108 -0
  22. package/dist/esm/shim.js.map +1 -0
  23. package/dist/esm/web.d.ts +12 -0
  24. package/dist/esm/web.js +69 -0
  25. package/dist/esm/web.js.map +1 -0
  26. package/dist/esm/webauthn.d.ts +23 -0
  27. package/dist/esm/webauthn.js +440 -0
  28. package/dist/esm/webauthn.js.map +1 -0
  29. package/dist/plugin.cjs.js +707 -0
  30. package/dist/plugin.cjs.js.map +1 -0
  31. package/dist/plugin.js +710 -0
  32. package/dist/plugin.js.map +1 -0
  33. package/ios/Sources/CapacitorPasskeyPlugin/CapacitorPasskey.swift +39 -0
  34. package/ios/Sources/CapacitorPasskeyPlugin/CapacitorPasskeyPlugin.swift +388 -0
  35. package/ios/Tests/CapacitorPasskeyPluginTests/CapacitorPasskeyPluginTests.swift +17 -0
  36. package/package.json +105 -0
  37. package/scripts/configure-native.js +263 -0
package/dist/plugin.js ADDED
@@ -0,0 +1,710 @@
1
+ var capacitorPasskey = (function (exports, core) {
2
+ 'use strict';
3
+
4
+ const PASSKEY_TYPE = 'public-key';
5
+ function createNativeRequest(options) {
6
+ if ('mediation' in options) {
7
+ const getOptions = options;
8
+ return {
9
+ origin: resolveOrigin(getOptions.origin, getOptions.publicKey.rpId),
10
+ requestJson: JSON.stringify(normalizeRequestOptionsJSON(getOptions.publicKey, getOptions.origin)),
11
+ };
12
+ }
13
+ const createOptions = options;
14
+ return {
15
+ origin: resolveOrigin(createOptions.origin, createOptions.publicKey.rp.id),
16
+ requestJson: JSON.stringify(normalizeCreationOptionsJSON(createOptions.publicKey, createOptions.origin)),
17
+ };
18
+ }
19
+ function webAuthnCreationOptionsToJSON(options, origin) {
20
+ var _a;
21
+ return normalizeCreationOptionsJSON({
22
+ attestation: options.attestation,
23
+ authenticatorSelection: options.authenticatorSelection
24
+ ? {
25
+ authenticatorAttachment: options.authenticatorSelection.authenticatorAttachment,
26
+ requireResidentKey: options.authenticatorSelection.requireResidentKey,
27
+ residentKey: options.authenticatorSelection.residentKey,
28
+ userVerification: options.authenticatorSelection.userVerification,
29
+ }
30
+ : undefined,
31
+ challenge: encodeBase64Url(options.challenge),
32
+ excludeCredentials: (_a = options.excludeCredentials) === null || _a === void 0 ? void 0 : _a.map(descriptorToJSON),
33
+ extensions: cloneExtensions(options.extensions),
34
+ pubKeyCredParams: options.pubKeyCredParams.map((entry) => ({
35
+ alg: entry.alg,
36
+ type: entry.type,
37
+ })),
38
+ rp: {
39
+ id: options.rp.id,
40
+ name: options.rp.name,
41
+ },
42
+ timeout: options.timeout,
43
+ user: {
44
+ displayName: options.user.displayName,
45
+ id: encodeBase64Url(options.user.id),
46
+ name: options.user.name,
47
+ },
48
+ }, origin);
49
+ }
50
+ function webAuthnRequestOptionsToJSON(options, origin) {
51
+ var _a;
52
+ return normalizeRequestOptionsJSON({
53
+ allowCredentials: (_a = options.allowCredentials) === null || _a === void 0 ? void 0 : _a.map(descriptorToJSON),
54
+ challenge: encodeBase64Url(options.challenge),
55
+ extensions: cloneExtensions(options.extensions),
56
+ rpId: options.rpId,
57
+ timeout: options.timeout,
58
+ userVerification: options.userVerification,
59
+ }, origin);
60
+ }
61
+ function normalizeCreationOptionsJSON(options, origin) {
62
+ var _a, _b;
63
+ const rpId = (_a = options.rp.id) !== null && _a !== void 0 ? _a : deriveRpId(resolveOrigin(origin, options.rp.id));
64
+ if (!rpId) {
65
+ throw createPasskeyDomException('DataError', 'A relying party id or HTTPS origin is required.');
66
+ }
67
+ return Object.assign(Object.assign({}, options), { excludeCredentials: (_b = options.excludeCredentials) === null || _b === void 0 ? void 0 : _b.map(descriptorToJSON), pubKeyCredParams: options.pubKeyCredParams.map((entry) => {
68
+ var _a;
69
+ return ({
70
+ alg: entry.alg,
71
+ type: (_a = entry.type) !== null && _a !== void 0 ? _a : PASSKEY_TYPE,
72
+ });
73
+ }), rp: Object.assign(Object.assign({}, options.rp), { id: rpId }) });
74
+ }
75
+ function normalizeRequestOptionsJSON(options, origin) {
76
+ var _a, _b;
77
+ const rpId = (_a = options.rpId) !== null && _a !== void 0 ? _a : deriveRpId(resolveOrigin(origin, options.rpId));
78
+ return Object.assign(Object.assign({}, options), { allowCredentials: (_b = options.allowCredentials) === null || _b === void 0 ? void 0 : _b.map(descriptorToJSON), rpId });
79
+ }
80
+ function browserCreationOptionsFromJSON(options) {
81
+ var _a, _b;
82
+ return {
83
+ attestation: options.attestation,
84
+ authenticatorSelection: options.authenticatorSelection
85
+ ? {
86
+ authenticatorAttachment: options.authenticatorSelection.authenticatorAttachment,
87
+ requireResidentKey: options.authenticatorSelection.requireResidentKey,
88
+ residentKey: options.authenticatorSelection.residentKey,
89
+ userVerification: options.authenticatorSelection.userVerification,
90
+ }
91
+ : undefined,
92
+ challenge: decodeBase64Url(options.challenge),
93
+ excludeCredentials: (_a = options.excludeCredentials) === null || _a === void 0 ? void 0 : _a.map(jsonDescriptorToBrowser),
94
+ extensions: cloneExtensions(options.extensions),
95
+ pubKeyCredParams: options.pubKeyCredParams.map((entry) => {
96
+ var _a;
97
+ return ({
98
+ alg: entry.alg,
99
+ type: (_a = entry.type) !== null && _a !== void 0 ? _a : PASSKEY_TYPE,
100
+ });
101
+ }),
102
+ rp: {
103
+ id: options.rp.id,
104
+ name: options.rp.name,
105
+ },
106
+ timeout: options.timeout,
107
+ user: {
108
+ displayName: (_b = options.user.displayName) !== null && _b !== void 0 ? _b : options.user.name,
109
+ id: decodeBase64Url(options.user.id),
110
+ name: options.user.name,
111
+ },
112
+ };
113
+ }
114
+ function browserRequestOptionsFromJSON(options) {
115
+ var _a;
116
+ return {
117
+ allowCredentials: (_a = options.allowCredentials) === null || _a === void 0 ? void 0 : _a.map(jsonDescriptorToBrowser),
118
+ challenge: decodeBase64Url(options.challenge),
119
+ extensions: cloneExtensions(options.extensions),
120
+ rpId: options.rpId,
121
+ timeout: options.timeout,
122
+ userVerification: options.userVerification,
123
+ };
124
+ }
125
+ async function browserRegistrationToJSON(credential) {
126
+ var _a;
127
+ const publicKeyCredential = expectPublicKeyCredential(credential, 'registration');
128
+ const response = publicKeyCredential.response;
129
+ const nativeJson = credentialWithToJSON(publicKeyCredential);
130
+ if (nativeJson && 'attestationObject' in nativeJson.response) {
131
+ return normalizeRegistrationCredential(nativeJson);
132
+ }
133
+ return {
134
+ authenticatorAttachment: normalizeAttachment(publicKeyCredential.authenticatorAttachment),
135
+ clientExtensionResults: clientExtensionResultsToRecord((_a = publicKeyCredential.getClientExtensionResults) === null || _a === void 0 ? void 0 : _a.call(publicKeyCredential)),
136
+ id: publicKeyCredential.id,
137
+ rawId: encodeBase64Url(publicKeyCredential.rawId),
138
+ response: {
139
+ attestationObject: encodeBase64Url(response.attestationObject),
140
+ clientDataJSON: encodeBase64Url(response.clientDataJSON),
141
+ publicKey: response.getPublicKey ? nullableBufferToOptionalBase64Url(response.getPublicKey()) : undefined,
142
+ publicKeyAlgorithm: response.getPublicKeyAlgorithm ? response.getPublicKeyAlgorithm() : undefined,
143
+ transports: response.getTransports ? response.getTransports() : undefined,
144
+ },
145
+ type: PASSKEY_TYPE,
146
+ };
147
+ }
148
+ async function browserAuthenticationToJSON(credential) {
149
+ var _a;
150
+ const publicKeyCredential = expectPublicKeyCredential(credential, 'authentication');
151
+ const response = publicKeyCredential.response;
152
+ const nativeJson = credentialWithToJSON(publicKeyCredential);
153
+ if (nativeJson && 'authenticatorData' in nativeJson.response) {
154
+ return normalizeAuthenticationCredential(nativeJson);
155
+ }
156
+ return {
157
+ authenticatorAttachment: normalizeAttachment(publicKeyCredential.authenticatorAttachment),
158
+ clientExtensionResults: clientExtensionResultsToRecord((_a = publicKeyCredential.getClientExtensionResults) === null || _a === void 0 ? void 0 : _a.call(publicKeyCredential)),
159
+ id: publicKeyCredential.id,
160
+ rawId: encodeBase64Url(publicKeyCredential.rawId),
161
+ response: {
162
+ authenticatorData: encodeBase64Url(response.authenticatorData),
163
+ clientDataJSON: encodeBase64Url(response.clientDataJSON),
164
+ signature: encodeBase64Url(response.signature),
165
+ userHandle: nullableBufferToBase64Url(response.userHandle),
166
+ },
167
+ type: PASSKEY_TYPE,
168
+ };
169
+ }
170
+ function credentialFromJSON(credential) {
171
+ const shimCredential = new PasskeyPublicKeyCredential(credential);
172
+ if (typeof globalThis.PublicKeyCredential === 'function') {
173
+ Object.setPrototypeOf(shimCredential, globalThis.PublicKeyCredential.prototype);
174
+ }
175
+ return shimCredential;
176
+ }
177
+ function parseCreationOptionsFromJSON(options) {
178
+ return browserCreationOptionsFromJSON(normalizeCreationOptionsJSON(options));
179
+ }
180
+ function parseRequestOptionsFromJSON(options) {
181
+ return browserRequestOptionsFromJSON(normalizeRequestOptionsJSON(options));
182
+ }
183
+ function decodeBase64Url(value) {
184
+ const bytes = base64UrlToBytes(value);
185
+ return bytes.slice().buffer;
186
+ }
187
+ function encodeBase64Url(value) {
188
+ if (typeof value === 'undefined' || value === null) {
189
+ return '';
190
+ }
191
+ const bytes = toUint8Array(value);
192
+ let binary = '';
193
+ bytes.forEach((byte) => {
194
+ binary += String.fromCharCode(byte);
195
+ });
196
+ return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/u, '');
197
+ }
198
+ function resolveOrigin(explicitOrigin, rpId) {
199
+ if (explicitOrigin) {
200
+ return explicitOrigin;
201
+ }
202
+ if (typeof globalThis.location !== 'undefined' && /^https?:$/u.test(globalThis.location.protocol)) {
203
+ return globalThis.location.origin;
204
+ }
205
+ if (rpId) {
206
+ return `https://${rpId}`;
207
+ }
208
+ return undefined;
209
+ }
210
+ function createPasskeyDomException(name, message) {
211
+ if (typeof DOMException !== 'undefined') {
212
+ return new DOMException(message, name);
213
+ }
214
+ const error = new Error(message);
215
+ error.name = name;
216
+ return error;
217
+ }
218
+ function abortIfNeeded(signal) {
219
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
220
+ throw createPasskeyDomException('AbortError', 'The passkey request was aborted.');
221
+ }
222
+ }
223
+ function normalizePluginError(error) {
224
+ if (error instanceof Error) {
225
+ return error;
226
+ }
227
+ const message = typeof error === 'object' && error && 'message' in error ? String(error.message) : String(error);
228
+ const name = resolveErrorName(error);
229
+ return createPasskeyDomException(name, message);
230
+ }
231
+ function credentialWithToJSON(credential) {
232
+ const toJSON = credential.toJSON;
233
+ if (typeof toJSON !== 'function') {
234
+ return null;
235
+ }
236
+ const json = toJSON();
237
+ if (!json || typeof json !== 'object') {
238
+ return null;
239
+ }
240
+ const response = json.response;
241
+ if (!response) {
242
+ return null;
243
+ }
244
+ if ('attestationObject' in response) {
245
+ return normalizeRegistrationCredential(json);
246
+ }
247
+ if ('authenticatorData' in response) {
248
+ return normalizeAuthenticationCredential(json);
249
+ }
250
+ return null;
251
+ }
252
+ function normalizeRegistrationCredential(credential) {
253
+ var _a, _b, _c, _d, _e;
254
+ return {
255
+ authenticatorAttachment: normalizeAttachment(credential.authenticatorAttachment),
256
+ clientExtensionResults: (_a = credential.clientExtensionResults) !== null && _a !== void 0 ? _a : {},
257
+ id: String((_b = credential.id) !== null && _b !== void 0 ? _b : ''),
258
+ rawId: String((_d = (_c = credential.rawId) !== null && _c !== void 0 ? _c : credential.id) !== null && _d !== void 0 ? _d : ''),
259
+ response: normalizeAttestationResponse((_e = credential.response) !== null && _e !== void 0 ? _e : {}),
260
+ type: PASSKEY_TYPE,
261
+ };
262
+ }
263
+ function normalizeAuthenticationCredential(credential) {
264
+ var _a, _b, _c, _d, _e;
265
+ return {
266
+ authenticatorAttachment: normalizeAttachment(credential.authenticatorAttachment),
267
+ clientExtensionResults: (_a = credential.clientExtensionResults) !== null && _a !== void 0 ? _a : {},
268
+ id: String((_b = credential.id) !== null && _b !== void 0 ? _b : ''),
269
+ rawId: String((_d = (_c = credential.rawId) !== null && _c !== void 0 ? _c : credential.id) !== null && _d !== void 0 ? _d : ''),
270
+ response: normalizeAssertionResponse((_e = credential.response) !== null && _e !== void 0 ? _e : {}),
271
+ type: PASSKEY_TYPE,
272
+ };
273
+ }
274
+ function normalizeAttestationResponse(response) {
275
+ var _a, _b;
276
+ return {
277
+ attestationObject: String((_a = response.attestationObject) !== null && _a !== void 0 ? _a : ''),
278
+ clientDataJSON: String((_b = response.clientDataJSON) !== null && _b !== void 0 ? _b : ''),
279
+ publicKey: response.publicKey,
280
+ publicKeyAlgorithm: response.publicKeyAlgorithm,
281
+ transports: response.transports,
282
+ };
283
+ }
284
+ function normalizeAssertionResponse(response) {
285
+ var _a, _b, _c;
286
+ return {
287
+ authenticatorData: String((_a = response.authenticatorData) !== null && _a !== void 0 ? _a : ''),
288
+ clientDataJSON: String((_b = response.clientDataJSON) !== null && _b !== void 0 ? _b : ''),
289
+ signature: String((_c = response.signature) !== null && _c !== void 0 ? _c : ''),
290
+ userHandle: typeof response.userHandle === 'undefined' ? null : response.userHandle,
291
+ };
292
+ }
293
+ function expectPublicKeyCredential(credential, flow) {
294
+ if (!credential || !(credential instanceof PublicKeyCredential)) {
295
+ throw createPasskeyDomException('InvalidStateError', `The browser did not return a ${flow} credential.`);
296
+ }
297
+ return credential;
298
+ }
299
+ function descriptorToJSON(descriptor) {
300
+ var _a;
301
+ return {
302
+ id: typeof descriptor.id === 'string' ? descriptor.id : encodeBase64Url(descriptor.id),
303
+ transports: descriptor.transports ? [...descriptor.transports] : undefined,
304
+ type: (_a = descriptor.type) !== null && _a !== void 0 ? _a : PASSKEY_TYPE,
305
+ };
306
+ }
307
+ function jsonDescriptorToBrowser(descriptor) {
308
+ var _a;
309
+ return {
310
+ id: decodeBase64Url(descriptor.id),
311
+ transports: descriptor.transports,
312
+ type: (_a = descriptor.type) !== null && _a !== void 0 ? _a : PASSKEY_TYPE,
313
+ };
314
+ }
315
+ function cloneExtensions(extensions) {
316
+ if (!extensions) {
317
+ return undefined;
318
+ }
319
+ return JSON.parse(JSON.stringify(extensions));
320
+ }
321
+ function nullableBufferToBase64Url(value) {
322
+ if (typeof value === 'undefined') {
323
+ return undefined;
324
+ }
325
+ if (value === null) {
326
+ return null;
327
+ }
328
+ return encodeBase64Url(value);
329
+ }
330
+ function nullableBufferToOptionalBase64Url(value) {
331
+ const encoded = nullableBufferToBase64Url(value);
332
+ return encoded !== null && encoded !== void 0 ? encoded : undefined;
333
+ }
334
+ function deriveRpId(origin) {
335
+ if (!origin) {
336
+ return undefined;
337
+ }
338
+ try {
339
+ return new URL(origin).hostname;
340
+ }
341
+ catch (_a) {
342
+ return undefined;
343
+ }
344
+ }
345
+ function base64UrlToBytes(value) {
346
+ const normalized = value.replace(/-/g, '+').replace(/_/g, '/');
347
+ const padding = normalized.length % 4 === 0 ? '' : '='.repeat(4 - (normalized.length % 4));
348
+ const binary = atob(`${normalized}${padding}`);
349
+ const bytes = new Uint8Array(binary.length);
350
+ for (let index = 0; index < binary.length; index += 1) {
351
+ bytes[index] = binary.charCodeAt(index);
352
+ }
353
+ return bytes;
354
+ }
355
+ function toUint8Array(value) {
356
+ if (Array.isArray(value)) {
357
+ return Uint8Array.from(value);
358
+ }
359
+ if (ArrayBuffer.isView(value)) {
360
+ return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
361
+ }
362
+ return new Uint8Array(value);
363
+ }
364
+ function normalizeAttachment(value) {
365
+ if (value === 'platform' || value === 'cross-platform') {
366
+ return value;
367
+ }
368
+ return undefined;
369
+ }
370
+ function clientExtensionResultsToRecord(value) {
371
+ if (!value) {
372
+ return {};
373
+ }
374
+ return JSON.parse(JSON.stringify(value));
375
+ }
376
+ class PasskeyAuthenticatorAttestationResponse {
377
+ constructor(json) {
378
+ this.json = json;
379
+ this.clientDataJSON = decodeBase64Url(json.clientDataJSON);
380
+ this.attestationObject = decodeBase64Url(json.attestationObject);
381
+ }
382
+ getPublicKey() {
383
+ return this.json.publicKey ? decodeBase64Url(this.json.publicKey) : null;
384
+ }
385
+ getPublicKeyAlgorithm() {
386
+ var _a;
387
+ return (_a = this.json.publicKeyAlgorithm) !== null && _a !== void 0 ? _a : -7;
388
+ }
389
+ getTransports() {
390
+ return this.json.transports ? [...this.json.transports] : [];
391
+ }
392
+ toJSON() {
393
+ return Object.assign({}, this.json);
394
+ }
395
+ }
396
+ class PasskeyAuthenticatorAssertionResponse {
397
+ constructor(json) {
398
+ this.json = json;
399
+ this.clientDataJSON = decodeBase64Url(json.clientDataJSON);
400
+ this.authenticatorData = decodeBase64Url(json.authenticatorData);
401
+ this.signature = decodeBase64Url(json.signature);
402
+ this.userHandle = json.userHandle ? decodeBase64Url(json.userHandle) : null;
403
+ }
404
+ toJSON() {
405
+ return Object.assign({}, this.json);
406
+ }
407
+ }
408
+ class PasskeyPublicKeyCredential {
409
+ constructor(json) {
410
+ var _a;
411
+ this.json = json;
412
+ this.type = PASSKEY_TYPE;
413
+ this.id = json.id;
414
+ this.rawId = decodeBase64Url(json.rawId);
415
+ this.authenticatorAttachment = (_a = json.authenticatorAttachment) !== null && _a !== void 0 ? _a : null;
416
+ this.response =
417
+ 'attestationObject' in json.response
418
+ ? new PasskeyAuthenticatorAttestationResponse(json.response)
419
+ : new PasskeyAuthenticatorAssertionResponse(json.response);
420
+ }
421
+ getClientExtensionResults() {
422
+ var _a;
423
+ return ((_a = this.json.clientExtensionResults) !== null && _a !== void 0 ? _a : {});
424
+ }
425
+ toJSON() {
426
+ return JSON.parse(JSON.stringify(this.json));
427
+ }
428
+ }
429
+ function resolveErrorName(error) {
430
+ if (typeof error === 'object' && error) {
431
+ if ('code' in error && typeof error.code === 'string') {
432
+ return error.code;
433
+ }
434
+ if ('data' in error && error.data && typeof error.data === 'object' && 'name' in error.data) {
435
+ const candidate = error.data.name;
436
+ if (typeof candidate === 'string') {
437
+ return candidate;
438
+ }
439
+ }
440
+ }
441
+ return 'UnknownError';
442
+ }
443
+
444
+ function installWebAuthnShim({ root = globalThis, plugin, options }) {
445
+ var _a;
446
+ const shimRoot = root;
447
+ const config = Object.assign(Object.assign({}, ((_a = shimRoot.__capgoPasskeyShimConfig) !== null && _a !== void 0 ? _a : {})), (options !== null && options !== void 0 ? options : {}));
448
+ shimRoot.__capgoPasskeyShimConfig = config;
449
+ if (shimRoot.__capgoPasskeyShimInstalled && !config.force) {
450
+ return;
451
+ }
452
+ const originalCredentials = shimRoot.navigator.credentials;
453
+ const patchedCredentials = {
454
+ create: async (request) => {
455
+ var _a;
456
+ const currentConfig = (_a = shimRoot.__capgoPasskeyShimConfig) !== null && _a !== void 0 ? _a : {};
457
+ if (!(request === null || request === void 0 ? void 0 : request.publicKey)) {
458
+ if (originalCredentials === null || originalCredentials === void 0 ? void 0 : originalCredentials.create) {
459
+ return originalCredentials.create.call(originalCredentials, request);
460
+ }
461
+ throw createPasskeyDomException('NotSupportedError', 'Only public-key credential creation is supported.');
462
+ }
463
+ abortIfNeeded(request.signal);
464
+ const response = (await plugin.createCredential({
465
+ origin: currentConfig.origin,
466
+ publicKey: webAuthnCreationOptionsToJSON(request.publicKey, currentConfig.origin),
467
+ }));
468
+ return credentialFromJSON(response);
469
+ },
470
+ get: async (request) => {
471
+ var _a;
472
+ const currentConfig = (_a = shimRoot.__capgoPasskeyShimConfig) !== null && _a !== void 0 ? _a : {};
473
+ if (!(request === null || request === void 0 ? void 0 : request.publicKey)) {
474
+ if (originalCredentials === null || originalCredentials === void 0 ? void 0 : originalCredentials.get) {
475
+ return originalCredentials.get.call(originalCredentials, request);
476
+ }
477
+ throw createPasskeyDomException('NotSupportedError', 'Only public-key credential requests are supported.');
478
+ }
479
+ abortIfNeeded(request.signal);
480
+ if (request.mediation === 'conditional') {
481
+ throw createPasskeyDomException('NotSupportedError', 'Conditional mediation is not available through the Capacitor passkey shim.');
482
+ }
483
+ const response = (await plugin.getCredential({
484
+ mediation: request.mediation,
485
+ origin: currentConfig.origin,
486
+ publicKey: webAuthnRequestOptionsToJSON(request.publicKey, currentConfig.origin),
487
+ }));
488
+ return credentialFromJSON(response);
489
+ },
490
+ preventSilentAccess: async () => {
491
+ if (originalCredentials === null || originalCredentials === void 0 ? void 0 : originalCredentials.preventSilentAccess) {
492
+ await originalCredentials.preventSilentAccess.call(originalCredentials);
493
+ }
494
+ },
495
+ store: async (credential) => {
496
+ if ((originalCredentials === null || originalCredentials === void 0 ? void 0 : originalCredentials.store) && credential) {
497
+ await originalCredentials.store.call(originalCredentials, credential);
498
+ }
499
+ },
500
+ };
501
+ try {
502
+ Object.defineProperty(shimRoot.navigator, 'credentials', {
503
+ configurable: true,
504
+ value: patchedCredentials,
505
+ writable: false,
506
+ });
507
+ }
508
+ catch (_b) {
509
+ shimRoot.navigator.credentials = patchedCredentials;
510
+ }
511
+ installPublicKeyCredentialStatics(shimRoot, plugin);
512
+ shimRoot.__capgoPasskeyShimInstalled = true;
513
+ }
514
+ function installPublicKeyCredentialStatics(root, plugin) {
515
+ var _a;
516
+ const nativePublicKeyCredential = (_a = root.PublicKeyCredential) !== null && _a !== void 0 ? _a : class {
517
+ };
518
+ const supportRequest = async () => {
519
+ try {
520
+ return await plugin.isSupported();
521
+ }
522
+ catch (error) {
523
+ throw normalizePluginError(error);
524
+ }
525
+ };
526
+ defineStatic(nativePublicKeyCredential, 'isUserVerifyingPlatformAuthenticatorAvailable', async () => {
527
+ const support = await supportRequest();
528
+ return support.available;
529
+ });
530
+ defineStatic(nativePublicKeyCredential, 'isConditionalMediationAvailable', async () => {
531
+ const support = await supportRequest();
532
+ return support.conditionalMediation;
533
+ });
534
+ defineStatic(nativePublicKeyCredential, 'parseCreationOptionsFromJSON', parseCreationOptionsFromJSON);
535
+ defineStatic(nativePublicKeyCredential, 'parseRequestOptionsFromJSON', parseRequestOptionsFromJSON);
536
+ root.PublicKeyCredential = nativePublicKeyCredential;
537
+ }
538
+ function defineStatic(target, key, value) {
539
+ try {
540
+ Object.defineProperty(target, key, {
541
+ configurable: true,
542
+ value,
543
+ writable: true,
544
+ });
545
+ }
546
+ catch (_a) {
547
+ Reflect.set(target, key, value);
548
+ }
549
+ }
550
+
551
+ const NativeCapacitorPasskey = core.registerPlugin('CapacitorPasskey', {
552
+ web: () => Promise.resolve().then(function () { return web; }).then((module) => new module.CapacitorPasskeyWeb()),
553
+ });
554
+ let configurationPromise;
555
+ let cachedConfiguration;
556
+ function mergeShimOptions(configuration, options) {
557
+ var _a;
558
+ const origin = (_a = options === null || options === void 0 ? void 0 : options.origin) !== null && _a !== void 0 ? _a : configuration === null || configuration === void 0 ? void 0 : configuration.origin;
559
+ const force = options === null || options === void 0 ? void 0 : options.force;
560
+ if (!origin && force === undefined) {
561
+ return undefined;
562
+ }
563
+ return {
564
+ force,
565
+ origin,
566
+ };
567
+ }
568
+ function mergeRuntimeConfiguration(configuration, options) {
569
+ var _a;
570
+ const origin = (_a = options === null || options === void 0 ? void 0 : options.origin) !== null && _a !== void 0 ? _a : configuration.origin;
571
+ const domains = new Set(configuration.domains);
572
+ if (origin) {
573
+ try {
574
+ domains.add(new URL(origin).hostname);
575
+ }
576
+ catch (_b) {
577
+ // Ignore invalid override values here and let native APIs validate later.
578
+ }
579
+ }
580
+ return Object.assign(Object.assign({}, configuration), { domains: [...domains], origin });
581
+ }
582
+ async function loadConfiguration() {
583
+ try {
584
+ const configuration = await NativeCapacitorPasskey.getConfiguration();
585
+ cachedConfiguration = configuration;
586
+ return configuration;
587
+ }
588
+ catch (error) {
589
+ configurationPromise = undefined;
590
+ throw error;
591
+ }
592
+ }
593
+ const CapacitorPasskey = {
594
+ async autoShimWebAuthn(options) {
595
+ const configuration = await CapacitorPasskey.getConfiguration();
596
+ if (!configuration.autoShim && !(options === null || options === void 0 ? void 0 : options.force)) {
597
+ return configuration;
598
+ }
599
+ const effectiveConfiguration = mergeRuntimeConfiguration(configuration, options);
600
+ cachedConfiguration = effectiveConfiguration;
601
+ installWebAuthnShim({
602
+ options: mergeShimOptions(effectiveConfiguration, options),
603
+ plugin: CapacitorPasskey,
604
+ });
605
+ return effectiveConfiguration;
606
+ },
607
+ async createCredential(options) {
608
+ return NativeCapacitorPasskey.createCredential(createNativeRequest(options));
609
+ },
610
+ async getConfiguration() {
611
+ if (!configurationPromise) {
612
+ configurationPromise = loadConfiguration();
613
+ }
614
+ return configurationPromise;
615
+ },
616
+ async getCredential(options) {
617
+ return NativeCapacitorPasskey.getCredential(createNativeRequest(options));
618
+ },
619
+ async getPluginVersion() {
620
+ return NativeCapacitorPasskey.getPluginVersion();
621
+ },
622
+ async isSupported() {
623
+ return NativeCapacitorPasskey.isSupported();
624
+ },
625
+ shimWebAuthn(options) {
626
+ installWebAuthnShim({
627
+ options: mergeShimOptions(cachedConfiguration, options),
628
+ plugin: CapacitorPasskey,
629
+ });
630
+ },
631
+ };
632
+
633
+ class CapacitorPasskeyWeb extends core.WebPlugin {
634
+ async createCredential(options) {
635
+ const credentials = this.ensureCredentials();
636
+ const parsed = JSON.parse(options.requestJson);
637
+ const credential = await credentials.create({
638
+ publicKey: browserCreationOptionsFromJSON(parsed),
639
+ });
640
+ return browserRegistrationToJSON(credential);
641
+ }
642
+ async getCredential(options) {
643
+ const credentials = this.ensureCredentials();
644
+ const parsed = JSON.parse(options.requestJson);
645
+ const credential = await credentials.get({
646
+ publicKey: browserRequestOptionsFromJSON(parsed),
647
+ });
648
+ return browserAuthenticationToJSON(credential);
649
+ }
650
+ async getConfiguration() {
651
+ const origin = this.currentOrigin();
652
+ const domains = origin ? [new URL(origin).hostname] : [];
653
+ return {
654
+ autoShim: true,
655
+ domains,
656
+ origin,
657
+ platform: 'web',
658
+ };
659
+ }
660
+ async isSupported() {
661
+ var _a, _b, _c, _d;
662
+ const available = typeof globalThis.PublicKeyCredential !== 'undefined' &&
663
+ typeof ((_b = (_a = globalThis.navigator) === null || _a === void 0 ? void 0 : _a.credentials) === null || _b === void 0 ? void 0 : _b.create) === 'function' &&
664
+ typeof ((_d = (_c = globalThis.navigator) === null || _c === void 0 ? void 0 : _c.credentials) === null || _d === void 0 ? void 0 : _d.get) === 'function';
665
+ let conditionalMediation = false;
666
+ if (available && typeof globalThis.PublicKeyCredential.isConditionalMediationAvailable === 'function') {
667
+ conditionalMediation = await globalThis.PublicKeyCredential.isConditionalMediationAvailable();
668
+ }
669
+ return {
670
+ available,
671
+ conditionalMediation,
672
+ platform: 'web',
673
+ };
674
+ }
675
+ async getPluginVersion() {
676
+ return {
677
+ version: 'web',
678
+ };
679
+ }
680
+ ensureCredentials() {
681
+ var _a, _b, _c, _d;
682
+ if (typeof globalThis.PublicKeyCredential === 'undefined' ||
683
+ typeof ((_b = (_a = globalThis.navigator) === null || _a === void 0 ? void 0 : _a.credentials) === null || _b === void 0 ? void 0 : _b.create) !== 'function' ||
684
+ typeof ((_d = (_c = globalThis.navigator) === null || _c === void 0 ? void 0 : _c.credentials) === null || _d === void 0 ? void 0 : _d.get) !== 'function') {
685
+ throw createPasskeyDomException('NotSupportedError', 'WebAuthn is not available in this browser.');
686
+ }
687
+ return globalThis.navigator.credentials;
688
+ }
689
+ currentOrigin() {
690
+ if (typeof globalThis.location === 'undefined') {
691
+ return undefined;
692
+ }
693
+ if (globalThis.location.protocol !== 'http:' && globalThis.location.protocol !== 'https:') {
694
+ return undefined;
695
+ }
696
+ return globalThis.location.origin;
697
+ }
698
+ }
699
+
700
+ var web = /*#__PURE__*/Object.freeze({
701
+ __proto__: null,
702
+ CapacitorPasskeyWeb: CapacitorPasskeyWeb
703
+ });
704
+
705
+ exports.CapacitorPasskey = CapacitorPasskey;
706
+
707
+ return exports;
708
+
709
+ })({}, capacitorExports);
710
+ //# sourceMappingURL=plugin.js.map