1e41f4b71Sopenharmony_ci# Querying an Asset with User Authentication (ArkTS)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Available APIs
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciThe following table describes the APIs used for querying an asset with user authentication. For more information, see the API reference.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci| Asynchronous API| Synchronous API| Description|
8e41f4b71Sopenharmony_ci| ----- | ------ | ------- |
9e41f4b71Sopenharmony_ci| [preQuery(query: AssetMap)](../../reference/apis-asset-store-kit/js-apis-asset.md#assetprequery) | [preQuerySync(query: AssetMap)](../../reference/apis-asset-store-kit/js-apis-asset.md#assetprequerysync12) | Performs preprocessing.|
10e41f4b71Sopenharmony_ci| [query(query: AssetMap)](../../reference/apis-asset-store-kit/js-apis-asset.md#assetquery) | [querySync(query: AssetMap)](../../reference/apis-asset-store-kit/js-apis-asset.md#assetquerysync12) | Queries the asset.|
11e41f4b71Sopenharmony_ci| [postQuery(handle: AssetMap)](../../reference/apis-asset-store-kit/js-apis-asset.md#assetpostquery) | [postQuerySync(handle: AssetMap)](../../reference/apis-asset-store-kit/js-apis-asset.md#assetpostquerysync12) | Performs postprocessing.|
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ciThe following table describes the attributes of **AssetMap** used for querying an asset with user authentication.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci>**NOTE**
16e41f4b71Sopenharmony_ci>
17e41f4b71Sopenharmony_ci>In the following table, the attributes starting with **DATA_LABEL** are custom asset attributes reserved for services. These attributes are not encrypted. Therefore, do not put personal data in these attributes.
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci- **preQuery()** parameters
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci  | Attribute Name (Tag)       | Value          | Mandatory | Description                 |
22e41f4b71Sopenharmony_ci  | --------------------- | ---------------------------| -------- | -------------------------- |
23e41f4b71Sopenharmony_ci  | ALIAS                 | Type: Uint8Array<br>Length: 1-256 bytes                           | No    | Asset alias, which uniquely identifies an asset.         |
24e41f4b71Sopenharmony_ci  | ACCESSIBILITY         | Type: number<br>Value range: see [Accessibility](../../reference/apis-asset-store-kit/js-apis-asset.md#accessibility)| No    | Access control based on the lock screen status.                                    |
25e41f4b71Sopenharmony_ci  | REQUIRE_PASSWORD_SET  | Type: bool                                                  | No    | Whether the asset is accessible only when a lock screen password is set.    |
26e41f4b71Sopenharmony_ci  | AUTH_TYPE             | Type: number<br>Value range: see [AuthType](../../reference/apis-asset-store-kit/js-apis-asset.md#authtype)| No    | Type of user authentication required for accessing the asset.                 |
27e41f4b71Sopenharmony_ci  | AUTH_VALIDITY_PERIOD  | Type: number<br>Value range: 1-600 seconds       | No    | Validity period of the user authentication.            |
28e41f4b71Sopenharmony_ci  | SYNC_TYPE             | Type: number<br>Value range: see [SyncType](../../reference/apis-asset-store-kit/js-apis-asset.md#synctype)| No    | Type of sync supported by the asset.                          |
29e41f4b71Sopenharmony_ci  | IS_PERSISTENT         | Type: bool                                                  | No    | Whether to retain the asset when the application is uninstalled.                |
30e41f4b71Sopenharmony_ci  | DATA_LABEL_CRITICAL_1 | Type: Uint8Array<br>Length: 1-2048 bytes                      | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
31e41f4b71Sopenharmony_ci  | DATA_LABEL_CRITICAL_2 | Type: Uint8Array<br>Length: 1-2048 bytes                     | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
32e41f4b71Sopenharmony_ci  | DATA_LABEL_CRITICAL_3 | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
33e41f4b71Sopenharmony_ci  | DATA_LABEL_CRITICAL_4 | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
34e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_1   | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
35e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_2   | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
36e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_3   | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
37e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_4   | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
38e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_LOCAL_1<sup>12+</sup> | Type: Uint8Array<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
39e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_LOCAL_2<sup>12+</sup> | Type: Uint8Array<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
40e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_LOCAL_3<sup>12+</sup> | Type: Uint8Array<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
41e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_LOCAL_4<sup>12+</sup> | Type: Uint8Array<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
42e41f4b71Sopenharmony_ci  | REQUIRE_ATTR_ENCRYPTED<sup>13+</sup> | Type: bool| No| Whether to query the customized asset attribute information that is encrypted. By default, the unencrypted, customized asset attribute information is queried.|
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci- **query()** parameters
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci  | Attribute Name (Tag)       | Value                                            | Mandatory | Description                                            |
47e41f4b71Sopenharmony_ci  | --------------------- | ------------------------------------------------------------ | -------- | ------------------------------------------------ |
48e41f4b71Sopenharmony_ci  | ALIAS                 | Type: Uint8Array<br>Length: 1-256 bytes                           | Yes    | Asset alias, which uniquely identifies an asset.      |
49e41f4b71Sopenharmony_ci  | AUTH_CHALLENGE        | Type: Uint8Array<br>Length: 32 bytes                              | Yes    | Challenge for the user authentication.                             |
50e41f4b71Sopenharmony_ci  | AUTH_TOKEN            | Type: Uint8Array<br>Length: 148 bytes                             | Yes    | Authorization token obtained after the user authentication is successful.                        |
51e41f4b71Sopenharmony_ci  | RETURN_TYPE           | Type: number                          | Yes    | Type of the asset query result to return.                   |
52e41f4b71Sopenharmony_ci  | ACCESSIBILITY         | Type: number<br>Value range: see [Accessibility](../../reference/apis-asset-store-kit/js-apis-asset.md#accessibility)| No    | Access control based on the lock screen status.                                    |
53e41f4b71Sopenharmony_ci  | REQUIRE_PASSWORD_SET  | Type: bool                                                  | No    | Whether the asset is accessible only when a lock screen password is set.    |
54e41f4b71Sopenharmony_ci  | AUTH_TYPE             | Type: number<br>Value range: see [AuthType](../../reference/apis-asset-store-kit/js-apis-asset.md#authtype)| No    | Type of user authentication required for accessing the asset.                 |
55e41f4b71Sopenharmony_ci  | SYNC_TYPE             | Type: number<br>Value range: see [SyncType](../../reference/apis-asset-store-kit/js-apis-asset.md#synctype)| No    | Type of sync supported by the asset.                          |
56e41f4b71Sopenharmony_ci  | IS_PERSISTENT         | Type: bool                                                  | No    | Whether to retain the asset when the application is uninstalled.                |
57e41f4b71Sopenharmony_ci  | DATA_LABEL_CRITICAL_1 | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
58e41f4b71Sopenharmony_ci  | DATA_LABEL_CRITICAL_2 | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
59e41f4b71Sopenharmony_ci  | DATA_LABEL_CRITICAL_3 | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
60e41f4b71Sopenharmony_ci  | DATA_LABEL_CRITICAL_4 | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
61e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_1   | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
62e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_2   | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
63e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_3   | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
64e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_4   | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
65e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_LOCAL_1<sup>12+</sup> | Type: Uint8Array<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
66e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_LOCAL_2<sup>12+</sup> | Type: Uint8Array<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
67e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_LOCAL_3<sup>12+</sup> | Type: Uint8Array<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
68e41f4b71Sopenharmony_ci  | DATA_LABEL_NORMAL_LOCAL_4<sup>12+</sup> | Type: Uint8Array<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
69e41f4b71Sopenharmony_ci  | REQUIRE_ATTR_ENCRYPTED<sup>13+</sup> | Type: bool| No| Whether to query the customized asset attribute information that is encrypted. By default, the unencrypted, customized asset attribute information is queried.|
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci- **postQuery()** parameters
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci  | Attribute Name (Tag)     | Value              | Mandatory | Description                |
74e41f4b71Sopenharmony_ci  | ------------------- | ------------------------------ | -------- | -------------------- |
75e41f4b71Sopenharmony_ci  | AUTH_CHALLENGE      | Type: Uint8Array<br>Length: 32 bytes| Yes    | Challenge for the user authentication.|
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci## Example
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci> **NOTE**
80e41f4b71Sopenharmony_ci>
81e41f4b71Sopenharmony_ci> The **asset** module provides asynchronous and synchronous APIs. The following uses the asynchronous APIs as an example. For more information about the APIs, see [Asset Store Service](../../reference/apis-asset-store-kit/js-apis-asset.md).
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ciQuery asset **demo_alias** with user authentication.
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci```typescript
86e41f4b71Sopenharmony_ciimport { asset } from '@kit.AssetStoreKit';
87e41f4b71Sopenharmony_ciimport { util } from '@kit.ArkTS';
88e41f4b71Sopenharmony_ciimport userAuth from '@ohos.userIAM.userAuth';
89e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_cifunction stringToArray(str: string): Uint8Array {
92e41f4b71Sopenharmony_ci  let textEncoder = new util.TextEncoder();
93e41f4b71Sopenharmony_ci  return textEncoder.encodeInto(str);
94e41f4b71Sopenharmony_ci}
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_cifunction arrayToString(arr: Uint8Array): string {
97e41f4b71Sopenharmony_ci  let textDecoder = util.TextDecoder.create("utf-8", { ignoreBOM: true });
98e41f4b71Sopenharmony_ci  let str = textDecoder.decodeWithStream(arr, { stream: false })
99e41f4b71Sopenharmony_ci  return str;
100e41f4b71Sopenharmony_ci}
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ciasync function userAuthenticate(challenge: Uint8Array): Promise<Uint8Array> {
103e41f4b71Sopenharmony_ci  return new Promise((resolve, reject) => {
104e41f4b71Sopenharmony_ci    const authParam: userAuth.AuthParam = {
105e41f4b71Sopenharmony_ci      challenge: challenge,
106e41f4b71Sopenharmony_ci      authType: [userAuth.UserAuthType.PIN],
107e41f4b71Sopenharmony_ci      authTrustLevel: userAuth.AuthTrustLevel.ATL1,
108e41f4b71Sopenharmony_ci    };
109e41f4b71Sopenharmony_ci    const widgetParam: userAuth.WidgetParam = { title:' Enter the lock screen password. '};
110e41f4b71Sopenharmony_ci    try {
111e41f4b71Sopenharmony_ci      let userAuthInstance = userAuth.getUserAuthInstance(authParam, widgetParam);
112e41f4b71Sopenharmony_ci      userAuthInstance.on('result', {
113e41f4b71Sopenharmony_ci        onResult(result) {
114e41f4b71Sopenharmony_ci          if (result.result == userAuth.UserAuthResultCode.SUCCESS) {
115e41f4b71Sopenharmony_ci            console.info(`User identity authentication succeeded.`);
116e41f4b71Sopenharmony_ci            resolve(result.token);
117e41f4b71Sopenharmony_ci          } else {
118e41f4b71Sopenharmony_ci            console.error(`User identity authentication failed.`);
119e41f4b71Sopenharmony_ci            reject();
120e41f4b71Sopenharmony_ci          }
121e41f4b71Sopenharmony_ci        }
122e41f4b71Sopenharmony_ci      });
123e41f4b71Sopenharmony_ci      userAuthInstance.start();
124e41f4b71Sopenharmony_ci    } catch (error) {
125e41f4b71Sopenharmony_ci      let err = error as BusinessError;
126e41f4b71Sopenharmony_ci      console.error(`User identity authentication failed. Code is ${err.code}, message is ${err.message}`);
127e41f4b71Sopenharmony_ci      reject();
128e41f4b71Sopenharmony_ci    }
129e41f4b71Sopenharmony_ci  })
130e41f4b71Sopenharmony_ci}
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_cifunction preQueryAsset(): Promise<Uint8Array> {
133e41f4b71Sopenharmony_ci  return new Promise((resolve, reject) => {
134e41f4b71Sopenharmony_ci    try {
135e41f4b71Sopenharmony_ci      let query: asset.AssetMap = new Map();
136e41f4b71Sopenharmony_ci      query.set(asset.Tag.ALIAS, stringToArray('demo_alias'));
137e41f4b71Sopenharmony_ci      asset.preQuery(query).then((challenge: Uint8Array) => {
138e41f4b71Sopenharmony_ci        resolve(challenge);
139e41f4b71Sopenharmony_ci      }).catch(() => {
140e41f4b71Sopenharmony_ci        reject();
141e41f4b71Sopenharmony_ci      })
142e41f4b71Sopenharmony_ci    } catch (error) {
143e41f4b71Sopenharmony_ci      let err = error as BusinessError;
144e41f4b71Sopenharmony_ci      console.error(`Failed to pre-query Asset. Code is ${err.code}, message is ${err.message}`);
145e41f4b71Sopenharmony_ci      reject();
146e41f4b71Sopenharmony_ci    }
147e41f4b71Sopenharmony_ci  });
148e41f4b71Sopenharmony_ci}
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ciasync function postQueryAsset(challenge: Uint8Array) {
151e41f4b71Sopenharmony_ci  let handle: asset.AssetMap = new Map();
152e41f4b71Sopenharmony_ci  handle.set(asset.Tag.AUTH_CHALLENGE, challenge);
153e41f4b71Sopenharmony_ci  try {
154e41f4b71Sopenharmony_ci    await asset.postQuery(handle);
155e41f4b71Sopenharmony_ci    console.info(`Succeeded in post-querying Asset.`);
156e41f4b71Sopenharmony_ci  } catch (error) {
157e41f4b71Sopenharmony_ci    let err = error as BusinessError;
158e41f4b71Sopenharmony_ci    console.error(`Failed to post-query Asset. Code is ${err.code}, message is ${err.message}`);
159e41f4b71Sopenharmony_ci  }
160e41f4b71Sopenharmony_ci}
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ciasync function queryAsset() {
163e41f4b71Sopenharmony_ci  // step1. Call asset.preQuery to obtain the challenge value.
164e41f4b71Sopenharmony_ci  preQueryAsset().then(async (challenge: Uint8Array) => {
165e41f4b71Sopenharmony_ci    try {
166e41f4b71Sopenharmony_ci      // Step 2. Pass in the challenge value to start the user authentication dialog box.
167e41f4b71Sopenharmony_ci      let authToken: Uint8Array = await userAuthenticate(challenge);
168e41f4b71Sopenharmony_ci      // Step 3 After the user authentication is successful, pass in the challenge value and authorization token to query the plaintext of the asset.
169e41f4b71Sopenharmony_ci      let query: asset.AssetMap = new Map();
170e41f4b71Sopenharmony_ci      query.set(asset.Tag.ALIAS, stringToArray('demo_alias'));
171e41f4b71Sopenharmony_ci      query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL);
172e41f4b71Sopenharmony_ci      query.set(asset.Tag.AUTH_CHALLENGE, challenge);
173e41f4b71Sopenharmony_ci      query.set(asset.Tag.AUTH_TOKEN, authToken);
174e41f4b71Sopenharmony_ci      let res: Array<asset.AssetMap> = await asset.query(query);
175e41f4b71Sopenharmony_ci      for (let i = 0; i < res.length; i++) {
176e41f4b71Sopenharmony_ci        // parse the secret.
177e41f4b71Sopenharmony_ci        let secret: Uint8Array = res[i].get(asset.Tag.SECRET) as Uint8Array;
178e41f4b71Sopenharmony_ci        // parse uint8array to string
179e41f4b71Sopenharmony_ci        let secretStr: string = arrayToString(secret);
180e41f4b71Sopenharmony_ci      }
181e41f4b71Sopenharmony_ci      // Step 4. After the plaintext is obtained, call asset.postQuery to perform postprocessing.
182e41f4b71Sopenharmony_ci      postQueryAsset(challenge);
183e41f4b71Sopenharmony_ci    } catch (error) {
184e41f4b71Sopenharmony_ci      // Step 5. If the operation after preQuery() fails, call asset.postQuery to perform postprocessing.
185e41f4b71Sopenharmony_ci      postQueryAsset(challenge);
186e41f4b71Sopenharmony_ci    }
187e41f4b71Sopenharmony_ci  }).catch ((err: BusinessError) => {
188e41f4b71Sopenharmony_ci    console.error(`Failed to pre-query Asset. Code is ${err.code}, message is ${err.message}`);
189e41f4b71Sopenharmony_ci  })
190e41f4b71Sopenharmony_ci}
191e41f4b71Sopenharmony_ci```
192