1bae4d13cSopenharmony_ci/* 2bae4d13cSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3bae4d13cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4bae4d13cSopenharmony_ci * you may not use this file except in compliance with the License. 5bae4d13cSopenharmony_ci * You may obtain a copy of the License at 6bae4d13cSopenharmony_ci * 7bae4d13cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8bae4d13cSopenharmony_ci * 9bae4d13cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10bae4d13cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11bae4d13cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12bae4d13cSopenharmony_ci * See the License for the specific language governing permissions and 13bae4d13cSopenharmony_ci * limitations under the License. 14bae4d13cSopenharmony_ci */ 15bae4d13cSopenharmony_ci 16bae4d13cSopenharmony_ci#include "permission_util.h" 17bae4d13cSopenharmony_ci 18bae4d13cSopenharmony_ci#include "accesstoken_kit.h" 19bae4d13cSopenharmony_ci#include "privacy_kit.h" 20bae4d13cSopenharmony_ci#include "sensor_agent_type.h" 21bae4d13cSopenharmony_ci#include "sensor_log.h" 22bae4d13cSopenharmony_ci 23bae4d13cSopenharmony_ci#undef LOG_TAG 24bae4d13cSopenharmony_ci#define LOG_TAG "PermissionUtil" 25bae4d13cSopenharmony_ci 26bae4d13cSopenharmony_cinamespace OHOS { 27bae4d13cSopenharmony_cinamespace Sensors { 28bae4d13cSopenharmony_ciusing namespace OHOS::HiviewDFX; 29bae4d13cSopenharmony_ci 30bae4d13cSopenharmony_cistd::unordered_map<int32_t, std::string> PermissionUtil::sensorPermissions_ = { 31bae4d13cSopenharmony_ci { SENSOR_TYPE_ID_ACCELEROMETER, ACCELEROMETER_PERMISSION }, 32bae4d13cSopenharmony_ci { SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, ACCELEROMETER_PERMISSION }, 33bae4d13cSopenharmony_ci { SENSOR_TYPE_ID_LINEAR_ACCELERATION, ACCELEROMETER_PERMISSION }, 34bae4d13cSopenharmony_ci { SENSOR_TYPE_ID_GYROSCOPE, GYROSCOPE_PERMISSION }, 35bae4d13cSopenharmony_ci { SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, GYROSCOPE_PERMISSION }, 36bae4d13cSopenharmony_ci { SENSOR_TYPE_ID_PEDOMETER_DETECTION, ACTIVITY_MOTION_PERMISSION }, 37bae4d13cSopenharmony_ci { SENSOR_TYPE_ID_PEDOMETER, ACTIVITY_MOTION_PERMISSION }, 38bae4d13cSopenharmony_ci { SENSOR_TYPE_ID_HEART_RATE, READ_HEALTH_DATA_PERMISSION } 39bae4d13cSopenharmony_ci}; 40bae4d13cSopenharmony_ci 41bae4d13cSopenharmony_ciint32_t PermissionUtil::CheckSensorPermission(AccessTokenID callerToken, int32_t sensorTypeId) 42bae4d13cSopenharmony_ci{ 43bae4d13cSopenharmony_ci if (sensorPermissions_.find(sensorTypeId) == sensorPermissions_.end()) { 44bae4d13cSopenharmony_ci return PERMISSION_GRANTED; 45bae4d13cSopenharmony_ci } 46bae4d13cSopenharmony_ci std::string permissionName = sensorPermissions_[sensorTypeId]; 47bae4d13cSopenharmony_ci int32_t ret = AccessTokenKit::VerifyAccessToken(callerToken, permissionName); 48bae4d13cSopenharmony_ci if ((permissionName == ACTIVITY_MOTION_PERMISSION) 49bae4d13cSopenharmony_ci || (permissionName == READ_HEALTH_DATA_PERMISSION)) { 50bae4d13cSopenharmony_ci AddPermissionRecord(callerToken, permissionName, (ret == PERMISSION_GRANTED)); 51bae4d13cSopenharmony_ci } 52bae4d13cSopenharmony_ci return ret; 53bae4d13cSopenharmony_ci} 54bae4d13cSopenharmony_ci 55bae4d13cSopenharmony_civoid PermissionUtil::AddPermissionRecord(AccessTokenID tokenID, const std::string &permissionName, bool status) 56bae4d13cSopenharmony_ci{ 57bae4d13cSopenharmony_ci int32_t successCount = status ? 1 : 0; 58bae4d13cSopenharmony_ci int32_t failCount = status ? 0 : 1; 59bae4d13cSopenharmony_ci int32_t ret = PrivacyKit::AddPermissionUsedRecord(tokenID, permissionName, successCount, failCount); 60bae4d13cSopenharmony_ci if (ret != 0) { 61bae4d13cSopenharmony_ci SEN_HILOGE("AddPermissionUsedRecord fail, permissionName:%{public}s, successCount:%{public}d, failCount:%{public}d", 62bae4d13cSopenharmony_ci permissionName.c_str(), successCount, failCount); 63bae4d13cSopenharmony_ci } 64bae4d13cSopenharmony_ci} 65bae4d13cSopenharmony_ci 66bae4d13cSopenharmony_cibool PermissionUtil::IsNativeToken(AccessTokenID callerToken) 67bae4d13cSopenharmony_ci{ 68bae4d13cSopenharmony_ci int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(callerToken); 69bae4d13cSopenharmony_ci if (tokenType != ATokenTypeEnum::TOKEN_NATIVE) { 70bae4d13cSopenharmony_ci SEN_HILOGE("TokenType is not TOKEN_NATIVE, tokenType:%{public}d", tokenType); 71bae4d13cSopenharmony_ci return false; 72bae4d13cSopenharmony_ci } 73bae4d13cSopenharmony_ci return true; 74bae4d13cSopenharmony_ci} 75bae4d13cSopenharmony_ci 76bae4d13cSopenharmony_ciint32_t PermissionUtil::CheckManageSensorPermission(AccessTokenID callerToken) 77bae4d13cSopenharmony_ci{ 78bae4d13cSopenharmony_ci int32_t ret = AccessTokenKit::VerifyAccessToken(callerToken, MANAGE_SENSOR_PERMISSION); 79bae4d13cSopenharmony_ci if (ret != PERMISSION_GRANTED) { 80bae4d13cSopenharmony_ci SEN_HILOGE("Verify MANAGE_SENSOR permission failed, ret:%{public}d", ret); 81bae4d13cSopenharmony_ci } 82bae4d13cSopenharmony_ci return ret; 83bae4d13cSopenharmony_ci} 84bae4d13cSopenharmony_ci} // namespace Sensors 85bae4d13cSopenharmony_ci} // namespace OHOS 86