js-apis-abilityAccessCtrl.md 26.6 KB
Newer Older
A
Annie_wang 已提交
1
# @ohos.abilityAccessCtrl (Ability Access Control)
W
wusongqing 已提交
2

W
wusongqing 已提交
3
The **AbilityAccessCtrl** module provides APIs for application permission management, including authentication, authorization, and revocation.
W
wusongqing 已提交
4 5

> **NOTE**
W
wusongqing 已提交
6 7 8 9
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

## Modules to Import

W
wusongqing 已提交
10
```js
W
wusongqing 已提交
11 12 13 14 15 16 17 18 19
import abilityAccessCtrl from '@ohos.abilityAccessCtrl'
```

## abilityAccessCtrl.createAtManager

createAtManager(): AtManager

Creates an **AtManager** instance, which is used for ability access control.

W
wusongqing 已提交
20 21 22
**System capability**: SystemCapability.Security.AccessToken


W
wusongqing 已提交
23 24
**Return value**

W
wusongqing 已提交
25 26
| Type| Description|
| -------- | -------- |
G
Gloria 已提交
27
| [AtManager](#atmanager) | **AtManager** instance created.|
W
wusongqing 已提交
28 29 30

**Example**

W
wusongqing 已提交
31
```js
A
Annie_wang 已提交
32
let atManager = abilityAccessCtrl.createAtManager();
W
wusongqing 已提交
33 34 35 36 37 38
```

## AtManager

Implements ability access control.

G
Gloria 已提交
39
### checkAccessToken<sup>9+</sup>
W
wusongqing 已提交
40

A
Annie_wang 已提交
41
checkAccessToken(tokenID: number, permissionName: Permissions): Promise&lt;GrantStatus&gt;
W
wusongqing 已提交
42

A
Annie_wang 已提交
43
Checks whether an application has the specified permission. This API uses a promise to return the result.
W
wusongqing 已提交
44 45

**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
46 47 48

**Parameters**

W
wusongqing 已提交
49 50
| Name  | Type                | Mandatory| Description                                      |
| -------- | -------------------  | ---- | ------------------------------------------ |
A
Annie_wang 已提交
51 52
| tokenID   |  number   | Yes  | Token ID of the application. The value can be obtained from [ApplicationInfo](js-apis-bundle-ApplicationInfo.md).            |
| permissionName | Permissions | Yes  | Permission to check.|
W
wusongqing 已提交
53 54 55

**Return value**

W
wusongqing 已提交
56 57
| Type         | Description                               |
| :------------ | :---------------------------------- |
G
Gloria 已提交
58
| Promise&lt;GrantStatus&gt; | Promise used to return the permission grant state.|
W
wusongqing 已提交
59

A
Annie_wang 已提交
60 61 62 63 64 65 66 67
**Error codes**

For details about the error codes, see [Ability Access Control Error Codes](../errorcodes/errorcode-access-token.md).

| ID| Error Message|
| -------- | -------- |
| 12100001 | The parameter is invalid. The tokenID is 0 |

W
wusongqing 已提交
68 69
**Example**

W
wusongqing 已提交
70
```js
A
Annie_wang 已提交
71
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
G
Gloria 已提交
72

A
Annie_wang 已提交
73
let atManager = abilityAccessCtrl.createAtManager();
G
Gloria 已提交
74 75
let tokenID = 0; // You can use getApplicationInfo to obtain the access token ID.
try {
A
Annie_wang 已提交
76
    atManager.checkAccessToken(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS").then((data) => {
G
Gloria 已提交
77 78 79 80 81 82 83
        console.log(`checkAccessToken success, data->${JSON.stringify(data)}`);
    }).catch((err) => {
        console.log(`checkAccessToken fail, err->${JSON.stringify(err)}`);
    });
} catch(err) {
    console.log(`catch err->${JSON.stringify(err)}`);
}
W
wusongqing 已提交
84 85
```

W
wusongqing 已提交
86 87
### verifyAccessTokenSync<sup>9+</sup>

A
Annie_wang 已提交
88
verifyAccessTokenSync(tokenID: number, permissionName: Permissions): GrantStatus
W
wusongqing 已提交
89

A
Annie_wang 已提交
90
Verifies whether an application has the specified permission. This API returns the result synchronously.
W
wusongqing 已提交
91 92 93 94 95 96 97

**System capability**: SystemCapability.Security.AccessToken

**Parameters**

| Name  | Type                | Mandatory| Description                                      |
| -------- | -------------------  | ---- | ------------------------------------------ |
G
Gloria 已提交
98
| tokenID   |  number   | Yes  | Token ID of the application.             |
A
Annie_wang 已提交
99
| permissionName | Permissions | Yes  | Name of the permission to verify.|
W
wusongqing 已提交
100 101 102 103 104 105 106

**Return value**

| Type         | Description                               |
| :------------ | :---------------------------------- |
| [GrantStatus](#grantstatus) | Permission grant state.|

A
Annie_wang 已提交
107 108 109 110 111 112 113 114
**Error codes**

For details about the error codes, see [Ability Access Control Error Codes](../errorcodes/errorcode-access-token.md).

| ID| Error Message|
| -------- | -------- |
| 12100001 | The parameter is invalid. The tokenID is 0 |

W
wusongqing 已提交
115 116 117
**Example**

```js
A
Annie_wang 已提交
118
let atManager = abilityAccessCtrl.createAtManager();
W
wusongqing 已提交
119
let tokenID = 0;
A
Annie_wang 已提交
120
let data = atManager.verifyAccessTokenSync(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS");
W
wusongqing 已提交
121 122 123
console.log(`data->${JSON.stringify(data)}`);
```

W
wusongqing 已提交
124 125
### grantUserGrantedPermission

A
Annie_wang 已提交
126
grantUserGrantedPermission(tokenID: number, permissionName: Permissions, permissionFlag: number): Promise&lt;void&gt;
W
wusongqing 已提交
127

A
Annie_wang 已提交
128
Grants a user_grant permission to an application. This API uses a promise to return the result.
W
wusongqing 已提交
129

A
Annie_wang 已提交
130
**System API**: This is a system API.
W
wusongqing 已提交
131

A
Annie_wang 已提交
132
**Required permissions**: ohos.permission.GRANT_SENSITIVE_PERMISSIONS (available only to system applications)
W
wusongqing 已提交
133

W
wusongqing 已提交
134
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
135 136 137

**Parameters**

W
wusongqing 已提交
138 139
| Name   | Type               | Mandatory| Description                                                        |
| --------- | ------------------- | ---- | ------------------------------------------------------------ |
G
Gloria 已提交
140
| tokenID      | number              | Yes  | Token ID of the application. The value can be obtained from [ApplicationInfo](js-apis-bundle-ApplicationInfo.md).           |
A
Annie_wang 已提交
141
| permissionName | Permissions              | Yes  | Name of the permission to grant.|
G
Gloria 已提交
142
| permissionFlag  | number | Yes  | Permission flag. The value **1** means that the permission request dialog box will still be displayed after the user grants or denies the permission. The value **2** means that no dialog box will be displayed after the user grants or denies the permission. The value **3** means a system permission that cannot be changed. |
W
wusongqing 已提交
143 144 145

**Return value**

W
wusongqing 已提交
146 147
| Type         | Description                               |
| :------------ | :---------------------------------- |
G
Gloria 已提交
148
| Promise&lt;void&gt; | Promise that returns no value.|
W
wusongqing 已提交
149

A
Annie_wang 已提交
150 151 152 153 154 155 156 157 158 159 160 161
**Error codes**

For details about the error codes, see [Ability Access Control Error Codes](../errorcodes/errorcode-access-token.md).

| ID| Error Message|
| -------- | -------- |
| 12100001 | The parameter is invalid. The tokenID is 0 |
| 12100002 | The specified tokenID does not exist. |
| 12100003 | The specified permission does not exist. |
| 12100006 | The application specified by the tokenID is not allowed to be granted with the specified permission. Either the application is a sandbox or the tokenID is from a remote device. |
| 12100007 | Service is abnormal. |

W
wusongqing 已提交
162 163
**Example**

W
wusongqing 已提交
164
```js
A
Annie_wang 已提交
165
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
G
Gloria 已提交
166

A
Annie_wang 已提交
167
let atManager = abilityAccessCtrl.createAtManager();
G
Gloria 已提交
168
let tokenID = 0; // You can use getApplicationInfo to obtain the access token ID.
W
wusongqing 已提交
169
let permissionFlag = 1;
G
Gloria 已提交
170
try {
A
Annie_wang 已提交
171
    atManager.grantUserGrantedPermission(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS", permissionFlag).then(() => {
G
Gloria 已提交
172 173 174 175 176 177 178
        console.log('grantUserGrantedPermission success');
    }).catch((err) => {
        console.log(`grantUserGrantedPermission fail, err->${JSON.stringify(err)}`);
    });
} catch(err) {
    console.log(`catch err->${JSON.stringify(err)}`);
}
W
wusongqing 已提交
179 180 181 182
```

### grantUserGrantedPermission

A
Annie_wang 已提交
183
grantUserGrantedPermission(tokenID: number, permissionName: Permissions, permissionFlag: number, callback: AsyncCallback&lt;void&gt;): void
W
wusongqing 已提交
184

A
Annie_wang 已提交
185
Grants a user_grant permission to an application. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
186

A
Annie_wang 已提交
187
**System API**: This is a system API.
W
wusongqing 已提交
188

A
Annie_wang 已提交
189
**Required permissions**: ohos.permission.GRANT_SENSITIVE_PERMISSIONS (available only to system applications)
W
wusongqing 已提交
190

W
wusongqing 已提交
191
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
192 193 194

**Parameters**

W
wusongqing 已提交
195 196
| Name   | Type               | Mandatory| Description                         |
| --------- | ------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
197 198
| tokenID      | number              | Yes  | Token ID of the application. The value can be obtained from [ApplicationInfo](js-apis-bundle-ApplicationInfo.md).|
| permissionName | Permissions              | Yes  | Name of the permission to grant.|
G
Gloria 已提交
199
| permissionFlag  | number | Yes  | Permission flag. The value **1** means that the permission request dialog box will still be displayed after the user grants or denies the permission. The value **2** means that no dialog box will be displayed after the user grants or denies the permission. The value **3** means a system permission that cannot be changed. |
A
Annie_wang 已提交
200 201 202 203 204 205 206 207 208 209 210 211
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the permission is granted successfully, **err** is **undefine**. Otherwise, **err** is an error object.|

**Error codes**

For details about the error codes, see [Ability Access Control Error Codes](../errorcodes/errorcode-access-token.md).

| ID| Error Message|
| -------- | -------- |
| 12100001 | The parameter is invalid. The tokenID is 0 |
| 12100002 | TokenId does not exist. |
| 12100003 | Permission does not exist. |
| 12100006 | The specified application does not support the permissions granted or ungranted as specified. |
W
wusongqing 已提交
212 213 214

**Example**

W
wusongqing 已提交
215
```js
A
Annie_wang 已提交
216
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
G
Gloria 已提交
217

A
Annie_wang 已提交
218
let atManager = abilityAccessCtrl.createAtManager();
G
Gloria 已提交
219
let tokenID = 0; // You can use getApplicationInfo to obtain the access token ID.
W
wusongqing 已提交
220
let permissionFlag = 1;
G
Gloria 已提交
221
try {
A
Annie_wang 已提交
222
    atManager.grantUserGrantedPermission(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS", permissionFlag, (err, data) => {
G
Gloria 已提交
223 224 225 226 227 228 229 230 231
        if (err) {
            console.log(`grantUserGrantedPermission fail, err->${JSON.stringify(err)}`);
        } else {
            console.log('grantUserGrantedPermission success');
        }
    });
} catch(err) {
    console.log(`catch err->${JSON.stringify(err)}`);
}
W
wusongqing 已提交
232 233 234 235
```

### revokeUserGrantedPermission

A
Annie_wang 已提交
236
revokeUserGrantedPermission(tokenID: number, permissionName: Permissions, permissionFlag: number): Promise&lt;void&gt;
W
wusongqing 已提交
237

A
Annie_wang 已提交
238
Revokes a user_grant permission from an application. This API uses a promise to return the result.
W
wusongqing 已提交
239

A
Annie_wang 已提交
240
**System API**: This is a system API.
W
wusongqing 已提交
241

A
Annie_wang 已提交
242
**Required permissions**: ohos.permission.REVOKE_SENSITIVE_PERMISSIONS (available only to system applications)
W
wusongqing 已提交
243

W
wusongqing 已提交
244
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
245 246 247

**Parameters**

W
wusongqing 已提交
248 249
| Name   | Type               | Mandatory| Description                                                        |
| --------- | ------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
250 251
| tokenID      | number              | Yes  | Token ID of the application. The value can be obtained from [ApplicationInfo](js-apis-bundle-ApplicationInfo.md).          |
| permissionName | Permissions              | Yes  | Name of the permission to revoke.|
G
Gloria 已提交
252
| permissionFlag  | number | Yes  | Permission flag. The value **1** means that the permission request dialog box will still be displayed after the user grants or denies the permission. The value **2** means that no dialog box will be displayed after the user grants or denies the permission. The value **3** means a system permission that cannot be changed. |
W
wusongqing 已提交
253 254 255

**Return value**

W
wusongqing 已提交
256 257
| Type         | Description                               |
| :------------ | :---------------------------------- |
G
Gloria 已提交
258
| Promise&lt;void&gt; | Promise that returns no value.|
W
wusongqing 已提交
259

A
Annie_wang 已提交
260 261 262 263 264 265 266 267 268 269 270 271
**Error codes**

For details about the error codes, see [Ability Access Control Error Codes](../errorcodes/errorcode-access-token.md).

| ID| Error Message|
| -------- | -------- |
| 12100001 | The parameter is invalid. The tokenID is 0 |
| 12100002 | The specified tokenID does not exist. |
| 12100003 | The specified permission does not exist. |
| 12100006 | The application specified by the tokenID is not allowed to be revoked with the specified permission. Either the application is a sandbox or the tokenID is from a remote device. |
| 12100007 | Service is abnormal. |

W
wusongqing 已提交
272 273
**Example**

W
wusongqing 已提交
274
```js
A
Annie_wang 已提交
275
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
G
Gloria 已提交
276

A
Annie_wang 已提交
277
let atManager = abilityAccessCtrl.createAtManager();
G
Gloria 已提交
278
let tokenID = 0; // You can use getApplicationInfo to obtain the access token ID.
W
wusongqing 已提交
279
let permissionFlag = 1;
G
Gloria 已提交
280
try {
A
Annie_wang 已提交
281
    atManager.revokeUserGrantedPermission(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS", permissionFlag).then(() => {
G
Gloria 已提交
282 283 284 285 286 287 288
        console.log('revokeUserGrantedPermission success');
    }).catch((err) => {
        console.log(`revokeUserGrantedPermission fail, err->${JSON.stringify(err)}`);
    });
} catch(err) {
    console.log(`catch err->${JSON.stringify(err)}`);
}
W
wusongqing 已提交
289 290 291 292
```

### revokeUserGrantedPermission

A
Annie_wang 已提交
293
revokeUserGrantedPermission(tokenID: number, permissionName: Permissions, permissionFlag: number, callback: AsyncCallback&lt;void&gt;): void
W
wusongqing 已提交
294

A
Annie_wang 已提交
295
Revokes a user_grant permission from an application. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
296

A
Annie_wang 已提交
297
**System API**: This is a system API.
W
wusongqing 已提交
298

A
Annie_wang 已提交
299
**Required permissions**: ohos.permission.REVOKE_SENSITIVE_PERMISSIONS (available only to system applications)
W
wusongqing 已提交
300

W
wusongqing 已提交
301
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
302 303 304

**Parameters**

W
wusongqing 已提交
305 306
| Name   | Type               | Mandatory| Description                         |
| --------- | ------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
307 308
| tokenID      | number              | Yes  | Token ID of the application. The value can be obtained from [ApplicationInfo](js-apis-bundle-ApplicationInfo.md).          |
| permissionName | Permissions              | Yes  | Name of the permission to revoke.|
G
Gloria 已提交
309
| permissionFlag  | number | Yes  | Permission flag. The value **1** means that the permission request dialog box will still be displayed after the user grants or denies the permission. The value **2** means that no dialog box will be displayed after the user grants or denies the permission. The value **3** means a system permission that cannot be changed. |
A
Annie_wang 已提交
310 311 312 313 314 315 316 317 318 319 320 321
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the permission is revoked successfully, **err** is **undefine**. Otherwise, **err** is an error object.|

**Error codes**

For details about the error codes, see [Ability Access Control Error Codes](../errorcodes/errorcode-access-token.md).

| ID| Error Message|
| -------- | -------- |
| 12100001 | The parameter is invalid. The tokenID is 0 |
| 12100002 | TokenId does not exist. |
| 12100003 | Permission does not exist. |
| 12100006 | The specified application does not support the permissions granted or ungranted as specified. |
W
wusongqing 已提交
322 323 324

**Example**

W
wusongqing 已提交
325
```js
A
Annie_wang 已提交
326
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
G
Gloria 已提交
327

A
Annie_wang 已提交
328
let atManager = abilityAccessCtrl.createAtManager();
G
Gloria 已提交
329
let tokenID = 0; // You can use getApplicationInfo to obtain the access token ID.
W
wusongqing 已提交
330
let permissionFlag = 1;
G
Gloria 已提交
331
try {
A
Annie_wang 已提交
332
    atManager.revokeUserGrantedPermission(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS", permissionFlag, (err, data) => {
G
Gloria 已提交
333 334 335 336 337 338 339 340 341
        if (err) {
            console.log(`revokeUserGrantedPermission fail, err->${JSON.stringify(err)}`);
        } else {
            console.log('revokeUserGrantedPermission success');
        }
    });
} catch(err) {
    console.log(`catch err->${JSON.stringify(err)}`);
}
W
wusongqing 已提交
342 343 344 345
```

### getPermissionFlags

A
Annie_wang 已提交
346
getPermissionFlags(tokenID: number, permissionName: Permissions): Promise&lt;number&gt;
W
wusongqing 已提交
347

A
Annie_wang 已提交
348
Obtains the flags of the specified permission of an application. This API uses a promise to return the result.
W
wusongqing 已提交
349

A
Annie_wang 已提交
350
**System API**: This is a system API.
W
wusongqing 已提交
351

A
Annie_wang 已提交
352
**Required permissions**: ohos.permission.GET_SENSITIVE_PERMISSIONS, ohos.permission.GRANT_SENSITIVE_PERMISSIONS, or ohos.permission.REVOKE_SENSITIVE_PERMISSIONS (available only to system applications)
W
wusongqing 已提交
353

W
wusongqing 已提交
354
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
355 356 357

**Parameters**

W
wusongqing 已提交
358 359
| Name   | Type               | Mandatory| Description                         |
| --------- | ------------------- | ---- | ------------------------------------------------------------ |
G
Gloria 已提交
360
| tokenID      | number              | Yes  | Token ID of the application. The value can be obtained from [ApplicationInfo](js-apis-bundle-ApplicationInfo.md).           |
A
Annie_wang 已提交
361
| permissionName | Permissions              | Yes  | Name of the permission to query.|
W
wusongqing 已提交
362 363 364

**Return value**

W
wusongqing 已提交
365 366
| Type         | Description                               |
| :------------ | :---------------------------------- |
G
Gloria 已提交
367 368
| Promise&lt;number&gt; | Promise used to return the result.|

A
Annie_wang 已提交
369 370 371 372 373 374 375 376 377 378 379 380
**Error codes**

For details about the error codes, see [Ability Access Control Error Codes](../errorcodes/errorcode-access-token.md).

| ID| Error Message|
| -------- | -------- |
| 12100001 | The parameter is invalid. The tokenID is 0 |
| 12100002 | The specified tokenID does not exist. |
| 12100003 | The specified permission does not exist. |
| 12100006 | The operation is not allowd. Either the application is a sandbox or the tokenID is from a remote device. |
| 12100007 | Service is abnormal. |

G
Gloria 已提交
381 382 383
**Example**

```js
A
Annie_wang 已提交
384
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
G
Gloria 已提交
385

A
Annie_wang 已提交
386
let atManager = abilityAccessCtrl.createAtManager();
G
Gloria 已提交
387 388
let tokenID = 0; // You can use getApplicationInfo to obtain the access token ID.
try {
A
Annie_wang 已提交
389
    atManager.getPermissionFlags(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS").then((data) => {
G
Gloria 已提交
390
        console.log(`getPermissionFlags success, data->${JSON.stringify(data)}`);
A
Annie_wang 已提交
391
    }).catch((err) => {
G
Gloria 已提交
392 393 394 395 396 397 398 399 400 401 402
        console.log(`getPermissionFlags fail, err->${JSON.stringify(err)}`);
    });
} catch(err) {
    console.log(`catch err->${JSON.stringify(err)}`);
}
```

### getVersion<sup>9+</sup>

getVersion(): Promise&lt;number&gt;

A
Annie_wang 已提交
403
Obtains the data version of the permission management. This API uses a promise to return the result.
G
Gloria 已提交
404

A
Annie_wang 已提交
405
**System API**: This is a system API.
G
Gloria 已提交
406 407 408 409 410 411 412 413

**System capability**: SystemCapability.Security.AccessToken

**Return value**

| Type         | Description                               |
| :------------ | :---------------------------------- |
| Promise&lt;number&gt; | Promise used to return the version.|
W
wusongqing 已提交
414 415 416

**Example**

W
wusongqing 已提交
417
```js
A
Annie_wang 已提交
418 419
let atManager = abilityAccessCtrl.createAtManager();
let promise = atManager.getVersion();
G
Gloria 已提交
420 421 422 423 424 425 426
promise.then(data => {
    console.log(`promise: data->${JSON.stringify(data)}`);
});
```

### on<sup>9+</sup>

A
Annie_wang 已提交
427
on(type: 'permissionStateChange', tokenIDList: Array&lt;number&gt;, permissionNameList: Array&lt;Permissions&gt;, callback: Callback&lt;PermissionStateChangeInfo&gt;): void;
G
Gloria 已提交
428

A
Annie_wang 已提交
429
Subscribes to permission grant state changes of the specified applications and permissions.
G
Gloria 已提交
430

A
Annie_wang 已提交
431
**System API**: This is a system API.
G
Gloria 已提交
432

A
Annie_wang 已提交
433
**Required permissions**: ohos.permission.GET_SENSITIVE_PERMISSIONS (available only to system applications)
G
Gloria 已提交
434 435 436 437 438 439 440 441

**System capability**: SystemCapability.Security.AccessToken

**Parameters**

| Name            | Type                  | Mandatory| Description                                                         |
| ------------------ | --------------------- | ---- | ------------------------------------------------------------ |
| type               | string                | Yes  | Event type. The value is fixed at **'permissionStateChange'**, indicating the permission grant state change event. |
A
Annie_wang 已提交
442 443
| tokenIDList        | Array&lt;number&gt;   | Yes  | List of token IDs. If this parameter is left empty, the permission grant state changes of all applications are subscribed to.       |
| permissionNameList | Array&lt;Permissions&gt;   | Yes  | List of permission names. If this parameter is left empty, the permission grant state changes of all permissions are subscribed to.              |
G
Gloria 已提交
444 445
| callback | Callback&lt;[PermissionStateChangeInfo](#permissionstatechangeinfo9)&gt; | Yes| Callback used to return the permission grant state change information.|

A
Annie_wang 已提交
446 447 448 449 450 451 452 453 454 455 456 457
**Error codes**

For details about the error codes, see [Ability Access Control Error Codes](../errorcodes/errorcode-access-token.md).

| ID| Error Message|
| -------- | -------- |
| 12100001 | The parameter is invalid. The tokenID is 0 |
| 12100004 | The interface is called repeatedly with the same input. |
| 12100005 | The registration time has exceeded the limitation. |
| 12100007 | Service is abnormal. |
| 12100008 | Out of memory. |

G
Gloria 已提交
458 459 460
**Example**

```js
L
lsq 已提交
461
import abilityAccessCtrl, {Permissions} from '@ohos.abilityAccessCtrl';
G
Gloria 已提交
462 463

let atManager = abilityAccessCtrl.createAtManager();
L
lsq 已提交
464 465 466
let appInfo = bundle.getApplicationInfoSync('com.example.myapplication', 0, 100);
let tokenIDList: Array<number> = [appInfo.accessTokenId];
let permissionNameList: Array<Permissions> = ["ohos.permission.DISTRIBUTED_DATASYNC"];
G
Gloria 已提交
467 468 469 470 471 472 473 474 475 476 477
try {
    atManager.on('permissionStateChange', tokenIDList, permissionNameList, (data) => {
        console.debug("receive permission state change, data:" + JSON.stringify(data));
    });
} catch(err) {
    console.log(`catch err->${JSON.stringify(err)}`);
}
```

### off<sup>9+</sup>

A
Annie_wang 已提交
478
off(type: 'permissionStateChange', tokenIDList: Array&lt;number&gt;, permissionNameList: Array&lt;Permissions&gt;, callback?: Callback&lt;PermissionStateChangeInfo&gt;): void;
G
Gloria 已提交
479

A
Annie_wang 已提交
480
Unsubscribes from permission grant state changes of the specified applications and permissions. This API uses an asynchronous callback to return the result.
G
Gloria 已提交
481

A
Annie_wang 已提交
482
**System API**: This is a system API.
G
Gloria 已提交
483

A
Annie_wang 已提交
484
**Required permissions**: ohos.permission.GET_SENSITIVE_PERMISSIONS (available only to system applications)
G
Gloria 已提交
485 486 487 488 489 490 491 492

**System capability**: SystemCapability.Security.AccessToken

**Parameters**

| Name            | Type                  | Mandatory| Description                                                         |
| ------------------ | --------------------- | ---- | ------------------------------------------------------------ |
| type               | string                | Yes  | Event type. The value is fixed at **'permissionStateChange'**, indicating the permission grant state change event. |
A
Annie_wang 已提交
493 494
| tokenIDList        | Array&lt;number&gt;   | Yes  | List of token IDs. If this parameter is left empty, the permission grant state changes of all applications are unsubscribed from. The value must be the same as that passed in **on()**.|
| permissionNameList | Array&lt;Permissions&gt;   | Yes  | List of permission names. If this parameter is left empty, the permission grant state changes of all permissions are unsubscribed from. The value must be the same as that passed in **on()**.|
G
Gloria 已提交
495 496
| callback | Callback&lt;[PermissionStateChangeInfo](#permissionstatechangeinfo9)&gt; | No| Callback used to return the permission grant state change information.|

A
Annie_wang 已提交
497 498 499 500 501 502 503 504 505 506 507
**Error codes**

For details about the error codes, see [Ability Access Control Error Codes](../errorcodes/errorcode-access-token.md).

| ID| Error Message|
| -------- | -------- |
| 12100001 | The parameter is invalid. The tokenID in list is all invalid |
| 12100004 | The interface is not used with |
| 12100007 | Service is abnormal. |
| 12100008 | Out of memory. |

G
Gloria 已提交
508 509 510
**Example**

```js
L
lsq 已提交
511
import abilityAccessCtrl, {Permissions} from '@ohos.abilityAccessCtrl';
G
Gloria 已提交
512 513

let atManager = abilityAccessCtrl.createAtManager();
L
lsq 已提交
514 515 516
let appInfo = bundle.getApplicationInfoSync('com.example.myapplication', 0, 100);
let tokenIDList: Array<number> = [appInfo.accessTokenId];
let permissionNameList: Array<Permissions> = ["ohos.permission.DISTRIBUTED_DATASYNC"];
G
Gloria 已提交
517 518 519 520 521 522 523
try {
    atManager.off('permissionStateChange', tokenIDList, permissionNameList);
} catch(err) {
    console.log(`catch err->${JSON.stringify(err)}`);
}
```

A
Annie_wang 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
### verifyAccessToken<sup>9+</sup>

verifyAccessToken(tokenID: number, permissionName: Permissions): Promise&lt;GrantStatus&gt;

Verifies whether an application has the specified permission. This API uses a promise to return the result.

> **NOTE**<br>You are advised to use [checkAccessToken](#checkaccesstoken9).

**System capability**: SystemCapability.Security.AccessToken

**Parameters**

| Name  | Type                | Mandatory| Description                                      |
| -------- | -------------------  | ---- | ------------------------------------------ |
| tokenID   |  number   | Yes  | Token ID of the application. The value can be obtained from [ApplicationInfo](js-apis-bundle-ApplicationInfo.md).            |
| permissionName | Permissions | Yes  | Name of the permission to verify. Only valid permission names are supported.|

**Return value**

| Type         | Description                               |
| :------------ | :---------------------------------- |
| Promise&lt;GrantStatus&gt; | Promise used to return the permission grant state.|

**Example**

```js
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';

let atManager = abilityAccessCtrl.createAtManager();
let tokenID = 0; // You can use getApplicationInfo to obtain the access token ID.
let promise = atManager.verifyAccessToken(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS");
promise.then(data => {
    console.log(`promise: data->${JSON.stringify(data)}`);
});
```

G
Gloria 已提交
560 561 562 563
### verifyAccessToken<sup>(deprecated)</sup>

verifyAccessToken(tokenID: number, permissionName: string): Promise&lt;GrantStatus&gt;

A
Annie_wang 已提交
564
Verifies whether an application has the specified permission. This API uses a promise to return the result.
G
Gloria 已提交
565

A
Annie_wang 已提交
566
> NOTE<br>This API is deprecated since API version 9. You are advised to use [checkAccessToken](#checkaccesstoken9).
G
Gloria 已提交
567 568 569 570 571 572 573

**System capability**: SystemCapability.Security.AccessToken

**Parameters**

| Name  | Type                | Mandatory| Description                                      |
| -------- | -------------------  | ---- | ------------------------------------------ |
A
Annie_wang 已提交
574
| tokenID   |  number   | Yes  | Token ID of the application. The value can be obtained from [ApplicationInfo](js-apis-bundle-ApplicationInfo.md).            |
G
Gloria 已提交
575 576 577 578 579 580 581 582 583 584 585
| permissionName | string | Yes  | Name of the permission to verify.|

**Return value**

| Type         | Description                               |
| :------------ | :---------------------------------- |
| Promise&lt;GrantStatus&gt; | Promise used to return the permission grant state.|

**Example**

```js
A
Annie_wang 已提交
586
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
G
Gloria 已提交
587

A
Annie_wang 已提交
588
let atManager = abilityAccessCtrl.createAtManager();
G
Gloria 已提交
589
let tokenID = 0; // You can use getApplicationInfo to obtain the access token ID.
A
Annie_wang 已提交
590
let promise = atManager.verifyAccessToken(tokenID, "ohos.permission.GRANT_SENSITIVE_PERMISSIONS");
W
wusongqing 已提交
591 592 593 594
promise.then(data => {
    console.log(`promise: data->${JSON.stringify(data)}`);
});
```
W
wusongqing 已提交
595 596 597 598 599

### GrantStatus

Enumerates the permission grant states.

W
wusongqing 已提交
600
**System capability**: SystemCapability.Security.AccessToken
W
wusongqing 已提交
601

A
Annie_wang 已提交
602
| Name              |    Value| Description       |
G
Gloria 已提交
603 604 605 606 607 608 609 610 611 612 613 614
| ------------------ | ----- | ----------- |
| PERMISSION_DENIED  | -1    | Permission denied.|
| PERMISSION_GRANTED | 0     | Permission granted.|

### PermissionStateChangeType<sup>9+</sup>

Enumerates the operations that trigger permission grant state changes.

**System API**: This is a system API.

**System capability**: SystemCapability.Security.AccessToken

A
Annie_wang 已提交
615
| Name                    |    Value| Description             |
G
Gloria 已提交
616 617 618 619 620 621 622 623 624 625
| ----------------------- | ------ | ----------------- |
| PERMISSION_REVOKED_OPER | 0      | Operation to revoke the permission.|
| PERMISSION_GRANTED_OPER | 1      | Operation to grant the permission.|

### PermissionStateChangeInfo<sup>9+</sup>

Defines the detailed permission grant state change information.

**System API**: This is a system API.

A
Annie_wang 已提交
626
**System capability**: SystemCapability.Security.AccessToken
G
Gloria 已提交
627 628 629 630 631

| Name          | Type                      | Readable| Writable| Description               |
| -------------- | ------------------------- | ---- | ---- | ------------------ |
| change         | [PermissionStateChangeType](#permissionstatechangetype9) | Yes  | No  | Operation that triggers the permission grant state change.       |
| tokenID        | number                    | Yes  | No  | Token ID of the application whose permission grant state changes are subscribed.|
A
Annie_wang 已提交
632
| permissionName | Permissions                    | Yes  | No  | Name of the permission whose grant state is changed.|