1885b47fbSopenharmony_ci/*
2885b47fbSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3885b47fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4885b47fbSopenharmony_ci * you may not use this file except in compliance with the License.
5885b47fbSopenharmony_ci * You may obtain a copy of the License at
6885b47fbSopenharmony_ci *
7885b47fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8885b47fbSopenharmony_ci *
9885b47fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10885b47fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11885b47fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12885b47fbSopenharmony_ci * See the License for the specific language governing permissions and
13885b47fbSopenharmony_ci * limitations under the License.
14885b47fbSopenharmony_ci */
15885b47fbSopenharmony_ci
16885b47fbSopenharmony_ci#include "accessibility_permission.h"
17885b47fbSopenharmony_ci#include <accesstoken_kit.h>
18885b47fbSopenharmony_ci#include <ipc_skeleton.h>
19885b47fbSopenharmony_ci#include "hilog_wrapper.h"
20885b47fbSopenharmony_ci#include "tokenid_kit.h"
21885b47fbSopenharmony_ci
22885b47fbSopenharmony_cinamespace OHOS {
23885b47fbSopenharmony_cinamespace Accessibility {
24885b47fbSopenharmony_cibool Permission::CheckCallingPermission(const std::string& permission)
25885b47fbSopenharmony_ci{
26885b47fbSopenharmony_ci    HILOG_DEBUG("permission %{public}s", permission.c_str());
27885b47fbSopenharmony_ci    if (Security::AccessToken::AccessTokenKit::VerifyAccessToken(IPCSkeleton::GetCallingTokenID(), permission) !=
28885b47fbSopenharmony_ci        Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
29885b47fbSopenharmony_ci        HILOG_DEBUG("permission denied");
30885b47fbSopenharmony_ci        return false;
31885b47fbSopenharmony_ci    }
32885b47fbSopenharmony_ci
33885b47fbSopenharmony_ci    HILOG_DEBUG("permission ok!");
34885b47fbSopenharmony_ci    return true;
35885b47fbSopenharmony_ci}
36885b47fbSopenharmony_ci
37885b47fbSopenharmony_cibool Permission::IsStartByHdcd()
38885b47fbSopenharmony_ci{
39885b47fbSopenharmony_ci    auto id = IPCSkeleton::GetCallingTokenID();
40885b47fbSopenharmony_ci    Security::AccessToken::NativeTokenInfo info;
41885b47fbSopenharmony_ci    auto ret = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(id, info);
42885b47fbSopenharmony_ci    if (ret != 0) {
43885b47fbSopenharmony_ci        HILOG_DEBUG("get native token info failed %{public}d", ret);
44885b47fbSopenharmony_ci        return false;
45885b47fbSopenharmony_ci    }
46885b47fbSopenharmony_ci
47885b47fbSopenharmony_ci    if (info.processName.compare("hdcd") == 0) {
48885b47fbSopenharmony_ci        HILOG_DEBUG("process name is hdcd");
49885b47fbSopenharmony_ci        return true;
50885b47fbSopenharmony_ci    }
51885b47fbSopenharmony_ci
52885b47fbSopenharmony_ci    return false;
53885b47fbSopenharmony_ci}
54885b47fbSopenharmony_ci
55885b47fbSopenharmony_cibool Permission::IsSystemApp()
56885b47fbSopenharmony_ci{
57885b47fbSopenharmony_ci    HILOG_DEBUG();
58885b47fbSopenharmony_ci
59885b47fbSopenharmony_ci    Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
60885b47fbSopenharmony_ci    Security::AccessToken::ATokenTypeEnum tokenType =
61885b47fbSopenharmony_ci        Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken);
62885b47fbSopenharmony_ci    if (tokenType !=  Security::AccessToken::TOKEN_HAP) {
63885b47fbSopenharmony_ci        HILOG_INFO("Caller is not a application.");
64885b47fbSopenharmony_ci        return true;
65885b47fbSopenharmony_ci    }
66885b47fbSopenharmony_ci    uint64_t accessTokenId = IPCSkeleton::GetCallingFullTokenID();
67885b47fbSopenharmony_ci    bool isSystemApplication = Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(accessTokenId);
68885b47fbSopenharmony_ci    return isSystemApplication;
69885b47fbSopenharmony_ci}
70885b47fbSopenharmony_ci
71885b47fbSopenharmony_ci} // namespace Accessibility
72885b47fbSopenharmony_ci} // namespace OHOS