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