forked from btoews/security-key
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathu2f_polyfill.js
61 lines (61 loc) · 1.85 KB
/
u2f_polyfill.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
(function(exports, global) {
var pingerPonger = {
pingPong: function() {
this.whenReady_ = [];
this.isReady_ = false;
this.send("ping");
this.receive("pong", this.isReady);
this.receive("ping", function() {
this.send("pong");
this.isReady();
});
},
receive: function(name, cb) {
window.addEventListener("u2f-" + name, function(e) {
cb.apply(this, e.detail);
}.bind(this));
},
send: function(name) {
var args = Array.from(arguments).slice(1);
window.dispatchEvent(new CustomEvent("u2f-" + name, {
detail: args
}));
},
whenReady: function(cb) {
if (this.isReady_) {
cb.apply(this);
} else {
this.whenReady_.push(cb);
}
},
isReady: function() {
this.isReady_ = true;
while (cb = this.whenReady_.shift()) {
cb.apply(this);
}
}
};
var u2fClient = function() {
this.rpcRequester("register");
this.rpcRequester("sign");
this.pingPong();
};
u2fClient.prototype = pingerPonger;
u2fClient.prototype.rpcRequester = function(name) {
this[name] = function() {
var args = Array.from(arguments);
args.unshift(name + "-request");
var cb = args.pop();
this.receive(name + "-response", cb);
this.whenReady(function() {
this.send.apply(this, args);
});
};
};
if (!global.u2f && !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform)) {
global.u2f = new u2fClient();
}
global[""] = exports;
})({}, function() {
return this;
}());