• Home
Name
Date
Size
#Lines
LOC

..--

frameworks/12-May-2024-7,3976,504

interfaces/12-May-2024-8,8916,478

services/12-May-2024-14,41910,205

tools/12-May-2024-335207

BUILD.gnD12-May-20242.5 KiB6156

LICENSED12-May-202410.1 KiB177150

OAT.xmlD12-May-20245.7 KiB8434

README.mdD12-May-20245.7 KiB7558

README_zh.mdD12-May-20245.8 KiB8064

access_token.gniD12-May-2024765 2018

bundle.jsonD12-May-20242.5 KiB8786

ohos.buildD12-May-20241.7 KiB5150

README.md

1# Access Control<a name="EN-US_TOPIC_0000001101239136"></a>
2
3
4## Introduction<a name="section11660541593"></a>
5
6AccessTokenManager (ATM) provides unified app permission management based on access tokens on OpenHarmony.
7
8The access token information of an app includes the app identifier (**APPID**), user ID, app twin index, app Ability Privilege Level (APL), and permission information. The access token of each app is identified by a 32-bit token identity (**TokenID**) in the device.
9
10The ATM module provides the following functions:
11-   Verifying app permissions based on the token ID before an app accesses sensitive data or calls an API.
12-   Obtaining access token information (for example, APL) based on the token ID.
13
14## Directory Structure<a name="section161941989596"></a>
15
16```
17/base/security/access_token
18├── frameworks                  # Stores code of basic functionalities.
19│   ├── accesstoken             # Stores code of the ATM framework.
20│   ├── tokensync               # Stores code of the access token synchronization framework.
21│   └── common                  # Stores framework common code.
22├── interfaces                  # Stores the APIs.
23│   ├── innerkits               # Stores internal APIs.
24│       ├── accesstoken         # Stores code of access token internal APIs.
25│       ├── nativetoken         # Stores code of native token APIs.
26│       └── tokensync           # Stores code of the internal APIs for access token synchronization.
27└── services                    # Services
28    ├── accesstokenmanager      # Stores ATM service code.
29    └── tokensyncmanager        # Stores code of the access token synchronization service.
30```
31
32## Usage<a name="section137768191623"></a>
33### Available APIs<a name="section1551164914237"></a>
34
35| **API**| **Description**|
36| --- | --- |
37| AccessTokenIDEx AllocHapToken(const HapInfoParams& info, const HapPolicyParams& policy); | Allocates a token ID to an app.|
38| AccessTokenID AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID); | Allocates a local token ID to the app of a remote device.|
39| int UpdateHapToken(AccessTokenID tokenID, const std::string& appIDDesc, const HapPolicyParams& policy); | Updates token information.|
40| int DeleteToken(AccessTokenID tokenID); | Deletes the app's token ID and information.|
41| int GetTokenType(AccessTokenID tokenID); | Obtains the type of an access token.|
42| int CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap); | Checks whether the native process corresponding to the given token ID has the specified distributed capability.|
43| AccessTokenID GetHapTokenID(int userID, const std::string& bundleName, int instIndex); | Obtains the token ID of an app.|
44| int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes); | Obtains the token information about a HAP.|
45| int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes); | Obtains information about a native token.|
46| int VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName); | Checks whether an access token has the specified permission.|
47| int GetDefPermission(const std::string& permissionName, PermissionDef& permissionDefResult); | Obtains definition information about the specified permission.|
48| int GetDefPermissions(AccessTokenID tokenID, std::vector<PermissionDef>& permList); | Obtains the permission definition set of a HAP.|
49| int GetReqPermissions(AccessTokenID tokenID, std::vector<PermissionStateFull>& reqPermList, bool isSystemGrant); | Obtains the status set of the permission requested by a HAP.|
50| int GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName); | Obtains the permissions of the app with the specified token ID.|
51| int GrantPermission(AccessTokenID tokenID, const std::string& permissionName, int flag); | Grants the specified permission to the app with the specified token ID.|
52| int RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag); | Revokes the specified permission from the app with the specified token ID.|
53| int ClearUserGrantedPermissionState(AccessTokenID tokenID); | Clears the user_grant permission status of the app with the specified token ID.|
54| uint64_t GetAccessTokenId(const char *processname, const char **dcap, int32_t dacpNum, const char *aplStr); | Obtains the token ID of a native process.|
55
56### Usage Guidelines<a name="section129654513264"></a>
57ATM provides unified access control for apps and allows apps or service abilities to obtain and verify app permissions and APL. The ATM APIs can be called by a service ability started by a native process or an app HAP.
58
59#### Native Process
60-  Before a native process starts, it calls **GetAccessTokenId** to obtain a token ID, and then calls **SetSelfTokenID** to set the token ID to the kernel.
61-  During the running of a native process, it calls **GetNativeTokenInfo** or **CheckNativeDCap** to obtain the token information, including the distributed capability and APL.
62
63#### App HAP
64-  When an app is installed, **AllocHapToken** is called to obtain the token ID of the app.
65-  When an authentication is required during app running, **VerifyAccessToken** or **GetReqPermissions** is called to obtain and verify the app permissions and APL.
66-  When an app is uninstalled, **DeleteToken** is called to delete the related access token information.
67
68## Repositories Involved<a name="section1371113476307"></a>
69
70[startup\_init\_lite](https://gitee.com/openharmony/startup_init_lite/blob/master/README.md)
71
72[security\_deviceauth](https://gitee.com/openharmony/security_deviceauth/blob/master/README.md)
73
74[security\_access\_token](https://gitee.com/openharmony/security_access_token/blob/master/README.md)
75

README_zh.md

1# 访问控制<a name="ZH-CN_TOPIC_0000001101239136"></a>
2
3-   [简介](#section11660541593)
4-   [目录](#section119744591305)
5-   [使用](#section137768191623)
6    -   [接口说明](#section1551164914237)
7    -   [使用说明](#section129654513264)
8-   [相关仓](#section1371113476307)
9
10## 简介<a name="section11660541593"></a>
11
12ATM(AccessTokenManager)是OpenHarmony上基于AccessToken构建的统一的应用权限管理能力。
13
14应用的Accesstoken信息主要包括应用身份标识APPID、用户ID,应用分身索引、应用APL(Ability Privilege Level)等级、应用权限信息等。每个应用的Accestoken信息由一个32bits的设备内唯一标识符TokenID(Token identity)来标识。
15
16ATM模块主要提供如下功能:
17-   提供基于TokenID的应用权限校验机制,应用访问敏感数据或者API时可以检查是否有对应的权限。
18-   提供基于TokenID的Accestoken信息查询,应用可以根据TokenID查询自身的APL等级等信息。
19
20## 目录<a name="section161941989596"></a>
21
22```
23/base/security/access_token
24├── frameworks                  # 框架层,基础功能代码存放目录
25│   ├── accesstoken             # Accesstoken管理框架代码存放目录
26│   ├── tokensync               # Accesstoken信息同步框架代码存放目录
27│   └── common                  # 框架公共代码存放目录
28├── interfaces                  # 接口层
29│   └── innerkits               # 内部接口层
30│       ├── accesstoken         # Accesstoken内部接口代码存放目录
31│       ├── nativetoken         # nativetoken内部接口代码存放目录
32│       └── tokensync           # Accesstoken信息同步内部接口代码存放目录
33└── services                    # 服务层
34    ├── accesstokenmanager      # Accesstoken管理服务代码存放目录
35    └── tokensyncmanager        # Accesstoken信息同步服务代码存放目录
36```
37
38## 使用<a name="section137768191623"></a>
39### 接口说明<a name="section1551164914237"></a>
40
41| **接口申明** | **接口描述** |
42| --- | --- |
43| AccessTokenIDEx AllocHapToken(const HapInfoParams& info, const HapPolicyParams& policy); | 为应用进程分配一个tokenID |
44| AccessTokenID AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID); | 为远端设备的应用进程分配一个本地tokenID |
45| int UpdateHapToken(AccessTokenID tokenID, const std::string& appIDDesc, const HapPolicyParams& policy); | 更新tokenId对应的tokenInfo信息 |
46| int DeleteToken(AccessTokenID tokenID); | 删除应用tokenID及其对应的tokenInfo信息 |
47| int GetTokenType(AccessTokenID tokenID); | 查询指定tokenID的类型 |
48| int CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap); | 检测指定tokenID对应的native进程是否具有指定的分布式能力 |
49| AccessTokenID GetHapTokenID(int userID, const std::string& bundleName, int instIndex); | 查询指定应用的tokenId |
50| int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes); | 查询指定tokenID对应的hap包的tokenInfo信息 |
51| int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes); | 查询指定tokenID对应的native的tokenInfo信息 |
52| int VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName); | 检查指定tokenID是否具有指定权限 |
53| int GetDefPermission(const std::string& permissionName, PermissionDef& permissionDefResult); | 查询指定权限的权限定义信息 |
54| int GetDefPermissions(AccessTokenID tokenID, std::vector<PermissionDef>& permList); | 查询指定tokenID对应的hap包的权限定义集合 |
55| int GetReqPermissions(AccessTokenID tokenID, std::vector<PermissionStateFull>& reqPermList, bool isSystemGrant); | 查询指定tokenID对应的hap包申请的权限状态集合 |
56| int GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName); | 查询指定tokenID的应用的指定权限 |
57| int GrantPermission(AccessTokenID tokenID, const std::string& permissionName, int flag); | 授予指定tokenID的应用的指定权限 |
58| int RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag); | 撤销指定tokenID的应用的指定权限 |
59| int ClearUserGrantedPermissionState(AccessTokenID tokenID); | 清空指定tokenID的应用的user_grant权限状态 |
60| uint64_t GetAccessTokenId(const char *processname, const char **dcap, int32_t dacpNum, const char *aplStr); | 创建获取native进程的tokenId |
61
62### 使用说明<a name="section129654513264"></a>
63ATM提供了统一的应用权限访问控制功能,支持应用程序或者SA查询校验应用权限、APL等信息。从使用者角度,可以分为基于native进程启动的SA和应用Hap两类使用者。
64
65#### native进程
66-  在native进程拉起前,需要调用GetAccessTokenId函数,获取该native进程的TokenID;再调用SetSelfTokenID将进程TokenID设置到内核中。
67-  在native进程运行过程中,可以通过调用GetNativeTokenInfo、CheckNativeDCap来查验对应进程所具备的token信息,包括分布式能力、APL等级等信息。
68
69#### 应用hap
70-  在应用安装时,需要调用AllocHapToken创建获取该应用的TokenID。
71-  在应用运行过程中,需要进行鉴权等操作时,可调用VerifyAccessToken、GetReqPermissions等函数查询校验应用权限、APL等信息。
72-  在应用卸载时,需要调用DeleteToken函数删除系统中管理的对应Accesstoken信息。
73
74## 相关仓<a name="section1371113476307"></a>
75
76[startup\_init\_lite](https://gitee.com/openharmony/startup_init_lite/blob/master/README_zh.md)
77
78[security\_deviceauth](https://gitee.com/openharmony/security_deviceauth/blob/master/README_zh.md)
79
80**[security\_access\_token](https://gitee.com/openharmony/security_access_token/blob/master/README_zh.md)**