-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathweb-eid.js
More file actions
423 lines (402 loc) · 16.1 KB
/
web-eid.js
File metadata and controls
423 lines (402 loc) · 16.1 KB
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
/**
* MIT License
*
* Copyright (c) 2020-2023 Estonian Information System Authority
*
* 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.
*/
var Action;
(function (Action) {
Action["WARNING"] = "web-eid:warning";
Action["STATUS"] = "web-eid:status";
Action["STATUS_ACK"] = "web-eid:status-ack";
Action["STATUS_SUCCESS"] = "web-eid:status-success";
Action["STATUS_FAILURE"] = "web-eid:status-failure";
Action["AUTHENTICATE"] = "web-eid:authenticate";
Action["AUTHENTICATE_ACK"] = "web-eid:authenticate-ack";
Action["AUTHENTICATE_SUCCESS"] = "web-eid:authenticate-success";
Action["AUTHENTICATE_FAILURE"] = "web-eid:authenticate-failure";
Action["GET_SIGNING_CERTIFICATE"] = "web-eid:get-signing-certificate";
Action["GET_SIGNING_CERTIFICATE_ACK"] = "web-eid:get-signing-certificate-ack";
Action["GET_SIGNING_CERTIFICATE_SUCCESS"] = "web-eid:get-signing-certificate-success";
Action["GET_SIGNING_CERTIFICATE_FAILURE"] = "web-eid:get-signing-certificate-failure";
Action["SIGN"] = "web-eid:sign";
Action["SIGN_ACK"] = "web-eid:sign-ack";
Action["SIGN_SUCCESS"] = "web-eid:sign-success";
Action["SIGN_FAILURE"] = "web-eid:sign-failure";
})(Action || (Action = {}));
var Action$1 = Action;
var ErrorCode;
(function (ErrorCode) {
ErrorCode["ERR_WEBEID_ACTION_TIMEOUT"] = "ERR_WEBEID_ACTION_TIMEOUT";
ErrorCode["ERR_WEBEID_USER_TIMEOUT"] = "ERR_WEBEID_USER_TIMEOUT";
ErrorCode["ERR_WEBEID_VERSION_MISMATCH"] = "ERR_WEBEID_VERSION_MISMATCH";
ErrorCode["ERR_WEBEID_VERSION_INVALID"] = "ERR_WEBEID_VERSION_INVALID";
ErrorCode["ERR_WEBEID_EXTENSION_UNAVAILABLE"] = "ERR_WEBEID_EXTENSION_UNAVAILABLE";
ErrorCode["ERR_WEBEID_NATIVE_UNAVAILABLE"] = "ERR_WEBEID_NATIVE_UNAVAILABLE";
ErrorCode["ERR_WEBEID_UNKNOWN_ERROR"] = "ERR_WEBEID_UNKNOWN_ERROR";
ErrorCode["ERR_WEBEID_CONTEXT_INSECURE"] = "ERR_WEBEID_CONTEXT_INSECURE";
ErrorCode["ERR_WEBEID_USER_CANCELLED"] = "ERR_WEBEID_USER_CANCELLED";
ErrorCode["ERR_WEBEID_NATIVE_INVALID_ARGUMENT"] = "ERR_WEBEID_NATIVE_INVALID_ARGUMENT";
ErrorCode["ERR_WEBEID_NATIVE_FATAL"] = "ERR_WEBEID_NATIVE_FATAL";
ErrorCode["ERR_WEBEID_ACTION_PENDING"] = "ERR_WEBEID_ACTION_PENDING";
ErrorCode["ERR_WEBEID_MISSING_PARAMETER"] = "ERR_WEBEID_MISSING_PARAMETER";
})(ErrorCode || (ErrorCode = {}));
var ErrorCode$1 = ErrorCode;
class MissingParameterError extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
this.code = ErrorCode$1.ERR_WEBEID_MISSING_PARAMETER;
}
}
class ActionPendingError extends Error {
constructor(message = "same action for Web-eID browser extension is already pending") {
super(message);
this.name = this.constructor.name;
this.code = ErrorCode$1.ERR_WEBEID_ACTION_PENDING;
}
}
class ActionTimeoutError extends Error {
constructor(message = "extension message timeout") {
super(message);
this.name = this.constructor.name;
this.code = ErrorCode$1.ERR_WEBEID_ACTION_TIMEOUT;
}
}
const SECURE_CONTEXTS_INFO_URL = "https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts";
class ContextInsecureError extends Error {
constructor(message = "Secure context required, see " + SECURE_CONTEXTS_INFO_URL) {
super(message);
this.name = this.constructor.name;
this.code = ErrorCode$1.ERR_WEBEID_CONTEXT_INSECURE;
}
}
class ExtensionUnavailableError extends Error {
constructor(message = "Web-eID extension is not available") {
super(message);
this.name = this.constructor.name;
this.code = ErrorCode$1.ERR_WEBEID_EXTENSION_UNAVAILABLE;
}
}
var config = Object.freeze({
VERSION: "2.0.2",
EXTENSION_HANDSHAKE_TIMEOUT: 1000,
NATIVE_APP_HANDSHAKE_TIMEOUT: 5 * 1000,
DEFAULT_USER_INTERACTION_TIMEOUT: 2 * 60 * 1000,
MAX_EXTENSION_LOAD_DELAY: 1000,
});
class NativeFatalError extends Error {
constructor(message = "native application terminated with a fatal error") {
super(message);
this.name = this.constructor.name;
this.code = ErrorCode$1.ERR_WEBEID_NATIVE_FATAL;
}
}
class NativeInvalidArgumentError extends Error {
constructor(message = "native application received an invalid argument") {
super(message);
this.name = this.constructor.name;
this.code = ErrorCode$1.ERR_WEBEID_NATIVE_INVALID_ARGUMENT;
}
}
class NativeUnavailableError extends Error {
constructor(message = "Web-eID native application is not available") {
super(message);
this.name = this.constructor.name;
this.code = ErrorCode$1.ERR_WEBEID_NATIVE_UNAVAILABLE;
}
}
class UnknownError extends Error {
constructor(message = "an unknown error occurred") {
super(message);
this.name = this.constructor.name;
this.code = ErrorCode$1.ERR_WEBEID_UNKNOWN_ERROR;
}
}
class UserCancelledError extends Error {
constructor(message = "request was cancelled by the user") {
super(message);
this.name = this.constructor.name;
this.code = ErrorCode$1.ERR_WEBEID_USER_CANCELLED;
}
}
class UserTimeoutError extends Error {
constructor(message = "user failed to respond in time") {
super(message);
this.name = this.constructor.name;
this.code = ErrorCode$1.ERR_WEBEID_USER_TIMEOUT;
}
}
class VersionInvalidError extends Error {
constructor(message = "invalid version string") {
super(message);
this.name = this.constructor.name;
this.code = ErrorCode$1.ERR_WEBEID_VERSION_INVALID;
}
}
function tmpl(strings, requiresUpdate) {
return `Update required for Web-eID ${requiresUpdate}`;
}
class VersionMismatchError extends Error {
constructor(message, versions, requiresUpdate) {
if (!message) {
if (!requiresUpdate) {
message = "requiresUpdate not provided";
}
else if (requiresUpdate.extension && requiresUpdate.nativeApp) {
message = tmpl `${"extension and native app"}`;
}
else if (requiresUpdate.extension) {
message = tmpl `${"extension"}`;
}
else if (requiresUpdate.nativeApp) {
message = tmpl `${"native app"}`;
}
}
super(message);
this.name = this.constructor.name;
this.code = ErrorCode$1.ERR_WEBEID_VERSION_MISMATCH;
this.requiresUpdate = requiresUpdate;
if (versions) {
const { library, extension, nativeApp } = versions;
Object.assign(this, { library, extension, nativeApp });
}
}
}
const errorCodeToErrorClass = {
[ErrorCode$1.ERR_WEBEID_ACTION_PENDING]: ActionPendingError,
[ErrorCode$1.ERR_WEBEID_ACTION_TIMEOUT]: ActionTimeoutError,
[ErrorCode$1.ERR_WEBEID_CONTEXT_INSECURE]: ContextInsecureError,
[ErrorCode$1.ERR_WEBEID_EXTENSION_UNAVAILABLE]: ExtensionUnavailableError,
[ErrorCode$1.ERR_WEBEID_NATIVE_INVALID_ARGUMENT]: NativeInvalidArgumentError,
[ErrorCode$1.ERR_WEBEID_NATIVE_FATAL]: NativeFatalError,
[ErrorCode$1.ERR_WEBEID_NATIVE_UNAVAILABLE]: NativeUnavailableError,
[ErrorCode$1.ERR_WEBEID_USER_CANCELLED]: UserCancelledError,
[ErrorCode$1.ERR_WEBEID_USER_TIMEOUT]: UserTimeoutError,
[ErrorCode$1.ERR_WEBEID_VERSION_INVALID]: VersionInvalidError,
[ErrorCode$1.ERR_WEBEID_VERSION_MISMATCH]: VersionMismatchError,
};
function deserializeError(errorObject) {
let error;
if (typeof errorObject.code == "string" && errorObject.code in errorCodeToErrorClass) {
const CustomError = errorCodeToErrorClass[errorObject.code];
error = new CustomError();
}
else {
error = new UnknownError();
}
for (const [key, value] of Object.entries(errorObject)) {
error[key] = value;
}
return error;
}
class WebExtensionService {
constructor() {
this.loggedWarnings = [];
this.queue = [];
window.addEventListener("message", (event) => this.receive(event));
}
receive(event) {
var _a, _b, _c, _d, _e, _f;
if (!/^web-eid:/.test((_a = event.data) === null || _a === void 0 ? void 0 : _a.action))
return;
const message = event.data;
const suffix = (_c = (_b = message.action) === null || _b === void 0 ? void 0 : _b.match(/success$|failure$|ack$/)) === null || _c === void 0 ? void 0 : _c[0];
const initialAction = this.getInitialAction(message.action);
const pending = this.getPendingMessage(initialAction);
if (message.action === Action$1.WARNING) {
(_d = message.warnings) === null || _d === void 0 ? void 0 : _d.forEach((warning) => {
if (!this.loggedWarnings.includes(warning)) {
this.loggedWarnings.push(warning);
console.warn(warning.replace(/\n|\r/g, ""));
}
});
}
else if (pending) {
switch (suffix) {
case "ack": {
clearTimeout(pending.ackTimer);
break;
}
case "success": {
this.removeFromQueue(initialAction);
(_e = pending.resolve) === null || _e === void 0 ? void 0 : _e.call(pending, message);
break;
}
case "failure": {
const failureMessage = message;
this.removeFromQueue(initialAction);
(_f = pending.reject) === null || _f === void 0 ? void 0 : _f.call(pending, failureMessage.error ? deserializeError(failureMessage.error) : failureMessage);
break;
}
}
}
}
send(message, timeout) {
if (this.getPendingMessage(message.action)) {
return Promise.reject(new ActionPendingError());
}
else if (!window.isSecureContext) {
return Promise.reject(new ContextInsecureError());
}
else {
const pending = { message };
this.queue.push(pending);
pending.promise = new Promise((resolve, reject) => {
pending.resolve = resolve;
pending.reject = reject;
});
pending.ackTimer = window.setTimeout(() => this.onAckTimeout(pending), config.EXTENSION_HANDSHAKE_TIMEOUT);
pending.replyTimer = window.setTimeout(() => this.onReplyTimeout(pending), timeout);
window.postMessage(message, "*");
return pending.promise;
}
}
onReplyTimeout(pending) {
var _a;
this.removeFromQueue(pending.message.action);
(_a = pending.reject) === null || _a === void 0 ? void 0 : _a.call(pending, new ActionTimeoutError());
}
onAckTimeout(pending) {
var _a;
clearTimeout(pending.replyTimer);
this.removeFromQueue(pending.message.action);
(_a = pending.reject) === null || _a === void 0 ? void 0 : _a.call(pending, new ExtensionUnavailableError());
}
getPendingMessage(action) {
return this.queue.find((pm) => {
return pm.message.action === action;
});
}
getInitialAction(action) {
return action.replace(/-success$|-failure$|-ack$/, "");
}
removeFromQueue(action) {
const pending = this.getPendingMessage(action);
clearTimeout(pending === null || pending === void 0 ? void 0 : pending.replyTimer);
this.queue = this.queue.filter((pending) => (pending.message.action !== action));
}
}
/**
* Sleeps for a specified time before resolving the returned promise.
*
* @param milliseconds Time in milliseconds until the promise is resolved
*
* @returns Empty promise
*/
function sleep(milliseconds) {
return new Promise((resolve) => {
setTimeout(() => resolve(), milliseconds);
});
}
const webExtensionService = new WebExtensionService();
const initializationTime = +new Date();
async function extensionLoadDelay() {
const now = +new Date();
await sleep(initializationTime + config.MAX_EXTENSION_LOAD_DELAY - now);
}
async function status() {
await extensionLoadDelay();
const timeout = config.EXTENSION_HANDSHAKE_TIMEOUT + config.NATIVE_APP_HANDSHAKE_TIMEOUT;
const message = {
action: Action$1.STATUS,
libraryVersion: config.VERSION,
};
try {
const { library, extension, nativeApp, } = await webExtensionService.send(message, timeout);
return {
library,
extension,
nativeApp,
};
}
catch (error) {
error.library = config.VERSION;
throw error;
}
}
async function authenticate(challengeNonce, options) {
await extensionLoadDelay();
if (!challengeNonce) {
throw new MissingParameterError("authenticate function requires a challengeNonce");
}
const timeout = (config.EXTENSION_HANDSHAKE_TIMEOUT +
config.NATIVE_APP_HANDSHAKE_TIMEOUT +
((options === null || options === void 0 ? void 0 : options.userInteractionTimeout) || config.DEFAULT_USER_INTERACTION_TIMEOUT));
const message = {
action: Action$1.AUTHENTICATE,
libraryVersion: config.VERSION,
challengeNonce,
options,
};
const { unverifiedCertificate, algorithm, signature, format, appVersion, } = await webExtensionService.send(message, timeout);
return {
unverifiedCertificate,
algorithm,
signature,
format,
appVersion,
};
}
async function getSigningCertificate(options) {
await extensionLoadDelay();
const timeout = (config.EXTENSION_HANDSHAKE_TIMEOUT +
config.NATIVE_APP_HANDSHAKE_TIMEOUT +
((options === null || options === void 0 ? void 0 : options.userInteractionTimeout) || config.DEFAULT_USER_INTERACTION_TIMEOUT) * 2);
const message = {
action: Action$1.GET_SIGNING_CERTIFICATE,
libraryVersion: config.VERSION,
options,
};
const { certificate, supportedSignatureAlgorithms, } = await webExtensionService.send(message, timeout);
return {
certificate,
supportedSignatureAlgorithms,
};
}
async function sign(certificate, hash, hashFunction, options) {
await extensionLoadDelay();
if (!certificate) {
throw new MissingParameterError("sign function requires a certificate as parameter");
}
if (!hash) {
throw new MissingParameterError("sign function requires a hash as parameter");
}
if (!hashFunction) {
throw new MissingParameterError("sign function requires a hashFunction as parameter");
}
const timeout = (config.EXTENSION_HANDSHAKE_TIMEOUT +
config.NATIVE_APP_HANDSHAKE_TIMEOUT +
((options === null || options === void 0 ? void 0 : options.userInteractionTimeout) || config.DEFAULT_USER_INTERACTION_TIMEOUT) * 2);
const message = {
action: Action$1.SIGN,
libraryVersion: config.VERSION,
certificate,
hash,
hashFunction,
options,
};
const { signature, signatureAlgorithm, } = await webExtensionService.send(message, timeout);
return {
signature,
signatureAlgorithm,
};
}
export { Action$1 as Action, ErrorCode$1 as ErrorCode, authenticate, config, getSigningCertificate, sign, status };