10fbfc30aSopenharmony_ci/*
20fbfc30aSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
30fbfc30aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40fbfc30aSopenharmony_ci * you may not use this file except in compliance with the License.
50fbfc30aSopenharmony_ci * You may obtain a copy of the License at
60fbfc30aSopenharmony_ci *
70fbfc30aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
80fbfc30aSopenharmony_ci *
90fbfc30aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100fbfc30aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110fbfc30aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120fbfc30aSopenharmony_ci * See the License for the specific language governing permissions and
130fbfc30aSopenharmony_ci * limitations under the License.
140fbfc30aSopenharmony_ci */
150fbfc30aSopenharmony_ci#include "get_self_permissions.h"
160fbfc30aSopenharmony_ci
170fbfc30aSopenharmony_ci#include <thread>
180fbfc30aSopenharmony_ci
190fbfc30aSopenharmony_ci#include "accesstoken_kit.h"
200fbfc30aSopenharmony_ci#include "calendar_log.h"
210fbfc30aSopenharmony_ci#include "nativetoken_kit.h"
220fbfc30aSopenharmony_ci#include "token_setproc.h"
230fbfc30aSopenharmony_ci
240fbfc30aSopenharmony_cinamespace OHOS::CalendarApi {
250fbfc30aSopenharmony_civoid SetAccessTokenPermission(const std::string &processName,
260fbfc30aSopenharmony_ci    const std::vector<std::string> &permission, uint64_t &tokenId)
270fbfc30aSopenharmony_ci{
280fbfc30aSopenharmony_ci    auto perms = std::make_unique<const char *[]>(permission.size());
290fbfc30aSopenharmony_ci    for (size_t i = 0; i < permission.size(); i++) {
300fbfc30aSopenharmony_ci        perms[i] = permission[i].c_str();
310fbfc30aSopenharmony_ci    }
320fbfc30aSopenharmony_ci
330fbfc30aSopenharmony_ci    NativeTokenInfoParams infoInstance = {
340fbfc30aSopenharmony_ci        .dcapsNum = 0,
350fbfc30aSopenharmony_ci        .permsNum = permission.size(),
360fbfc30aSopenharmony_ci        .aclsNum = 0,
370fbfc30aSopenharmony_ci        .dcaps = nullptr,
380fbfc30aSopenharmony_ci        .perms = perms.get(),
390fbfc30aSopenharmony_ci        .acls = nullptr,
400fbfc30aSopenharmony_ci        .processName = processName.c_str(),
410fbfc30aSopenharmony_ci        .aplStr = "normal",
420fbfc30aSopenharmony_ci    };
430fbfc30aSopenharmony_ci    tokenId = GetAccessTokenId(&infoInstance);
440fbfc30aSopenharmony_ci    if (tokenId == 0) {
450fbfc30aSopenharmony_ci        LOG_ERROR("Get Acess Token Id Failed");
460fbfc30aSopenharmony_ci        return;
470fbfc30aSopenharmony_ci    }
480fbfc30aSopenharmony_ci    int ret = SetSelfTokenID(tokenId);
490fbfc30aSopenharmony_ci    if (ret != 0) {
500fbfc30aSopenharmony_ci        LOG_ERROR("Set Acess Token Id Failed");
510fbfc30aSopenharmony_ci        return;
520fbfc30aSopenharmony_ci    }
530fbfc30aSopenharmony_ci    ret = Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
540fbfc30aSopenharmony_ci    if (ret < 0) {
550fbfc30aSopenharmony_ci        LOG_ERROR("Reload Native Token Info Failed");
560fbfc30aSopenharmony_ci        return;
570fbfc30aSopenharmony_ci    }
580fbfc30aSopenharmony_ci}
590fbfc30aSopenharmony_ci} // namespace OHOS::CalendarApi
600fbfc30aSopenharmony_ci
61