From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id DCACB7D724 for ; Tue, 9 Nov 2021 12:27:53 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 413ECB094 for ; Tue, 9 Nov 2021 12:27:48 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 1CA57B071 for ; Tue, 9 Nov 2021 12:27:33 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id DE5314257F for ; Tue, 9 Nov 2021 12:27:32 +0100 (CET) From: Wolfgang Bumiller To: pve-devel@lists.proxmox.com Date: Tue, 9 Nov 2021 12:27:17 +0100 Message-Id: <20211109112721.130935-29-w.bumiller@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211109112721.130935-1-w.bumiller@proxmox.com> References: <20211109112721.130935-1-w.bumiller@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SPAM-LEVEL: Spam detection results: 0 AWL 0.392 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_SHORT 0.001 Use of a URL Shortener for very short URL PROLO_LEO1 0.1 Meta Catches all Leo drug variations so far PROLO_LEO3 0.1 Meta Catches all Leo drug variations so far SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [message.data] Subject: [pve-devel] [PATCH widget-toolkit 3/7] add u2f-api.js and qrcode.min.js X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Nov 2021 11:27:54 -0000 copied from pve/pbs Signed-off-by: Wolfgang Bumiller --- src/Makefile | 2 + src/qrcode.min.js | 1 + src/u2f-api.js | 748 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 751 insertions(+) create mode 100644 src/qrcode.min.js create mode 100644 src/u2f-api.js diff --git a/src/Makefile b/src/Makefile index cc464c3..fe915dd 100644 --- a/src/Makefile +++ b/src/Makefile @@ -113,6 +113,8 @@ proxmoxlib.js: .lint-incremental ${JSSRC} install: proxmoxlib.js install -d -m 755 ${WWWBASEDIR} install -m 0644 proxmoxlib.js ${WWWBASEDIR} + install -m 0644 u2f-api.js ${WWWBASEDIR} + install -m 0644 qrcode.min.js ${WWWBASEDIR} set -e && for i in ${SUBDIRS}; do ${MAKE} -C $$i $@; done =20 .PHONY: clean diff --git a/src/qrcode.min.js b/src/qrcode.min.js new file mode 100644 index 0000000..993e88f --- /dev/null +++ b/src/qrcode.min.js @@ -0,0 +1 @@ +var QRCode;!function(){function a(a){this.mode=3Dc.MODE_8BIT_BYTE,this.dat= a=3Da,this.parsedData=3D[];for(var b=3D[],d=3D0,e=3Dthis.data.length;e>d;d+= +){var f=3Dthis.data.charCodeAt(d);f>65536?(b[0]=3D240|(1835008&f)>>>18,b[1= ]=3D128|(258048&f)>>>12,b[2]=3D128|(4032&f)>>>6,b[3]=3D128|63&f):f>2048?(b[= 0]=3D224|(61440&f)>>>12,b[1]=3D128|(4032&f)>>>6,b[2]=3D128|63&f):f>128?(b[0= ]=3D192|(1984&f)>>>6,b[1]=3D128|63&f):b[0]=3Df,this.parsedData=3Dthis.parse= dData.concat(b)}this.parsedData.length!=3Dthis.data.length&&(this.parsedDat= a.unshift(191),this.parsedData.unshift(187),this.parsedData.unshift(239))}f= unction b(a,b){this.typeNumber=3Da,this.errorCorrectLevel=3Db,this.modules= =3Dnull,this.moduleCount=3D0,this.dataCache=3Dnull,this.dataList=3D[]}funct= ion i(a,b){if(void 0=3D=3Da.length)throw new Error(a.length+"/"+b);for(var = c=3D0;c=3Df;f++){var h=3D0;switch(b){case d.L:h=3Dl[f][0];break;case d.M:h=3Dl[= f][1];break;case d.Q:h=3Dl[f][2];break;case d.H:h=3Dl[f][3]}if(h>=3De)break= ;c++}if(c>l.length)throw new Error("Too long data");return c}function s(a){= var b=3DencodeURI(a).toString().replace(/\%[0-9a-fA-F]{2}/g,"a");return b.l= ength+(b.length!=3Da?3:0)}a.prototype=3D{getLength:function(){return this.p= arsedData.length},write:function(a){for(var b=3D0,c=3Dthis.parsedData.lengt= h;c>b;b++)a.put(this.parsedData[b],8)}},b.prototype=3D{addData:function(b){= var c=3Dnew a(b);this.dataList.push(c),this.dataCache=3Dnull},isDark:functi= on(a,b){if(0>a||this.moduleCount<=3Da||0>b||this.moduleCount<=3Db)throw new= Error(a+","+b);return this.modules[a][b]},getModuleCount:function(){return= this.moduleCount},make:function(){this.makeImpl(!1,this.getBestMaskPattern= ())},makeImpl:function(a,c){this.moduleCount=3D4*this.typeNumber+17,this.mo= dules=3Dnew Array(this.moduleCount);for(var d=3D0;d=3D7&&this.setupTypeNumbe= r(a),null=3D=3Dthis.dataCache&&(this.dataCache=3Db.createData(this.typeNumb= er,this.errorCorrectLevel,this.dataList)),this.mapData(this.dataCache,c)},s= etupPositionProbePattern:function(a,b){for(var c=3D-1;7>=3Dc;c++)if(!(-1>= =3Da+c||this.moduleCount<=3Da+c))for(var d=3D-1;7>=3Dd;d++)-1>=3Db+d||this.= moduleCount<=3Db+d||(this.modules[a+c][b+d]=3Dc>=3D0&&6>=3Dc&&(0=3D=3Dd||6= =3D=3Dd)||d>=3D0&&6>=3Dd&&(0=3D=3Dc||6=3D=3Dc)||c>=3D2&&4>=3Dc&&d>=3D2&&4>= =3Dd?!0:!1)},getBestMaskPattern:function(){for(var a=3D0,b=3D0,c=3D0;8>c;c+= +){this.makeImpl(!0,c);var d=3Df.getLostPoint(this);(0=3D=3Dc||a>d)&&(a=3Dd= ,b=3Dc)}return b},createMovieClip:function(a,b,c){var d=3Da.createEmptyMovi= eClip(b,c),e=3D1;this.make();for(var f=3D0;f=3Dg;g++)for(var h=3D-2;2>=3Dh;h++)t= his.modules[d+g][e+h]=3D-2=3D=3Dg||2=3D=3Dg||-2=3D=3Dh||2=3D=3Dh||0=3D=3Dg&= &0=3D=3Dh?!0:!1}},setupTypeNumber:function(a){for(var b=3Df.getBCHTypeNumbe= r(this.typeNumber),c=3D0;18>c;c++){var d=3D!a&&1=3D=3D(1&b>>c);this.modules= [Math.floor(c/3)][c%3+this.moduleCount-8-3]=3Dd}for(var c=3D0;18>c;c++){var= d=3D!a&&1=3D=3D(1&b>>c);this.modules[c%3+this.moduleCount-8-3][Math.floor(= c/3)]=3Dd}},setupTypeInfo:function(a,b){for(var c=3Dthis.errorCorrectLevel<= <3|b,d=3Df.getBCHTypeInfo(c),e=3D0;15>e;e++){var g=3D!a&&1=3D=3D(1&d>>e);6>= e?this.modules[e][8]=3Dg:8>e?this.modules[e+1][8]=3Dg:this.modules[this.mod= uleCount-15+e][8]=3Dg}for(var e=3D0;15>e;e++){var g=3D!a&&1=3D=3D(1&d>>e);8= >e?this.modules[8][this.moduleCount-e-1]=3Dg:9>e?this.modules[8][15-e-1+1]= =3Dg:this.modules[8][15-e-1]=3Dg}this.modules[this.moduleCount-8][8]=3D!a},= mapData:function(a,b){for(var c=3D-1,d=3Dthis.moduleCount-1,e=3D7,g=3D0,h= =3Dthis.moduleCount-1;h>0;h-=3D2)for(6=3D=3Dh&&h--;;){for(var i=3D0;2>i;i++= )if(null=3D=3Dthis.modules[d][h-i]){var j=3D!1;g>>e));var k=3Df.getMask(b,d,h-i);k&&(j=3D!j),this.modules[d][h-i]=3Dj,e= --,-1=3D=3De&&(g++,e=3D7)}if(d+=3Dc,0>d||this.moduleCount<=3Dd){d-=3Dc,c=3D= -c;break}}}},b.PAD0=3D236,b.PAD1=3D17,b.createData=3Dfunction(a,c,d){for(va= r e=3Dj.getRSBlocks(a,c),g=3Dnew k,h=3D0;h8= *l)throw new Error("code length overflow. ("+g.getLengthInBits()+">"+8*l+")= ");for(g.getLengthInBits()+4<=3D8*l&&g.put(0,4);0!=3Dg.getLengthInBits()%8;= )g.putBit(!1);for(;;){if(g.getLengthInBits()>=3D8*l)break;if(g.put(b.PAD0,8= ),g.getLengthInBits()>=3D8*l)break;g.put(b.PAD1,8)}return b.createBytes(g,e= )},b.createBytes=3Dfunction(a,b){for(var c=3D0,d=3D0,e=3D0,g=3Dnew Array(b.= length),h=3Dnew Array(b.length),j=3D0;j=3D0?p.get(q):0}}for(var r=3D= 0,m=3D0;mm;m++)for(var j=3D0;jm;m++)for(var j=3D0;j=3D0;)b^=3Df.G15<=3D0;)b^=3Df.G18<>>=3D1;return b},getPatternPosition:function(a){return f.PATTERN_POSIT= ION_TABLE[a-1]},getMask:function(a,b,c){switch(a){case e.PATTERN000:return = 0=3D=3D(b+c)%2;case e.PATTERN001:return 0=3D=3Db%2;case e.PATTERN010:return= 0=3D=3Dc%3;case e.PATTERN011:return 0=3D=3D(b+c)%3;case e.PATTERN100:retur= n 0=3D=3D(Math.floor(b/2)+Math.floor(c/3))%2;case e.PATTERN101:return 0=3D= =3Db*c%2+b*c%3;case e.PATTERN110:return 0=3D=3D(b*c%2+b*c%3)%2;case e.PATTE= RN111:return 0=3D=3D(b*c%3+(b+c)%2)%2;default:throw new Error("bad maskPatt= ern:"+a)}},getErrorCorrectPolynomial:function(a){for(var b=3Dnew i([1],0),c= =3D0;a>c;c++)b=3Db.multiply(new i([1,g.gexp(c)],0));return b},getLengthInBi= ts:function(a,b){if(b>=3D1&&10>b)switch(a){case c.MODE_NUMBER:return 10;cas= e c.MODE_ALPHA_NUM:return 9;case c.MODE_8BIT_BYTE:return 8;case c.MODE_KANJ= I:return 8;default:throw new Error("mode:"+a)}else if(27>b)switch(a){case c= .MODE_NUMBER:return 12;case c.MODE_ALPHA_NUM:return 11;case c.MODE_8BIT_BYT= E:return 16;case c.MODE_KANJI:return 10;default:throw new Error("mode:"+a)}= else{if(!(41>b))throw new Error("type:"+b);switch(a){case c.MODE_NUMBER:ret= urn 14;case c.MODE_ALPHA_NUM:return 13;case c.MODE_8BIT_BYTE:return 16;case= c.MODE_KANJI:return 12;default:throw new Error("mode:"+a)}}},getLostPoint:= function(a){for(var b=3Da.getModuleCount(),c=3D0,d=3D0;b>d;d++)for(var e=3D= 0;b>e;e++){for(var f=3D0,g=3Da.isDark(d,e),h=3D-1;1>=3Dh;h++)if(!(0>d+h||d+= h>=3Db))for(var i=3D-1;1>=3Di;i++)0>e+i||e+i>=3Db||(0!=3Dh||0!=3Di)&&g=3D= =3Da.isDark(d+h,e+i)&&f++;f>5&&(c+=3D3+f-5)}for(var d=3D0;b-1>d;d++)for(var= e=3D0;b-1>e;e++){var j=3D0;a.isDark(d,e)&&j++,a.isDark(d+1,e)&&j++,a.isDar= k(d,e+1)&&j++,a.isDark(d+1,e+1)&&j++,(0=3D=3Dj||4=3D=3Dj)&&(c+=3D3)}for(var= d=3D0;b>d;d++)for(var e=3D0;b-6>e;e++)a.isDark(d,e)&&!a.isDark(d,e+1)&&a.i= sDark(d,e+2)&&a.isDark(d,e+3)&&a.isDark(d,e+4)&&!a.isDark(d,e+5)&&a.isDark(= d,e+6)&&(c+=3D40);for(var e=3D0;b>e;e++)for(var d=3D0;b-6>d;d++)a.isDark(d,= e)&&!a.isDark(d+1,e)&&a.isDark(d+2,e)&&a.isDark(d+3,e)&&a.isDark(d+4,e)&&!a= .isDark(d+5,e)&&a.isDark(d+6,e)&&(c+=3D40);for(var k=3D0,e=3D0;b>e;e++)for(= var d=3D0;b>d;d++)a.isDark(d,e)&&k++;var l=3DMath.abs(100*k/b/b-50)/5;retur= n c+=3D10*l}},g=3D{glog:function(a){if(1>a)throw new Error("glog("+a+")");r= eturn g.LOG_TABLE[a]},gexp:function(a){for(;0>a;)a+=3D255;for(;a>=3D256;)a-= =3D255;return g.EXP_TABLE[a]},EXP_TABLE:new Array(256),LOG_TABLE:new Array(= 256)},h=3D0;8>h;h++)g.EXP_TABLE[h]=3D1<h;h++)g.EXP_TAB= LE[h]=3Dg.EXP_TABLE[h-4]^g.EXP_TABLE[h-5]^g.EXP_TABLE[h-6]^g.EXP_TABLE[h-8]= ;for(var h=3D0;255>h;h++)g.LOG_TABLE[g.EXP_TABLE[h]]=3Dh;i.prototype=3D{get= :function(a){return this.num[a]},getLength:function(){return this.num.lengt= h},multiply:function(a){for(var b=3Dnew Array(this.getLength()+a.getLength(= )-1),c=3D0;cf;f++)for(var g=3Dc[3*f+0],h=3D= c[3*f+1],i=3Dc[3*f+2],k=3D0;g>k;k++)e.push(new j(h,i));return e},j.getRsBlo= ckTable=3Dfunction(a,b){switch(b){case d.L:return j.RS_BLOCK_TABLE[4*(a-1)+= 0];case d.M:return j.RS_BLOCK_TABLE[4*(a-1)+1];case d.Q:return j.RS_BLOCK_T= ABLE[4*(a-1)+2];case d.H:return j.RS_BLOCK_TABLE[4*(a-1)+3];default:return = void 0}},k.prototype=3D{get:function(a){var b=3DMath.floor(a/8);return 1=3D= =3D(1&this.buffer[b]>>>7-a%8)},put:function(a,b){for(var c=3D0;b>c;c++)this= .putBit(1=3D=3D(1&a>>>b-c-1))},getLengthInBits:function(){return this.lengt= h},putBit:function(a){var b=3DMath.floor(this.length/8);this.buffer.length<= =3Db&&this.buffer.push(0),a&&(this.buffer[b]|=3D128>>>this.length%8),this.l= ength++}};var l=3D[[17,14,11,7],[32,26,20,14],[53,42,32,24],[78,62,46,34],[= 106,84,60,44],[134,106,74,58],[154,122,86,64],[192,152,108,84],[230,180,130= ,98],[271,213,151,119],[321,251,177,137],[367,287,203,155],[425,331,241,177= ],[458,362,258,194],[520,412,292,220],[586,450,322,250],[644,504,364,280],[= 718,560,394,310],[792,624,442,338],[858,666,482,382],[929,711,509,403],[100= 3,779,565,439],[1091,857,611,461],[1171,911,661,511],[1273,997,715,535],[13= 67,1059,751,593],[1465,1125,805,625],[1528,1190,868,658],[1628,1264,908,698= ],[1732,1370,982,742],[1840,1452,1030,790],[1952,1538,1112,842],[2068,1628,= 1168,898],[2188,1722,1228,958],[2303,1809,1283,983],[2431,1911,1351,1051],[= 2563,1989,1423,1093],[2699,2099,1499,1139],[2809,2213,1579,1219],[2953,2331= ,1663,1273]],o=3Dfunction(){var a=3Dfunction(a,b){this._el=3Da,this._htOpti= on=3Db};return a.prototype.draw=3Dfunction(a){function g(a,b){var c=3Ddocum= ent.createElementNS("http://www.w3.org/2000/svg",a);for(var d in b)b.hasOwn= Property(d)&&c.setAttribute(d,b[d]);return c}var b=3Dthis._htOption,c=3Dthi= s._el,d=3Da.getModuleCount();Math.floor(b.width/d),Math.floor(b.height/d),t= his.clear();var h=3Dg("svg",{viewBox:"0 0 "+String(d)+" "+String(d),width:"= 100%",height:"100%",fill:b.colorLight});h.setAttributeNS("http://www.w3.org= /2000/xmlns/","xmlns:xlink","http://www.w3.org/1999/xlink"),c.appendChild(h= ),h.appendChild(g("rect",{fill:b.colorDark,width:"1",height:"1",id:"templat= e"}));for(var i=3D0;d>i;i++)for(var j=3D0;d>j;j++)if(a.isDark(i,j)){var k= =3Dg("use",{x:String(i),y:String(j)});k.setAttributeNS("http://www.w3.org/1= 999/xlink","href","#template"),h.appendChild(k)}},a.prototype.clear=3Dfunct= ion(){for(;this._el.hasChildNodes();)this._el.removeChild(this._el.lastChil= d)},a}(),p=3D"svg"=3D=3D=3Ddocument.documentElement.tagName.toLowerCase(),q= =3Dp?o:m()?function(){function a(){this._elImage.src=3Dthis._elCanvas.toDat= aURL("image/png"),this._elImage.style.display=3D"block",this._elCanvas.styl= e.display=3D"none"}function d(a,b){var c=3Dthis;if(c._fFail=3Db,c._fSuccess= =3Da,null=3D=3D=3Dc._bSupportDataURI){var d=3Ddocument.createElement("img")= ,e=3Dfunction(){c._bSupportDataURI=3D!1,c._fFail&&_fFail.call(c)},f=3Dfunct= ion(){c._bSupportDataURI=3D!0,c._fSuccess&&c._fSuccess.call(c)};return d.on= abort=3De,d.onerror=3De,d.onload=3Df,d.src=3D"data:image/gif;base64,iVBORw0= KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgl= jNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=3D=3D",void 0}c._bSupportDataURI=3D=3D=3D!= 0&&c._fSuccess?c._fSuccess.call(c):c._bSupportDataURI=3D=3D=3D!1&&c._fFail&= &c._fFail.call(c)}if(this._android&&this._android<=3D2.1){var b=3D1/window.= devicePixelRatio,c=3DCanvasRenderingContext2D.prototype.drawImage;CanvasRen= deringContext2D.prototype.drawImage=3Dfunction(a,d,e,f,g,h,i,j){if("nodeNam= e"in a&&/img/i.test(a.nodeName))for(var l=3Darguments.length-1;l>=3D1;l--)a= rguments[l]=3Darguments[l]*b;else"undefined"=3D=3Dtypeof j&&(arguments[1]*= =3Db,arguments[2]*=3Db,arguments[3]*=3Db,arguments[4]*=3Db);c.apply(this,ar= guments)}}var e=3Dfunction(a,b){this._bIsPainted=3D!1,this._android=3Dn(),t= his._htOption=3Db,this._elCanvas=3Ddocument.createElement("canvas"),this._e= lCanvas.width=3Db.width,this._elCanvas.height=3Db.height,a.appendChild(this= ._elCanvas),this._el=3Da,this._oContext=3Dthis._elCanvas.getContext("2d"),t= his._bIsPainted=3D!1,this._elImage=3Ddocument.createElement("img"),this._el= Image.style.display=3D"none",this._el.appendChild(this._elImage),this._bSup= portDataURI=3Dnull};return e.prototype.draw=3Dfunction(a){var b=3Dthis._elI= mage,c=3Dthis._oContext,d=3Dthis._htOption,e=3Da.getModuleCount(),f=3Dd.wid= th/e,g=3Dd.height/e,h=3DMath.round(f),i=3DMath.round(g);b.style.display=3D"= none",this.clear();for(var j=3D0;e>j;j++)for(var k=3D0;e>k;k++){var l=3Da.i= sDark(j,k),m=3Dk*f,n=3Dj*g;c.strokeStyle=3Dl?d.colorDark:d.colorLight,c.lin= eWidth=3D1,c.fillStyle=3Dl?d.colorDark:d.colorLight,c.fillRect(m,n,f,g),c.s= trokeRect(Math.floor(m)+.5,Math.floor(n)+.5,h,i),c.strokeRect(Math.ceil(m)-= .5,Math.ceil(n)-.5,h,i)}this._bIsPainted=3D!0},e.prototype.makeImage=3Dfunc= tion(){this._bIsPainted&&d.call(this,a)},e.prototype.isPainted=3Dfunction()= {return this._bIsPainted},e.prototype.clear=3Dfunction(){this._oContext.cle= arRect(0,0,this._elCanvas.width,this._elCanvas.height),this._bIsPainted=3D!= 1},e.prototype.round=3Dfunction(a){return a?Math.floor(1e3*a)/1e3:a},e}():f= unction(){var a=3Dfunction(a,b){this._el=3Da,this._htOption=3Db};return a.p= rototype.draw=3Dfunction(a){for(var b=3Dthis._htOption,c=3Dthis._el,d=3Da.g= etModuleCount(),e=3DMath.floor(b.width/d),f=3DMath.floor(b.height/d),g=3D['= '],h=3D0;d>h;h++){g.pus= h("");for(var i=3D0;d>i;i++)g.push('');g.push("")}g.= push("
"),c.innerHTML=3Dg.join("");var j=3Dc.childNodes[0],k=3D(b.wi= dth-j.offsetWidth)/2,l=3D(b.height-j.offsetHeight)/2;k>0&&l>0&&(j.style.mar= gin=3Dl+"px "+k+"px")},a.prototype.clear=3Dfunction(){this._el.innerHTML=3D= ""},a}();QRCode=3Dfunction(a,b){if(this._htOption=3D{width:256,height:256,t= ypeNumber:4,colorDark:"#000000",colorLight:"#ffffff",correctLevel:d.H},"str= ing"=3D=3Dtypeof b&&(b=3D{text:b}),b)for(var c in b)this._htOption[c]=3Db[c= ];"string"=3D=3Dtypeof a&&(a=3Ddocument.getElementById(a)),this._android=3D= n(),this._el=3Da,this._oQRCode=3Dnull,this._oDrawing=3Dnew q(this._el,this.= _htOption),this._htOption.text&&this.makeCode(this._htOption.text)},QRCode.= prototype.makeCode=3Dfunction(a){this._oQRCode=3Dnew b(r(a,this._htOption.c= orrectLevel),this._htOption.correctLevel),this._oQRCode.addData(a),this._oQ= RCode.make(),this._el.title=3Da,this._oDrawing.draw(this._oQRCode),this.mak= eImage()},QRCode.prototype.makeImage=3Dfunction(){"function"=3D=3Dtypeof th= is._oDrawing.makeImage&&(!this._android||this._android>=3D3)&&this._oDrawin= g.makeImage()},QRCode.prototype.clear=3Dfunction(){this._oDrawing.clear()},= QRCode.CorrectLevel=3Dd}(); \ No newline at end of file diff --git a/src/u2f-api.js b/src/u2f-api.js new file mode 100644 index 0000000..9244d14 --- /dev/null +++ b/src/u2f-api.js @@ -0,0 +1,748 @@ +//Copyright 2014-2015 Google Inc. All rights reserved. + +//Use of this source code is governed by a BSD-style +//license that can be found in the LICENSE file or at +//https://developers.google.com/open-source/licenses/bsd + +/** + * @fileoverview The U2F api. + */ +'use strict'; + + +/** + * Namespace for the U2F api. + * @type {Object} + */ +var u2f =3D u2f || {}; + +/** + * FIDO U2F Javascript API Version + * @number + */ +var js_api_version; + +/** + * The U2F extension id + * @const {string} + */ +// The Chrome packaged app extension ID. +// Uncomment this if you want to deploy a server instance that uses +// the package Chrome app and does not require installing the U2F Chrome e= xtension. + u2f.EXTENSION_ID =3D 'kmendfapggjehodndflmmgagdbamhnfd'; +// The U2F Chrome extension ID. +// Uncomment this if you want to deploy a server instance that uses +// the U2F Chrome extension to authenticate. +// u2f.EXTENSION_ID =3D 'pfboblefjcgdjicmnffhdgionmgcdmne'; + + +/** + * Message types for messsages to/from the extension + * @const + * @enum {string} + */ +u2f.MessageTypes =3D { + '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' +}; + + +/** + * Response status codes + * @const + * @enum {number} + */ +u2f.ErrorCodes =3D { + 'OK': 0, + 'OTHER_ERROR': 1, + 'BAD_REQUEST': 2, + 'CONFIGURATION_UNSUPPORTED': 3, + 'DEVICE_INELIGIBLE': 4, + 'TIMEOUT': 5 +}; + + +/** + * A message for registration requests + * @typedef {{ + * type: u2f.MessageTypes, + * appId: ?string, + * timeoutSeconds: ?number, + * requestId: ?number + * }} + */ +u2f.U2fRequest; + + +/** + * A message for registration responses + * @typedef {{ + * type: u2f.MessageTypes, + * responseData: (u2f.Error | u2f.RegisterResponse | u2f.SignResponse), + * requestId: ?number + * }} + */ +u2f.U2fResponse; + + +/** + * An error object for responses + * @typedef {{ + * errorCode: u2f.ErrorCodes, + * errorMessage: ?string + * }} + */ +u2f.Error; + +/** + * Data object for a single sign request. + * @typedef {enum {BLUETOOTH_RADIO, BLUETOOTH_LOW_ENERGY, USB, NFC}} + */ +u2f.Transport; + + +/** + * Data object for a single sign request. + * @typedef {Array} + */ +u2f.Transports; + +/** + * Data object for a single sign request. + * @typedef {{ + * version: string, + * challenge: string, + * keyHandle: string, + * appId: string + * }} + */ +u2f.SignRequest; + + +/** + * Data object for a sign response. + * @typedef {{ + * keyHandle: string, + * signatureData: string, + * clientData: string + * }} + */ +u2f.SignResponse; + + +/** + * Data object for a registration request. + * @typedef {{ + * version: string, + * challenge: string + * }} + */ +u2f.RegisterRequest; + + +/** + * Data object for a registration response. + * @typedef {{ + * version: string, + * keyHandle: string, + * transports: Transports, + * appId: string + * }} + */ +u2f.RegisterResponse; + + +/** + * Data object for a registered key. + * @typedef {{ + * version: string, + * keyHandle: string, + * transports: ?Transports, + * appId: ?string + * }} + */ +u2f.RegisteredKey; + + +/** + * Data object for a get API register response. + * @typedef {{ + * js_api_version: number + * }} + */ +u2f.GetJsApiVersionResponse; + + +//Low level MessagePort API support + +/** + * Sets up a MessagePort to the U2F extension using the + * available mechanisms. + * @param {function((MessagePort|u2f.WrappedChromeRuntimePort_))} callback + */ +u2f.getMessagePort =3D function(callback) { + if (typeof chrome !=3D 'undefined' && chrome.runtime) { + // The actual message here does not matter, but we need to get a reply + // for the callback to run. Thus, send an empty signature request + // in order to get a failure response. + var msg =3D { + type: u2f.MessageTypes.U2F_SIGN_REQUEST, + signRequests: [] + }; + chrome.runtime.sendMessage(u2f.EXTENSION_ID, msg, function() { + if (!chrome.runtime.lastError) { + // We are on a whitelisted origin and can talk directly + // with the extension. + u2f.getChromeRuntimePort_(callback); + } else { + // chrome.runtime was available, but we couldn't message + // the extension directly, use iframe + u2f.getIframePort_(callback); + } + }); + } else if (u2f.isAndroidChrome_()) { + u2f.getAuthenticatorPort_(callback); + } else if (u2f.isIosChrome_()) { + u2f.getIosPort_(callback); + } else { + // chrome.runtime was not available at all, which is normal + // when this origin doesn't have access to any extensions. + u2f.getIframePort_(callback); + } +}; + +/** + * Detect chrome running on android based on the browser's useragent. + * @private + */ +u2f.isAndroidChrome_ =3D function() { + var userAgent =3D navigator.userAgent; + return userAgent.indexOf('Chrome') !=3D -1 && + userAgent.indexOf('Android') !=3D -1; +}; + +/** + * Detect chrome running on iOS based on the browser's platform. + * @private + */ +u2f.isIosChrome_ =3D function() { + return ["iPhone", "iPad", "iPod"].indexOf(navigator.platform) > -1; +}; + +/** + * Connects directly to the extension via chrome.runtime.connect. + * @param {function(u2f.WrappedChromeRuntimePort_)} callback + * @private + */ +u2f.getChromeRuntimePort_ =3D function(callback) { + var port =3D chrome.runtime.connect(u2f.EXTENSION_ID, + {'includeTlsChannelId': true}); + setTimeout(function() { + callback(new u2f.WrappedChromeRuntimePort_(port)); + }, 0); +}; + +/** + * Return a 'port' abstraction to the Authenticator app. + * @param {function(u2f.WrappedAuthenticatorPort_)} callback + * @private + */ +u2f.getAuthenticatorPort_ =3D function(callback) { + setTimeout(function() { + callback(new u2f.WrappedAuthenticatorPort_()); + }, 0); +}; + +/** + * Return a 'port' abstraction to the iOS client app. + * @param {function(u2f.WrappedIosPort_)} callback + * @private + */ +u2f.getIosPort_ =3D function(callback) { + setTimeout(function() { + callback(new u2f.WrappedIosPort_()); + }, 0); +}; + +/** + * A wrapper for chrome.runtime.Port that is compatible with MessagePort. + * @param {Port} port + * @constructor + * @private + */ +u2f.WrappedChromeRuntimePort_ =3D function(port) { + this.port_ =3D port; +}; + +/** + * Format and return a sign request compliant with the JS API version supp= orted by the extension. + * @param {Array} signRequests + * @param {number} timeoutSeconds + * @param {number} reqId + * @return {Object} + */ +u2f.formatSignRequest_ =3D + function(appId, challenge, registeredKeys, timeoutSeconds, reqId) { + if (js_api_version =3D=3D=3D undefined || js_api_version < 1.1) { + // Adapt request to the 1.0 JS API + var signRequests =3D []; + for (var i =3D 0; i < registeredKeys.length; i++) { + signRequests[i] =3D { + version: registeredKeys[i].version, + challenge: challenge, + keyHandle: registeredKeys[i].keyHandle, + appId: appId + }; + } + return { + type: u2f.MessageTypes.U2F_SIGN_REQUEST, + signRequests: signRequests, + timeoutSeconds: timeoutSeconds, + requestId: reqId + }; + } + // JS 1.1 API + return { + type: u2f.MessageTypes.U2F_SIGN_REQUEST, + appId: appId, + challenge: challenge, + registeredKeys: registeredKeys, + timeoutSeconds: timeoutSeconds, + requestId: reqId + }; +}; + +/** + * Format and return a register request compliant with the JS API version = supported by the extension.. + * @param {Array} signRequests + * @param {Array} signRequests + * @param {number} timeoutSeconds + * @param {number} reqId + * @return {Object} + */ +u2f.formatRegisterRequest_ =3D + function(appId, registeredKeys, registerRequests, timeoutSeconds, reqId)= { + if (js_api_version =3D=3D=3D undefined || js_api_version < 1.1) { + // Adapt request to the 1.0 JS API + for (var i =3D 0; i < registerRequests.length; i++) { + registerRequests[i].appId =3D appId; + } + var signRequests =3D []; + for (var i =3D 0; i < registeredKeys.length; i++) { + signRequests[i] =3D { + version: registeredKeys[i].version, + challenge: registerRequests[0], + keyHandle: registeredKeys[i].keyHandle, + appId: appId + }; + } + return { + type: u2f.MessageTypes.U2F_REGISTER_REQUEST, + signRequests: signRequests, + registerRequests: registerRequests, + timeoutSeconds: timeoutSeconds, + requestId: reqId + }; + } + // JS 1.1 API + return { + type: u2f.MessageTypes.U2F_REGISTER_REQUEST, + appId: appId, + registerRequests: registerRequests, + registeredKeys: registeredKeys, + timeoutSeconds: timeoutSeconds, + requestId: reqId + }; +}; + + +/** + * Posts a message on the underlying channel. + * @param {Object} message + */ +u2f.WrappedChromeRuntimePort_.prototype.postMessage =3D function(message) { + this.port_.postMessage(message); +}; + + +/** + * Emulates the HTML 5 addEventListener interface. Works only for the + * onmessage event, which is hooked up to the chrome.runtime.Port.onMessag= e. + * @param {string} eventName + * @param {function({data: Object})} handler + */ +u2f.WrappedChromeRuntimePort_.prototype.addEventListener =3D + function(eventName, handler) { + var name =3D eventName.toLowerCase(); + if (name =3D=3D 'message' || name =3D=3D 'onmessage') { + this.port_.onMessage.addListener(function(message) { + // Emulate a minimal MessageEvent object + handler({'data': message}); + }); + } else { + console.error('WrappedChromeRuntimePort only supports onMessage'); + } +}; + +/** + * Wrap the Authenticator app with a MessagePort interface. + * @constructor + * @private + */ +u2f.WrappedAuthenticatorPort_ =3D function() { + this.requestId_ =3D -1; + this.requestObject_ =3D null; +} + +/** + * Launch the Authenticator intent. + * @param {Object} message + */ +u2f.WrappedAuthenticatorPort_.prototype.postMessage =3D function(message) { + var intentUrl =3D + u2f.WrappedAuthenticatorPort_.INTENT_URL_BASE_ + + ';S.request=3D' + encodeURIComponent(JSON.stringify(message)) + + ';end'; + document.location =3D intentUrl; +}; + +/** + * Tells what type of port this is. + * @return {String} port type + */ +u2f.WrappedAuthenticatorPort_.prototype.getPortType =3D function() { + return "WrappedAuthenticatorPort_"; +}; + + +/** + * Emulates the HTML 5 addEventListener interface. + * @param {string} eventName + * @param {function({data: Object})} handler + */ +u2f.WrappedAuthenticatorPort_.prototype.addEventListener =3D function(even= tName, handler) { + var name =3D eventName.toLowerCase(); + if (name =3D=3D 'message') { + var self =3D this; + /* Register a callback to that executes when + * chrome injects the response. */ + window.addEventListener( + 'message', self.onRequestUpdate_.bind(self, handler), false); + } else { + console.error('WrappedAuthenticatorPort only supports message'); + } +}; + +/** + * Callback invoked when a response is received from the Authenticator. + * @param function({data: Object}) callback + * @param {Object} message message Object + */ +u2f.WrappedAuthenticatorPort_.prototype.onRequestUpdate_ =3D + function(callback, message) { + var messageObject =3D JSON.parse(message.data); + var intentUrl =3D messageObject['intentURL']; + + var errorCode =3D messageObject['errorCode']; + var responseObject =3D null; + if (messageObject.hasOwnProperty('data')) { + responseObject =3D /** @type {Object} */ ( + JSON.parse(messageObject['data'])); + } + + callback({'data': responseObject}); +}; + +/** + * Base URL for intents to Authenticator. + * @const + * @private + */ +u2f.WrappedAuthenticatorPort_.INTENT_URL_BASE_ =3D + 'intent:#Intent;action=3Dcom.google.android.apps.authenticator.AUTHENTIC= ATE'; + +/** + * Wrap the iOS client app with a MessagePort interface. + * @constructor + * @private + */ +u2f.WrappedIosPort_ =3D function() {}; + +/** + * Launch the iOS client app request + * @param {Object} message + */ +u2f.WrappedIosPort_.prototype.postMessage =3D function(message) { + var str =3D JSON.stringify(message); + var url =3D "u2f://auth?" + encodeURI(str); + location.replace(url); +}; + +/** + * Tells what type of port this is. + * @return {String} port type + */ +u2f.WrappedIosPort_.prototype.getPortType =3D function() { + return "WrappedIosPort_"; +}; + +/** + * Emulates the HTML 5 addEventListener interface. + * @param {string} eventName + * @param {function({data: Object})} handler + */ +u2f.WrappedIosPort_.prototype.addEventListener =3D function(eventName, han= dler) { + var name =3D eventName.toLowerCase(); + if (name !=3D=3D 'message') { + console.error('WrappedIosPort only supports message'); + } +}; + +/** + * Sets up an embedded trampoline iframe, sourced from the extension. + * @param {function(MessagePort)} callback + * @private + */ +u2f.getIframePort_ =3D function(callback) { + // Create the iframe + var iframeOrigin =3D 'chrome-extension://' + u2f.EXTENSION_ID; + var iframe =3D document.createElement('iframe'); + iframe.src =3D iframeOrigin + '/u2f-comms.html'; + iframe.setAttribute('style', 'display:none'); + document.body.appendChild(iframe); + + var channel =3D new MessageChannel(); + var ready =3D function(message) { + if (message.data =3D=3D 'ready') { + channel.port1.removeEventListener('message', ready); + callback(channel.port1); + } else { + console.error('First event on iframe port was not "ready"'); + } + }; + channel.port1.addEventListener('message', ready); + channel.port1.start(); + + iframe.addEventListener('load', function() { + // Deliver the port to the iframe and initialize + iframe.contentWindow.postMessage('init', iframeOrigin, [channel.port2]= ); + }); +}; + + +//High-level JS API + +/** + * Default extension response timeout in seconds. + * @const + */ +u2f.EXTENSION_TIMEOUT_SEC =3D 30; + +/** + * A singleton instance for a MessagePort to the extension. + * @type {MessagePort|u2f.WrappedChromeRuntimePort_} + * @private + */ +u2f.port_ =3D null; + +/** + * Callbacks waiting for a port + * @type {Array} + * @private + */ +u2f.waitingForPort_ =3D []; + +/** + * A counter for requestIds. + * @type {number} + * @private + */ +u2f.reqCounter_ =3D 0; + +/** + * A map from requestIds to client callbacks + * @type {Object.} + * @private + */ +u2f.callbackMap_ =3D {}; + +/** + * Creates or retrieves the MessagePort singleton to use. + * @param {function((MessagePort|u2f.WrappedChromeRuntimePort_))} callback + * @private + */ +u2f.getPortSingleton_ =3D function(callback) { + if (u2f.port_) { + callback(u2f.port_); + } else { + if (u2f.waitingForPort_.length =3D=3D 0) { + u2f.getMessagePort(function(port) { + u2f.port_ =3D port; + u2f.port_.addEventListener('message', + /** @type {function(Event)} */ (u2f.responseHandler_)); + + // Careful, here be async callbacks. Maybe. + while (u2f.waitingForPort_.length) + u2f.waitingForPort_.shift()(u2f.port_); + }); + } + u2f.waitingForPort_.push(callback); + } +}; + +/** + * Handles response messages from the extension. + * @param {MessageEvent.} message + * @private + */ +u2f.responseHandler_ =3D function(message) { + var response =3D message.data; + var reqId =3D response['requestId']; + if (!reqId || !u2f.callbackMap_[reqId]) { + console.error('Unknown or missing requestId in response.'); + return; + } + var cb =3D u2f.callbackMap_[reqId]; + delete u2f.callbackMap_[reqId]; + cb(response['responseData']); +}; + +/** + * Dispatches an array of sign requests to available U2F tokens. + * If the JS API version supported by the extension is unknown, it first s= ends a + * message to the extension to find out the supported API version and then= it sends + * the sign request. + * @param {string=3D} appId + * @param {string=3D} challenge + * @param {Array} registeredKeys + * @param {function((u2f.Error|u2f.SignResponse))} callback + * @param {number=3D} opt_timeoutSeconds + */ +u2f.sign =3D function(appId, challenge, registeredKeys, callback, opt_time= outSeconds) { + if (js_api_version =3D=3D=3D undefined) { + // Send a message to get the extension to JS API version, then send th= e actual sign request. + u2f.getApiVersion( + function (response) { + js_api_version =3D response['js_api_version'] =3D=3D=3D undefine= d ? 0 : response['js_api_version']; + console.log("Extension JS API Version: ", js_api_version); + u2f.sendSignRequest(appId, challenge, registeredKeys, callback, = opt_timeoutSeconds); + }); + } else { + // We know the JS API version. Send the actual sign request in the sup= ported API version. + u2f.sendSignRequest(appId, challenge, registeredKeys, callback, opt_ti= meoutSeconds); + } +}; + +/** + * Dispatches an array of sign requests to available U2F tokens. + * @param {string=3D} appId + * @param {string=3D} challenge + * @param {Array} registeredKeys + * @param {function((u2f.Error|u2f.SignResponse))} callback + * @param {number=3D} opt_timeoutSeconds + */ +u2f.sendSignRequest =3D function(appId, challenge, registeredKeys, callbac= k, opt_timeoutSeconds) { + u2f.getPortSingleton_(function(port) { + var reqId =3D ++u2f.reqCounter_; + u2f.callbackMap_[reqId] =3D callback; + var timeoutSeconds =3D (typeof opt_timeoutSeconds !=3D=3D 'undefined' ? + opt_timeoutSeconds : u2f.EXTENSION_TIMEOUT_SEC); + var req =3D u2f.formatSignRequest_(appId, challenge, registeredKeys, t= imeoutSeconds, reqId); + port.postMessage(req); + }); +}; + +/** + * Dispatches register requests to available U2F tokens. An array of sign + * requests identifies already registered tokens. + * If the JS API version supported by the extension is unknown, it first s= ends a + * message to the extension to find out the supported API version and then= it sends + * the register request. + * @param {string=3D} appId + * @param {Array} registerRequests + * @param {Array} registeredKeys + * @param {function((u2f.Error|u2f.RegisterResponse))} callback + * @param {number=3D} opt_timeoutSeconds + */ +u2f.register =3D function(appId, registerRequests, registeredKeys, callbac= k, opt_timeoutSeconds) { + if (js_api_version =3D=3D=3D undefined) { + // Send a message to get the extension to JS API version, then send th= e actual register request. + u2f.getApiVersion( + function (response) { + js_api_version =3D response['js_api_version'] =3D=3D=3D undefine= d ? 0: response['js_api_version']; + console.log("Extension JS API Version: ", js_api_version); + u2f.sendRegisterRequest(appId, registerRequests, registeredKeys, + callback, opt_timeoutSeconds); + }); + } else { + // We know the JS API version. Send the actual register request in the= supported API version. + u2f.sendRegisterRequest(appId, registerRequests, registeredKeys, + callback, opt_timeoutSeconds); + } +}; + +/** + * Dispatches register requests to available U2F tokens. An array of sign + * requests identifies already registered tokens. + * @param {string=3D} appId + * @param {Array} registerRequests + * @param {Array} registeredKeys + * @param {function((u2f.Error|u2f.RegisterResponse))} callback + * @param {number=3D} opt_timeoutSeconds + */ +u2f.sendRegisterRequest =3D function(appId, registerRequests, registeredKe= ys, callback, opt_timeoutSeconds) { + u2f.getPortSingleton_(function(port) { + var reqId =3D ++u2f.reqCounter_; + u2f.callbackMap_[reqId] =3D callback; + var timeoutSeconds =3D (typeof opt_timeoutSeconds !=3D=3D 'undefined' ? + opt_timeoutSeconds : u2f.EXTENSION_TIMEOUT_SEC); + var req =3D u2f.formatRegisterRequest_( + appId, registeredKeys, registerRequests, timeoutSeconds, reqId); + port.postMessage(req); + }); +}; + + +/** + * Dispatches a message to the extension to find out the supported + * JS API version. + * If the user is on a mobile phone and is thus using Google Authenticator= instead + * of the Chrome extension, don't send the request and simply return 0. + * @param {function((u2f.Error|u2f.GetJsApiVersionResponse))} callback + * @param {number=3D} opt_timeoutSeconds + */ +u2f.getApiVersion =3D function(callback, opt_timeoutSeconds) { + u2f.getPortSingleton_(function(port) { + // If we are using Android Google Authenticator or iOS client app, + // do not fire an intent to ask which JS API version to use. + if (port.getPortType) { + var apiVersion; + switch (port.getPortType()) { + case 'WrappedIosPort_': + case 'WrappedAuthenticatorPort_': + apiVersion =3D 1.1; + break; + + default: + apiVersion =3D 0; + break; + } + callback({ 'js_api_version': apiVersion }); + return; + } + var reqId =3D ++u2f.reqCounter_; + u2f.callbackMap_[reqId] =3D callback; + var req =3D { + type: u2f.MessageTypes.U2F_GET_API_VERSION_REQUEST, + timeoutSeconds: (typeof opt_timeoutSeconds !=3D=3D 'undefined' ? + opt_timeoutSeconds : u2f.EXTENSION_TIMEOUT_SEC), + requestId: reqId + }; + port.postMessage(req); + }); +}; --=20 2.30.2