Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix U2F feature detection in Firefox #285

Merged
merged 5 commits into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions includes/Google/u2f-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
*/
var u2f = u2f || {};

/**
* Check if browser supports U2F API before this wrapper was added.
* @type {int}
*/
u2f.HasNativeApiSupport = ( ( u2f && u2f.register ) || ( chrome && chrome.runtime ) );

/**
* FIDO U2F Javascript API Version
* @number
Expand Down
20 changes: 16 additions & 4 deletions providers/class.two-factor-fido-u2f-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ public static function enqueue_assets( $hook ) {
'fido-u2f-admin',
plugins_url( 'css/fido-u2f-admin.css', __FILE__ ),
null,
'0.1.0-dev.1'
self::asset_version()
);

wp_enqueue_script(
'fido-u2f-admin',
plugins_url( 'js/fido-u2f-admin.js', __FILE__ ),
array( 'jquery', 'fido-u2f-api' ),
'0.1.0-dev.3',
self::asset_version(),
true
);

Expand All @@ -97,7 +97,7 @@ public static function enqueue_assets( $hook ) {
4 => esc_html__( 'U2F device ineligible.', 'two-factor' ),
5 => esc_html__( 'U2F request timeout reached.', 'two-factor' ),
),
'u2f_not_supported' => esc_html__( 'FIDO U2F is not supported in your web browser. Try using Google Chrome.', 'two-factor' ),
'u2f_not_supported' => esc_html__( 'FIDO U2F appears to be not supported by your web browser. Try using Google Chrome or Firefox.', 'two-factor' ),
),
);

Expand All @@ -115,7 +115,7 @@ public static function enqueue_assets( $hook ) {
'inline-edit-key',
plugins_url( 'js/fido-u2f-admin-inline-edit.js', __FILE__ ),
array( 'jquery' ),
'0.1.0-dev.1',
self::asset_version(),
true
);

Expand All @@ -128,6 +128,18 @@ public static function enqueue_assets( $hook ) {
);
}

/**
* Return the current asset version number.
*
* Added as own helper to allow swapping the implementation once we inject
* it as a dependency.
*
* @return string
*/
protected static function asset_version() {
return Two_Factor_FIDO_U2F::asset_version();
}

/**
* Display the security key section in a users profile.
*
Expand Down
22 changes: 20 additions & 2 deletions providers/class.two-factor-fido-u2f.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class Two_Factor_FIDO_U2F extends Two_Factor_Provider {
*/
const AUTH_DATA_USER_META_KEY = '_two_factor_fido_u2f_login_request';

/**
* Version number for the bundled assets.
*
* @var string
*/
const U2F_ASSET_VERSION = '0.2.0';

/**
* Ensures only one instance of this class exists in memory at any one time.
*
Expand Down Expand Up @@ -64,15 +71,15 @@ protected function __construct() {
'fido-u2f-api',
plugins_url( 'includes/Google/u2f-api.js', dirname( __FILE__ ) ),
null,
'0.1.0-dev.2',
self::asset_version(),
true
);

wp_register_script(
'fido-u2f-login',
plugins_url( 'js/fido-u2f-login.js', __FILE__ ),
array( 'jquery', 'fido-u2f-api' ),
'0.1.0-dev.2',
self::asset_version(),
true
);

Expand All @@ -81,6 +88,17 @@ protected function __construct() {
return parent::__construct();
}

/**
* Get the asset version number.
*
* TODO: There should be a plugin-level helper for getting the current plugin version.
*
* @return string
*/
public static function asset_version() {
return self::U2F_ASSET_VERSION;
}

/**
* Return the U2F AppId. U2F requires the AppID to use HTTPS
* and a top-level domain.
Expand Down
4 changes: 2 additions & 2 deletions providers/js/fido-u2f-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
( function( $ ) {
var $button = $( '#register_security_key' );
var $statusNotice = $( '#security-keys-section .security-key-status' );
var u2fSupported = ( u2f && u2f.HasNativeApiSupport );
var u2fSupported = ( window.u2f && 'register' in window.u2f );

if ( ! u2fSupported ) {
$statusNotice.text( u2fL10n.text.u2f_not_supported );
Expand All @@ -24,7 +24,7 @@
challenge: u2fL10n.register.request.challenge
};

u2f.register( u2fL10n.register.request.appId, [ registerRequest ], u2fL10n.register.sigs, function( data ) {
window.u2f.register( u2fL10n.register.request.appId, [ registerRequest ], u2fL10n.register.sigs, function( data ) {
$( '.register-security-key .spinner' ).removeClass( 'is-active' );
$button.prop( 'disabled', false );

Expand Down