Latest commit ca85452 Nov 10, 2016

README.md

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
Source: https://github.com/aerogear/aerogear-android-cookbook/tree/master/Two-Factor

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

@Override
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());
            startActivity(intent);
            finish();
        } else {
            // Cancelled
            finish();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
        showAlertDialog();
    }
}

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);
}