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 "template_cache_manager.h"
17
18 #include <mutex>
19
20 #include "os_account_manager.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 #include "system_ability_status_change_stub.h"
24
25 #include "iam_check.h"
26 #include "iam_logger.h"
27 #include "iam_ptr.h"
28 #include "context_pool.h"
29 #include "resource_node_pool.h"
30 #include "resource_node_utils.h"
31 #include "user_idm_database.h"
32 #include "thread_handler.h"
33
34 #define LOG_TAG "USER_AUTH_SA"
35 namespace OHOS {
36 namespace UserIam {
37 namespace UserAuth {
TemplateCacheManager()38 TemplateCacheManager::TemplateCacheManager()
39 {
40 IAM_LOGI("init");
41 }
42
GetInstance()43 TemplateCacheManager &TemplateCacheManager::GetInstance()
44 {
45 static TemplateCacheManager templateCacheManager;
46 return templateCacheManager;
47 }
48
ProcessUserIdChange(const int newUserId)49 void TemplateCacheManager::ProcessUserIdChange(const int newUserId)
50 {
51 {
52 std::lock_guard<std::recursive_mutex> lock(recursiveMutex_);
53 if (newUserId == currUserId_) {
54 IAM_LOGE("same userId %{public}d", newUserId);
55 return;
56 }
57
58 IAM_LOGI("begin");
59 currUserId_ = newUserId;
60 }
61 UpdateTemplateCache(FACE);
62 UpdateTemplateCache(FINGERPRINT);
63 IAM_LOGI("done");
64 return;
65 }
66
UpdateTemplateCache(AuthType authType)67 void TemplateCacheManager::UpdateTemplateCache(AuthType authType)
68 {
69 IAM_LOGI("start");
70 int32_t currUserId = 0;
71 {
72 std::lock_guard<std::recursive_mutex> lock(recursiveMutex_);
73 currUserId = currUserId_;
74 }
75
76 IF_FALSE_LOGE_AND_RETURN(currUserId != INVALID_USER_ID);
77 if (authType != FACE && authType != FINGERPRINT) {
78 IAM_LOGI("this auth type is not cached");
79 return;
80 }
81
82 std::vector<std::shared_ptr<CredentialInfoInterface>> credentialInfos;
83 int32_t ret = UserIdmDatabase::Instance().GetCredentialInfo(currUserId, authType, credentialInfos);
84 if (ret != SUCCESS) {
85 IAM_LOGE("get credential fail, ret:%{public}d, userId:%{public}d, authType:%{public}d", ret,
86 currUserId, authType);
87 return;
88 }
89
90 if (credentialInfos.empty()) {
91 IAM_LOGI("user %{public}d has no credential type %{public}d", currUserId, authType);
92 ResourceNodePool::Instance().Enumerate([authType](const std::weak_ptr<ResourceNode> &node) {
93 auto nodeTmp = node.lock();
94 IF_FALSE_LOGE_AND_RETURN(nodeTmp != nullptr);
95
96 if (nodeTmp->GetAuthType() != authType) {
97 return;
98 }
99 IAM_LOGI("clear cached template for type %{public}d", authType);
100 ResourceNodeUtils::SetCachedTemplates(nodeTmp->GetExecutorIndex(),
101 std::vector<std::shared_ptr<CredentialInfoInterface>>());
102 });
103 return;
104 }
105
106 IAM_LOGI("user %{public}d type %{public}d credential info size %{public}zu",
107 currUserId, authType, credentialInfos.size());
108 std::map<uint64_t, std::vector<std::shared_ptr<CredentialInfoInterface>>> id2Cred;
109 ResultCode result = ResourceNodeUtils::ClassifyCredInfoByExecutor(credentialInfos, id2Cred);
110 IF_FALSE_LOGE_AND_RETURN(result == SUCCESS);
111
112 for (auto const &pair : id2Cred) {
113 ResourceNodeUtils::SetCachedTemplates(pair.first, pair.second);
114 }
115 }
116 } // namespace UserAuth
117 } // namespace UserIam
118 } // namespace OHOS