Sign In with Device

Our Android SDK offers the easiest way to integrate "Sign in with device" to your android app. You can simply call a function and it does most of the heavy lifting and authentication for you.

Overview

Authenticating users using Trusted Devices with Cotter's Android SDK consists of the following steps:

  1. Import and Initialize Cotter

  2. Calling functions to register devices a Trusted Device

  3. Calling functions to authenticate from Trusted and Non-Trusted Device

What you're building

Steps

  1. Set Allowed Methods in the Dashboard to allow Trusted Devices

  2. Create a User: Create a user in Cotter by specifying your user ID.

Step 1: Import Cotter as a dependency

Add JitPack repository your project level build.gradle at the end of repositories.

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

Add the Cotter's SDK as a dependency in your app level build.gradle .

android {
  ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

dependencies {
  ...
  implementation 'com.github.cotterapp:android-sdk:0.3.9'
}

Check the latest version here https://github.com/cotterapp/android-sdk/releases. Then sync your gradle files.

Step 2: Setting Authentication Methods

You need to set allowed methods for authenticating your users. To allow TRUSTED DEVICES, go to https://dev.cotter.app/rules

Remember to set the correct Project in the dropdown list

Step 3: Creating a User

To use Cotter's sign in with device functionality, you need to first register the user to Cotter.

Registering a User to Cotter

Your server should do this request to Cotter's server during Registration.

curl -XPOST \
-H 'API_KEY_ID: <your key id>' \
-H 'API_SECRET_KEY: <your secret key>' \
-H "Content-Type: application/json" \
-d '{"client_user_id": "<Your User Identifier (string)>"}' \
'https://www.cotter.app/api/v0/user/create'

Check out Create a User API Reference for full Description

Step 4: Initialize Cotter

You can now use the SDK to enroll and verify Trusted Devices for the user you just created.

In your MainActivity, initialize Cotter with API keys, Cotter's URL to use (prod/test), and your User ID.

Production Endpoint: https://www.cotter.app/api/v0

Cotter.init(
    this.getApplicationContext(),
    "https://www.cotter.app/api/v0",
    "<User ID>", // fill with empty string for Verify EMAIL & PHONE
    "<API_KEY_ID>",
    "<API_SECRET_KEY>" // fill with empty string for Verify EMAIL & PHONE
);

Example:

MainActivity.java
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...

        Cotter.init(
            this.getApplicationContext(),
            "https://www.cotter.app/api/v0",
            "hello@example.com",
            "<API_KEY_ID>",
            "<API_SECRET_KEY>");
    }
    ...

Step 5: Register this device as a Registered Device

Right after your user finished their registration to your app, you should automatically register the device as a Trusted Device (or prompt your user if they want to make this device as a Trusted Device).

To enroll the current device as a TrustedDevice:

TrustedDeviceHelper.enrollDevice(this, new Callback() {
    @Override
    public void onSuccess(JSONObject result) {
        Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError(String error) {
        Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show();
    }
});

This method is only for the first Trusted Device. You will get an error if you attempt to enroll another Trusted Device using enrollDevice when there's already a Trusted Device for the account. To enroll other devices, see Add a new Trusted Device.

Step 6: Authenticate from a Trusted Device

To authenticate a device, call the TrustedDeviceHelper.requestAuth function. This will automatically detect whether the current device is a Trusted Device or not.

TrustedDeviceHelper.requestAuth(this, "<EVENT NAME>", this, Dashboard.class, new Callback() {
    @Override
    public void onSuccess(JSONObject result) {
        Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_SHORT).show();
        
        // Pass in the `result` to your backend server when:
        // - Logging in your users
        // - making a transaction
        // - fetching sensitive data
    }

    @Override
    public void onError(String error) {
        Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show();
    }
});

Parameters

requestAuth(Context ctx, String event, AppCompatActivity act, Class callbackClass, Callback callback)
  • event: A name to identify the event action type, like "LOGIN" or "TRANSACTION".

  • callbackClass : The next activity class that you want to redirect to when the authentication request is finished

  • callback : A custom Callback function that implements 2 methods: onSuccess and onError.

When an Authentication Event is requested using method TRUSTED_DEVICE, there are 2 possible cases:

Case 1: The current device is a Trusted Device

If the current device is a Trusted Device, it should automatically be approved, and you will receive a JSON result containing the requested Event and whether or not it's approved. The approval is based on whether or not the signature included in the request from the SDK is valid.

{
  ...
  "method": "TRUSTED_DEVICE",
  "new": false, // Is this a new pending event (should be false).
  "approved": true, // Is this event approved (should be true).
  
  "oauth_token": {
    "access_token": "eyJhbGciOiJFUz...", // validate this access token
    "id_token": "eyJhbGciOiJFUz...",
    "refresh_token": "5:cYIfabtspE1cBeD7KP...",
    "expires_in": 3600,
    "token_type": "Bearer",
    "auth_method": "TRUSTED_DEVICE"
  }
}

You should see a result that the event is not new, and that it's approved. This is because the signature from the Trusted Device is sufficient to prove that the device is authorized.

When passing this Response to your backend, you need to check if this JSON is valid and if it comes from Cotter's server by validating the access token.

Checkout how to verify the OAuth Tokens from Cotter here:

pageVerifying JWT Tokens

Case 2: The current device is NOT a Trusted Device

We'll cover this in the next guide:

pageAuthenticate from a Non-Trusted Device

🎉 You're done!

Next Steps

pageAuthenticate from a Non-Trusted DevicepageAdd a new Trusted DevicepageCustomization

Add Biometric / PIN

pageBiometric/Pin

Last updated