• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "local_remote_auth_context.h"
17 
18 #include "device_manager_util.h"
19 
20 #include "iam_check.h"
21 #include "iam_common_defines.h"
22 #include "iam_logger.h"
23 #include "iam_para2str.h"
24 #include "iam_ptr.h"
25 #include "relative_timer.h"
26 
27 #define LOG_TAG "USER_AUTH_SA"
28 
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
32 namespace {
33 constexpr uint32_t AUTH_TIME_OUT_MS = 3 * 60 * 1000; // 3min
34 }
35 
LocalRemoteAuthContext(uint64_t contextId,std::shared_ptr<Authentication> auth,LocalRemoteAuthContextParam & param,std::shared_ptr<ContextCallback> callback)36 LocalRemoteAuthContext::LocalRemoteAuthContext(uint64_t contextId, std::shared_ptr<Authentication> auth,
37     LocalRemoteAuthContextParam &param, std::shared_ptr<ContextCallback> callback)
38     : SimpleAuthContext("LocalRemoteAuthContext", contextId, auth, callback),
39       collectorNetworkId_(param.collectorNetworkId)
40 {
41 }
42 
~LocalRemoteAuthContext()43 LocalRemoteAuthContext::~LocalRemoteAuthContext()
44 {
45     std::lock_guard<std::recursive_mutex> lock(mutex_);
46     if (cancelTimerId_.has_value()) {
47         RelativeTimer::GetInstance().Unregister(cancelTimerId_.value());
48     }
49     IAM_LOGI("%{public}s destroy", GetDescription());
50 }
51 
GetContextType() const52 ContextType LocalRemoteAuthContext::GetContextType() const
53 {
54     return LOCAL_REMOTE_AUTH_CONTEXT;
55 }
56 
OnStart()57 bool LocalRemoteAuthContext::OnStart()
58 {
59     std::string collectorUdid;
60     bool getCollectorUdidRet = DeviceManagerUtil::GetInstance().GetUdidByNetworkId(collectorNetworkId_, collectorUdid);
61     IF_FALSE_LOGE_AND_RETURN_VAL(getCollectorUdidRet, false);
62 
63     IAM_LOGI("%{public}s start", GetDescription());
64     IF_FALSE_LOGE_AND_RETURN_VAL(auth_ != nullptr, false);
65     auth_->SetCollectorUdid(collectorUdid);
66 
67     {
68         std::lock_guard<std::recursive_mutex> lock(mutex_);
69         cancelTimerId_ = RelativeTimer::GetInstance().Register(
70             [weakThis = weak_from_this(), this]() {
71                 auto sharedThis = weakThis.lock();
72                 IF_FALSE_LOGE_AND_RETURN(sharedThis != nullptr);
73                 OnTimeOut();
74             },
75             AUTH_TIME_OUT_MS);
76     }
77 
78     bool startAuthRet = SimpleAuthContext::OnStart();
79     IF_FALSE_LOGE_AND_RETURN_VAL(startAuthRet, false);
80     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_.size() == 1, false);
81     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_[0] != nullptr, false);
82 
83     IAM_LOGI("%{public}s start local remote auth success, scheduleId:%{public}s", GetDescription(),
84         GET_MASKED_STRING(scheduleList_[0]->GetScheduleId()).c_str());
85     return true;
86 }
87 
OnTimeOut()88 void LocalRemoteAuthContext::OnTimeOut()
89 {
90     IAM_LOGI("%{public}s timeout", GetDescription());
91     std::lock_guard<std::recursive_mutex> lock(mutex_);
92     IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr);
93     Attributes attr;
94     callback_->SetTraceAuthFinishReason("LocalRemoteAuthContext OnTimeOut");
95     callback_->OnResult(ResultCode::TIMEOUT, attr);
96 }
97 
98 } // namespace UserAuth
99 } // namespace UserIam
100 } // namespace OHOS
101