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