1 /*
2 * Copyright (c) 2024 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 "user_access_ctrl_client_impl.h"
17
18 #include "system_ability_definition.h"
19
20 #include "callback_manager.h"
21 #include "iam_common_defines.h"
22 #include "iam_logger.h"
23 #include "ipc_client_utils.h"
24 #include "user_access_ctrl_callback_service.h"
25
26 #define LOG_TAG "USER_ACCESS_CTRL_SDK"
27
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
VerifyAuthToken(const std::vector<uint8_t> & tokenIn,uint64_t allowableDuration,const std::shared_ptr<VerifyTokenCallback> & callback)31 void UserAccessCtrlClientImpl::VerifyAuthToken(const std::vector<uint8_t> &tokenIn, uint64_t allowableDuration,
32 const std::shared_ptr<VerifyTokenCallback> &callback)
33 {
34 IAM_LOGI("start");
35 if (!callback) {
36 IAM_LOGE("user access ctrl client callback is nullptr");
37 return;
38 }
39 auto proxy = GetProxy();
40 if (!proxy) {
41 IAM_LOGE("proxy is nullptr");
42 Attributes extraInfo;
43 callback->OnResult(GENERAL_ERROR, extraInfo);
44 return;
45 }
46
47 sptr<IVerifyTokenCallback> wrapper(new (std::nothrow) VerifyTokenCallbackService(callback));
48 if (wrapper == nullptr) {
49 IAM_LOGE("failed to create wrapper");
50 Attributes extraInfo;
51 callback->OnResult(GENERAL_ERROR, extraInfo);
52 return;
53 }
54 proxy->VerifyAuthToken(tokenIn, allowableDuration, wrapper);
55 }
56
GetProxy()57 sptr<IUserAuth> UserAccessCtrlClientImpl::GetProxy()
58 {
59 std::lock_guard<std::mutex> lock(mutex_);
60 if (proxy_ != nullptr) {
61 return proxy_;
62 }
63 sptr<IRemoteObject> obj = IpcClientUtils::GetRemoteObject(SUBSYS_USERIAM_SYS_ABILITY_USERAUTH);
64 if (obj == nullptr) {
65 IAM_LOGE("remote object is null");
66 return proxy_;
67 }
68 sptr<IRemoteObject::DeathRecipient> dr(new (std::nothrow) UserAccessCtrlImplDeathRecipient());
69 if ((dr == nullptr) || (obj->IsProxyObject() && !obj->AddDeathRecipient(dr))) {
70 IAM_LOGE("add death recipient fail");
71 return proxy_;
72 }
73
74 proxy_ = iface_cast<IUserAuth>(obj);
75 deathRecipient_ = dr;
76 return proxy_;
77 }
78
ResetProxy(const wptr<IRemoteObject> & remote)79 void UserAccessCtrlClientImpl::ResetProxy(const wptr<IRemoteObject> &remote)
80 {
81 IAM_LOGI("start");
82 std::lock_guard<std::mutex> lock(mutex_);
83 if (proxy_ == nullptr) {
84 IAM_LOGE("proxy_ is null");
85 return;
86 }
87 auto serviceRemote = proxy_->AsObject();
88 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
89 IAM_LOGI("need reset");
90 serviceRemote->RemoveDeathRecipient(deathRecipient_);
91 proxy_ = nullptr;
92 deathRecipient_ = nullptr;
93 }
94 IAM_LOGI("end reset proxy");
95 }
96
OnRemoteDied(const wptr<IRemoteObject> & remote)97 void UserAccessCtrlClientImpl::UserAccessCtrlImplDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
98 {
99 IAM_LOGI("start");
100 if (remote == nullptr) {
101 IAM_LOGE("remote is nullptr");
102 return;
103 }
104 CallbackManager::GetInstance().OnServiceDeath();
105 UserAccessCtrlClientImpl::Instance().ResetProxy(remote);
106 }
107
Instance()108 UserAccessCtrlClientImpl &UserAccessCtrlClientImpl::Instance()
109 {
110 static UserAccessCtrlClientImpl impl;
111 return impl;
112 }
113
GetInstance()114 UserAccessCtrlClient &UserAccessCtrlClient::GetInstance()
115 {
116 return UserAccessCtrlClientImpl::Instance();
117 }
118 } // namespace UserAuth
119 } // namespace UserIam
120 } // namespace OHOS