• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "publish_event_adapter.h"
17 
18 #include "common_event_manager.h"
19 #include "event_listener_manager.h"
20 #include "iam_logger.h"
21 
22 #ifndef LOG_LABEL
23 #define LOG_TAG "USER_AUTH_SA"
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
27 namespace {
28 const std::string TAG_USERID = "userId";
29 const std::string TAG_AUTHTYPE = "authType";
30 const std::string TAG_CREDENTIALCOUNT = "credentialCount";
31 const std::string TAG_SCHEDULEID = "scheduleId";
32 const std::string TAG_IS_SILENT_CRED_CHANGE = "isSilentCredChange";
33 const std::string USER_PIN_CREATED_EVENT = "USER_PIN_CREATED_EVENT";
34 const std::string USER_PIN_DELETED_EVENT = "USER_PIN_DELETED_EVENT";
35 const std::string USER_PIN_UPDATED_EVENT = "USER_PIN_UPDATED_EVENT";
36 const std::string USER_CREDENTIAL_UPDATED_EVENT = "USER_CREDENTIAL_UPDATED_EVENT";
37 const std::string USERIAM_COMMON_EVENT_PERMISSION = "ohos.permission.USE_USER_IDM";
38 const std::string USERIAM_COMMON_EVENT_SAMGR_PERMISSION = "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS";
39 
PublishEvent(EventFwk::CommonEventData data,const std::string & permission)40 void PublishEvent(EventFwk::CommonEventData data, const std::string &permission)
41 {
42     EventFwk::CommonEventPublishInfo publishInfo;
43     publishInfo.SetSubscriberPermissions({permission});
44     publishInfo.SetSticky(false);
45     if (!EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo)) {
46         IAM_LOGE("PublishCommonEvent failed, eventAction is %{public}s", data.GetWant().GetAction().c_str());
47         return;
48     }
49     IAM_LOGI("PublishCommonEvent succeed, eventAction is %{public}s", data.GetWant().GetAction().c_str());
50 }
51 } // namespace
52 
GetInstance()53 PublishEventAdapter &PublishEventAdapter::GetInstance()
54 {
55     static PublishEventAdapter instance;
56     return instance;
57 }
58 
PublishDeletedEvent(int32_t userId)59 void PublishEventAdapter::PublishDeletedEvent(int32_t userId)
60 {
61     EventFwk::Want want;
62     want.SetAction(USER_PIN_DELETED_EVENT);
63     EventFwk::CommonEventData data(want);
64     data.SetCode(userId);
65     PublishEvent(data, USERIAM_COMMON_EVENT_SAMGR_PERMISSION);
66     return;
67 }
68 
PublishCreatedEvent(int32_t userId,uint64_t scheduleId)69 void PublishEventAdapter::PublishCreatedEvent(int32_t userId, uint64_t scheduleId)
70 {
71     if (scheduleId == 0) {
72         IAM_LOGE("Bad Parameter!");
73         return;
74     }
75     EventFwk::Want want;
76     want.SetAction(USER_PIN_CREATED_EVENT);
77     want.SetParam(TAG_SCHEDULEID, std::to_string(scheduleId));
78     EventFwk::CommonEventData data(want);
79     data.SetCode(userId);
80     PublishEvent(data, USERIAM_COMMON_EVENT_SAMGR_PERMISSION);
81     return;
82 }
83 
PublishUpdatedEvent(int32_t userId,uint64_t credentialId)84 void PublishEventAdapter::PublishUpdatedEvent(int32_t userId, uint64_t credentialId)
85 {
86     std::lock_guard<std::mutex> lock(mutex_);
87     if (userId != userId_ || credentialId != credChangeEventInfo_.lastCredentialId) {
88         IAM_LOGE("Bad Parameter!");
89         return;
90     }
91     CredChangeEventListenerManager::GetInstance().OnNotifyCredChangeEvent(userId, PIN, UPDATE_CRED,
92         credChangeEventInfo_);
93     EventFwk::Want want;
94     want.SetAction(USER_PIN_UPDATED_EVENT);
95     want.SetParam(TAG_SCHEDULEID, std::to_string(scheduleId_));
96     want.SetParam(TAG_IS_SILENT_CRED_CHANGE, std::to_string(credChangeEventInfo_.isSilentCredChange));
97     EventFwk::CommonEventData data(want);
98     data.SetCode(userId);
99     PublishEvent(data, USERIAM_COMMON_EVENT_SAMGR_PERMISSION);
100     ClearPinUpdateCacheInfo();
101     return;
102 }
103 
CachePinUpdateParam(int32_t userId,uint64_t scheduleId,const CredChangeEventInfo & changeInfo)104 void PublishEventAdapter::CachePinUpdateParam(int32_t userId, uint64_t scheduleId,
105     const CredChangeEventInfo &changeInfo)
106 {
107     std::lock_guard<std::mutex> lock(mutex_);
108     userId_ = userId;
109     scheduleId_ = scheduleId;
110     credChangeEventInfo_ = changeInfo;
111     credChangeEventInfo_.isSilentCredChange = reEnrollFlag_;
112 }
113 
CachePinUpdateParam(bool reEnrollFlag)114 void PublishEventAdapter::CachePinUpdateParam(bool reEnrollFlag)
115 {
116     std::lock_guard<std::mutex> lock(mutex_);
117     reEnrollFlag_ = reEnrollFlag;
118 }
119 
ClearPinUpdateCacheInfo()120 void PublishEventAdapter::ClearPinUpdateCacheInfo()
121 {
122     credChangeEventInfo_ = {};
123     reEnrollFlag_ = false;
124 }
125 
PublishCredentialUpdatedEvent(int32_t userId,int32_t authType,uint32_t credentialCount)126 void PublishEventAdapter::PublishCredentialUpdatedEvent(int32_t userId, int32_t authType, uint32_t credentialCount)
127 {
128     EventFwk::Want want;
129     want.SetAction(USER_CREDENTIAL_UPDATED_EVENT);
130     want.SetParam(TAG_USERID, std::to_string(userId));
131     want.SetParam(TAG_AUTHTYPE, std::to_string(authType));
132     want.SetParam(TAG_CREDENTIALCOUNT, std::to_string(credentialCount));
133     if (authType == AuthType::PIN) {
134         want.SetParam(TAG_IS_SILENT_CRED_CHANGE, std::to_string(credChangeEventInfo_.isSilentCredChange));
135     } else {
136         want.SetParam(TAG_IS_SILENT_CRED_CHANGE, std::to_string(false));
137     }
138     EventFwk::CommonEventData data(want);
139     data.SetCode(0);
140     PublishEvent(data, USERIAM_COMMON_EVENT_PERMISSION);
141     IAM_LOGI("PublishCredentialUpdatedEvent, userId: %{public}d, authType: %{public}d, credentialCount: %{public}u",
142         userId, authType, credentialCount);
143     return;
144 }
145 } // namespace UserAuth
146 } // namespace UserIam
147 } // namespace OHOS
148 #endif