Latest commit ca85452 Nov 10, 2016

Mobile app showing the AeroGear OTP feature on Android

Author: Daniel Passos (dpassos)
Level: Beginner
Technologies: Java, Android
Summary: A basic example how to use OTP

What is it?

Two-Factor project demonstrates how to include OTP functionality in Android applications.

How do I run it?

System Requirements

Build Application

$ cd /path/to/Two-Factor/
$ ./gradlew clean build

Running the app

To deploy, run and debug the application on an Android device attached to your system, on the command line enter the following:

  1. Install generated apk to device

    $ cd /path/to/Two-Factor
    $ ./gradlew installDebug
  2. Open app on device

  3. You can use the browser-authenticator website to play with the app.

How does it work?

OTPQRCodeActivity will invoke the zxing

new IntentIntegrator(this).initiateScan();

After zxing finish the onActivityResult will be called, with a otpauth. It will be passed to OTPCodeActivity for show the token

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null) {
        if(result.getContents() != null) {
            Log.d(TAG, result.getContents());
            Intent intent = new Intent(this, OTPCodeActivity.class);
            intent.putExtra("otpauth", result.getContents());
        } else {
            // Cancelled
    } else {
        super.onActivityResult(requestCode, resultCode, data);

parseOtpPath will parse the secret and Totp to show generate the token

private void parseOtpPath() {
    String otpauth = getIntent().getStringExtra("otpauth");
    Uri otpUri = Uri.parse(otpauth);

    name = otpUri.getQueryParameter("issuer");
    secret = otpUri.getQueryParameter("secret");

    totp = new Totp(secret);