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 "auth_widget_helper.h"
17
18 #include "iam_logger.h"
19 #include "resource_node_pool.h"
20 #include "system_param_manager.h"
21 #include "user_idm_database.h"
22 #include "widget_client.h"
23
24 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
25
26 namespace OHOS {
27 namespace UserIam {
28 namespace UserAuth {
29
InitWidgetContextParam(const AuthParam & authParam,std::vector<AuthType> & validType,const WidgetParam & widgetParam,ContextFactory::AuthWidgetContextPara & para)30 bool AuthWidgetHelper::InitWidgetContextParam(const AuthParam &authParam, std::vector<AuthType> &validType,
31 const WidgetParam &widgetParam, ContextFactory::AuthWidgetContextPara ¶)
32 {
33 for (auto &authType : validType) {
34 ContextFactory::AuthWidgetContextPara::AuthProfile profile;
35 if (!GetUserAuthProfile(para.userId, authType, profile)) {
36 IAM_LOGE("get user auth profile failed");
37 return false;
38 }
39 para.authProfileMap[authType] = profile;
40 if (authType == AuthType::PIN) {
41 WidgetClient::Instance().SetPinSubType(static_cast<PinSubType>(profile.pinSubType));
42 } else if (authType == AuthType::FINGERPRINT) {
43 WidgetClient::Instance().SetSensorInfo(profile.sensorInfo);
44 }
45 }
46 para.challenge = std::move(authParam.challenge);
47 para.authTypeList = std::move(validType);
48 para.atl = authParam.authTrustLevel;
49 para.widgetParam = widgetParam;
50 if (widgetParam.windowMode == WindowModeType::UNKNOWN_WINDOW_MODE) {
51 para.widgetParam.windowMode = WindowModeType::DIALOG_BOX;
52 }
53 return true;
54 }
55
GetUserAuthProfile(int32_t userId,const AuthType & authType,ContextFactory::AuthWidgetContextPara::AuthProfile & profile)56 bool AuthWidgetHelper::GetUserAuthProfile(int32_t userId, const AuthType &authType,
57 ContextFactory::AuthWidgetContextPara::AuthProfile &profile)
58 {
59 Attributes values;
60 auto credentialInfos = UserIdmDatabase::Instance().GetCredentialInfo(userId, authType);
61 if (credentialInfos.empty() || credentialInfos[0] == nullptr) {
62 IAM_LOGE("user %{public}d has no credential type %{public}d", userId, authType);
63 return false;
64 }
65 uint64_t executorIndex = credentialInfos[0]->GetExecutorIndex();
66 auto resourceNode = ResourceNodePool::Instance().Select(executorIndex).lock();
67 if (resourceNode == nullptr) {
68 IAM_LOGE("resourceNode is nullptr");
69 return false;
70 }
71
72 std::vector<uint64_t> templateIds;
73 templateIds.reserve(credentialInfos.size());
74 for (auto &info : credentialInfos) {
75 templateIds.push_back(info->GetTemplateId());
76 }
77 std::vector<uint32_t> uint32Keys = {
78 Attributes::ATTR_SENSOR_INFO,
79 Attributes::ATTR_REMAIN_TIMES,
80 Attributes::ATTR_FREEZING_TIME
81 };
82 if (authType == AuthType::PIN) {
83 uint32Keys.push_back(Attributes::ATTR_PIN_SUB_TYPE);
84 }
85
86 Attributes attr;
87 attr.SetInt32Value(Attributes::ATTR_AUTH_TYPE, authType);
88 attr.SetUint32Value(Attributes::ATTR_PROPERTY_MODE, PROPERTY_MODE_GET);
89 attr.SetUint64ArrayValue(Attributes::ATTR_TEMPLATE_ID_LIST, templateIds);
90 attr.SetUint32ArrayValue(Attributes::ATTR_KEY_LIST, uint32Keys);
91 int32_t result = resourceNode->GetProperty(attr, values);
92 if (result != SUCCESS) {
93 IAM_LOGE("failed to get property, result = %{public}d", result);
94 return false;
95 }
96 return ParseAttributes(values, authType, profile);
97 }
98
ParseAttributes(const Attributes & values,const AuthType & authType,ContextFactory::AuthWidgetContextPara::AuthProfile & profile)99 bool AuthWidgetHelper::ParseAttributes(const Attributes &values, const AuthType &authType,
100 ContextFactory::AuthWidgetContextPara::AuthProfile &profile)
101 {
102 if (authType == AuthType::PIN) {
103 if (!values.GetInt32Value(Attributes::ATTR_PIN_SUB_TYPE, profile.pinSubType)) {
104 IAM_LOGE("get ATTR_PIN_SUB_TYPE failed");
105 return false;
106 }
107 }
108 if (!values.GetStringValue(Attributes::ATTR_SENSOR_INFO, profile.sensorInfo)) {
109 IAM_LOGE("get ATTR_SENSOR_INFO failed");
110 return false;
111 }
112 if (!values.GetInt32Value(Attributes::ATTR_REMAIN_TIMES, profile.remainTimes)) {
113 IAM_LOGE("get ATTR_REMAIN_TIMES failed");
114 return false;
115 }
116 if (!values.GetInt32Value(Attributes::ATTR_FREEZING_TIME, profile.freezingTime)) {
117 IAM_LOGE("get ATTR_FREEZING_TIME failed");
118 return false;
119 }
120 return true;
121 }
122
CheckValidSolution(int32_t userId,const std::vector<AuthType> & authTypeList,const AuthTrustLevel & atl,std::vector<AuthType> & validTypeList)123 int32_t AuthWidgetHelper::CheckValidSolution(int32_t userId,
124 const std::vector<AuthType> &authTypeList, const AuthTrustLevel &atl, std::vector<AuthType> &validTypeList)
125 {
126 IAM_LOGI("start userId:%{public}d atl:%{public}u typeSize:%{public}zu", userId, atl, authTypeList.size());
127 auto hdi = HdiWrapper::GetHdiInstance();
128 if (hdi == nullptr) {
129 IAM_LOGE("hdi interface is nullptr");
130 return GENERAL_ERROR;
131 }
132 std::vector<HdiAuthType> inputAuthType;
133 std::vector<HdiAuthType> validTypes;
134 uint32_t inputAtl = atl;
135 for (auto &type : authTypeList) {
136 if (!SystemParamManager::GetInstance().IsAuthTypeEnable(type)) {
137 IAM_LOGE("authType:%{public}d not enable", type);
138 continue;
139 }
140 inputAuthType.emplace_back(static_cast<HdiAuthType>(type));
141 }
142 int32_t result = hdi->GetValidSolution(userId, inputAuthType, inputAtl, validTypes);
143 if (result != SUCCESS) {
144 IAM_LOGE("GetValidSolution failed result:%{public}d userId:%{public}d", result, userId);
145 return result;
146 }
147 validTypeList.clear();
148 for (auto &type : validTypes) {
149 IAM_LOGI("get valid authType:%{public}d", type);
150 validTypeList.emplace_back(static_cast<AuthType>(type));
151 }
152 return result;
153 }
154 } // namespace UserAuth
155 } // namespace UserIam
156 } // namespace OHOS