-
Notifications
You must be signed in to change notification settings - Fork 2
/
soft-authn.js
1186 lines (1045 loc) · 43 KB
/
soft-authn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SECURITY TODO: make sure this meets the requirements for secure context
// for browsers that don't support secure context
if (window.isSecureContext === undefined && location.origin.match(/:\/\/localhost|https:\/\//g)) {
window.isSecureContext = true;
}
// for safari's version of webCrypto
if (window.crypto && !window.crypto.subtle && window.crypto.webkitSubtle) {
window.crypto.subtle = window.crypto.webkitSubtle;
}
// IIFE for clean namespace
(function() {
if (navigator.authentication === undefined) {
console.log("WebAuthn API not found, can't load authenticator.");
return;
}
if (navigator.authentication.fidoAuthenticator === undefined ||
navigator.authentication.addAuthenticator === undefined) {
console.log("Unsupported version of WebAuthn API in use, can't load authenticator");
return;
}
/**
* The main class for the soft authenticator
*/
class softAuthn extends navigator.authentication.fidoAuthenticator {
constructor(opt) {
super(opt);
if (window.crypto.subtle === undefined) {
throw new Error("Creating authenticator: window.crypto.subtle not found");
}
if (window.indexedDB === undefined) {
throw new Error("Creating authenticator: window.indexedDB not found");
}
// TODO: manage / verify options
// -- crypto params
// -- authn type (PIN, pop-up, none, etc.)
// -- attestation type
this.name = "softAuthenticator";
this.preferredCrypto = "RSASSA-PKCS1-v1_5";
this.cryptoBits = 2048;
this.dbName = "scoped-cred-store";
this.dbTableName = "creds";
this.debug = 0;
this.confirmType = "none"; // TODO: shouldn't be on the object
// TODO: debug should be private and static to strip out some of these options in minified code?
if (this.debug) {
console.log("IN DEBUG MODE");
this.confirmType = "none";
console.log("Deleting db:", this.dbName);
// _dbDelete.call(this);
var f = _dbDelete.bind(this);
f();
}
}
// TODO: credentialType arg (ScopedCred)
// TODO: list of Credentials arg, that are already known so that new creds. aren't needlessly created
// TODO: attestationChallenge arg
authenticatorMakeCredential(rpIdHash, account, clientDataHash, scopedCredentialType, blacklist, extensions) {
return new Promise((resolve, reject) => { // TODO: just reurn the inner promise
console.log("!!! MAKE CREDENTIAL");
console.log("RP ID Hash:", rpIdHash);
console.log("account", account);
console.log("clientDataHash", clientDataHash);
console.log("scopedCredentialType:", scopedCredentialType);
console.log("blacklist:", blacklist);
console.log("extensions:", extensions);
// TODO: verify arguments
if (!(rpIdHash instanceof ArrayBuffer)) {
throw new TypeError("authenticatorMakeCredential expected rpIdHash to be ArrayBuffer");
}
console.log(account);
if (typeof account !== "object" ||
typeof account.rpDisplayName !== "string" ||
typeof account.displayName !== "string" ||
typeof account.id !== "string") {
throw new TypeError("authenticatorMakeCredential expected 'account' to be object containing rpDisplayName, displayName, and id");
}
console.log("clientDataHash", clientDataHash);
if (!(clientDataHash instanceof ArrayBuffer)) {
throw new TypeError("authenticatorMakeCredential expected clientDataHash to be ArrayBuffer");
}
if (typeof scopedCredentialType !== "string") {
throw new TypeError("authenticatorMakeCredential expected scopedCredentialType to be string");
}
// TODO: process extension data
var credObj, webAuthnAttestation;
// prompt for user permission
return _userConfirmation.call(this, "Would you like to create an new account?", account.rpDisplayName, account.displayName)
.then((confirm) => {
// create a credential
return _createCredential();
})
.then((co) => {
// save credential object
credObj = co;
console.log("created credential:", credObj);
// create attestation
return _createPackedAttestation(rpIdHash, clientDataHash, credObj, {
attestation: true
});
})
.then((a) => {
// save attestation
webAuthnAttestation = a;
return _dbInit.call(this);
})
.then(() => {
// store credential ID and RP ID for future use
return _dbCredCreate.call(this, account, rpIdHash, credObj);
})
// TODO: _dbClose()?
.then(() => {
var ret = {
credential: {
type: "ScopedCred",
id: credObj.id
},
attestation: webAuthnAttestation
};
// resolve with credential, publicKey, rawAttestation = { attestation.type, attestation.statement, attestation.clientData }
return resolve(ret);
});
});
}
authenticatorGetAssertion(rpIdHash, clientDataHash, whitelist, extensions) {
return new Promise((resolve, reject) => {
console.log("authenticatorGetAssertion");
// verify arguments
if (typeof rpId === "string") {
throw new TypeError("authenticatorGetAssertion expected rpId to be string");
}
if (!(clientDataHash instanceof ArrayBuffer)) {
throw new TypeError("authenticatorGetAssertion expected clientDataHash to be ArrayBuffer");
}
// TODO: process whitelist
// TODO: process extensions
// lookup credentials by RP ID
var selectedCred, authnrData;
return _dbInit.call(this)
.then((db) => {
return _dbCredLookup(rpIdHash);
})
.then((co) => {
// prompt for user permission
// TODO: filter found credentials by whitelist
// TODO: _userConfirmation should allow user to pick from an array of accounts
selectedCred = co;
console.log("Using credential:", selectedCred);
return _userConfirmation.call(this, "Would you like to login to this account?", selectedCred.rpName, selectedCred.userName);
})
.then((confirm) => {
return _createAuthenticatorData(rpIdHash, selectedCred);
})
.then((ad) => {
authnrData = ad;
return _createSignature(selectedCred.keyPair, authnrData, clientDataHash);
})
.then((signature) => {
// resolve with credential, authenticatorData, signature
console.log("Signature length:", signature.byteLength);
var ret = {
credential: {
type: "ScopedCred",
id: selectedCred.id,
},
authenticatorData: authnrData,
signature: signature
};
console.log("All done", ret);
return resolve(ret);
})
.catch((err) => {
console.log("error in authenticatorGetAssertion:", err);
return reject(err);
});
});
}
/**
* Cancels any pending actions
* @return {Promise} Promise that resolves to an Error with the message "Not Implemented"
* @todo Not Implemented
*/
authenticatorCancel() {
// not sure how to handle this... maybe throw? set flag and check above?
return Promise.reject(new Error("Not Implemented"));
}
}
// extend the authenticator object
softAuthn.prototype = new navigator.authentication.fidoAuthenticator();
function _dbDelete() {
if (this.dbName === undefined) {
throw new Error("Trying to delete undefined database");
}
var deleteRequest = window.indexedDB.deleteDatabase(this.dbName);
deleteRequest.onerror = function(e) {
console.log("Error deleting database");
};
deleteRequest.onsuccess = function(e) {
console.log("Database successfully deleted:", this.dbName);
}.bind(this);
}
// private variable for credential database
var _credDb = null;
function _dbInit() {
if (_credDb) {
return Promise.resolve(_credDb);
}
if (this.dbName === undefined) {
console.log("dbName not found:", this.dbName);
throw new Error("Trying to init database, but no name found");
}
return new Promise((resolve, reject) => {
// create IndexedDatabase for storing Cred IDs / RPIDs?
var request = indexedDB.open(this.dbName);
request.onupgradeneeded = function() {
console.log("Creating database...");
var db = request.result;
var store = db.createObjectStore("creds", {
keyPath: "id"
});
var idIdx = store.createIndex("by_rpId", "rpId", {
unique: false
});
};
request.onsuccess = function() {
console.log("Database created!");
_credDb = request.result;
return resolve(_credDb);
};
request.onerror = function() {
return reject(new Error("Couldn't initialize DB"));
};
});
}
// TODO: lookup is supposed to be by RP ID ("example.com")
// but databases are stored by origin ("https://subdomain.example.com:443")
// so I'm not sure that this is going to work as expected if there
// are multiple ports or subdomains that the credentials are supposed to work with
function _dbCredLookup(rpIdHash) {
return new Promise(function(resolve, reject) {
var db = _credDb;
var tx = db.transaction("creds", "readonly");
var store = tx.objectStore("creds");
var index = store.index("by_rpId", "rpId", {
unique: false
});
var rpId = _buf2hex(rpIdHash);
var request = index.get(rpId);
request.onsuccess = function() {
var matching = request.result;
if (matching !== undefined) {
console.log("Found match:", matching);
matching.id = matching.idBuf; // convert credential.id back to an ArrayBuffer
return resolve(matching);
} else {
console.log("No match found.");
return reject(new Error("Credential not found"));
}
};
});
}
function _dbCredCreate(account, rpIdHash, credObj) {
return new Promise((resolve, reject) => {
var db = _credDb;
var tx = db.transaction(this.dbTableName, "readwrite");
var store = tx.objectStore(this.dbTableName);
// TODO: create credential ID here
var newCred = {
rpName: account.rpDisplayName,
userName: account.displayName,
accountName: account.name,
accountId: account.id,
imageURL: account.imageURL,
rpId: _buf2hex(rpIdHash),
rpIdHash: rpIdHash,
id: _buf2hex(credObj.id),
idBuf: credObj.id,
credCbor: credObj.cbor,
credJwk: credObj.jwk,
// TODO: Safari can't save keyPairs
keyPair: credObj.keyPair,
counter: 0
};
// console.log("New Credential ID is:", _buf2hex(credObj.id));
// console.log("Saving New Credential:", newCred);
store.put(newCred);
tx.oncomplete = function() {
return resolve(true);
};
tx.onerror = function(e) {
console.log("ERROR");
console.log(e);
return reject(new Error("Couldn't create credential"));
};
});
}
function _userConfirmation(msg, rpDisplayName, displayName) {
return new Promise((resolve, reject) => {
console.log("Confirmation Type:", this.confirmType);
switch (this.confirmType) {
case "ok":
var result = confirm(msg + "\n" +
"Service: " + rpDisplayName + "\n" +
// "Website: " + rpId + "\n" +
"Account: " + displayName + "\n"
);
if (result === true) {
return resolve(true);
} else {
return reject(new Error("User declined confirmation"));
}
break;
case "none":
return resolve(true);
default:
return reject(new Error("Unknown User Confirmation Type:", this.confirmType));
}
});
}
/**
* Creates a random credential ID
* @param {Number} len The length of the credential ID to create (default: 32)
* @return {ArrayBuffer} Credential ID as an ArrayBuffer
* @private
*/
function createCredentialId(len) {
// TODO: length should be a parameter
// default length is 32 bytes
len = len || 32;
var ret = new Uint8Array(len);
window.crypto.getRandomValues(ret);
return ret.buffer;
}
/**
* Converts a credential object to a CBOR representation of the credential
* @param {String} alg The JWK crypto algorithm for the credential. Currently only "RS256" is supported.
* @param {Object} credObj The credential object to be converted to CBOR
* @return {ArrayBuffer} The CBOR representation of the credential
* @private
*/
function credentialToCbor(alg, credObj) {
switch (alg) {
case "RS256":
return CBOR.encode({
alg: alg,
n: new Uint8Array(credObj.n),
e: new Uint8Array(credObj.e)
});
default:
throw new TypeError(`${alg} not supported in credentialToCbor()`);
}
}
/**
* Fixes up Safari's JWK export, which is an ArrayBuffer
* @param {ArrayBuffer} jwk An ArrayBuffer containing a UTF-8 JSON Web Key (JWK)
* @return {Object} The Object represention of the JWK
*/
function jwkAb2Obj(jwk) {
if (!(jwk instanceof ArrayBuffer)) return jwk;
// printHex ("jwk", jwkPk);
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
return JSON.parse(ab2str(jwk));
}
/**
* Creates a key pair and returns them along with all the information needed for a credential
* @return {Object} A Promise that resolves to a credential object with n, e, jwk, and keyPair attributes
* @private
*/
function _createCredential(opts) {
// TODO:
// opts
// idLen: length of credential (default: 32)
// cryptoType: type of credential (default: ECDSA)
// crytpoBits: number bits to be used for crypto keys (default: 256)
var keyPair;
var credId = createCredentialId();
// generate a credential key pair
return window.crypto.subtle.generateKey({
// TODO: should be options for crypto, bits, hash, etc.
name: "RSASSA-PKCS1-v1_5",
modulusLength: 2048, //can be 1024, 2048, or 4096
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {
name: "SHA-256" //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
},
},
false, ["sign", "verify"]
).then((keys) => {
// export public key
keyPair = keys;
return window.crypto.subtle.exportKey("jwk", keyPair.publicKey);
}).then((jwkPk) => { // grab the right values from the JWK and return them
jwkPk = jwkAb2Obj(jwkPk);
console.log(jwkPk);
var ret = {
n: b64decode(jwkPk.n),
e: b64decode(jwkPk.e),
jwk: jwkPk,
keyPair: keyPair,
id: credId,
idLen: credId.byteLength,
};
// convert the JWK to CBOR
console.log("x");
ret.cbor = credentialToCbor(jwkPk.alg, ret);
ret.cborLen = ret.cbor.byteLength;
console.log("cred obj:", ret);
return ret;
});
}
function createAttestationData(credObj, opts) {
opts = opts || {};
if (typeof credObj !== "object") {
throw new TypeError("createAttestationData: expected credObj to be object");
}
// TODO: real AAGUID
var myAaguid = [0xF1, 0xD0, 0xF1, 0xD0, 0xF1, 0xD0, 0xF1, 0xD0, 0xF1, 0xD0, 0xF1, 0xD0, 0xF1, 0xD0, 0xF1, 0xD0];
// create a new dataview for the attestation
var attestDataBuf = new ArrayBuffer(
16 + // AAGUID
2 + // Cred ID Len
credObj.idLen + // Credential ID
credObj.cborLen // Credential in CBOR Format
);
var attestData = new DataView(attestDataBuf);
var offset = 0;
// append AAGUID
for (let i = 0; i < 16; i++) {
attestData.setUint8(i, myAaguid[i]);
}
offset += 16;
// set credential ID length
attestData.setUint16(offset, credObj.idLen);
offset += 2;
// append credential ID
var id = new Uint8Array(credObj.id);
for (let i = 0; i < credObj.idLen; i++) {
attestData.setUint8(i + offset, id[i]);
}
offset += credObj.idLen;
// append credential in CBOR format
var cbor = new Uint8Array(credObj.cbor);
for (let i = 0; i < credObj.cborLen; i++) {
attestData.setUint8(i + offset, cbor[i]);
}
return attestDataBuf;
}
function _createAuthenticatorData(rpIdHash, credObj, opts) {
opts = opts || {};
var counter = 1; // TODO: manage counter
var attData;
return Promise.resolve(true) // get TUP -- TODO
.then((tup) => { // get attestation data, if required
if (tup !== true) {
throw new Error("TUP rejected");
}
if (opts.attestation) return createAttestationData(credObj);
else return new ArrayBuffer(0);
})
.then((ad) => { // get extension data, if required
attData = ad;
if (opts.extensions) throw new Error("extensions not supported in createAuthenticatorData");
else return new ArrayBuffer(0);
})
.then((extData) => { // create the authenticator data
var hasAd = (attData.byteLength > 0);
var hasEd = (extData.byteLength > 0);
// create authenticator data
var authnrDataBuf = new ArrayBuffer(
32 + // RPID Hash
1 + // Flags
4 + // Signature Counter
attData.byteLength + // attestation data
extData.byteLength // extensions
);
var authnrData = new DataView(authnrDataBuf);
var offset = 0;
// copy RPID
var rpid = new Uint8Array(rpIdHash);
for (let i = 0; i < 32; i++) {
authnrData.setUint8(i + offset, rpid[i]);
}
offset += 32;
// set flags
var flags = 0;
flags |= 0x01; // TUP flag
if (hasAd) flags |= 0x40; // AT flag
if (hasEd) flags |= 0x80; // ED flag
authnrData.setUint8(offset, flags);
offset++;
// set counter
authnrData.setUint32(offset, counter, false);
offset += 4;
if (hasAd) {
var ad = new Uint8Array(attData);
for (let i = 0; i < attData.byteLength; i++) {
authnrData.setUint8(offset + i, ad[i]);
}
offset += attData.byteLength;
}
return authnrDataBuf;
});
}
function _createSignature(keyPair, authnrData, clientDataHash) {
// for future reference...
// window.RSASSA = {
// name: "RSASSA-PKCS1-v1_5",
// modulusLength: 2048,
// publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
// hash: {name: "SHA-256"},
// }
// printHex("authnrData", authnrData);
// printHex("clientDataHash", clientDataHash);
var sigDataBuf = new ArrayBuffer(
authnrData.byteLength +
clientDataHash.byteLength
);
var sigData = new Uint8Array(sigDataBuf);
// copy authenticator data
var ad = new Uint8Array(authnrData);
for (let i = 0; i < authnrData.byteLength; i++) {
sigData[i] = ad[i];
}
// copy client data hash
var cd = new Uint8Array(clientDataHash);
var offset = authnrData.byteLength;
for (let i = 0; i < clientDataHash.byteLength; i++) {
sigData[offset + i] = cd[i];
}
// printHex("sigData", sigData);
// sign over the combination of authenticator and client data,
// and return the Promise that will resolve to the result
return window.crypto.subtle.sign({
name: "RSASSA-PKCS1-v1_5",
hash: {
name: "SHA-256"// Safari requires naming the hash...
}
},
keyPair.privateKey,
sigDataBuf
).then((sig) => {
// printHex("sig", sig);
return sig;
});
}
function _createPackedAttestation(rpIdHash, clientDataHash, credObj, opts) {
var authnrData;
console.log(credObj);
printHex("client data hash", clientDataHash);
// create authenticator data, create a signature, then form a packed attestation
var opts = {
attestation: true,
extensions: false
};
return _createAuthenticatorData(rpIdHash, credObj, opts)
.then((ad) => {
authnrData = ad;
var p = _createSignature(credObj.keyPair, authnrData, clientDataHash);
assert.instanceOf(p, Promise);
return p;
})
.then((sig) => {
var attestationObject = {
fmt: "packed",
authData: new Uint8Array(authnrData),
attStmt: {
alg: credObj.jwk.alg,
sig: new Uint8Array(sig)
}
};
console.log("doing CBOR encode of attestation:", attestationObject);
return CBOR.encode(attestationObject);
})
.then((cbor) => {
return {
format: "packed",
attestation: cbor,
authenticatorData: authnrData
};
});
}
function _buf2hex(buffer) { // buffer is an ArrayBuffer
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}
// borrowed from:
// https://github.com/niklasvh/base64-arraybuffer/blob/master/lib/base64-arraybuffer.js
// modified to base64url by Yuriy :)
/*
* base64-arraybuffer
* https://github.com/niklasvh/base64-arraybuffer
*
* Copyright (c) 2012 Niklas von Hertzen
* Licensed under the MIT license.
*/
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
// Use a lookup table to find the index.
var lookup = new Uint8Array(256);
for (var i = 0; i < chars.length; i++) {
lookup[chars.charCodeAt(i)] = i;
}
function b64decode(base64) {
var bufferLength = base64.length * 0.75,
len = base64.length,
i, p = 0,
encoded1, encoded2, encoded3, encoded4;
if (base64[base64.length - 1] === "=") {
bufferLength--;
if (base64[base64.length - 2] === "=") {
bufferLength--;
}
}
var arraybuffer = new ArrayBuffer(bufferLength),
bytes = new Uint8Array(arraybuffer);
for (i = 0; i < len; i += 4) {
encoded1 = lookup[base64.charCodeAt(i)];
encoded2 = lookup[base64.charCodeAt(i + 1)];
encoded3 = lookup[base64.charCodeAt(i + 2)];
encoded4 = lookup[base64.charCodeAt(i + 3)];
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
}
return arraybuffer;
}
console.log("Loading soft authn...");
navigator.authentication.addAuthenticator(new softAuthn());
})();
// borrowed from: https://github.com/paroga/cbor-js
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2016 Patrick Gansterer <paroga@paroga.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
(function(global, undefined) {
"use strict";
var POW_2_24 = 5.960464477539063e-8,
POW_2_32 = 4294967296,
POW_2_53 = 9007199254740992;
function encode(value) {
var data = new ArrayBuffer(256);
var dataView = new DataView(data);
var lastLength;
var offset = 0;
function prepareWrite(length) {
var newByteLength = data.byteLength;
var requiredLength = offset + length;
while (newByteLength < requiredLength)
newByteLength <<= 1;
if (newByteLength !== data.byteLength) {
var oldDataView = dataView;
data = new ArrayBuffer(newByteLength);
dataView = new DataView(data);
var uint32count = (offset + 3) >> 2;
for (var i = 0; i < uint32count; ++i)
dataView.setUint32(i << 2, oldDataView.getUint32(i << 2));
}
lastLength = length;
return dataView;
}
function commitWrite() {
offset += lastLength;
}
function writeFloat64(value) {
commitWrite(prepareWrite(8).setFloat64(offset, value));
}
function writeUint8(value) {
commitWrite(prepareWrite(1).setUint8(offset, value));
}
function writeUint8Array(value) {
var dataView = prepareWrite(value.length);
for (var i = 0; i < value.length; ++i)
dataView.setUint8(offset + i, value[i]);
commitWrite();
}
function writeUint16(value) {
commitWrite(prepareWrite(2).setUint16(offset, value));
}
function writeUint32(value) {
commitWrite(prepareWrite(4).setUint32(offset, value));
}
function writeUint64(value) {
var low = value % POW_2_32;
var high = (value - low) / POW_2_32;
var dataView = prepareWrite(8);
dataView.setUint32(offset, high);
dataView.setUint32(offset + 4, low);
commitWrite();
}
function writeTypeAndLength(type, length) {
if (length < 24) {
writeUint8(type << 5 | length);
} else if (length < 0x100) {
writeUint8(type << 5 | 24);
writeUint8(length);
} else if (length < 0x10000) {
writeUint8(type << 5 | 25);
writeUint16(length);
} else if (length < 0x100000000) {
writeUint8(type << 5 | 26);
writeUint32(length);
} else {
writeUint8(type << 5 | 27);
writeUint64(length);
}
}
function encodeItem(value) {
var i;
if (value === false)
return writeUint8(0xf4);
if (value === true)
return writeUint8(0xf5);
if (value === null)
return writeUint8(0xf6);
if (value === undefined)
return writeUint8(0xf7);
switch (typeof value) {
case "number":
if (Math.floor(value) === value) {
if (0 <= value && value <= POW_2_53)
return writeTypeAndLength(0, value);
if (-POW_2_53 <= value && value < 0)
return writeTypeAndLength(1, -(value + 1));
}
writeUint8(0xfb);
return writeFloat64(value);
case "string":
var utf8data = [];
for (i = 0; i < value.length; ++i) {
var charCode = value.charCodeAt(i);
if (charCode < 0x80) {
utf8data.push(charCode);
} else if (charCode < 0x800) {
utf8data.push(0xc0 | charCode >> 6);
utf8data.push(0x80 | charCode & 0x3f);
} else if (charCode < 0xd800) {
utf8data.push(0xe0 | charCode >> 12);
utf8data.push(0x80 | (charCode >> 6) & 0x3f);
utf8data.push(0x80 | charCode & 0x3f);
} else {
charCode = (charCode & 0x3ff) << 10;
charCode |= value.charCodeAt(++i) & 0x3ff;
charCode += 0x10000;
utf8data.push(0xf0 | charCode >> 18);
utf8data.push(0x80 | (charCode >> 12) & 0x3f);
utf8data.push(0x80 | (charCode >> 6) & 0x3f);
utf8data.push(0x80 | charCode & 0x3f);
}
}
writeTypeAndLength(3, utf8data.length);
return writeUint8Array(utf8data);
default:
var length;
if (Array.isArray(value)) {
length = value.length;
writeTypeAndLength(4, length);
for (i = 0; i < length; ++i)
encodeItem(value[i]);
} else if (value instanceof Uint8Array) {
writeTypeAndLength(2, value.length);
writeUint8Array(value);
} else {
var keys = Object.keys(value);
length = keys.length;
writeTypeAndLength(5, length);
for (i = 0; i < length; ++i) {
var key = keys[i];
encodeItem(key);
encodeItem(value[key]);
}
}
}
}
encodeItem(value);
if ("slice" in data)
return data.slice(0, offset);
var ret = new ArrayBuffer(offset);
var retView = new DataView(ret);
for (var i = 0; i < offset; ++i)
retView.setUint8(i, dataView.getUint8(i));
return ret;
}
function decode(data, tagger, simpleValue) {
var dataView = new DataView(data);
var offset = 0;
if (typeof tagger !== "function")
tagger = function(value) {
return value;
};
if (typeof simpleValue !== "function")
simpleValue = function() {
return undefined;
};
function commitRead(length, value) {
offset += length;
return value;
}
function readArrayBuffer(length) {
return commitRead(length, new Uint8Array(data, offset, length));
}
function readFloat16() {
var tempArrayBuffer = new ArrayBuffer(4);
var tempDataView = new DataView(tempArrayBuffer);
var value = readUint16();
var sign = value & 0x8000;
var exponent = value & 0x7c00;
var fraction = value & 0x03ff;
if (exponent === 0x7c00)
exponent = 0xff << 10;
else if (exponent !== 0)
exponent += (127 - 15) << 10;
else if (fraction !== 0)
return (sign ? -1 : 1) * fraction * POW_2_24;
tempDataView.setUint32(0, sign << 16 | exponent << 13 | fraction << 13);
return tempDataView.getFloat32(0);
}
function readFloat32() {
return commitRead(4, dataView.getFloat32(offset));
}
function readFloat64() {
return commitRead(8, dataView.getFloat64(offset));
}
function readUint8() {
return commitRead(1, dataView.getUint8(offset));
}
function readUint16() {
return commitRead(2, dataView.getUint16(offset));
}
function readUint32() {
return commitRead(4, dataView.getUint32(offset));
}
function readUint64() {
return readUint32() * POW_2_32 + readUint32();
}
function readBreak() {
if (dataView.getUint8(offset) !== 0xff)
return false;
offset += 1;
return true;
}
function readLength(additionalInformation) {
if (additionalInformation < 24)
return additionalInformation;
if (additionalInformation === 24)
return readUint8();
if (additionalInformation === 25)
return readUint16();
if (additionalInformation === 26)
return readUint32();
if (additionalInformation === 27)
return readUint64();
if (additionalInformation === 31)
return -1;
throw "Invalid length encoding";
}