3 lines
9.1 KiB
JavaScript
3 lines
9.1 KiB
JavaScript
/*! webauthn-ui library (C) 2018 - 2020 Thomas Bleeker (www.madwizard.org) - MIT license */
|
|
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).WebAuthnUI={})}(this,(function(t){"use strict";var e=function(t,n){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])})(t,n)};function n(t,e,n,r){return new(n||(n=Promise))((function(o,i){function a(t){try{s(r.next(t))}catch(t){i(t)}}function u(t){try{s(r.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,u)}s((r=r.apply(t,e||[])).next())}))}function r(t,e){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=e.call(t,a)}catch(t){i=[6,t],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,u])}}}function o(t,e,n){return t?Promise.resolve():new Promise((function(t){var r=function(){e.removeEventListener(n,r),t()};e.addEventListener(n,r)}))}function i(){return o("loading"!==document.readyState,document,"DOMContentLoaded")}var a=function(t){function n(e,n,r){var o=this.constructor,i=t.call(this,"WebAuthnUI error: "+(void 0!==n?n:e))||this;return Object.setPrototypeOf(i,o.prototype),i.name=e,i.innerError=r,i}return function(t,n){function r(){this.constructor=t}e(t,n),t.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}(n,t),n.fromError=function(t){var e,r="unknown";if(t instanceof DOMException){e=r={NotAllowedError:"dom-not-allowed",SecurityError:"dom-security",NotSupportedError:"dom-not-supported",AbortError:"dom-abort",InvalidStateError:"dom-invalid-state"}[t.name]||"dom-unknown"}else e="unknown ("+t.toString()+")";return new n(r,e,t instanceof Error?t:void 0)},n}(Error);function u(t){for(var e=new Uint8Array(t),n="",r=0;r<e.length;r++)n+=String.fromCharCode(e[r]);for(var o=window.btoa(n),i=o.length-1;i>0&&"="===o[i];)i--;return o=(o=o.substr(0,i+1)).replace(/\+/g,"-").replace(/\//g,"_")}function s(t){var e=t.replace(/-/g,"+").replace(/_/g,"/");switch(e.length%4){case 2:e+="==";break;case 3:e+="=";break;case 1:throw new a("parse-error")}for(var n=window.atob(e),r=new Uint8Array(n.length),o=0;o<n.length;o++)r[o]=n.charCodeAt(o);return r}function c(t,e){for(var n={},r=Object.keys(e),o=0;o<r.length;o++){var i=r[o],a=e[i],l=t[i];void 0!==l&&(n[i]=0===a?l:1===a?null===l?null:s(l):2===a?null===l?null:u(l):"object"==typeof a?c(l,a):a(l))}return n}function l(){return t={type:0,id:1,transports:0},function(e){for(var n=[],r=0;r<e.length;r++)n[r]=c(e[r],t);return n};var t}function f(t,e){var n=e.getClientExtensionResults();Object.keys(n).length>0&&(t.clientExtensionResults=c(n,{appid:0}))}var d=function(){function t(){}return t.convertCreationOptions=function(t){return c(t,{rp:0,user:{id:1,name:0,displayName:0,icon:0},challenge:1,pubKeyCredParams:0,timeout:0,excludeCredentials:l(),authenticatorSelection:0,attestation:0,extensions:{appid:0}})},t.convertCreationResponse=function(t){var e=c(t,{type:0,id:0,rawId:2,response:{clientDataJSON:2,attestationObject:2}});return f(e,t),e},t.convertRequestOptions=function(t){return c(t,{challenge:1,timeout:0,rpId:0,allowCredentials:l(),userVerification:0,extensions:{appid:0}})},t.convertRequestResponse=function(t){var e=c(t,{type:0,id:0,rawId:2,response:{clientDataJSON:2,authenticatorData:2,signature:2,userHandle:2}});return f(e,t),e},t}(),p={loaded:function(){return o("complete"===document.readyState,window,"load")},ready:i};function h(t){return"string"==typeof t?document.querySelectorAll(t):[t]}var v=function(){function t(){}return t.isSupported=function(){return void 0!==window.PublicKeyCredential},t.isUVPASupported=function(){return n(this,void 0,void 0,(function(){return r(this,(function(t){return this.isSupported()?[2,window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable()]:[2,!1]}))}))},t.checkSupport=function(){if(!t.isSupported())throw new a("unsupported")},t.createCredential=function(e){return n(this,void 0,void 0,(function(){var n,o,i;return r(this,(function(r){switch(r.label){case 0:t.checkSupport(),n={publicKey:d.convertCreationOptions(e)},r.label=1;case 1:return r.trys.push([1,3,,4]),[4,navigator.credentials.create(n)];case 2:return o=r.sent(),[3,4];case 3:throw i=r.sent(),a.fromError(i);case 4:return[2,d.convertCreationResponse(o)]}}))}))},t.getCredential=function(e){return n(this,void 0,void 0,(function(){var n,o,i;return r(this,(function(r){switch(r.label){case 0:t.checkSupport(),n={publicKey:d.convertRequestOptions(e)},r.label=1;case 1:return r.trys.push([1,3,,4]),[4,navigator.credentials.get(n)];case 2:return o=r.sent(),[3,4];case 3:throw i=r.sent(),a.fromError(i);case 4:return[2,d.convertRequestResponse(o)]}}))}))},t.setFeatureCssClasses=function(e){return n(this,void 0,void 0,(function(){var n,o;return r(this,(function(r){return n=h(e),(o=function(t){for(var e=0;e<n.length;e++)n[e].classList.add(t)})("webauthn-"+(t.isSupported()?"":"un")+"supported"),[2,t.isUVPASupported().then((function(t){o("webauthn-uvpa-"+(t?"":"un")+"supported")}))]}))}))},t.loadConfig=function(t){return n(this,void 0,void 0,(function(){var e,o,u,s,c,l=this;return r(this,(function(f){switch(f.label){case 0:return[4,i()];case 1:if(f.sent(),"string"==typeof(e=t.formField)){if(null===(o=document.querySelector(e)))throw new a("bad-config","Could not find formField.");e=o}if(!(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement))throw new a("bad-config","formField does not refer to an input element.");return u=!1!==t.submitForm,this.isSupported()||!0!==t.postUnsupportedImmediately?(c=e,[2,new Promise((function(e){var o=t.trigger,i=!1;if("click"!==o.event)throw new a("bad-config");for(var s=h(t.trigger.element),f=function(){return n(l,void 0,void 0,(function(){var n;return r(this,(function(r){switch(r.label){case 0:return[4,this.runAutoConfig(t)];case 1:return n=r.sent(),this.setForm(c,n,u),i||(i=!0,e(n)),[2]}}))}))},d=0;d<s.length;d++)s[d].addEventListener("click",f)}))]):(s={status:"failed",error:"unsupported"},this.setForm(e,s,u),[2,s])}}))}))},t.startConfig=function(t){return n(this,void 0,void 0,(function(){var e;return r(this,(function(n){switch(n.label){case 0:return"get"!==t.type?[3,2]:[4,this.getCredential(t.request)];case 1:return e=n.sent(),[3,5];case 2:return"create"!==t.type?[3,4]:[4,this.createCredential(t.request)];case 3:return e=n.sent(),[3,5];case 4:throw new a("bad-config","Invalid config.type "+t.type);case 5:return[2,{status:"ok",credential:e}]}}))}))},t.runAutoConfig=function(t){return n(this,void 0,void 0,(function(){var e,n,o;return r(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),[4,this.startConfig(t)];case 1:return e=r.sent(),[3,3];case 2:return n=r.sent(),o=n instanceof a,!0===t.debug&&(console.error(n),o&&n.innerError&&console.error(n.innerError)),e={status:"failed",error:o?n.name:a.fromError(n).name},[3,3];case 3:return[2,e]}}))}))},t.setForm=function(t,e,n){t.value=JSON.stringify(e),n&&t.form&&t.form.submit()},t.autoConfig=function(){return n(this,void 0,void 0,(function(){var t,e,n,o,i,u,s;return r(this,(function(r){switch(r.label){case 0:for(t=[],e=document.querySelectorAll("input[data-webauthn],textarea[data-webauthn],script[data-webauthn]"),n=0;n<e.length;n++){if(o=e[n],(i="SCRIPT"===o.tagName)&&"application/json"!==o.type)throw new a("bad-config","Expecting application/json script with data-webauthn");if(null===(u=i?o.textContent:o.getAttribute("data-webauthn")))throw new a("bad-config","Missing JSON in data-webauthn");s=void 0;try{s=JSON.parse(u)}catch(t){throw new a("bad-config","invalid JSON in data-webauthn on element")}i||void 0!==s.formField||(s.formField=o),t.push(this.loadConfig(s))}return[4,Promise.all(t)];case 1:return r.sent(),[2]}}))}))},t.inProgress=!1,t}();var w=function(){return n(this,void 0,void 0,(function(){var t,e;return r(this,(function(n){switch(n.label){case 0:return[4,i()];case 1:for(n.sent(),t=document.querySelectorAll(".webauthn-detect"),e=0;e<t.length;e++)v.setFeatureCssClasses(t[e]);return[2,v.autoConfig()]}}))}))}().catch((function(t){console&&console.error&&console.error(t)}));t.WebAuthnError=a,t.WebAuthnUI=v,t.autoPromise=w,t.loadEvents=p,Object.defineProperty(t,"__esModule",{value:!0})}));
|