This repository has been archived by the owner on Nov 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-eid.js
329 lines (296 loc) · 9.74 KB
/
web-eid.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
(function (window) {
'use strict'
var VERSION = '0.0.5'
var APPURL = 'wss://app.web-eid.com:42123'
// make a nonce
function getNonce (l) {
if (l === undefined) {
l = 24
}
var val = ''
var hex = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVXYZ'
for (var i = 0; i < l; i++) val += hex.charAt(Math.floor(Math.random() * hex.length))
return val
}
function ab2b (v) {
return window.btoa(String.fromCharCode.apply(null, new Uint8Array(v)))
}
function b2ab (v) {
return new Uint8Array(window.atob(v).split('').map(function (c) { return c.charCodeAt(0) })).buffer
}
var pending = {} // pending promises
var port = null
// Resolve or reject the promise if id matches
function processMessage (reply) {
// reply.hwcrypto is in window message listener.
if (!reply.hwcrypto && reply.id && reply.id in pending) {
console.log('RECV: ' + JSON.stringify(reply))
if (!reply.error) {
pending[reply.id].resolve(reply)
} else {
pending[reply.id].reject(new Error(reply.error))
}
delete pending[reply.id]
}
}
function toExtension (msg) {
return new Promise(function (resolve, reject) {
msg.id = getNonce()
msg.hwcrypto = true // This will be removed by content script
window.postMessage(msg, '*')
pending[msg.id] = {
resolve: resolve,
reject: reject
}
})
}
// Send a message and return the promise.
function msg2promise (msg) {
return new Promise(function (resolve, reject) {
// amend with necessary metadata
msg.id = msg.id || getNonce()
console.log('SEND: ' + JSON.stringify(msg))
// send message
if (!port) { reject(new Error('App has disappeared')) }
port.send(msg)
// and store promise callbacks
pending[msg['id']] = {
resolve: resolve,
reject: reject
}
})
}
// construct
var webeid = function () {
console.log('Web eID JS shim v' + VERSION)
// register incoming message handler for extension
window.addEventListener('message', function (message) { processMessage(message.data) })
// Fields to be exported
var fields = {}
// Returns app version
fields.getVersion = function () {
return msg2promise({
'version': {}
}).then(function (r) {
return r.version
})
}
fields.isAvailable = function (options) {
// Already open
if (port) {
return Promise.resolve(port.technology)
}
// If the extension is not responding, the only
// way to get a connection without reloading the page
// is if the application is download and started
// thus only websockets must be re-tried
var timeout = 0
if (options) { timeout = options.timeout || timeout }
if (typeof timeout === 'number') { timeout = timeout * 1000 }
if (timeout === 0) { timeout = 700 }
if (timeout === Infinity) { timeout = 10 * 60 * 1000 } // 10 minutes
console.log('Actual timeout is', timeout / 1000, 'seconds')
var retry = true
// Try to open the websocket and increase the timeout if it fails
// and our timeout is Infinity
// This will only successfully resolve
function openSocket () {
var delay = 1000 // delay before trying to re-connect socket
return new Promise(function (resolve, reject) {
function connect () {
if (!retry) { return reject(new Error('Already connected')) }
delay = delay * 1.3
try {
var ws = {}
ws.socket = new WebSocket(APPURL)
ws.technology = 'websocket'
ws.socket.addEventListener('open', function (event) {
console.log('WS open', event)
// clearTimeout(retry)
ws.socket.addEventListener('message', function (m) {
processMessage(JSON.parse(m.data))
})
ws.send = function (msg) {
ws.socket.send(JSON.stringify(msg))
}
ws.socket.addEventListener('close', function (event) {
console.error('WS close: ', event)
if (port.technology === 'websocket') { port = null }
})
resolve(ws)
})
ws.socket.addEventListener('error', function (event) {
console.error('WS error: ', event)
if (retry) {
setTimeout(function () {
console.log('Will retry in', delay / 1000, 'seconds')
connect()
}, delay)
}
})
} catch (e) {
console.log('Could not create WS', e)
reject(e)
}
}
// give extension head start
setTimeout(connect, 700)
})
}
// Race for a port
// Resolves if extension replies. Will never happen if no extension
var e = toExtension({version: {}}).then(function (response) {
return {
send: function (message) {
message.hwcrypto = true
window.postMessage(message, '*')
},
technology: 'webextension'
}
})
// Rejects after timeout
var t = new Promise(function (resolve, reject) {
setTimeout(function () {
reject(new Error('timeout'))
}, timeout)
})
// resolves to websocket lookalike with .send() if open is successful
var s = openSocket()
// Race to connection
return Promise.race([e, s, t]).then(function (r) {
retry = false
console.log('race resolved to', r.technology)
port = r
return r.technology
}).catch(function (err) {
retry = false
console.log('race failed', err)
return false
})
}
fields.getCertificate = function (options) {
options = options || {}
// resolves to a certificate handle (in real life b64)
return msg2promise({ 'certificate': options }).then(function (r) {
return b2ab(r.certificate)
})
}
fields.sign = function (cert, hash, options) {
return msg2promise({
'sign': {
'certificate': ab2b(cert),
'hash': ab2b(hash),
'hashalgo': options.hashalgo
}
}).then(function (r) {
return b2ab(r.signature)
})
}
// TODO: return an object where onLogout resolves if cert removed
function authenticate (nonce) {
return msg2promise({
'authenticate': { 'nonce': nonce }
}).then(function (r) {
return r.token
})
}
fields.authenticate = authenticate
// Connect to a card reader in plain PC/SC mode
fields.connect = function (options) {
if (options === 'undefined') { options = {} }
var timeout = options.timeout || Infinity
// Infinity is actually 1 hour
if (timeout === Infinity) { timeout = 3600 }
var atrs = options.atrs || []
atrs = atrs.map(function (x) { return ab2b(x) })
var protocol = options.protocol || '*'
return msg2promise({
SCardConnect: {protocol: protocol, atrs: atrs}
}).then(function (r) {
return {
name: r.name,
atr: b2ab(r.atr),
protocol: r.protocol,
transmit: function (bytes) {
return msg2promise({
SCardTransmit: {reader: r.name, bytes: ab2b(bytes)}
}).then(function (r) {
return b2ab(r.bytes)
})
},
reconnect: function (protocol) {
return msg2promise({
SCardReconnect: {reader: r.name, protocol: protocol}
}).then(function (r) {
return true
})
},
disconnect: function () {
return msg2promise({
SCardDisconnect: {reader: r.name}
}).then(function (r) {
return true
})
},
control: function (code, bytes) {
return msg2promise({
SCardControl: {reader: r.name, code: code, bytes: ab2b(bytes)}
}).then(function (r) {
return b2ab(r.bytes)
})
}
}
})
}
fields.authenticatedWebSocket = function (url, options) {
return new Promise(function (resolve, reject) {
var socket = new WebSocket(url)
function errorHandler (event) {
reject(event)
}
function messageHandler (event) {
socket.removeEventListener('message', messageHandler)
var msg = JSON.parse(event.data)
if (!msg.nonce) {
reject(new Error('No .nonce in first message'))
}
authenticate(msg.nonce).then(function (token) {
socket.send(JSON.stringify({token: token}))
socket.removeEventListener('error', errorHandler)
resolve(socket)
}, function (reason) {
socket.close()
reject(reason)
})
}
function openHandler (event) {
socket.addEventListener('message', messageHandler)
}
socket.addEventListener('error', errorHandler)
socket.addEventListener('open', openHandler)
})
}
fields.VERSION = VERSION
fields.promisify = msg2promise
return fields
}
// Register
if (typeof exports !== 'undefined') {
// nodejs
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = webeid()
} else {
exports.webeid = webeid()
}
} else {
// requirejs
if (typeof define === 'function' && define.amd) {
define(function () {
return webeid()
})
} else {
// browser
window.webeid = webeid()
}
}
})(typeof window === 'object' ? window : this)