TL;DR: Get a sample Android app with Smart Lock configured and ready to use from this Github repository.
Members of our team participated in the last Google I/O to learn about the latest trends. There were a lot of cool new things announced, but these two particularly caught our eyes: Android Fingerprint Support & Smart Lock for Passwords. In this blog post, we’ll focus on the latter.
Smart Lock for Passwords is a service that makes handling your user’s credentials a pain-free process. It also allow friction-free log-in and sign-up for your web & Android apps. In other words, the idea is that you never have to remember your passwords again :). Have you heard about 1Password or LastPass? Well, imagine that, but from Google.
If you’re writing a WebApp, password management can be handled by Chrome itself. However, for Android apps. you need to do some extra work to add it. After playing around with the samples, we decided that it would be great to integrate it with our Lock for Android to make it even easier to authenticate your users and keep their credentials safe. With that in mind, we created a new library Lock-SmartLock. In this blog post, we’ll learn how to use it!
Before we start
First, you need to make sure you have updated Google Play services in your Android SDK installation. (You can update it using Android SDK Manager)
Then, follow this guide to configure your Google Developers Console project and associate it with your Android application so it can use SmartLock API.
In order to use Lock-SmartLock, you’ll need to create an Auth0 application. You can create one from the Auth0 dashboard
Let’s add Lock & SmartLock
Configuring Lock-Smartlock
First, add Lock-SmartLock to your build.gradle
file.
compile ‘com.auth0.android:appcompat-v7:22.2.0’
Then, in your res/values
folder, create a resource file named auth0.xml
that will include your Auth0 credentials:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="auth0_client_id">{YOUR_CLIENT_ID}</string>
<string name="auth0_domain_name">{YOUR_DOMAIN}</string>
<string name="auth0_scheme">a0{YOUR_CLIENT_ID_LOWERCASE}</string>
</resources>
In order to authenticate the user, Lock needs access to the Internet. Let’s request that permission in your AndroidManifest.xml
:
<uses-permission android:name="android.permission.INTERNET"/>
Implementing Lock-Smartlock
Now it’s time to register Lock for Android and Google Play services for your application in your AndroidManifest.xml
:
<!--Auth0 Lock-->
<activity
android:name="com.auth0.lock.LockActivity"
android:theme="@style/Lock.Theme"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="@string/auth0_scheme" android:host="@string/auth0_domain_name"/>
</intent-filter>
</activity>
<meta-data android:name="com.auth0.lock.client-id" android:value="@string/auth0_client_id"/>
<meta-data android:name="com.auth0.lock.domain-url" android:value="@string/auth0_domain_name"/>
<!--Auth0 Lock End-->
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
Then, we need to create a custom class that extends from android.app.Application
and implements LockProvider
. In this class, we’ll create an instance of SmartLock
and store it so that we can use it from any Activity
.
public class MyApplication extends Application implements LockProvider {
private Lock lock;
@Override
public void onCreate() {
super.onCreate();
lock = new SmartLock.Builder(this)
.loadFromApplication(this)
.build();
}
@Override
public Lock getLock() {
return lock;
}
}
Remember to register this class as your application class in the
AndroidManfifest.xml
, too.Then, in the Activity that will trigger authentication, override the following lifecycle callbacks that will allow Lock to interact with Smart Lock:
@Override
protected void onStart() {
super.onStart();
SmartLock.getSmartLock(this).onStart();
}
@Override
protected void onStop() {
super.onStop();
SmartLock.getSmartLock(this).onStop();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
SmartLock.getSmartLock(this).onActivityResult(this, requestCode, resultCode, data);
}
The layout of the Activity is up to you; we only need a way to trigger the authentication (either a button or automatically from code).
Then, we need to create a BroadcastReceiver
to handle the events broadcasted by Lock when the user authenticates:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//... Setup your Activity
broadcastManager = LocalBroadcastManager.getInstance(this);
broadcastManager.registerReceiver(authenticationReceiver, new IntentFilter(Lock.AUTHENTICATION_ACTION));
}
@Override
protected void onDestroy() {
super.onDestroy();
//... Clean up your Activity
broadcastManager.unregisterReceiver(authenticationReceiver);
}
private BroadcastReceiver authenticationReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
UserProfile profile = intent.getParcelableExtra(Lock.AUTHENTICATION_ACTION_PROFILE_PARAMETER);
Token token = intent.getParcelableExtra(Lock.AUTHENTICATION_ACTION_TOKEN_PARAMETER);
}
};
Finally, in order to authenticate the user, you just call the following method method inside your Activity
SmartLock.getSmartLock(this).loginFromActivity(this);
Connect your device, and run the app. You’ll see something like this:
That’s it all, you have successfully integrated Auth0 and SmartLock!
Congrats ;)