• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 
16 #include "accesstoken_kit.h"
17 
18 #include "sec_comp_log.h"
19 
20 namespace OHOS {
21 namespace Security {
22 namespace AccessToken {
23 int32_t AccessTokenKit::getHapTokenInfoRes = 0;
24 std::mutex AccessTokenKit::mutex_;
25 std::map<AccessTokenID, std::set<std::string>> AccessTokenKit::permMap_;
26 
RevokePermission(AccessTokenID tokenID,const std::string & permissionName,int flag)27 int AccessTokenKit::RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag)
28 {
29     std::lock_guard<std::mutex> lock(mutex_);
30     auto iter = permMap_.find(tokenID);
31     if (iter == permMap_.end()) {
32         return 0;
33     }
34 
35     permMap_[tokenID].erase(permissionName);
36     if (permMap_[tokenID].size() == 0) {
37         permMap_.erase(tokenID);
38     }
39     return 0;
40 };
41 
GrantPermission(AccessTokenID tokenID,const std::string & permissionName,int flag)42 int AccessTokenKit::GrantPermission(AccessTokenID tokenID, const std::string& permissionName, int flag)
43 {
44     std::lock_guard<std::mutex> lock(mutex_);
45     auto iter = permMap_.find(tokenID);
46     if (iter != permMap_.end()) {
47         iter->second.insert(permissionName);
48         return 0;
49     }
50     std::set<std::string> permSet;
51     permSet.insert(permissionName);
52     permMap_[tokenID] = permSet;
53     return 0;
54 };
55 
VerifyAccessToken(AccessTokenID tokenID,const std::string & permissionName)56 int AccessTokenKit::VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName)
57 {
58     std::lock_guard<std::mutex> lock(mutex_);
59     auto iter = permMap_.find(tokenID);
60     if (iter == permMap_.end() || permMap_[tokenID].count(permissionName) < 1) {
61         return -1;
62     }
63     return 0;
64 };
65 } // namespace SECURITY_COMPONENT_INTERFACES_INNER_KITS_ACCESSTOKEN_KIT_H
66 } // namespace Security
67 } // namespace OHOS
68