|
| 1 | +import Alpine from 'alpinejs'; |
| 2 | +import { base64url } from 'rfc4648'; |
| 3 | + |
| 4 | +type DataType = { |
| 5 | + $refs: RefsType; |
| 6 | + $store: StoreType; |
| 7 | +}; |
| 8 | + |
| 9 | +type RefsType = { |
| 10 | + authenticatorDataInput: HTMLInputElement; |
| 11 | + authnSelectForm: HTMLFormElement; |
| 12 | + clientDataJSONInput: HTMLInputElement; |
| 13 | + credentialIdInput: HTMLInputElement; |
| 14 | + errorInput: HTMLInputElement; |
| 15 | + signatureInput: HTMLInputElement; |
| 16 | + userHandleInput: HTMLInputElement; |
| 17 | + webAuthnForm: HTMLFormElement; |
| 18 | +}; |
| 19 | + |
| 20 | +type StoreType = { |
| 21 | + webAuthnAuthenticate: { |
| 22 | + challenge: string; |
| 23 | + createTimeout: string; |
| 24 | + isUserIdentified: string; |
| 25 | + rpId: string; |
| 26 | + unsupportedBrowserText: string; |
| 27 | + userVerification: UserVerificationRequirement | 'not specified'; |
| 28 | + }; |
| 29 | +}; |
| 30 | + |
| 31 | +document.addEventListener('alpine:init', () => { |
| 32 | + Alpine.data('webAuthnAuthenticate', function (this: DataType) { |
| 33 | + const { |
| 34 | + authenticatorDataInput, |
| 35 | + authnSelectForm, |
| 36 | + clientDataJSONInput, |
| 37 | + credentialIdInput, |
| 38 | + errorInput, |
| 39 | + signatureInput, |
| 40 | + userHandleInput, |
| 41 | + webAuthnForm, |
| 42 | + } = this.$refs; |
| 43 | + const { |
| 44 | + challenge, |
| 45 | + createTimeout, |
| 46 | + isUserIdentified, |
| 47 | + rpId, |
| 48 | + unsupportedBrowserText, |
| 49 | + userVerification, |
| 50 | + } = this.$store.webAuthnAuthenticate; |
| 51 | + |
| 52 | + const doAuthenticate = (allowCredentials: PublicKeyCredentialDescriptor[]) => { |
| 53 | + if (!window.PublicKeyCredential) { |
| 54 | + errorInput.value = unsupportedBrowserText; |
| 55 | + webAuthnForm.submit(); |
| 56 | + |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + const publicKey: PublicKeyCredentialRequestOptions = { |
| 61 | + challenge: base64url.parse(challenge, { loose: true }), |
| 62 | + rpId: rpId, |
| 63 | + }; |
| 64 | + |
| 65 | + if (allowCredentials.length) { |
| 66 | + publicKey.allowCredentials = allowCredentials; |
| 67 | + } |
| 68 | + |
| 69 | + if (parseInt(createTimeout) !== 0) publicKey.timeout = parseInt(createTimeout) * 1000; |
| 70 | + |
| 71 | + if (userVerification !== 'not specified') publicKey.userVerification = userVerification; |
| 72 | + |
| 73 | + navigator.credentials |
| 74 | + .get({ publicKey }) |
| 75 | + .then((result) => { |
| 76 | + if ( |
| 77 | + result instanceof PublicKeyCredential && |
| 78 | + result.response instanceof AuthenticatorAssertionResponse |
| 79 | + ) { |
| 80 | + window.result = result; |
| 81 | + |
| 82 | + authenticatorDataInput.value = base64url.stringify( |
| 83 | + new Uint8Array(result.response.authenticatorData), |
| 84 | + { pad: false } |
| 85 | + ); |
| 86 | + |
| 87 | + clientDataJSONInput.value = base64url.stringify( |
| 88 | + new Uint8Array(result.response.clientDataJSON), |
| 89 | + { pad: false } |
| 90 | + ); |
| 91 | + |
| 92 | + signatureInput.value = base64url.stringify(new Uint8Array(result.response.signature), { |
| 93 | + pad: false, |
| 94 | + }); |
| 95 | + |
| 96 | + credentialIdInput.value = result.id; |
| 97 | + |
| 98 | + if (result.response.userHandle) { |
| 99 | + userHandleInput.value = base64url.stringify( |
| 100 | + new Uint8Array(result.response.userHandle), |
| 101 | + { pad: false } |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + webAuthnForm.submit(); |
| 106 | + } |
| 107 | + }) |
| 108 | + .catch((error) => { |
| 109 | + errorInput.value = error; |
| 110 | + webAuthnForm.submit(); |
| 111 | + }); |
| 112 | + }; |
| 113 | + |
| 114 | + const checkAllowCredentials = () => { |
| 115 | + const allowCredentials: PublicKeyCredentialDescriptor[] = []; |
| 116 | + |
| 117 | + const authnSelectFormElements = Array.from(authnSelectForm.elements); |
| 118 | + |
| 119 | + if (authnSelectFormElements.length) { |
| 120 | + authnSelectFormElements.forEach((element) => { |
| 121 | + if (element instanceof HTMLInputElement) { |
| 122 | + allowCredentials.push({ |
| 123 | + id: base64url.parse(element.value, { loose: true }), |
| 124 | + type: 'public-key', |
| 125 | + }); |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + doAuthenticate(allowCredentials); |
| 131 | + }; |
| 132 | + |
| 133 | + return { |
| 134 | + webAuthnAuthenticate: () => { |
| 135 | + if (!isUserIdentified) { |
| 136 | + doAuthenticate([]); |
| 137 | + |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + checkAllowCredentials(); |
| 142 | + }, |
| 143 | + }; |
| 144 | + }); |
| 145 | +}); |
0 commit comments