目录

Integrating Keyring

Overview

Keyring allows you to share the sign-in credentials between your Android apps, quick apps, and web apps.

Currently, quick apps have supported Keyring. With Keyring, your app can quickly obtain sign-in information based on user sign-in credentials, simplifying the sign-in process and reducing the sign-in cost.

Before You Start

Before integrating Keyring into your app, complete the following preparations:

Integration Guide

[h2]Checking Whether Keyring Is Supported by Your Device

Call keyring.getProvider to check whether Keyring is supported. If not, an empty string is returned. In this case, do not call other APIs; otherwise, an exception will occur. If a service provider is returned, you can call other APIs.

[h2]Saving a User Credential

Call keyring.saveCredential to save a user credential.

The following sample code saves new data:

keyring.saveCredential({
  type: 0,
  syncable: "false",
  userAuth: "true",
  username: "Zhang San",
  newContent: "123456",
  success: function (msg) {
    prompt.showToast({
      message: msg,
      duration: 2000,
      gravity: 'bottom'
    })
  },
  fail: function (code,msg) {
    prompt.showToast({
      message: code + msg,
      duration: 2000,
      gravity: 'bottom'
    })
  }
})

The following sample code saves updated data:

keyring.saveCredential({
  type: 0,
  syncable: false,
  userAuth: true,
  username: "Zhang San",
  newContent: "12345678",
  sharedWith: [{
    certHash: "${signing certificate fingerprint for Android}",
    name: "${Android app name}",
    packageName: "${Android app package name}",
    type: "android_app"
  },
  {
    name: "${website name}",
    domain: "${website domain name}",
    type: "web"
  }],
  owner: {
    certHash: "3a:25:d7:19:53:9b:39:b9:67:48:c4:85:86:bb:d7:f1:dd:53:e5:e9:29:12:77:0e:f2:5f:4e:7e:9a:b3:7f:cc",
    name: "Keyring_Test",
    packageName: "com.huawei.keyring",
    type: "quick_app"
  },
  success: function (msg) {
    prompt.showToast({
      message: msg,
      duration: 2000,
      gravity: 'bottom'
    })
  },
  fail: function (msg) {
    prompt.showToast({
      message: msg,
      duration: 2000,
      gravity: 'bottom'
    })
  }
})

[h2]Deleting a User Credential

Call keyring.deleteCredential to delete a saved user credential.

keyring.deleteCredential({
  owner: {
    certHash: "3a:25:d7:19:53:9b:39:b9:67:48:c4:85:86:bb:d7:f1:dd:53:e5:e9:29:12:77:0e:f2:5f:4e:7e:9a:b3:7f:cc",
    name: "Keyring_Test",
    packageName: "com.huawei.keyring",
    type: "quick_app"
  },
  "type": 0,
  "username": "Zhang San",
  success: function (msg) {
    prompt.showToast({
      message: msg,
      duration: 2000,
      gravity: 'bottom'
    })
  },
  fail: function (code, msg) {
    prompt.showToast({
      message: code + msg,
      duration: 2000,
      gravity: 'bottom'
    })
  }
})

[h2]Querying a User Credential

Call keyring.findCredential to query the sign-in credential of a user.

keyring.findCredential({
  trustedApps: [{
      type: 'quick_app',
      name: 'AndroidAppIdentity',
      packageName: 'com.huawei.hms.keyring.test',
      certHash: "3C:99:C3:69:27:75:46:FD:E4:14:82:C0:C1:FB:75:54:94:A7:A8:63:CA:9C:A7:BB:4B:9D:48:C4:04:86:9A:93"
    }],
  success: function (data) {
    credential = data // Returned data list.
    prompt.showToast({
      message: data,
      duration: 2000,
      gravity: 'bottom'
    })
  },
  fail: function (code, msg) {
    prompt.showToast({
      message: code + msg,
      duration: 2000,
      gravity: 'bottom'
    })
  }
})

[h2]Obtaining Sensitive Information from the Target Credential

Call keyring.findCredential to query the credential list of the app and then call keyring.getCredentialContent to obtain sensitive information from the target credential.

keyring.getCredentialContent({
  owner: {
    certHash: "3C:99:C3:69:27:75:46:FD:E4:14:82:C0:C1:FB:75:54:94:A7:A8:63:CA:9C:A7:BB:4B:9D:48:C4:04:86:9A:93",
    name: "AndroidAppIdentity",
    packageName: "com.huawei.hms.keyring.test",
    type: "quick_app"
  },
  type: 0,
  username: "Zhang San",
  success: function (data) {
    prompt.showToast({
      message: data, // Returned sensitive information.
      duration: 2000,
      gravity: 'bottom'
    })
  },
  fail: function (msg, code) {
    prompt.showToast({
      message: code + msg,
      duration: 2000,
      gravity: 'bottom'
    })
  }
})