lemonldap-ng/lemonldap-ng-portal/site/htdocs/static/common/js/u2f-api.min.js
2017-02-02 21:48:32 +00:00

1 line
6.9 KiB
JavaScript

"use strict";var u2f=u2f||{};var js_api_version;u2f.EXTENSION_ID="kmendfapggjehodndflmmgagdbamhnfd";u2f.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"};u2f.ErrorCodes={OK:0,OTHER_ERROR:1,BAD_REQUEST:2,CONFIGURATION_UNSUPPORTED:3,DEVICE_INELIGIBLE:4,TIMEOUT:5};u2f.U2fRequest;u2f.U2fResponse;u2f.Error;u2f.Transport;u2f.Transports;u2f.SignRequest;u2f.SignResponse;u2f.RegisterRequest;u2f.RegisterResponse;u2f.RegisteredKey;u2f.GetJsApiVersionResponse;u2f.getMessagePort=function(b){if(typeof chrome!="undefined"&&chrome.runtime){var a={type:u2f.MessageTypes.U2F_SIGN_REQUEST,signRequests:[]};chrome.runtime.sendMessage(u2f.EXTENSION_ID,a,function(){if(!chrome.runtime.lastError){u2f.getChromeRuntimePort_(b)}else{u2f.getIframePort_(b)}})}else{if(u2f.isAndroidChrome_()){u2f.getAuthenticatorPort_(b)}else{if(u2f.isIosChrome_()){u2f.getIosPort_(b)}else{u2f.getIframePort_(b)}}}};u2f.isAndroidChrome_=function(){var a=navigator.userAgent;return a.indexOf("Chrome")!=-1&&a.indexOf("Android")!=-1};u2f.isIosChrome_=function(){return $.inArray(navigator.platform,["iPhone","iPad","iPod"])>-1};u2f.getChromeRuntimePort_=function(b){var a=chrome.runtime.connect(u2f.EXTENSION_ID,{includeTlsChannelId:true});setTimeout(function(){b(new u2f.WrappedChromeRuntimePort_(a))},0)};u2f.getAuthenticatorPort_=function(a){setTimeout(function(){a(new u2f.WrappedAuthenticatorPort_())},0)};u2f.getIosPort_=function(a){setTimeout(function(){a(new u2f.WrappedIosPort_())},0)};u2f.WrappedChromeRuntimePort_=function(a){this.port_=a};u2f.formatSignRequest_=function(g,d,b,a,f){if(js_api_version===undefined||js_api_version<1.1){var e=[];for(var c=0;c<b.length;c++){e[c]={version:b[c].version,challenge:d,keyHandle:b[c].keyHandle,appId:g}}return{type:u2f.MessageTypes.U2F_SIGN_REQUEST,signRequests:e,timeoutSeconds:a,requestId:f}}return{type:u2f.MessageTypes.U2F_SIGN_REQUEST,appId:g,challenge:d,registeredKeys:b,timeoutSeconds:a,requestId:f}};u2f.formatRegisterRequest_=function(g,c,a,b,f){if(js_api_version===undefined||js_api_version<1.1){for(var d=0;d<a.length;d++){a[d].appId=g}var e=[];for(var d=0;d<c.length;d++){e[d]={version:c[d].version,challenge:a[0],keyHandle:c[d].keyHandle,appId:g}}return{type:u2f.MessageTypes.U2F_REGISTER_REQUEST,signRequests:e,registerRequests:a,timeoutSeconds:b,requestId:f}}return{type:u2f.MessageTypes.U2F_REGISTER_REQUEST,appId:g,registerRequests:a,registeredKeys:c,timeoutSeconds:b,requestId:f}};u2f.WrappedChromeRuntimePort_.prototype.postMessage=function(a){this.port_.postMessage(a)};u2f.WrappedChromeRuntimePort_.prototype.addEventListener=function(a,c){var b=a.toLowerCase();if(b=="message"||b=="onmessage"){this.port_.onMessage.addListener(function(d){c({data:d})})}else{console.error("WrappedChromeRuntimePort only supports onMessage")}};u2f.WrappedAuthenticatorPort_=function(){this.requestId_=-1;this.requestObject_=null};u2f.WrappedAuthenticatorPort_.prototype.postMessage=function(a){var b=u2f.WrappedAuthenticatorPort_.INTENT_URL_BASE_+";S.request="+encodeURIComponent(JSON.stringify(a))+";end";document.location=b};u2f.WrappedAuthenticatorPort_.prototype.getPortType=function(){return"WrappedAuthenticatorPort_"};u2f.WrappedAuthenticatorPort_.prototype.addEventListener=function(b,d){var c=b.toLowerCase();if(c=="message"){var a=this;window.addEventListener("message",a.onRequestUpdate_.bind(a,d),false)}else{console.error("WrappedAuthenticatorPort only supports message")}};u2f.WrappedAuthenticatorPort_.prototype.onRequestUpdate_=function(f,c){var b=JSON.parse(c.data);var e=b.intentURL;var d=b.errorCode;var a=null;if(b.hasOwnProperty("data")){a=(JSON.parse(b.data))}f({data:a})};u2f.WrappedAuthenticatorPort_.INTENT_URL_BASE_="intent:#Intent;action=com.google.android.apps.authenticator.AUTHENTICATE";u2f.WrappedIosPort_=function(){};u2f.WrappedIosPort_.prototype.postMessage=function(b){var c=JSON.stringify(b);var a="u2f://auth?"+encodeURI(c);location.replace(a)};u2f.WrappedIosPort_.prototype.getPortType=function(){return"WrappedIosPort_"};u2f.WrappedIosPort_.prototype.addEventListener=function(a,c){var b=a.toLowerCase();if(b!=="message"){console.error("WrappedIosPort only supports message")}};u2f.getIframePort_=function(e){var d="chrome-extension://"+u2f.EXTENSION_ID;var b=document.createElement("iframe");b.src=d+"/u2f-comms.html";b.setAttribute("style","display:none");document.body.appendChild(b);var c=new MessageChannel();var a=function(f){if(f.data=="ready"){c.port1.removeEventListener("message",a);e(c.port1)}else{console.error('First event on iframe port was not "ready"')}};c.port1.addEventListener("message",a);c.port1.start();b.addEventListener("load",function(){b.contentWindow.postMessage("init",d,[c.port2])})};u2f.EXTENSION_TIMEOUT_SEC=30;u2f.port_=null;u2f.waitingForPort_=[];u2f.reqCounter_=0;u2f.callbackMap_={};u2f.getPortSingleton_=function(a){if(u2f.port_){a(u2f.port_)}else{if(u2f.waitingForPort_.length==0){u2f.getMessagePort(function(b){u2f.port_=b;u2f.port_.addEventListener("message",(u2f.responseHandler_));while(u2f.waitingForPort_.length){u2f.waitingForPort_.shift()(u2f.port_)}})}u2f.waitingForPort_.push(a)}};u2f.responseHandler_=function(c){var b=c.data;var d=b.requestId;if(!d||!u2f.callbackMap_[d]){console.error("Unknown or missing requestId in response.");return}var a=u2f.callbackMap_[d];delete u2f.callbackMap_[d];a(b.responseData)};u2f.sign=function(d,b,a,e,c){if(js_api_version===undefined){u2f.getApiVersion(function(f){js_api_version=f.js_api_version===undefined?0:f.js_api_version;console.log("Extension JS API Version: ",js_api_version);u2f.sendSignRequest(d,b,a,e,c)})}else{u2f.sendSignRequest(d,b,a,e,c)}};u2f.sendSignRequest=function(d,b,a,e,c){u2f.getPortSingleton_(function(f){var i=++u2f.reqCounter_;u2f.callbackMap_[i]=e;var g=(typeof c!=="undefined"?c:u2f.EXTENSION_TIMEOUT_SEC);var h=u2f.formatSignRequest_(d,b,a,g,i);f.postMessage(h)})};u2f.register=function(d,a,b,e,c){if(js_api_version===undefined){u2f.getApiVersion(function(f){js_api_version=f.js_api_version===undefined?0:f.js_api_version;console.log("Extension JS API Version: ",js_api_version);u2f.sendRegisterRequest(d,a,b,e,c)})}else{u2f.sendRegisterRequest(d,a,b,e,c)}};u2f.sendRegisterRequest=function(d,a,b,e,c){u2f.getPortSingleton_(function(f){var i=++u2f.reqCounter_;u2f.callbackMap_[i]=e;var g=(typeof c!=="undefined"?c:u2f.EXTENSION_TIMEOUT_SEC);var h=u2f.formatRegisterRequest_(d,b,a,g,i);f.postMessage(h)})};u2f.getApiVersion=function(b,a){u2f.getPortSingleton_(function(c){if(c.getPortType){var e;switch(c.getPortType()){case"WrappedIosPort_":case"WrappedAuthenticatorPort_":e=1.1;break;default:e=0;break}b({js_api_version:e});return}var f=++u2f.reqCounter_;u2f.callbackMap_[f]=b;var d={type:u2f.MessageTypes.U2F_GET_API_VERSION_REQUEST,timeoutSeconds:(typeof a!=="undefined"?a:u2f.EXTENSION_TIMEOUT_SEC),requestId:f};c.postMessage(d)})};