accesstoken-guidelines.md 6.7 KB
Newer Older
A
Annie_wang 已提交
1
# Access Control (Permission) Development
A
annie_wangli 已提交
2

A
Annie_wang 已提交
3
## When to Use
A
annie_wangli 已提交
4 5 6

In this example, the app requires the **ohos.permission.PERMISSION1** and **ohos.permission.PERMISSION2** permissions to implement core functions.

7 8 9
- The ability privilege level (APL) of the app is **normal**.
- The level of **ohos.permission.PERMISSION1** is **normal**, and the authorization mode is **system_grant**.
- The level of **ohos.permission.PERMISSION2** is **system_basic**, and the authorization mode is **user_grant**.
A
annie_wangli 已提交
10

A
Annie_wang 已提交
11
> **CAUTION**
A
annie_wangli 已提交
12
>
13
> In this scenario, the required permissions include a **user_grant** permission. You can check whether the caller has the required permission through permission verification.
A
annie_wangli 已提交
14 15 16 17 18 19 20 21 22
>
> If the permission verification result indicates that the app has not obtained that permission, dynamic user authorization is required.
>
## Available APIs

The table below lists only the API used in this guide. For more information, see the [API Reference](../reference/apis/js-apis-abilityAccessCtrl.md).

| API                                                      | Description                                            |
| ------------------------------------------------------------ | --------------------------------------------------- |
A
Annie_wang 已提交
23
| verifyAccessToken(tokenID: number, permissionName: string): Promise<GrantStatus> | Verifies whether an app has the specified permission. This API uses a promise to return the result.|
A
annie_wangli 已提交
24 25 26

## Declaring Permissions

A
Annie_wang 已提交
27
Declare the permissions required by the app one by one in the project configuration file. The app cannot obtain the permissions that are not declared in the configuration file. The ability framework provides two models: Feature Ability (FA) model and stage model. For more information, see [Ability Framework Overview](../ability/ability-brief.md).
A
annie_wangli 已提交
28

A
Annie_wang 已提交
29 30
Note that the app bundle structure and configuration file vary with the ability framework model.

A
Annie_wang 已提交
31
The following table describes the tags in the configuration file.
A
annie_wangli 已提交
32 33 34 35

| Field     | Description                                                        |
| --------- | ------------------------------------------------------------ |
| name      | Name of the permission.                                                  |
A
Annie_wang 已提交
36 37
| reason    | Reason for requesting the permission. This field is mandatory for a user_grant permission.|
| usedScene | Scenario of the permission. This field is mandatory for a user_grant permission.|
A
Annie_wang 已提交
38 39
| ability   | Abilities that use the permission. The value is an array.<br>**Applicable model**: FA           |
| abilities | Abilities that use the permission. The value is an array.<br>**Applicable model**: stage           |
A
Annie_wang 已提交
40
| when      | Time when the permission is used. The value can be **inuse** (the permission can be used only in the foreground) or **always** (the permission can be used in foreground and background).|
A
annie_wangli 已提交
41

A
Annie_wang 已提交
42 43 44 45
### FA Model

For the apps based on the FA model, declare the required permissions in the **config.json** file.

A
annie_wangli 已提交
46 47
**Example**

A
Annie_wang 已提交
48 49
```json
{
A
Annie_wang 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  "module" : {
    "reqPermissions":[
      {
        "name" : "ohos.permission.PERMISSION1",
        "reason": "$string:reason",
        "usedScene": {
          "ability": [
            "FormAbility"
          ],
          "when":"inuse"
        }
      },
      {
        "name" : "ohos.permission.PERMISSION2",
        "reason": "$string:reason",
        "usedScene": {
          "ability": [
            "FormAbility"
          ],
          "when":"always"
        }
      }
    ]
  }
A
Annie_wang 已提交
74 75 76 77 78
}
```

### Stage Model

A
Annie_wang 已提交
79
For the apps based on the stage model, declare the required permissions in the **module.json5** file.
A
Annie_wang 已提交
80 81 82

**Example**

A
annie_wangli 已提交
83 84
```json
{
A
Annie_wang 已提交
85 86 87 88 89 90
  "module" : {
    "requestPermissions":[
      {
        "name" : "ohos.permission.PERMISSION1",
        "reason": "$string:reason",
        "usedScene": {
A
annie_wangli 已提交
91 92 93 94
                     "abilities": [
                         "FormAbility"
                     ],
                     "when":"inuse"
A
Annie_wang 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108
        }
      },
      {
        "name" : "ohos.permission.PERMISSION2",
        "reason": "$string:reason",
        "usedScene": {
          "abilities": [
            "FormAbility"
          ],
        "when":"always"
        }
      }
    ]
  }
A
annie_wangli 已提交
109 110
}
```
A
Annie_wang 已提交
111

A
annie_wangli 已提交
112 113
## Declaring the ACL

114
The permission level of **ohos.permission.PERMISSION2** is **system_basic**, which is higher than the app's APL. In this case, use the ACL.
A
annie_wangli 已提交
115

A
Annie_wang 已提交
116 117 118 119
In addition to declaring all the permissions in the configuration file, you must declare the permissions whose levels are higher that the app's APL in the app's profile. For details about the fields in the profile, see [HarmonyAppProvision Configuration File](../quick-start/app-provision-structure.md).

In this example, declare the permission under the **acls** field:

A
annie_wangli 已提交
120 121
```json
{
A
Annie_wang 已提交
122 123 124 125
  "acls": {
    "allowed-acls": [
      "ohos.permission.PERMISSION2"
    ]
126
  }
A
annie_wangli 已提交
127 128 129 130 131 132 133
}
```

## Applying for the user_grant Permission

After the permissions are declared, the system grants the system_grant permission during the installation of the app. The user_grant permission must be authorized by the user.

A
Annie_wang 已提交
134
Therefore, before allowing the app to call the API protected by the **ohos.permission.PERMISSION2** permission, the system needs to verify whether the app has the permission to do so.
A
annie_wangli 已提交
135 136 137

If the verification result indicates that the app has the permission, the app can access the target API. Otherwise, the app needs to request user authorization and then proceeds based on the authorization result. For details, see [Access Control Overview](accesstoken-overview.md).

A
Annie_wang 已提交
138
> **CAUTION**
A
annie_wangli 已提交
139 140 141 142 143 144 145
>
> The permissions authorized by user are not permanent, because the user may revoke the authorization at any time. Therefore, even if the user has granted the requested permission to an app, the app's permission must be verified before the app calls an API protected by the permission.

## Example

The procedure is as follows:

A
Annie_wang 已提交
146 147 148
1. Obtain the ability context.
2. Call **requestPermissionsFromUser** to verify whether the app has required permissions.
3. Proceed based on the permission verification result.
A
annie_wangli 已提交
149 150

```js
A
Annie_wang 已提交
151 152 153 154 155 156
  // OnWindowStageCreate lifecycle of the ability
  onWindowStageCreate() {
    var context = this.context
    let array:Array<string> = ["ohos.permission.PERMISSION2"];
    // requestPermissionsFromUser determines whether to invoke a pop-up window based on the permission authorization status.
    context.requestPermissionsFromUser(array).then(function(data) {
A
Annie_wang 已提交
157 158 159 160
      console.log("data type:" + typeof(data));
      console.log("data:" + data);
      console.log("data permissions:" + data.permissions);
      console.log("data result:" + data.authResults);
A
Annie_wang 已提交
161
    }, (err) => {
A
Annie_wang 已提交
162
      console.error('Failed to start ability', err.code);
A
Annie_wang 已提交
163
    });
A
annie_wangli 已提交
164 165 166
  }

```
A
Annie_wang 已提交
167
> **NOTE**<br>
168
> For details about how to use **requestPermissionsFromUser**, see [API Reference](../reference/apis/js-apis-ability-context.md).