@@ -21,8 +21,7 @@ export interface RegisterRequest {
21
21
challenge : string ;
22
22
}
23
23
24
- export interface SignRequest
25
- extends RegisterRequest {
24
+ export interface SignRequest extends RegisterRequest {
26
25
keyHandle : string ;
27
26
}
28
27
@@ -38,6 +37,16 @@ export interface SignResponse {
38
37
signatureData : string ;
39
38
}
40
39
40
+ export type Transport = 'bt' | 'ble' | 'nfc' | 'usb' ;
41
+ export type Transports = Array < Transport > ;
42
+
43
+ export interface RegisteredKey {
44
+ version : string ;
45
+ keyHandle : string ;
46
+ transports : Transports ;
47
+ appId : string ;
48
+ }
49
+
41
50
var _backend : Promise < API > = null ;
42
51
function getBackend ( )
43
52
{
@@ -147,6 +156,22 @@ export function ensureSupport( )
147
156
. then ( _ensureSupport ) ;
148
157
}
149
158
159
+ function arrayify < T > (
160
+ value :
161
+ T | Array < T > | Readonly < T > | ReadonlyArray < T > | undefined | null
162
+ )
163
+ : Array < T >
164
+ {
165
+ if ( value != null && Array . isArray ( value ) )
166
+ return value ;
167
+
168
+ return value == null
169
+ ? [ ]
170
+ : Array . isArray ( value )
171
+ ? [ ...value ]
172
+ : [ < T > value ] ;
173
+ }
174
+
150
175
export function register (
151
176
registerRequests : RegisterRequest | ReadonlyArray < RegisterRequest > ,
152
177
signRequests : SignRequest | ReadonlyArray < SignRequest > ,
@@ -163,17 +188,17 @@ export function register(
163
188
)
164
189
: Promise < RegisterResponse >
165
190
{
166
- if ( ! Array . isArray ( registerRequests ) )
167
- registerRequests = [ registerRequests ] as ReadonlyArray < RegisterRequest > ;
191
+ const _registerRequests = arrayify ( registerRequests ) ;
168
192
169
193
if ( typeof signRequests === 'number' && typeof timeout === 'undefined' )
170
194
{
171
195
timeout = signRequests ;
172
196
signRequests = null ;
173
197
}
174
198
175
- if ( ! signRequests )
176
- signRequests = [ ] ;
199
+ const _signRequests = arrayify (
200
+ < SignRequest | ReadonlyArray < SignRequest > > signRequests
201
+ ) ;
177
202
178
203
return getBackend ( )
179
204
. then ( function ( backend )
@@ -195,10 +220,10 @@ export function register(
195
220
}
196
221
}
197
222
198
- const appId = registerRequests [ 0 ] . appId ;
223
+ const appId = _registerRequests [ 0 ] . appId ;
199
224
200
225
u2f . register (
201
- appId , registerRequests , signRequests , callback , timeout ) ;
226
+ appId , _registerRequests , _signRequests , callback , timeout ) ;
202
227
} ) ;
203
228
} ) ;
204
229
}
@@ -209,8 +234,7 @@ export function sign(
209
234
)
210
235
: Promise < SignResponse >
211
236
{
212
- if ( ! Array . isArray ( signRequests ) )
213
- signRequests = [ signRequests ] as ReadonlyArray < SignRequest > ;
237
+ const _signRequests = arrayify ( signRequests ) ;
214
238
215
239
return getBackend ( )
216
240
. then ( function ( backend )
@@ -232,10 +256,14 @@ export function sign(
232
256
}
233
257
}
234
258
235
- const appId = signRequests [ 0 ] . appId ;
236
- const challenge = signRequests [ 0 ] . challenge ;
259
+ const appId = _signRequests [ 0 ] . appId ;
260
+ const challenge = _signRequests [ 0 ] . challenge ;
261
+ const registeredKeys = _signRequests
262
+ . map ( ( { version, keyHandle, appId } ) =>
263
+ ( { version, keyHandle, appId } as RegisteredKey )
264
+ ) ;
237
265
238
- u2f . sign ( appId , challenge , signRequests , callback , timeout ) ;
266
+ u2f . sign ( appId , challenge , registeredKeys , callback , timeout ) ;
239
267
} ) ;
240
268
} ) ;
241
269
}
0 commit comments