fix: update webauthn authenticate (#36)

Co-authored-by: Anthony Lukin <anthony@lukin.dev>
This commit is contained in:
horus 2023-03-20 06:00:00 +08:00
parent af375808da
commit 0c070894ac
2 changed files with 14 additions and 12 deletions

View file

@ -8,7 +8,7 @@ type DataType = {
type RefsType = { type RefsType = {
authenticatorDataInput: HTMLInputElement; authenticatorDataInput: HTMLInputElement;
authnSelectForm: HTMLFormElement; authnSelectForm?: HTMLFormElement;
clientDataJSONInput: HTMLInputElement; clientDataJSONInput: HTMLInputElement;
credentialIdInput: HTMLInputElement; credentialIdInput: HTMLInputElement;
errorInput: HTMLInputElement; errorInput: HTMLInputElement;
@ -114,17 +114,19 @@ document.addEventListener('alpine:init', () => {
const checkAllowCredentials = () => { const checkAllowCredentials = () => {
const allowCredentials: PublicKeyCredentialDescriptor[] = []; const allowCredentials: PublicKeyCredentialDescriptor[] = [];
const authnSelectFormElements = Array.from(authnSelectForm.elements); if (authnSelectForm) {
const authnSelectFormElements = Array.from(authnSelectForm.elements);
if (authnSelectFormElements.length) { if (authnSelectFormElements.length) {
authnSelectFormElements.forEach((element) => { authnSelectFormElements.forEach((element) => {
if (element instanceof HTMLInputElement) { if (element instanceof HTMLInputElement) {
allowCredentials.push({ allowCredentials.push({
id: base64url.parse(element.value, { loose: true }), id: base64url.parse(element.value, { loose: true }),
type: 'public-key', type: 'public-key',
}); });
} }
}); });
}
} }
doAuthenticate(allowCredentials); doAuthenticate(allowCredentials);

View file

@ -1 +1 @@
import{m as g}from"./assets/module.esm-62c37d0d.js";import{b as n}from"./assets/index-a7b84447.js";document.addEventListener("alpine:init",()=>{g.data("webAuthnAuthenticate",function(){const{authenticatorDataInput:c,authnSelectForm:l,clientDataJSONInput:p,credentialIdInput:f,errorInput:r,signatureInput:d,userHandleInput:h,webAuthnForm:i}=this.$refs,{challenge:m,createTimeout:s,isUserIdentified:A,rpId:w,unsupportedBrowserText:y,userVerification:o}=this.$store.webAuthnAuthenticate,u=a=>{if(!window.PublicKeyCredential){r.value=y,i.submit();return}const t={challenge:n.parse(m,{loose:!0}),rpId:w};a.length&&(t.allowCredentials=a),parseInt(s)!==0&&(t.timeout=parseInt(s)*1e3),o!=="not specified"&&(t.userVerification=o),navigator.credentials.get({publicKey:t}).then(e=>{e instanceof PublicKeyCredential&&e.response instanceof AuthenticatorAssertionResponse&&(window.result=e,c.value=n.stringify(new Uint8Array(e.response.authenticatorData),{pad:!1}),p.value=n.stringify(new Uint8Array(e.response.clientDataJSON),{pad:!1}),d.value=n.stringify(new Uint8Array(e.response.signature),{pad:!1}),f.value=e.id,e.response.userHandle&&(h.value=n.stringify(new Uint8Array(e.response.userHandle),{pad:!1})),i.submit())}).catch(e=>{r.value=e,i.submit()})},b=()=>{const a=[],t=Array.from(l.elements);t.length&&t.forEach(e=>{e instanceof HTMLInputElement&&a.push({id:n.parse(e.value,{loose:!0}),type:"public-key"})}),u(a)};return{webAuthnAuthenticate:()=>{if(!A){u([]);return}b()}}})}); import{m as g}from"./assets/module.esm-62c37d0d.js";import{b as n}from"./assets/index-a7b84447.js";document.addEventListener("alpine:init",()=>{g.data("webAuthnAuthenticate",function(){const{authenticatorDataInput:l,authnSelectForm:r,clientDataJSONInput:p,credentialIdInput:f,errorInput:s,signatureInput:d,userHandleInput:h,webAuthnForm:a}=this.$refs,{challenge:m,createTimeout:o,isUserIdentified:A,rpId:w,unsupportedBrowserText:y,userVerification:u}=this.$store.webAuthnAuthenticate,c=i=>{if(!window.PublicKeyCredential){s.value=y,a.submit();return}const t={challenge:n.parse(m,{loose:!0}),rpId:w};i.length&&(t.allowCredentials=i),parseInt(o)!==0&&(t.timeout=parseInt(o)*1e3),u!=="not specified"&&(t.userVerification=u),navigator.credentials.get({publicKey:t}).then(e=>{e instanceof PublicKeyCredential&&e.response instanceof AuthenticatorAssertionResponse&&(window.result=e,l.value=n.stringify(new Uint8Array(e.response.authenticatorData),{pad:!1}),p.value=n.stringify(new Uint8Array(e.response.clientDataJSON),{pad:!1}),d.value=n.stringify(new Uint8Array(e.response.signature),{pad:!1}),f.value=e.id,e.response.userHandle&&(h.value=n.stringify(new Uint8Array(e.response.userHandle),{pad:!1})),a.submit())}).catch(e=>{s.value=e,a.submit()})},b=()=>{const i=[];if(r){const t=Array.from(r.elements);t.length&&t.forEach(e=>{e instanceof HTMLInputElement&&i.push({id:n.parse(e.value,{loose:!0}),type:"public-key"})})}c(i)};return{webAuthnAuthenticate:()=>{if(!A){c([]);return}b()}}})});