Android Biometric

🚧

Migration to new JavaScript Bridge Interface

We have made it easier for you to interact with the native Android Biometrics via the GoNative JavaScript Bridge.

Previously, you would call the bridge using:
window.location.href = 'gonative://auth/status?callbackFunction=gonative_status_afterlogin'

Now you can do the same using:
gonative.auth.status({'callbackFunction': gonative_status_afterlogin});

Overview

Fingerprint technology has been accessible to apps running on Android devices starting with Android 6.0 (Marshmallow). GoNative's fingerprint module enables this convenient and secure means of authenticating users of your app.

GoNative's Android Biometric Authentication plugin uses GoNative JavaScript Bridge commands to secure storage and retrieval of user credentials. Communication from the web page to the native apps is done by opening a specific url with certain query parameters, detailed below.

Responses from the native app are delivered to a callback function in the window's javascript context. The plugin module allows the storage of a secret string. The contents and format of the string should be decided by the website developer. It may contain a JSON object containing the username and password, an authentication token, or anything else that would authenticate the user.

The first step is to check for the presence and availability of the fingerprint reader. If there is no fingerprint reader, or if the user has not enrolled any fingerprints, a fingerprint dialog should not be shown. If fingerprints are available, then the status check will also indicate the presence of a previously saved secret.

Once a user has logged in, and has fingerprints enrolled, the website may save the secret. We use the Android Keystore technologies, which in turn leverage cryptographic hardware, to ensure that the secret cannot be retrieved without fingerprint authentication from the user. Note that saving the secret does not require any specific user interaction on the device.

The next time the user needs to log in to the website, and the status check indicates a secret is available, the website should attempt to retrieve the secret from the device. At that point, the user will be shown a dialog prompting them to authenticate via their fingerprint. If the fingerprint check is successful, the secret will be sent to the website via the javascript callback. If not, the callback will receive an error.

Implementation Guide

Once the premium module has been added to your app, you may use the following GoNative JavaScript Bridge commands to access its functionality.

This guide assumes a working publicly-accessible website (or test site) with a username and password login system.

Save secret on successful login

After a user successfully logs in to your website with their username and password, check to see if fingerprint is available. If it is, save a secret for future retrieval. The secret must be a single string and can be a combination of the username and password, or an authentication token.

↔️GoNative JavaScript Bridge

For example, you may embed this javascript into your post-login page:

var username = 'andy'
var password = 'password';

gonative.auth.status({'callbackFunction': gonative_status_afterlogin}); // returns promise

function gonative_status_afterlogin(data) {
    if (data && data.hasTouchId) {
        var secret = JSON.stringify({
            username: username,
            password: password
        });
        
        gonative.auth.save({'secret': secret});
    }
}

Tip: Some JS Bridge commands return promises. They are usually declared as such through a comment to help you identify them. Learn More.

In this example, we have saved the username and password as the secret. You may choose to save an authentication token instead.

Check for secret on login page

↔️GoNative JavaScript Bridge

On the login page, you will need to know whether or not to prompt for fingerprint credentials. Start by getting the status:

gonative.auth.status({'callbackFunction': gonative_status_beforelogin}); // returns promise

function gonative_status_beforelogin(data) {
   if (data && data.hasTouchId && data.hasSecret) {
       // Prompt the user to use the fingerprint to log in
       gonative.auth.get({'callbackFunction': gonative_secret_callback});
   }
}

function gonative_secret_callback(data) {
    if (data && data.success && data.secret) {
        var credentials = JSON.parse(data.secret);
        var username = credentials.username;
        var password = credentials.password;
        
        // Use username and password to do login here,
        // e.g. an http post or ajax request
    } else {
        // Allow manual entry
    }
}

Once the gonative_secret_callback function is called with the previously saved secret, it should perform a request to log in the user. If the credentials are incorrect, you should delete the secret and allow manual login by running the function:

// delete secret if credentials are incorrect
gonative.auth.delete({'callbackFunction': CALLBACK});

GoNative JavaScript Bridge Reference

Retrieving fingerprint availability

↔️GoNative JavaScript Bridge

Run the JavaScript function:

gonative.auth.status({'callbackFunction': CALLBACK}); // returns promise

Callback is required. The app will execute CALLBACK with an object parameter containing the fields:

  • hasTouchId: true or false. Set to true on Android devices with fingerprints enrolled.
  • hasSecret: true or false

Saving a secret

Typically you will want to first check that biometrics are available via the status function above and then save the secret.

↔️GoNative JavaScript Bridge

Run the JavaScript function:

gonative.auth.save({'secret': secret, 'callbackFunction': CALLBACK});

callbackFunction is optional and will be called after the save operation with a success parameter.

Getting a secret

↔️GoNative JavaScript Bridge

Run the JavaScript function:

gonative.auth.get({'callbackFunction': CALLBACK, 'prompt': 'PROMPT'});

Callback is required. Prompt is only available for iOS and it has been included above to ensure cross-platform compatibility. The app will execute CALLBACK with an object parameter containing the fields:

  • success: true or false
  • error: provided success is false (see error codes below)
  • secret: the previously stored secret
    Another optional query parameter is callbackOnCancel. If set to 1 and the user cancels the authentication, the callback will be run with success=false, error=userCanceled. If callbackOnCancel is not set (or set to 0), the callback will not be run.

Deleting a secret

↔️GoNative JavaScript Bridge

Run the JavaScript function:

gonative.auth.delete({'callbackFunction': CALLBACK});

Callback is optional. The app will execute CALLBACK with an object parameter containing the fields:

  • success: true or false
  • error: provided if success is false

Possible error values

In general, you will only need to handle authenticationFailed in the "get secret" request.

  • duplicateItem
  • itemNotFound
  • authenticationFailed
  • genericError
  • userCanceled
  • unimplemented

White-listing access

By default, any page loaded in your app will be able to use GoNative JavaScript Bridge to retrieve secrets. If you are allowing any domains you do not control to be loaded within your app, we strongly recommend whitelisting only certain domains to access this plugin.

To do so, go to the Native Plugins page of your GoNative project, click Settings for this plugin and add your whitelisted URLs (comma separated for multiple URLs) as shown below:

26342634

Whitelisted URLs