Comparing version 0.0.9 to 0.1.0
@@ -7,2 +7,12 @@ 'use strict'; | ||
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 ) | ||
@@ -26,5 +36,18 @@ { | ||
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; | ||
} ); | ||
@@ -66,3 +89,3 @@ | ||
{ | ||
if ( disconnect ) | ||
if ( disconnect && !hasNativeSupport ) | ||
coreApi.disconnect( ); | ||
@@ -79,2 +102,5 @@ | ||
if ( hasNativeSupport ) | ||
return Promise.resolve( true ); | ||
var isSafari = navigator.userAgent.match( /Safari\// ) | ||
@@ -102,3 +128,3 @@ && !navigator.userAgent.match( /Chrome\// ); | ||
return defer( Promise, coreApi.isSupported ).promise | ||
return isSupported.call( Promise ) | ||
.then( function( value ) { | ||
@@ -130,12 +156,34 @@ if ( !value ) | ||
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 ); | ||
} | ||
coreApi.register( 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 ); | ||
} | ||
} ).promise; | ||
@@ -151,12 +199,35 @@ } | ||
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 ); | ||
} | ||
coreApi.sign( 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 ); | ||
} | ||
} ).promise; | ||
@@ -163,0 +234,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.0.9", | ||
"version": "0.1.0", | ||
"author": "Gustaf Räntilä <g.rantila@gmail.com>", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -9,7 +9,17 @@ # u2f-api | ||
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. | ||
@@ -20,8 +30,12 @@ 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. | ||
```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 | ||
``` | ||
@@ -31,8 +45,10 @@ | ||
```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 > | ||
``` | ||
@@ -44,7 +60,9 @@ | ||
```js | ||
Promise{ SignResponse } sign( | ||
[SignRequest] signRequests, | ||
Number timeout // optional | ||
) | ||
```ts | ||
import { sign } from 'u2f-api' | ||
sign( | ||
signRequests: SignRequest[], | ||
timeout: number // optional | ||
): Promise< SignResponse > | ||
``` | ||
@@ -58,3 +76,3 @@ | ||
``` | ||
```js | ||
OK = 0 // u2f-api will never throw errors with this code | ||
@@ -61,0 +79,0 @@ OTHER_ERROR = 1 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21923
577
149