'use strict';dotclear.ready(()=>{const uid=document.querySelector('input[name=user_id]');if(uid)uid?.focus();const ckh=document.getElementById('cookie_help');if(ckh)ckh.style.display=navigator.cookieEnabled?'none':'';dotclear.passwordStrength(dotclear.getData('pwstrength'));const upw=document.querySelector('input[name=user_pwd]');if(!upw||!uid){return;}
uid.addEventListener('keypress',(event)=>{if(event.key==='Enter'&&upw.value===''){upw.focus();event.preventDefault();}});dotclear.webAuthnAuthentication=()=>{const wanHelper={atb:(b)=>{const u=new Uint8Array(b);let s='';for(let i=0;i<u.byteLength;i++){s+=String.fromCharCode(u[i]);}
return btoa(s);},bta:(o)=>{const pre='=?BINARY?B?';const suf='?=';for(const k in o){if(typeof o[k]==='string'){const s=o[k];if(s.startsWith(pre)&&s.endsWith(suf)){const b=globalThis.atob(s.substring(pre.length,s.length-suf.length));const u=new Uint8Array(b.length);for(let i=0;i<b.length;i++){u[i]=b.charCodeAt(i);}
o[k]=u.buffer;}}else{wanHelper.bta(o[k]);}}},};try{if(!('credentials'in navigator)){throw new Error('Browser not supported.');}
dotclear.jsonServicesPost('webAuthnAuthentication',(prepareValidate)=>{if(prepareValidate.success===false){throw new Error(prepareValidate.message||'webauthn: prepareValidate failed');}
wanHelper.bta(prepareValidate.arguments);navigator.credentials.get(prepareValidate.arguments).then((publicKeyCredential)=>{dotclear.jsonServicesPost('webAuthnAuthentication',(processValidate)=>{if(processValidate.success===false){globalThis.alert(processValidate.message||'failed to authenticate with passkey');}else{globalThis.location.reload();}},{json:1,step:'process',id:publicKeyCredential.rawId?wanHelper.atb(publicKeyCredential.rawId):null,client:publicKeyCredential.response.clientDataJSON?wanHelper.atb(publicKeyCredential.response.clientDataJSON):null,authenticator:publicKeyCredential.response.authenticatorData?wanHelper.atb(publicKeyCredential.response.authenticatorData):null,signature:publicKeyCredential.response.signature?wanHelper.atb(publicKeyCredential.response.signature):null,user:publicKeyCredential.response.userHandle?wanHelper.atb(publicKeyCredential.response.userHandle):null,},(error)=>{console.log(error||'unknown error occured');},);}).catch((error)=>{console.log(error||'unknown error occured');});},{json:1,step:'prepare',},(error)=>{console.log(error||'unknown error occured');},);}catch(error){console.log(error.message||'unknown error occured');}};if('credentials'in navigator){$('#webauthn_action input').on('click',(e)=>{dotclear.webAuthnAuthentication();e.preventDefault();e.returnValue='';});}else{$('#webauthn_action').hide();}});