1# ATM
2
3
4## Introduction
5
6AccessTokenManager (ATM) implements 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
14The following figure shows the ATM architecture.
15![](figures/framework_en.png)
16
17## Directory Structure
18
19```
20/base/security/access_token
21├── frameworks                  # Code of basic functionalities.
22│   ├── accesstoken             # Code of the ATM framework.
23│   ├── common                  # Common code.
24│   ├── privacy                 # Code of the privacy framework.
25│   └── tokensync               # Code of the access token synchronization framework.
26├── interfaces                  # Interfaces
27│   ├── innerkits               # Internal interfaces.
28│   │   ├── accesstoken         # Code of the internal access token interfaces.
29│   │   ├── nativetoken         # Code of the internal native token interfaces.
30│   │   ├── privacy             # Code of the internal privacy interfaces.
31│   │   ├── token_callback      # Code of the internal callbacks.
32│   │   ├── token_setproc       # Code of internal interfaces for exchanging token IDs.
33│   │   └── tokensync           # Code of the internal access token synchronization interfaces.
34│   └── kits                    # External interfaces.
35│   │   ├── accesstoken         # Code of the external access token interfaces.
36│   │   ├── common              # Common code of external interfaces.
37│   │   └── privacy             # Code of the external privacy interfaces.
38└── services                    # Services
39    ├── accesstokenmanager      # ATM service code.
40    ├── privacymanager          # Privacy manager service code.
41    └── tokensyncmanager        # Code of the access token synchronization service.
42
43```
44
45## Usage
46### Available APIs
47
48| **API**| **Description**|
49| --- | --- |
50| AccessTokenIDEx AllocHapToken(const HapInfoParams& info, const HapPolicyParams& policy); | Allocates a token ID to an app.|
51| AccessTokenID AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID); | Allocates a local token ID to the app of a remote device.|
52| int UpdateHapToken(AccessTokenIDEx& tokenIdEx, bool isSystemApp, const std::string& appIDDesc, int32_t apiVersion, const HapPolicyParams& policy); | Updates token information.|
53| int DeleteToken(AccessTokenID tokenID); | Deletes the app's token ID and information.|
54| int GetTokenType(AccessTokenID tokenID); | Obtains the type of an access token.|
55| int GetTokenTypeFlag(AccessTokenID tokenID); | Obtains the type of a trusted token ID.|
56| int GetTokenType(FullTokenID tokenID); | Obtains the type of an access token.|
57| int GetTokenTypeFlag(FullTokenID tokenID); | Obtains the type of a trusted token ID.|
58| int CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap); | Checks whether the native process corresponding to the given token ID has the specified distributed capability.|
59| AccessTokenID GetHapTokenID(int32_t userID, const std::string& bundleName, int32_t instIndex); | Obtains the token ID of an app.|
60| AccessTokenIDEx GetHapTokenIDEx(int32_t userID, const std::string& bundleName, int32_t instIndex); | Obtains the token ID of an app.|
61| int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes); | Obtains the token information about an OpenHarmony Ability Package (HAP).|
62| int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes); | Obtains the native token information.|
63| int VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName); | Checks whether an access token has the specified permission.|
64| int GetDefPermission(const std::string& permissionName, PermissionDef& permissionDefResult); | Obtains definition information about the specified permission.|
65| int GetDefPermissions(AccessTokenID tokenID, std::vector<PermissionDef>& permList); | Obtains the permission definition set of a HAP.|
66| int GetReqPermissions(AccessTokenID tokenID, std::vector<PermissionStateFull>& reqPermList, bool isSystemGrant); | Obtains the status set of the permission requested by a HAP.|
67| int GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName); | Obtains the permissions of the app with the specified token ID.|
68| int GrantPermission(AccessTokenID tokenID, const std::string& permissionName, int flag); | Grants a permission to the app with the specified token ID.|
69| int RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag); | Revokes a permission from the app with the specified token ID.|
70| int ClearUserGrantedPermissionState(AccessTokenID tokenID); | Clears the user_grant permission status of the app with the specified token ID.|
71| uint64_t GetAccessTokenId(const char *processname, const char **dcap, int32_t dacpNum, const char *aplStr); | Obtains the token ID of a native process.|
72
73### How to Use
74ATM 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.
75
76#### Native Process
77-  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.
78-  During the running of a native process, it calls **GetNativeTokenInfo** or **CheckNativeDCap** to obtain the token information, including the distributed capability and APL.
79
80#### App HAP
81-  When an app is installed, **AllocHapToken** is called to obtain the token ID of the app.
82-  When an authentication is required during app running, **VerifyAccessToken** or **GetReqPermissions** is called to obtain and verify the app permissions and APL.
83-  When an app is uninstalled, **DeleteToken** is called to delete the related access token information.
84
85## Repositories Involved
86
87[startup\_init\_lite](https://gitee.com/openharmony/startup_init_lite)
88
89[security\_device\_auth](https://gitee.com/openharmony/security_device_auth)
90
91**[security\_access\_token](https://gitee.com/openharmony/security_access_token)**
92