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_client_impl.h"
17
18 #include "system_ability_definition.h"
19
20 #include "iam_logger.h"
21 #include "ipc_client_utils.h"
22 #include "user_auth_callback_service.h"
23
24 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SDK
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
GetAvailableStatus(AuthType authType,AuthTrustLevel authTrustLevel)28 int32_t UserAuthClientImpl::GetAvailableStatus(AuthType authType, AuthTrustLevel authTrustLevel)
29 {
30 return GetAvailableStatus(INT32_MAX, authType, authTrustLevel);
31 }
32
GetAvailableStatus(int32_t apiVersion,AuthType authType,AuthTrustLevel authTrustLevel)33 int32_t UserAuthClientImpl::GetAvailableStatus(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel)
34 {
35 auto proxy = GetProxy();
36 if (!proxy) {
37 IAM_LOGE("proxy is nullptr");
38 return GENERAL_ERROR;
39 }
40
41 return proxy->GetAvailableStatus(apiVersion, authType, authTrustLevel);
42 }
43
GetProperty(int32_t userId,const GetPropertyRequest & request,const std::shared_ptr<GetPropCallback> & callback)44 void UserAuthClientImpl::GetProperty(int32_t userId, const GetPropertyRequest &request,
45 const std::shared_ptr<GetPropCallback> &callback)
46 {
47 if (!callback) {
48 IAM_LOGE("get prop callback is nullptr");
49 return;
50 }
51
52 auto proxy = GetProxy();
53 if (!proxy) {
54 IAM_LOGE("proxy is nullptr");
55 Attributes extraInfo;
56 callback->OnResult(GENERAL_ERROR, extraInfo);
57 return;
58 }
59
60 sptr<GetExecutorPropertyCallbackInterface> wrapper =
61 new (std::nothrow) GetExecutorPropertyCallbackService(callback);
62 if (wrapper == nullptr) {
63 IAM_LOGE("failed to create wrapper");
64 Attributes extraInfo;
65 callback->OnResult(GENERAL_ERROR, extraInfo);
66 return;
67 }
68 proxy->GetProperty(userId, request.authType, request.keys, wrapper);
69 }
70
SetProperty(int32_t userId,const SetPropertyRequest & request,const std::shared_ptr<SetPropCallback> & callback)71 void UserAuthClientImpl::SetProperty(int32_t userId, const SetPropertyRequest &request,
72 const std::shared_ptr<SetPropCallback> &callback)
73 {
74 if (!callback) {
75 IAM_LOGE("set prop callback is nullptr");
76 return;
77 }
78
79 auto proxy = GetProxy();
80 if (!proxy) {
81 IAM_LOGE("proxy is nullptr");
82 Attributes extraInfo;
83 callback->OnResult(GENERAL_ERROR, extraInfo);
84 return;
85 }
86
87 sptr<SetExecutorPropertyCallbackInterface> wrapper =
88 new (std::nothrow) SetExecutorPropertyCallbackService(callback);
89 if (wrapper == nullptr) {
90 IAM_LOGE("failed to create wrapper");
91 Attributes extraInfo;
92 callback->OnResult(GENERAL_ERROR, extraInfo);
93 return;
94 }
95 proxy->SetProperty(userId, request.authType, request.attrs, wrapper);
96 }
97
BeginAuthentication(int32_t userId,const std::vector<uint8_t> & challenge,AuthType authType,AuthTrustLevel atl,const std::shared_ptr<AuthenticationCallback> & callback)98 uint64_t UserAuthClientImpl::BeginAuthentication(int32_t userId, const std::vector<uint8_t> &challenge,
99 AuthType authType, AuthTrustLevel atl, const std::shared_ptr<AuthenticationCallback> &callback)
100 {
101 if (!callback) {
102 IAM_LOGE("auth callback is nullptr");
103 return INVALID_SESSION_ID;
104 }
105
106 auto proxy = GetProxy();
107 if (!proxy) {
108 IAM_LOGE("proxy is nullptr");
109 Attributes extraInfo;
110 callback->OnResult(GENERAL_ERROR, extraInfo);
111 return INVALID_SESSION_ID;
112 }
113
114 sptr<UserAuthCallbackInterface> wrapper = new (std::nothrow) UserAuthCallbackService(callback);
115 if (wrapper == nullptr) {
116 IAM_LOGE("failed to create wrapper");
117 Attributes extraInfo;
118 callback->OnResult(GENERAL_ERROR, extraInfo);
119 return INVALID_SESSION_ID;
120 }
121 return proxy->AuthUser(userId, challenge, authType, atl, wrapper);
122 }
123
BeginNorthAuthentication(int32_t apiVersion,const std::vector<uint8_t> & challenge,AuthType authType,AuthTrustLevel atl,const std::shared_ptr<AuthenticationCallback> & callback)124 uint64_t UserAuthClientImpl::BeginNorthAuthentication(int32_t apiVersion, const std::vector<uint8_t> &challenge,
125 AuthType authType, AuthTrustLevel atl, const std::shared_ptr<AuthenticationCallback> &callback)
126 {
127 if (!callback) {
128 IAM_LOGE("auth callback is nullptr");
129 return INVALID_SESSION_ID;
130 }
131
132 auto proxy = GetProxy();
133 if (!proxy) {
134 IAM_LOGE("proxy is nullptr");
135 Attributes extraInfo;
136 callback->OnResult(GENERAL_ERROR, extraInfo);
137 return INVALID_SESSION_ID;
138 }
139
140 sptr<UserAuthCallbackInterface> wrapper = new (std::nothrow) UserAuthCallbackService(callback);
141 if (wrapper == nullptr) {
142 IAM_LOGE("failed to create wrapper");
143 Attributes extraInfo;
144 callback->OnResult(GENERAL_ERROR, extraInfo);
145 return INVALID_SESSION_ID;
146 }
147 return proxy->Auth(apiVersion, challenge, authType, atl, wrapper);
148 }
149
CancelAuthentication(uint64_t contextId)150 int32_t UserAuthClientImpl::CancelAuthentication(uint64_t contextId)
151 {
152 auto proxy = GetProxy();
153 if (!proxy) {
154 IAM_LOGE("proxy is nullptr");
155 return GENERAL_ERROR;
156 }
157
158 return proxy->CancelAuthOrIdentify(contextId);
159 }
160
BeginIdentification(const std::vector<uint8_t> & challenge,AuthType authType,const std::shared_ptr<IdentificationCallback> & callback)161 uint64_t UserAuthClientImpl::BeginIdentification(const std::vector<uint8_t> &challenge, AuthType authType,
162 const std::shared_ptr<IdentificationCallback> &callback)
163 {
164 if (!callback) {
165 IAM_LOGE("identify callback is nullptr");
166 return INVALID_SESSION_ID;
167 }
168
169 auto proxy = GetProxy();
170 if (!proxy) {
171 IAM_LOGE("proxy is nullptr");
172 Attributes extraInfo;
173 callback->OnResult(GENERAL_ERROR, extraInfo);
174 return INVALID_SESSION_ID;
175 }
176
177 sptr<UserAuthCallbackInterface> wrapper = new (std::nothrow) UserAuthCallbackService(callback);
178 if (wrapper == nullptr) {
179 IAM_LOGE("failed to create wrapper");
180 Attributes extraInfo;
181 callback->OnResult(GENERAL_ERROR, extraInfo);
182 return INVALID_SESSION_ID;
183 }
184 return proxy->Identify(challenge, authType, wrapper);
185 }
186
CancelIdentification(uint64_t contextId)187 int32_t UserAuthClientImpl::CancelIdentification(uint64_t contextId)
188 {
189 auto proxy = GetProxy();
190 if (!proxy) {
191 IAM_LOGE("proxy is nullptr");
192 return GENERAL_ERROR;
193 }
194
195 return proxy->CancelAuthOrIdentify(contextId);
196 }
197
GetVersion(int32_t & version)198 int32_t UserAuthClientImpl::GetVersion(int32_t &version)
199 {
200 auto proxy = GetProxy();
201 if (!proxy) {
202 IAM_LOGE("proxy is nullptr");
203 return GENERAL_ERROR;
204 }
205
206 return proxy->GetVersion(version);
207 }
208
GetProxy()209 sptr<UserAuthInterface> UserAuthClientImpl::GetProxy()
210 {
211 auto obj = IpcClientUtils::GetRemoteObject(SUBSYS_USERIAM_SYS_ABILITY_USERAUTH);
212 if (obj == nullptr) {
213 IAM_LOGE("failed to get userauth service");
214 return nullptr;
215 }
216
217 return iface_cast<UserAuthInterface>(obj);
218 }
219
Instance()220 UserAuthClientImpl &UserAuthClientImpl::Instance()
221 {
222 static UserAuthClientImpl impl;
223 return impl;
224 }
225
GetInstance()226 UserAuthClient &UserAuthClient::GetInstance()
227 {
228 return UserAuthClientImpl::Instance();
229 }
230 } // namespace UserAuth
231 } // namespace UserIam
232 } // namespace OHOS