Added support for Firefox 58 and later

This commit is contained in:
Gustaf Räntilä
2017-09-19 15:18:57 +02:00
parent 3f18d9912c
commit 808ef863b9
2 changed files with 130 additions and 41 deletions
+36 -18
View File
@@ -6,43 +6,61 @@ U2F API for browsers
### Support
U2F is tested to work with newer versions of Chrome for Mac. It doesn't work very well in Windows, and most other browsers don't support it, like Opera, Firefox and Safari.
U2F has for a long time been supported in Chrome, although not with the standard `window.u2f` methods, but through a built-in extension. Nowadays, browsers seem to use `window.u2f` to expose the functionality.
Supported browsers are:
* Chrome (unless ancient), using Chrome-specific hacks
* Firefox 58 and later, using `window.u2f`
Opera, Safari and other browsers still lack U2F support.
Since 0.1.0, this library supports the standard `window.u2f` methods.
The library should be complemented with server-side functionality, e.g. using the [`u2f`](https://www.npmjs.com/package/u2f) package.
### Basics
u2f-api exports two main functions and an error "enum". The main functions are `register()` and `sign()`, although since U2F isn't widely supported, the functions `isSupported()` as well as `ensureSupport()` helps you build applications which can use U2F only when the client supports it.
`u2f-api` exports two main functions and an error "enum". The main functions are `register()` and `sign()`, although since U2F isn't widely supported, the functions `isSupported()` as well as `ensureSupport()` helps you build applications which can use U2F only when the client supports it.
The `register()` and `sign()` functions return *cancellable promises*, i.e. promises you can cancel manually. This helps you to ensure your code doesn't continue in success flow and by mistake accept a registration or authentification request. The returned promise has a function `cancel()` which will immediately reject the promise.
#### Check or ensure support
```js
Promise{ Boolean } isSupported() // Doesn't throw/reject
```ts
import { isSupported } from 'u2f-api'
isSupported(): Promise< Boolean > // Doesn't throw/reject
```
```js
Promise{ undefined } ensureSupport() // Throws/rejects if not supported
```ts
import { ensureSupport } from 'u2f-api'
ensureSupport(): Promise< void > // Throws/rejects if not supported
```
#### Register
```js
Promise{ RegisterResponse } register(
[RegisterRequest] registerRequests,
[SignRequest] signRequests, // optional
Number timeout // optional
)
```ts
import { register } from 'u2f-api'
register(
registerRequests: RegisterRequest[],
signRequests: SignRequest[], // optional
timeout: number // optional
): Promise< RegisterResponse >
```
The `registerRequests` can be either a RegisterRequest or an array of such. The optional `signRequests` must be, unless ignored, an array of SignRequests. The optional `timeout` is in seconds, and will default to an implementation specific value, e.g. 30.
#### Sign
```js
Promise{ SignResponse } sign(
[SignRequest] signRequests,
Number timeout // optional
)
```ts
import { sign } from 'u2f-api'
sign(
signRequests: SignRequest[],
timeout: number // optional
): Promise< SignResponse >
```
The values and interpretation of the arguments are the same as with `register( )`.
@@ -51,7 +69,7 @@ The values and interpretation of the arguments are the same as with `register( )
`register()` and `sign()` can return rejected promises. The rejection error is an `Error` object with a `metaData` property containing `code` and `type`. The `code` is a numerical value describing the type of the error, and `type` is the name of the error, as defined by the `ErrorCodes` enum in the "FIDO U2F Javascript API" specification. They are:
```
```js
OK = 0 // u2f-api will never throw errors with this code
OTHER_ERROR = 1
BAD_REQUEST = 2
+94 -23
View File
@@ -4,6 +4,16 @@ var coreApi = require( './google-u2f-api' );
module.exports = API;
var _global =
typeof window !== 'undefined'
? window
: typeof global !== 'undefined'
? global
: { };
var hasNativeSupport = _global.u2f && typeof _global.u2f.sign === 'function';
var _u2f = hasNativeSupport ? _global.u2f : coreApi;
function API( Promise )
{
return {
@@ -22,9 +32,22 @@ var errorMap = {
'-1': 'CANCELED'
};
Object.keys( coreApi.ErrorCodes ).forEach( function( key ) {
API.ErrorCodes[ key ] = coreApi.ErrorCodes[ key ];
errorMap[ '' + coreApi.ErrorCodes[ key ] ] = key;
var backendErrorCodes = hasNativeSupport ? _global.u2f : coreApi.ErrorCodes;
(
hasNativeSupport
? [
'BAD_REQUEST',
'CONFIGURATION_UNSUPPORTED',
'DEVICE_INELIGIBLE',
'OK',
'OTHER_ERROR',
'TIMEOUT',
]
: Object.keys( coreApi.ErrorCodes )
)
.forEach( function( key ) {
API.ErrorCodes[ key ] = backendErrorCodes[ key ];
errorMap[ '' + backendErrorCodes[ key ] ] = key;
} );
function makeError( msg, err )
@@ -61,7 +84,7 @@ function defer( Promise, fun )
*/
ret.promise.cancel = function( msg, disconnect )
{
if ( disconnect )
if ( disconnect && !hasNativeSupport )
coreApi.disconnect( );
ret.reject( makeError( msg, { errorCode: -1 } ) );
@@ -73,6 +96,9 @@ function isSupported( ignorePreconditions /* = false */ )
{
var Promise = this;
if ( hasNativeSupport )
return Promise.resolve( true );
var isSafari = navigator.userAgent.match( /Safari\// )
&& !navigator.userAgent.match( /Chrome\// );
@@ -95,7 +121,7 @@ function ensureSupport( )
{
var Promise = this;
return defer( Promise, coreApi.isSupported ).promise
return isSupported.call( Promise )
.then( function( value ) {
if ( !value )
{
@@ -122,16 +148,38 @@ function register( registerRequests, signRequests /* = null */, timeout )
if ( !signRequests )
signRequests = [ ];
return defer( Promise, function( resolve, reject ) {
function cb( err, response ) {
if ( err )
reject( err );
else if ( response.errorCode )
reject( makeError( "Registration failed", response ) );
else
resolve( response );
return defer( Promise, function( resolve, reject )
{
if ( hasNativeSupport )
{
function cb( response )
{
if ( response.errorCode )
reject( makeError( "Registration failed", response ) );
else
{
delete response.errorCode;
resolve( response );
}
}
const { appId } = registerRequests[ 0 ];
_u2f.register( appId, registerRequests, signRequests, cb, timeout );
}
else
{
function cb( err, response )
{
if ( err )
reject( err );
else if ( response.errorCode )
reject( makeError( "Registration failed", response ) );
else
resolve( response );
}
coreApi.register( registerRequests, signRequests, cb, timeout );
}
coreApi.register( registerRequests, signRequests, cb, timeout );
} ).promise;
}
@@ -142,16 +190,39 @@ function sign( signRequests, timeout )
if ( !Array.isArray( signRequests ) )
signRequests = [ signRequests ];
return defer( Promise, function( resolve, reject ) {
function cb( err, response ) {
if ( err )
reject( err );
else if ( response.errorCode )
reject( makeError( "Sign failed", response ) );
else
resolve( response );
return defer( Promise, function( resolve, reject )
{
if ( hasNativeSupport )
{
function cb( response )
{
if ( response.errorCode )
reject( makeError( "Sign failed", response ) );
else
{
delete response.errorCode;
resolve( response );
}
}
const { appId, challenge } = signRequests[ 0 ];
_u2f.sign( appId, challenge, signRequests, cb, timeout );
}
else
{
function cb( err, response )
{
if ( err )
reject( err );
else if ( response.errorCode )
reject( makeError( "Sign failed", response ) );
else
resolve( response );
}
coreApi.sign( signRequests, cb, timeout );
}
coreApi.sign( signRequests, cb, timeout );
} ).promise;
}