[Huawei Developers] Basavaraj.navi:
Introduction

In this article, we will build two applications App1 and App2. And Shares the user's credentials between App1 and App2.

Keyring offers the Credentials Management API for storing user credentials locally on Android phones and tablets and sharing them between different apps and different platform versions of an app.

Shares the user's credentials between your Android apps, quick apps and web apps.

Credential Management Services

Secure local storage of user credentials

Your app stores a signed-in user's credentials in Keyring for automatic sign-in later. Keyring encrypts the credentials and stores them on the user's device. When storing credentials, you can set whether to verify the user's biometric feature or lock screen password when an app tries to access the stored credentials.

When the user opens your app next time, it will search Keyring for the available credentials, which may be those stored in this app or those from other apps and shared for this app to access.

Credential sharing

If you have multiple apps, you can share credentials stored on one app to other apps. Once a user signs in to this app, all the other apps can conveniently use the credentials from this app to sign the user in.

When storing credentials, you can set the sharing relationship of the credentials and share the credentials stored on one app to other apps. The credentials can be shared between Android apps, quick apps and web apps.

Prerequisites

JDK version: 1.7 or later

Android Studio version: 3.6.1 or later

minSdkVersion: 24 or later

targetSdkVersion: 30 (recommended)

compileSdkVersion: 30 (recommended)

Gradle version: 5.4.1 or later (recommended)

Test device: a Huawei phone running EMUI 5.0 or later

How to integrate Huawei Keyring in Android?

To achieve this you need to follow the steps.

1. Configure application on the AGC.

2. Client application development process.

Configure application on the AGC

Follow the steps.

Step 1: We need to register as a developer account in AppGallery Connect. If you are already developer ignore this step.

Step 2: Create an app by referring to Creating a Project and Creating an App in the Project

Step 3: Set the data storage location based on current location.

Step 4: Enabling Keyring Kit. Project setting > Manage API > Enable Keyring kit toggle button.

Step 5: Generating a Signing Certificate Fingerprint.

Step 6: Configuring the Signing Certificate Fingerprint.

Step 7: Download your agconnect-services.json file, paste it into the app root directory.

Client application development process

Follow the steps.

Step 1: Create Android application in the Android studio (Any IDE which is your favorite)

Step 2: Add the App level gradle dependencies. Choose inside project Android > app > build.gradle.

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
dependencies {
implementation "com.huawei.hms:keyring-credential:6.1.1.302"
}

Root level gradle dependencies.

maven { url 'https://developer.huawei.com/repo/' }

classpath 'com.huawei.agconnect:agcp:1.4.1.300'

Step 3: Build Application.

Application 1

private fun checkInput(): Boolean {
val username = mUsername!!.text.toString().trim { it <= ' ' }
val password = mPassword!!.text.toString().trim { it <= ' ' }
return if (username.isEmpty() || password.isEmpty()) {
showMessage(R.string.invalid_input)
false
} else {
true
}
}

private fun login(view: View) {
if (!checkInput()) {
return
}
val username = mUsername!!.text.toString().trim { it <= ' ' }
val password = mPassword!!.text.toString().trim { it <= ' ' }
saveCredential(username, password,
"app2", "com.huawei.hms.keyring.sample2",
"3E:29:29:33:68:91:28:FF:58:54:B7:1D:1B:B3:9E:61:AD:C8:32:78:A3:81:B0:E2:9A:9E:35:6E:02:5D:2E:FB",
true)
}

private fun saveCredential(username: String, password: String,
sharedToAppName: String, sharedToAppPackage: String,
sharedToAppCertHash: String, userAuth: Boolean) {

val app2 = AndroidAppIdentity(sharedToAppName,
sharedToAppPackage, sharedToAppCertHash)
val sharedAppList: MutableList<AppIdentity> = ArrayList()
sharedAppList.add(app2)
val credential = Credential(username, CredentialType.PASSWORD, userAuth, password.toByteArray())
credential.setDisplayName("nickname_" + username)
credential.setSharedWith(sharedAppList)
credential.syncable = true
val credentialClient = CredentialManager.getCredentialClient(this)
credentialClient.saveCredential(credential, object : CredentialCallback<Void?> {
override fun onSuccess(unused: Void?) {
showMessage(R.string.save_credential_ok)
}

override fun onFailure(errorCode: Long, description: CharSequence) {
showMessage(R.string.save_credential_failed.toString() + " " + errorCode + ":" + description)
}
})
}

private fun deleteCredential(credential: Credential) {
val credentialClient = CredentialManager.getCredentialClient(this)
credentialClient.deleteCredential(credential, object : CredentialCallback<Void?> {
override fun onSuccess(unused: Void?) {
val hint = String.format(resources.getString(R.string.delete_ok),
credential.getUsername())
showMessage(hint)
}

override fun onFailure(errorCode: Long, description: CharSequence) {
val hint = String.format(resources.getString(R.string.delete_failed),
description)
showMessage(hint)
}
})
}

private fun delete(view: View) {
val username = mUsername!!.text.toString().trim { it <= ' ' }
if (username.isEmpty()) {
return
}
val credentialClient = CredentialManager.getCredentialClient(this)
val trustedAppList: List<AppIdentity> = ArrayList()
credentialClient.findCredential(trustedAppList, object : CredentialCallback<List<Credential>> {
override fun onSuccess(credentials: List<Credential>) {
if (credentials.isEmpty()) {
showMessage(R.string.no_available_credential)
} else {
for (credential in credentials) {
if (credential.getUsername() == username) {
deleteCredential(credential)
break
}
}
}
}

override fun onFailure(errorCode: Long, description: CharSequence) {
showMessage(R.string.query_credential_failed)
}
})
}

Result​

View attachment 5549021

View attachment 5549023

Tips and Tricks​

Add the agconnect-service.json.

Make sure you are testing application in non-rooted device.

Latest HMS Core APK is required.

Set minSDK 24 or later.

Find all result code here

It also support non Huawei phones but android version should be 7 or later.

Conclusion​

In this article, we have learnt integration of Huawei Keyring. Sharing the password between two applications. And credential management services. Sign in with different application using single stored password.

Reference​

Keyring