From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <w.bumiller@proxmox.com> 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 <pve-devel@lists.proxmox.com>; 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 <pve-devel@lists.proxmox.com>; 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 <pve-devel@lists.proxmox.com>; 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 <pve-devel@lists.proxmox.com>; Tue, 9 Nov 2021 12:27:32 +0100 (CET) From: Wolfgang Bumiller <w.bumiller@proxmox.com> 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 <pve-devel.lists.proxmox.com> List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe> List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/> List-Post: <mailto:pve-devel@lists.proxmox.com> List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help> List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe> X-List-Received-Date: Tue, 09 Nov 2021 11:27:54 -0000 copied from pve/pbs Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com> --- 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<a.length&&0=3D=3Da[c];)c++;this.num=3Dnew Array(a.length-c+b);for(v= ar d=3D0;d<a.length-c;d++)this.num[d]=3Da[d+c]}function j(a,b){this.totalCo= unt=3Da,this.dataCount=3Db}function k(){this.buffer=3D[],this.length=3D0}fu= nction m(){return"undefined"!=3Dtypeof CanvasRenderingContext2D}function n(= ){var a=3D!1,b=3Dnavigator.userAgent;return/android/i.test(b)&&(a=3D!0,aMat= =3Db.toString().match(/android ([0-9]\.[0-9])/i),aMat&&aMat[1]&&(a=3DparseF= loat(aMat[1]))),a}function r(a,b){for(var c=3D1,e=3Ds(a),f=3D0,g=3Dl.length= ;g>=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<this.moduleCount;d++){t= his.modules[d]=3Dnew Array(this.moduleCount);for(var e=3D0;e<this.moduleCou= nt;e++)this.modules[d][e]=3Dnull}this.setupPositionProbePattern(0,0),this.s= etupPositionProbePattern(this.moduleCount-7,0),this.setupPositionProbePatte= rn(0,this.moduleCount-7),this.setupPositionAdjustPattern(),this.setupTiming= Pattern(),this.setupTypeInfo(a,c),this.typeNumber>=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<this.modules.length;f++)for(va= r g=3Df*e,h=3D0;h<this.modules[f].length;h++){var i=3Dh*e,j=3Dthis.modules[= f][h];j&&(d.beginFill(0,100),d.moveTo(i,g),d.lineTo(i+e,g),d.lineTo(i+e,g+e= ),d.lineTo(i,g+e),d.endFill())}return d},setupTimingPattern:function(){for(= var a=3D8;a<this.moduleCount-8;a++)null=3D=3Dthis.modules[a][6]&&(this.modu= les[a][6]=3D0=3D=3Da%2);for(var b=3D8;b<this.moduleCount-8;b++)null=3D=3Dth= is.modules[6][b]&&(this.modules[6][b]=3D0=3D=3Db%2)},setupPositionAdjustPat= tern:function(){for(var a=3Df.getPatternPosition(this.typeNumber),b=3D0;b<a= .length;b++)for(var c=3D0;c<a.length;c++){var d=3Da[b],e=3Da[c];if(null=3D= =3Dthis.modules[d][e])for(var g=3D-2;2>=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<a.length&&(j=3D1=3D=3D(1&a= [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;h<d.length;h++){var i=3Dd[h];g.put= (i.mode,4),g.put(i.getLength(),f.getLengthInBits(i.mode,a)),i.write(g)}for(= var l=3D0,h=3D0;h<e.length;h++)l+=3De[h].dataCount;if(g.getLengthInBits()>8= *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<b.length;j++){var k=3Db[j].dataCoun= t,l=3Db[j].totalCount-k;d=3DMath.max(d,k),e=3DMath.max(e,l),g[j]=3Dnew Arra= y(k);for(var m=3D0;m<g[j].length;m++)g[j][m]=3D255&a.buffer[m+c];c+=3Dk;var= n=3Df.getErrorCorrectPolynomial(l),o=3Dnew i(g[j],n.getLength()-1),p=3Do.m= od(n);h[j]=3Dnew Array(n.getLength()-1);for(var m=3D0;m<h[j].length;m++){va= r q=3Dm+p.getLength()-h[j].length;h[j][m]=3Dq>=3D0?p.get(q):0}}for(var r=3D= 0,m=3D0;m<b.length;m++)r+=3Db[m].totalCount;for(var s=3Dnew Array(r),t=3D0,= m=3D0;d>m;m++)for(var j=3D0;j<b.length;j++)m<g[j].length&&(s[t++]=3Dg[j][m]= );for(var m=3D0;e>m;m++)for(var j=3D0;j<b.length;j++)m<h[j].length&&(s[t++]= =3Dh[j][m]);return s};for(var c=3D{MODE_NUMBER:1,MODE_ALPHA_NUM:2,MODE_8BIT= _BYTE:4,MODE_KANJI:8},d=3D{L:1,M:0,Q:3,H:2},e=3D{PATTERN000:0,PATTERN001:1,= PATTERN010:2,PATTERN011:3,PATTERN100:4,PATTERN101:5,PATTERN110:6,PATTERN111= :7},f=3D{PATTERN_POSITION_TABLE:[[],[6,18],[6,22],[6,26],[6,30],[6,34],[6,2= 2,38],[6,24,42],[6,26,46],[6,28,50],[6,30,54],[6,32,58],[6,34,62],[6,26,46,= 66],[6,26,48,70],[6,26,50,74],[6,30,54,78],[6,30,56,82],[6,30,58,86],[6,34,= 62,90],[6,28,50,72,94],[6,26,50,74,98],[6,30,54,78,102],[6,28,54,80,106],[6= ,32,58,84,110],[6,30,58,86,114],[6,34,62,90,118],[6,26,50,74,98,122],[6,30,= 54,78,102,126],[6,26,52,78,104,130],[6,30,56,82,108,134],[6,34,60,86,112,13= 8],[6,30,58,86,114,142],[6,34,62,90,118,146],[6,30,54,78,102,126,150],[6,24= ,50,76,102,128,154],[6,28,54,80,106,132,158],[6,32,58,84,110,136,162],[6,26= ,54,82,110,138,166],[6,30,58,86,114,142,170]],G15:1335,G18:7973,G15_MASK:21= 522,getBCHTypeInfo:function(a){for(var b=3Da<<10;f.getBCHDigit(b)-f.getBCHD= igit(f.G15)>=3D0;)b^=3Df.G15<<f.getBCHDigit(b)-f.getBCHDigit(f.G15);return(= a<<10|b)^f.G15_MASK},getBCHTypeNumber:function(a){for(var b=3Da<<12;f.getBC= HDigit(b)-f.getBCHDigit(f.G18)>=3D0;)b^=3Df.G18<<f.getBCHDigit(b)-f.getBCHD= igit(f.G18);return a<<12|b},getBCHDigit:function(a){for(var b=3D0;0!=3Da;)b= ++,a>>>=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;for(var h=3D8;256>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;c<this.getLength();c++)for(var d=3D0;d<a.getLength();d++)b[c+d]^= =3Dg.gexp(g.glog(this.get(c))+g.glog(a.get(d)));return new i(b,0)},mod:func= tion(a){if(this.getLength()-a.getLength()<0)return this;for(var b=3Dg.glog(= this.get(0))-g.glog(a.get(0)),c=3Dnew Array(this.getLength()),d=3D0;d<this.= getLength();d++)c[d]=3Dthis.get(d);for(var d=3D0;d<a.getLength();d++)c[d]^= =3Dg.gexp(g.glog(a.get(d))+b);return new i(c,0).mod(a)}},j.RS_BLOCK_TABLE= =3D[[1,26,19],[1,26,16],[1,26,13],[1,26,9],[1,44,34],[1,44,28],[1,44,22],[1= ,44,16],[1,70,55],[1,70,44],[2,35,17],[2,35,13],[1,100,80],[2,50,32],[2,50,= 24],[4,25,9],[1,134,108],[2,67,43],[2,33,15,2,34,16],[2,33,11,2,34,12],[2,8= 6,68],[4,43,27],[4,43,19],[4,43,15],[2,98,78],[4,49,31],[2,32,14,4,33,15],[= 4,39,13,1,40,14],[2,121,97],[2,60,38,2,61,39],[4,40,18,2,41,19],[4,40,14,2,= 41,15],[2,146,116],[3,58,36,2,59,37],[4,36,16,4,37,17],[4,36,12,4,37,13],[2= ,86,68,2,87,69],[4,69,43,1,70,44],[6,43,19,2,44,20],[6,43,15,2,44,16],[4,10= 1,81],[1,80,50,4,81,51],[4,50,22,4,51,23],[3,36,12,8,37,13],[2,116,92,2,117= ,93],[6,58,36,2,59,37],[4,46,20,6,47,21],[7,42,14,4,43,15],[4,133,107],[8,5= 9,37,1,60,38],[8,44,20,4,45,21],[12,33,11,4,34,12],[3,145,115,1,146,116],[4= ,64,40,5,65,41],[11,36,16,5,37,17],[11,36,12,5,37,13],[5,109,87,1,110,88],[= 5,65,41,5,66,42],[5,54,24,7,55,25],[11,36,12],[5,122,98,1,123,99],[7,73,45,= 3,74,46],[15,43,19,2,44,20],[3,45,15,13,46,16],[1,135,107,5,136,108],[10,74= ,46,1,75,47],[1,50,22,15,51,23],[2,42,14,17,43,15],[5,150,120,1,151,121],[9= ,69,43,4,70,44],[17,50,22,1,51,23],[2,42,14,19,43,15],[3,141,113,4,142,114]= ,[3,70,44,11,71,45],[17,47,21,4,48,22],[9,39,13,16,40,14],[3,135,107,5,136,= 108],[3,67,41,13,68,42],[15,54,24,5,55,25],[15,43,15,10,44,16],[4,144,116,4= ,145,117],[17,68,42],[17,50,22,6,51,23],[19,46,16,6,47,17],[2,139,111,7,140= ,112],[17,74,46],[7,54,24,16,55,25],[34,37,13],[4,151,121,5,152,122],[4,75,= 47,14,76,48],[11,54,24,14,55,25],[16,45,15,14,46,16],[6,147,117,4,148,118],= [6,73,45,14,74,46],[11,54,24,16,55,25],[30,46,16,2,47,17],[8,132,106,4,133,= 107],[8,75,47,13,76,48],[7,54,24,22,55,25],[22,45,15,13,46,16],[10,142,114,= 2,143,115],[19,74,46,4,75,47],[28,50,22,6,51,23],[33,46,16,4,47,17],[8,152,= 122,4,153,123],[22,73,45,3,74,46],[8,53,23,26,54,24],[12,45,15,28,46,16],[3= ,147,117,10,148,118],[3,73,45,23,74,46],[4,54,24,31,55,25],[11,45,15,31,46,= 16],[7,146,116,7,147,117],[21,73,45,7,74,46],[1,53,23,37,54,24],[19,45,15,2= 6,46,16],[5,145,115,10,146,116],[19,75,47,10,76,48],[15,54,24,25,55,25],[23= ,45,15,25,46,16],[13,145,115,3,146,116],[2,74,46,29,75,47],[42,54,24,1,55,2= 5],[23,45,15,28,46,16],[17,145,115],[10,74,46,23,75,47],[10,54,24,35,55,25]= ,[19,45,15,35,46,16],[17,145,115,1,146,116],[14,74,46,21,75,47],[29,54,24,1= 9,55,25],[11,45,15,46,46,16],[13,145,115,6,146,116],[14,74,46,23,75,47],[44= ,54,24,7,55,25],[59,46,16,1,47,17],[12,151,121,7,152,122],[12,75,47,26,76,4= 8],[39,54,24,14,55,25],[22,45,15,41,46,16],[6,151,121,14,152,122],[6,75,47,= 34,76,48],[46,54,24,10,55,25],[2,45,15,64,46,16],[17,152,122,4,153,123],[29= ,74,46,14,75,47],[49,54,24,10,55,25],[24,45,15,46,46,16],[4,152,122,18,153,= 123],[13,74,46,32,75,47],[48,54,24,14,55,25],[42,45,15,32,46,16],[20,147,11= 7,4,148,118],[40,75,47,7,76,48],[43,54,24,22,55,25],[10,45,15,67,46,16],[19= ,148,118,6,149,119],[18,75,47,31,76,48],[34,54,24,34,55,25],[20,45,15,61,46= ,16]],j.getRSBlocks=3Dfunction(a,b){var c=3Dj.getRsBlockTable(a,b);if(void = 0=3D=3Dc)throw new Error("bad rs block @ typeNumber:"+a+"/errorCorrectLevel= :"+b);for(var d=3Dc.length/3,e=3D[],f=3D0;d>f;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['= <table style=3D"border:0;border-collapse:collapse;">'],h=3D0;d>h;h++){g.pus= h("<tr>");for(var i=3D0;d>i;i++)g.push('<td style=3D"border:0;border-collap= se:collapse;padding:0;margin:0;width:'+e+"px;height:"+f+"px;background-colo= r:"+(a.isDark(h,i)?b.colorDark:b.colorLight)+';"></td>');g.push("</tr>")}g.= push("</table>"),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.Transport>} + */ +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<u2f.SignRequest>} 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<u2f.SignRequest>} signRequests + * @param {Array<u2f.RegisterRequest>} 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<function((MessagePort|u2f.WrappedChromeRuntimePort_))>} + * @private + */ +u2f.waitingForPort_ =3D []; + +/** + * A counter for requestIds. + * @type {number} + * @private + */ +u2f.reqCounter_ =3D 0; + +/** + * A map from requestIds to client callbacks + * @type {Object.<number,(function((u2f.Error|u2f.RegisterResponse)) + * |function((u2f.Error|u2f.SignResponse)))>} + * @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.<u2f.Response>} 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<u2f.RegisteredKey>} 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<u2f.RegisteredKey>} 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<u2f.RegisterRequest>} registerRequests + * @param {Array<u2f.RegisteredKey>} 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<u2f.RegisterRequest>} registerRequests + * @param {Array<u2f.RegisteredKey>} 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