-
Notifications
You must be signed in to change notification settings - Fork 18
/
api.js
301 lines (265 loc) · 10.9 KB
/
api.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
/**
* U2F JavaScript API
*
* Method stubs based on Google's U2F Chrome Extension
* https://github.com/google/u2f-ref-code/blob/master/u2f-chrome-extension/
*/
(function () {
'use strict';
/**
* ID of the extension to talk to
* @const
* @type {string}
*/
var EXTENSION_ID = "iomeponhhjanajlkbdjnfjicdhcfjbmd";
/**
* Default time out in seconds
* @const
* @type {number}
*/
var TIMEOUT = 60;
/**
* Namespace for the U2F api.
* @type {*|Window.u2f|{}}
*/
window.u2f = window.u2f || {};
/**
* Message types for messsages to/from the extension
* @const
* @enum {string}
*/
u2f.MessageTypes = {
'U2F_REGISTER_REQUEST': 'u2f_register_request',
'U2F_SIGN_REQUEST': 'u2f_sign_request',
'U2F_REGISTER_RESPONSE': 'u2f_register_response',
'U2F_SIGN_RESPONSE': 'u2f_sign_response'
};
/**
* Response status codes
* @const
* @enum {number}
*/
u2f.ErrorCodes = {
'OK': 0,
'OTHER_ERROR': 1,
'BAD_REQUEST': 2,
'CONFIGURATION_UNSUPPORTED': 3,
'DEVICE_INELIGIBLE': 4,
'TIMEOUT': 5
};
/**
* A message type for registration requests
* @typedef {{type: u2f.MessageTypes, signRequests: Array.<u2f.SignRequest>, registerRequests: ?Array.<u2f.RegisterRequest>, timeoutSeconds: ?number, requestId: ?number}}
*/
u2f.Request;
/**
* A message for registration responses
* @typedef {{type: u2f.MessageTypes, responseData: (u2f.Error | u2f.RegisterResponse | u2f.SignResponse), requestId: ?number}}
*/
u2f.Response;
/**
* An error object for responses
* @typedef {{errorCode: u2f.ErrorCodes, errorMessage: ?string}}
*/
u2f.Error;
/**
* 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, appId: string}}
*/
u2f.RegisterRequest;
/**
* Data object for a registration response.
* @typedef {{registrationData: string, clientData: string}}
*/
u2f.RegisterResponse;
// High-level JS API
/**
* Dispatches an array of sign requests to available U2F tokens.
* @param {Array.<u2f.SignRequest>} signRequests
* @param {function((u2f.Error|u2f.SignResponse))} callback
* @param {number=} opt_timeoutSeconds
*/
u2f.sign = function(signRequests, callback, opt_timeoutSeconds) {
if (typeof signRequests === 'undefined' || typeof callback === 'undefined') {
throw new Error("Not all mandatory parameters provided");
}
/**
* Whether the call has been answered yet. Either by response or by timeout message.
* @type {boolean}
*/
var answered = false;
if (!originAllowed(signRequests)) {
//throw new Error("Origin not allowed");
console.warn("Origin host does not match app_id host.");
}
/**
* Milliseconds to pass before a request times out
* @type {number}
*/
var millis = (typeof opt_timeoutSeconds !== 'undefined' ? (opt_timeoutSeconds * 1000) : (u2f.EXTENSION_TIMEOUT_SEC *1000));
/**
* @type {{type: (u2f.MessageTypes.U2F_REGISTER_REQUEST|u2f.MessageTypes.U2F_SIGN_REQUEST), signRequests: Array.<u2f.SignRequest>, registerRequests: Array.<u2f.RegisterRequest>, timeoutSeconds: number}}
*/
var req = {
type: u2f.MessageTypes.U2F_SIGN_REQUEST,
signRequests: transformRequestChallenge(signRequests, u2f.MessageTypes.U2F_SIGN_REQUEST),
timeout : millis
};
var timeout = setTimeout(function () {
if (!answered) {
answered = true;
callback({
errorCode: u2f.ErrorCodes.TIMEOUT,
errorMessage: "Request timed out"
});
}
}, millis);
chrome.runtime.sendMessage(EXTENSION_ID, req, function (response) {
if (!answered) {
answered = true;
callback(response);
}
});
};
/**
* Dispatches register requests to available U2F tokens. An array of sign
* requests identifies already registered tokens.
* @param {Array.<u2f.RegisterRequest>} registerRequests
* @param {Array.<u2f.SignRequest>} signRequests
* @param {function((u2f.Error|u2f.RegisterResponse))} callback
* @param {number=} opt_timeoutSeconds
*/
u2f.register = function (registerRequests, signRequests, callback, opt_timeoutSeconds) {
if (typeof registerRequests === 'undefined' || typeof signRequests === 'undefined' || typeof callback === 'undefined') {
throw new Error("Not all mandatory parameters provided");
}
/**
* Whether the call has been answered yet. Either by response or by timeout message.
* @type {boolean}
*/
var answered = false;
if (!originAllowed(registerRequests) || !originAllowed(signRequests)) {
//throw new Error("Origin not allowed");
console.warn("Origin host does not match app_id host.");
}
/**
* Milliseconds to pass before a request times out
* @type {number}
*/
var millis = (typeof opt_timeoutSeconds !== 'undefined' ? (opt_timeoutSeconds * 1000) : (u2f.EXTENSION_TIMEOUT_SEC *1000));
/**
* @type {{type: (u2f.MessageTypes.U2F_REGISTER_REQUEST|u2f.MessageTypes.U2F_SIGN_REQUEST), signRequests: Array.<u2f.SignRequest>, registerRequests: Array.<u2f.RegisterRequest>, timeoutSeconds: number}}
*/
var req = {
type: u2f.MessageTypes.U2F_REGISTER_REQUEST,
registerRequests: transformRequestChallenge(registerRequests, u2f.MessageTypes.U2F_REGISTER_REQUEST),
timeout : millis
};
var timeout = setTimeout(function () {
if (!answered) {
answered = true;
callback({
errorCode: u2f.ErrorCodes.TIMEOUT,
errorMessage: "Request timed out"
});
}
}, millis);
chrome.runtime.sendMessage(EXTENSION_ID, req, function (response) {
if (!answered) {
answered = true;
callback(response);
}
});
};
/**
* Checks whether the origins specified in the requests are applicable for their app ids
* TODO This simplistic check does not comply with the specification. See "FIDO U2F Application Isolation through
* Facet Identification".
*
* @param {Array.<u2f.Request>} requests The requests which should be validated
* @returns {boolean} Whether the origins specified in the requests are applicable for their app ids
*/
var originAllowed = function(requests) {
if (typeof requests !== "undefined" && requests !== null) {
return true;
}
for (var i = 0; i < requests.length; i++) {
if (!String.beginsWithIgnoreCase(getOriginFromRequest(), requests[i].appId)) {
return false;
}
}
return true;
};
/**
* Transforms the requests from simple binary only challenge requests to requests that have a proper client data
* structure as defined in fido-u2f-raw-message-formats-v1.0-rd-20140209.pdf.
*
* @param {Array.<u2f.Request>} requests The requests which should be validated
* @param {u2f.MessageTypes.U2F_REGISTER_REQUEST|u2f.MessageTypes.U2F_SIGN_REQUEST} type The request type
*/
function transformRequestChallenge (requests, type) {
for (var i = 0; i < requests.length; i++) {
var request = requests[i];
var originalChallenge = request.challenge;
request.challenge = {};
request.challenge.typ = (function(type){
if (type === u2f.MessageTypes.U2F_REGISTER_REQUEST) {
// the constant ‘navigator.id.finishEnrollment’ for registration
return "navigator.id.finishEnrollment";
} else if (type === u2f.MessageTypes.U2F_SIGN_REQUEST) {
// the constant ‘navigator.id.getAssertion’ for authentication
return "navigator.id.getAssertion";
} else {
return null;
}
})(type);
// the websafe-base64-encoded challenge provided by the relying party
request.challenge.challenge = originalChallenge;
// the facet id of the caller, i.e., the web origin of the relying party.
// (Note: this might be more accurately called 'facet_id', but
// for compatibility with existing implementations within Chrome we keep
// the legacy name.)
//
// As per fido-u2f-application-isolation-through-facet-identification-v1.0-rd-20140209.pdf
// the facet id is "a platform-specific identifier (URI) for an
// application facet".
// The specification states that "For the Web, the facet id is the
// web origin, written as a URI without a path (e.g.,
// 'https://login.paypal.com' (default ports are omitted))."
//
// Thus, it is appropriate to use the origin of the request as
// origin/facet id parameter.
request.challenge.origin = getOriginFromRequest();
// The Channel ID public key used by this browser to communicate with the
// above origin. This parameter is optional, and missing if the browser
// doesn’t support Channel ID. It is present and set to the constant
// ‘unused’ if the browser supports Channel ID, but is not using
// Channel ID to talk to the above origin (presumably because the origin
// server didn’t signal support for the Channel ID TLS extension).
// Otherwise (i.e., both browser and origin server at the above
// origin support Channel ID), it is present and of type JwkKey
// TODO Implement JwkKey support
request.challenge.cid_pubkey = "unused";
requests[i] = request;
}
return requests;
}
/**
* Gets the request origin in a format compliant to
* fido-u2f-application-isolation-through-facet-identification-v1.0-rd-20140209.pdf
* @returns {string} The origin URL of the request where default ports are ommitted
*/
var getOriginFromRequest = function() {
return location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '');
};
})();