1 /*
2 * Copyright (c) 2022 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_auth.h"
17 #include <if_system_ability_manager.h>
18 #include <iservice_registry.h>
19 #include <system_ability_definition.h>
20 #include "system_ability_definition.h"
21 #include "userauth_hilog_wrapper.h"
22
23 namespace OHOS {
24 namespace UserIAM {
25 namespace UserAuth {
26 UserAuth::UserAuth() = default;
27 UserAuth::~UserAuth() = default;
28
GetProxy()29 sptr<IUserAuth> UserAuth::GetProxy()
30 {
31 USERAUTH_HILOGD(MODULE_INNERKIT, "GetProxy start");
32 std::lock_guard lock(mutex_);
33 if (proxy_ != nullptr) {
34 return proxy_;
35 }
36 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
37 if (sam == nullptr) {
38 USERAUTH_HILOGE(MODULE_INNERKIT, "Failed to get system manager");
39 return nullptr;
40 }
41 sptr<IRemoteObject> obj = sam->CheckSystemAbility(SUBSYS_USERIAM_SYS_ABILITY_USERAUTH);
42 if (obj == nullptr) {
43 USERAUTH_HILOGE(MODULE_INNERKIT, "Failed to get userauth service");
44 return nullptr;
45 }
46 sptr<IRemoteObject::DeathRecipient> dr = new UserAuthDeathRecipient();
47 if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) {
48 USERAUTH_HILOGE(MODULE_INNERKIT, "Failed to add death recipient");
49 return nullptr;
50 }
51
52 proxy_ = iface_cast<IUserAuth>(obj);
53 deathRecipient_ = dr;
54 USERAUTH_HILOGI(MODULE_INNERKIT, "Succeed to connect userauth service");
55 return proxy_;
56 }
57
ResetProxy(const wptr<IRemoteObject> & remote)58 void UserAuth::ResetProxy(const wptr<IRemoteObject> &remote)
59 {
60 USERAUTH_HILOGD(MODULE_INNERKIT, "ResetProxy start");
61 std::lock_guard<std::mutex> lock(mutex_);
62 if (proxy_ == nullptr) {
63 return;
64 }
65
66 auto serviceRemote = proxy_->AsObject();
67 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
68 serviceRemote->RemoveDeathRecipient(deathRecipient_);
69 proxy_ = nullptr;
70 }
71 }
72
OnRemoteDied(const wptr<IRemoteObject> & remote)73 void UserAuth::UserAuthDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
74 {
75 USERAUTH_HILOGD(MODULE_INNERKIT, "OnRemoteDied start");
76 if (remote == nullptr) {
77 USERAUTH_HILOGE(MODULE_INNERKIT, "OnRemoteDied failed, remote is nullptr");
78 return;
79 }
80
81 UserAuth::GetInstance().ResetProxy(remote);
82 }
83
GetAvailableStatus(const AuthType authType,const AuthTurstLevel authTurstLevel)84 int32_t UserAuth::GetAvailableStatus(const AuthType authType, const AuthTurstLevel authTurstLevel)
85 {
86 USERAUTH_HILOGD(MODULE_INNERKIT, "GetAvailableStatus start");
87 auto proxy = GetProxy();
88 if (proxy == nullptr) {
89 return E_RET_NOSERVER;
90 }
91
92 return proxy_->GetAvailableStatus(authType, authTurstLevel);
93 }
94
GetProperty(const GetPropertyRequest & request,std::shared_ptr<GetPropCallback> callback)95 void UserAuth::GetProperty(const GetPropertyRequest &request, std::shared_ptr<GetPropCallback> callback)
96 {
97 USERAUTH_HILOGD(MODULE_INNERKIT, "GetProperty start");
98 if (callback == nullptr) {
99 USERAUTH_HILOGE(MODULE_INNERKIT, "GetProperty callback is nullptr");
100 return;
101 }
102 auto proxy = GetProxy();
103 if (proxy == nullptr) {
104 ExecutorProperty result = {};
105 result.result = E_RET_NOSERVER;
106 callback->onGetProperty(result);
107 return;
108 }
109 sptr<IUserAuthCallback> asyncStub = new (std::nothrow) UserAuthAsyncStub(callback);
110 if (asyncStub == nullptr) {
111 USERAUTH_HILOGE(MODULE_INNERKIT, "GetProperty asyncStub is nullptr");
112 return;
113 }
114 proxy_->GetProperty(request, asyncStub);
115 }
116
SetProperty(const SetPropertyRequest & request,std::shared_ptr<SetPropCallback> callback)117 void UserAuth::SetProperty(const SetPropertyRequest &request, std::shared_ptr<SetPropCallback> callback)
118 {
119 USERAUTH_HILOGD(MODULE_INNERKIT, "SetProperty start");
120 if (callback == nullptr) {
121 USERAUTH_HILOGE(MODULE_INNERKIT, "SetProperty callback is nullptr");
122 return;
123 }
124 auto proxy = GetProxy();
125 if (proxy == nullptr) {
126 callback->onSetProperty(E_RET_NOSERVER);
127 return;
128 }
129 sptr<IUserAuthCallback> asyncStub = new (std::nothrow) UserAuthAsyncStub(callback);
130 if (asyncStub == nullptr) {
131 USERAUTH_HILOGE(MODULE_INNERKIT, "SetProperty asyncStub is nullptr");
132 return;
133 }
134 proxy_->SetProperty(request, asyncStub);
135 }
136
Auth(const uint64_t challenge,const AuthType authType,const AuthTurstLevel authTurstLevel,std::shared_ptr<UserAuthCallback> callback)137 uint64_t UserAuth::Auth(const uint64_t challenge, const AuthType authType, const AuthTurstLevel authTurstLevel,
138 std::shared_ptr<UserAuthCallback> callback)
139 {
140 USERAUTH_HILOGD(MODULE_INNERKIT, "Auth start");
141 if (callback == nullptr) {
142 USERAUTH_HILOGE(MODULE_INNERKIT, "Auth callback is nullptr");
143 return INVALID_PARAMETERS;
144 }
145 auto proxy = GetProxy();
146 if (proxy == nullptr) {
147 return E_RET_NOSERVER;
148 }
149 sptr<IUserAuthCallback> asyncStub = new (std::nothrow) UserAuthAsyncStub(callback);
150 if (asyncStub == nullptr) {
151 USERAUTH_HILOGE(MODULE_INNERKIT, "Auth asyncStub is nullptr");
152 return GENERAL_ERROR;
153 }
154 return proxy_->Auth(challenge, authType, authTurstLevel, asyncStub);
155 }
156
AuthUser(const int32_t userId,const uint64_t challenge,const AuthType authType,const AuthTurstLevel authTurstLevel,std::shared_ptr<UserAuthCallback> callback)157 uint64_t UserAuth::AuthUser(const int32_t userId, const uint64_t challenge, const AuthType authType,
158 const AuthTurstLevel authTurstLevel, std::shared_ptr<UserAuthCallback> callback)
159 {
160 USERAUTH_HILOGD(MODULE_INNERKIT, "AuthUser start");
161 if (callback == nullptr) {
162 USERAUTH_HILOGE(MODULE_INNERKIT, "AuthUser callback is nullptr");
163 return INVALID_PARAMETERS;
164 }
165 auto proxy = GetProxy();
166 if (proxy == nullptr) {
167 return E_RET_NOSERVER;
168 }
169 sptr<IUserAuthCallback> asyncStub = new (std::nothrow) UserAuthAsyncStub(callback);
170 if (asyncStub == nullptr) {
171 USERAUTH_HILOGE(MODULE_INNERKIT, "AuthUser asyncStub is nullptr");
172 return GENERAL_ERROR;
173 }
174 return proxy_->AuthUser(userId, challenge, authType, authTurstLevel, asyncStub);
175 }
176
CancelAuth(const uint64_t contextId)177 int32_t UserAuth::CancelAuth(const uint64_t contextId)
178 {
179 USERAUTH_HILOGD(MODULE_INNERKIT, "CancelAuth start");
180 auto proxy = GetProxy();
181 if (proxy == nullptr) {
182 return E_RET_NOSERVER;
183 }
184
185 return proxy_->CancelAuth(contextId);
186 }
187
GetVersion()188 int32_t UserAuth::GetVersion()
189 {
190 USERAUTH_HILOGD(MODULE_INNERKIT, "GetVersion start");
191 auto proxy = GetProxy();
192 if (proxy == nullptr) {
193 return INVALID_PARAMETERS;
194 }
195
196 return proxy_->GetVersion();
197 }
198 } // namespace UserAuth
199 } // namespace UserIAM
200 } // namespace OHOS
201