mirror of
https://github.com/bitwarden/web
synced 2025-12-06 00:03:28 +00:00
1 line
8.3 KiB
JavaScript
1 line
8.3 KiB
JavaScript
"use strict";function init(){start(),onMessage(),info("ready")}function start(){if(sentSuccess=!1,u2f.isSupported){var e=getQsParam("data");if(e)if(parentUrl=getQsParam("parent")){var t=document.createElement("a");t.href=parentUrl,parentOrigin=t.origin;var r=getQsParam("v");if(r){try{version=parseInt(r);var o=b64Decode(e),n=JSON.parse(o)}catch(e){return void error("Cannot parse data.")}n.appId&&n.challenge&&n.keys&&n.keys.length?(stop=!1,initU2f(n)):error("Invalid data parameters.")}else error("No version.")}else error("No parent.");else error("No data.")}else error("U2F is not supported in this browser.")}function initU2f(e){stop||u2f.sign(e.appId,e.challenge,e.keys,function(t){t.errorCode?5!==t.errorCode?(error("U2F Error: "+t.errorCode),setTimeout(function(){initU2f(e)},1e3)):initU2f(e):success(t)},10)}function onMessage(){window.addEventListener("message",function(e){e.origin&&""!==e.origin&&e.origin===parentOrigin&&("stop"===e.data?stop=!0:"start"===e.data&&stop&&start())},!1)}function error(e){parent.postMessage("error|"+e,parentUrl)}function success(e){if(!sentSuccess){var t=JSON.stringify(e);parent.postMessage("success|"+t,parentUrl),sentSuccess=!0}}function info(e){parent.postMessage("info|"+e,parentUrl)}function getQsParam(e){var t=window.location.href;e=e.replace(/[\[\]]/g,"\\$&");var r=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)").exec(t);return r?r[2]?decodeURIComponent(r[2].replace(/\+/g," ")):"":null}function b64Decode(e){return decodeURIComponent(Array.prototype.map.call(atob(e),function(e){return"%"+("00"+e.charCodeAt(0).toString(16)).slice(-2)}).join(""))}!function(e){var t=-1!==navigator.userAgent.indexOf("Firefox")||-1!==navigator.userAgent.indexOf("Gecko/"),r=!(void 0===e.u2f||!e.u2f.register);if(t&&r)e.u2f.isSupported=!0;else{var o=e.u2f||{};o.isSupported=!!(void 0!==o&&o.register||"undefined"!=typeof chrome&&chrome.runtime);var n;o.EXTENSION_ID="kmendfapggjehodndflmmgagdbamhnfd",o.MessageTypes={U2F_REGISTER_REQUEST:"u2f_register_request",U2F_REGISTER_RESPONSE:"u2f_register_response",U2F_SIGN_REQUEST:"u2f_sign_request",U2F_SIGN_RESPONSE:"u2f_sign_response",U2F_GET_API_VERSION_REQUEST:"u2f_get_api_version_request",U2F_GET_API_VERSION_RESPONSE:"u2f_get_api_version_response"},o.ErrorCodes={OK:0,OTHER_ERROR:1,BAD_REQUEST:2,CONFIGURATION_UNSUPPORTED:3,DEVICE_INELIGIBLE:4,TIMEOUT:5},o.U2fRequest,o.U2fResponse,o.Error,o.Transport,o.Transports,o.SignRequest,o.SignResponse,o.RegisterRequest,o.RegisterResponse,o.RegisteredKey,o.GetJsApiVersionResponse,o.getMessagePort=function(e){if("undefined"!=typeof chrome&&chrome.runtime){var t={type:o.MessageTypes.U2F_SIGN_REQUEST,signRequests:[]};chrome.runtime.sendMessage(o.EXTENSION_ID,t,function(){chrome.runtime.lastError?o.getIframePort_(e):o.getChromeRuntimePort_(e)})}else o.isAndroidChrome_()?o.getAuthenticatorPort_(e):o.isIosChrome_()?o.getIosPort_(e):o.getIframePort_(e)},o.isAndroidChrome_=function(){var e=navigator.userAgent;return-1!=e.indexOf("Chrome")&&-1!=e.indexOf("Android")},o.isIosChrome_=function(){return["iPhone","iPad","iPod"].indexOf(navigator.platform)>-1},o.getChromeRuntimePort_=function(e){var t=chrome.runtime.connect(o.EXTENSION_ID,{includeTlsChannelId:!0});setTimeout(function(){e(new o.WrappedChromeRuntimePort_(t))},0)},o.getAuthenticatorPort_=function(e){setTimeout(function(){e(new o.WrappedAuthenticatorPort_)},0)},o.getIosPort_=function(e){setTimeout(function(){e(new o.WrappedIosPort_)},0)},o.WrappedChromeRuntimePort_=function(e){this.port_=e},o.formatSignRequest_=function(e,t,r,s,i){if(void 0===n||n<1.1){for(var a=[],p=0;p<r.length;p++)a[p]={version:r[p].version,challenge:t,keyHandle:r[p].keyHandle,appId:e};return{type:o.MessageTypes.U2F_SIGN_REQUEST,signRequests:a,timeoutSeconds:s,requestId:i}}return{type:o.MessageTypes.U2F_SIGN_REQUEST,appId:e,challenge:t,registeredKeys:r,timeoutSeconds:s,requestId:i}},o.formatRegisterRequest_=function(e,t,r,s,i){if(void 0===n||n<1.1){for(p=0;p<r.length;p++)r[p].appId=e;for(var a=[],p=0;p<t.length;p++)a[p]={version:t[p].version,challenge:r[0],keyHandle:t[p].keyHandle,appId:e};return{type:o.MessageTypes.U2F_REGISTER_REQUEST,signRequests:a,registerRequests:r,timeoutSeconds:s,requestId:i}}return{type:o.MessageTypes.U2F_REGISTER_REQUEST,appId:e,registerRequests:r,registeredKeys:t,timeoutSeconds:s,requestId:i}},o.WrappedChromeRuntimePort_.prototype.postMessage=function(e){this.port_.postMessage(e)},o.WrappedChromeRuntimePort_.prototype.addEventListener=function(e,t){var r=e.toLowerCase();"message"==r||"onmessage"==r?this.port_.onMessage.addListener(function(e){t({data:e})}):console.error("WrappedChromeRuntimePort only supports onMessage")},o.WrappedAuthenticatorPort_=function(){this.requestId_=-1,this.requestObject_=null},o.WrappedAuthenticatorPort_.prototype.postMessage=function(e){var t=o.WrappedAuthenticatorPort_.INTENT_URL_BASE_+";S.request="+encodeURIComponent(JSON.stringify(e))+";end";document.location=t},o.WrappedAuthenticatorPort_.prototype.getPortType=function(){return"WrappedAuthenticatorPort_"},o.WrappedAuthenticatorPort_.prototype.addEventListener=function(e,t){if("message"==e.toLowerCase()){var r=this;window.addEventListener("message",r.onRequestUpdate_.bind(r,t),!1)}else console.error("WrappedAuthenticatorPort only supports message")},o.WrappedAuthenticatorPort_.prototype.onRequestUpdate_=function(e,t){var r=JSON.parse(t.data),o=(r.intentURL,r.errorCode,null);r.hasOwnProperty("data")&&(o=JSON.parse(r.data)),e({data:o})},o.WrappedAuthenticatorPort_.INTENT_URL_BASE_="intent:#Intent;action=com.google.android.apps.authenticator.AUTHENTICATE",o.WrappedIosPort_=function(){},o.WrappedIosPort_.prototype.postMessage=function(e){var t=JSON.stringify(e),r="u2f://auth?"+encodeURI(t);location.replace(r)},o.WrappedIosPort_.prototype.getPortType=function(){return"WrappedIosPort_"},o.WrappedIosPort_.prototype.addEventListener=function(e,t){"message"!==e.toLowerCase()&&console.error("WrappedIosPort only supports message")},o.getIframePort_=function(e){var t="chrome-extension://"+o.EXTENSION_ID,r=document.createElement("iframe");r.src=t+"/u2f-comms.html",r.setAttribute("style","display:none"),document.body.appendChild(r);var n=new MessageChannel,s=function(t){"ready"==t.data?(n.port1.removeEventListener("message",s),e(n.port1)):console.error('First event on iframe port was not "ready"')};n.port1.addEventListener("message",s),n.port1.start(),r.addEventListener("load",function(){r.contentWindow.postMessage("init",t,[n.port2])})},o.EXTENSION_TIMEOUT_SEC=30,o.port_=null,o.waitingForPort_=[],o.reqCounter_=0,o.callbackMap_={},o.getPortSingleton_=function(e){o.port_?e(o.port_):(0==o.waitingForPort_.length&&o.getMessagePort(function(e){for(o.port_=e,o.port_.addEventListener("message",o.responseHandler_);o.waitingForPort_.length;)o.waitingForPort_.shift()(o.port_)}),o.waitingForPort_.push(e))},o.responseHandler_=function(e){var t=e.data,r=t.requestId;if(r&&o.callbackMap_[r]){var n=o.callbackMap_[r];delete o.callbackMap_[r],n(t.responseData)}else console.error("Unknown or missing requestId in response.")},o.sign=function(e,t,r,s,i){void 0===n?o.getApiVersion(function(a){n=void 0===a.js_api_version?0:a.js_api_version,console.log("Extension JS API Version: ",n),o.sendSignRequest(e,t,r,s,i)}):o.sendSignRequest(e,t,r,s,i)},o.sendSignRequest=function(e,t,r,n,s){o.getPortSingleton_(function(i){var a=++o.reqCounter_;o.callbackMap_[a]=n;var p=void 0!==s?s:o.EXTENSION_TIMEOUT_SEC,u=o.formatSignRequest_(e,t,r,p,a);i.postMessage(u)})},o.register=function(e,t,r,s,i){void 0===n?o.getApiVersion(function(a){n=void 0===a.js_api_version?0:a.js_api_version,console.log("Extension JS API Version: ",n),o.sendRegisterRequest(e,t,r,s,i)}):o.sendRegisterRequest(e,t,r,s,i)},o.sendRegisterRequest=function(e,t,r,n,s){o.getPortSingleton_(function(i){var a=++o.reqCounter_;o.callbackMap_[a]=n;var p=void 0!==s?s:o.EXTENSION_TIMEOUT_SEC,u=o.formatRegisterRequest_(e,r,t,p,a);i.postMessage(u)})},o.getApiVersion=function(e,t){o.getPortSingleton_(function(r){if(r.getPortType){var n;switch(r.getPortType()){case"WrappedIosPort_":case"WrappedAuthenticatorPort_":n=1.1;break;default:n=0}e({js_api_version:n})}else{var s=++o.reqCounter_;o.callbackMap_[s]=e;var i={type:o.MessageTypes.U2F_GET_API_VERSION_REQUEST,timeoutSeconds:void 0!==t?t:o.EXTENSION_TIMEOUT_SEC,requestId:s};r.postMessage(i)}})},e.u2f=o}}(this),document.addEventListener("DOMContentLoaded",function(e){init()});var parentUrl=null,parentOrigin=null,version=null,stop=!1,sentSuccess=!1; |