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 "accessibility_permission.h"
17#include <accesstoken_kit.h>
18#include <ipc_skeleton.h>
19#include "hilog_wrapper.h"
20#include "tokenid_kit.h"
21
22namespace OHOS {
23namespace Accessibility {
24bool Permission::CheckCallingPermission(const std::string& permission)
25{
26    HILOG_DEBUG("permission %{public}s", permission.c_str());
27    if (Security::AccessToken::AccessTokenKit::VerifyAccessToken(IPCSkeleton::GetCallingTokenID(), permission) !=
28        Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
29        HILOG_DEBUG("permission denied");
30        return false;
31    }
32
33    HILOG_DEBUG("permission ok!");
34    return true;
35}
36
37bool Permission::IsStartByHdcd()
38{
39    auto id = IPCSkeleton::GetCallingTokenID();
40    Security::AccessToken::NativeTokenInfo info;
41    auto ret = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(id, info);
42    if (ret != 0) {
43        HILOG_DEBUG("get native token info failed %{public}d", ret);
44        return false;
45    }
46
47    if (info.processName.compare("hdcd") == 0) {
48        HILOG_DEBUG("process name is hdcd");
49        return true;
50    }
51
52    return false;
53}
54
55bool Permission::IsSystemApp()
56{
57    HILOG_DEBUG();
58
59    Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
60    Security::AccessToken::ATokenTypeEnum tokenType =
61        Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken);
62    if (tokenType !=  Security::AccessToken::TOKEN_HAP) {
63        HILOG_INFO("Caller is not a application.");
64        return true;
65    }
66    uint64_t accessTokenId = IPCSkeleton::GetCallingFullTokenID();
67    bool isSystemApplication = Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(accessTokenId);
68    return isSystemApplication;
69}
70
71} // namespace Accessibility
72} // namespace OHOS