1 /*
2  * Copyright (c) 2021-2023 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 #ifndef OS_ACCOUNT_INTERFACES_KITS_NAPI_APPACCOUNT_INCLUDE_NAPI_APP_ACCOUNT_COMMON_H
17 #define OS_ACCOUNT_INTERFACES_KITS_NAPI_APPACCOUNT_INCLUDE_NAPI_APP_ACCOUNT_COMMON_H
18 #include <mutex>
19 #include <thread>
20 #include <uv.h>
21 #include "app_account_authenticator_callback_stub.h"
22 #include "app_account_common.h"
23 #include "app_account_manager.h"
24 #include "app_account_subscriber.h"
25 #include "napi/native_api.h"
26 #include "napi/native_node_api.h"
27 #include "napi_account_common.h"
28 #include "want.h"
29 
30 namespace OHOS {
31 namespace AccountJsKit {
32 using namespace OHOS::AccountSA;
33 constexpr std::int32_t MAX_VALUE_LEN = 4096;
34 constexpr std::int32_t ARGS_SIZE_ONE = 1;
35 constexpr std::int32_t ARGS_SIZE_TWO = 2;
36 constexpr std::int32_t ARGS_SIZE_THREE = 3;
37 constexpr std::int32_t ARGS_SIZE_FOUR = 4;
38 constexpr std::int32_t ARGS_SIZE_FIVE = 5;
39 constexpr std::int32_t ARGS_SIZE_SIX = 6;
40 constexpr std::int32_t ARGS_SIZE_MAX = 10;
41 constexpr int RESULT_COUNT = 2;
42 constexpr int PARAMZERO = 0;
43 constexpr int PARAMONE = 1;
44 constexpr int PARAMTWO = 2;
45 constexpr int PARAMTHREE = 3;
46 constexpr int PARAMFOUR = 4;
47 constexpr int PARAMFIVE = 5;
48 
49 class AppAccountManagerCallback;
50 struct AsyncContextForSubscribe;
51 extern std::mutex g_lockForAppAccountSubscribers;
52 extern std::map<AppAccountManager *, std::vector<AsyncContextForSubscribe *>> g_AppAccountSubscribers;
53 
54 class SubscriberPtr : public AppAccountSubscriber {
55 public:
56     explicit SubscriberPtr(const AppAccountSubscribeInfo &subscribeInfo);
57 
58     void OnAccountsChanged(const std::vector<AppAccountInfo> &accounts) override;
59 
60     void SetEnv(const napi_env &env);
61     void SetCallbackRef(const napi_ref &ref);
62 
63 private:
64     napi_env env_ = nullptr;
65     napi_ref ref_ = nullptr;
66 };
67 
68 struct AppAccountAsyncContext : public CommonAsyncContext {
69     AppAccountAsyncContext(napi_env napiEnv, bool isThrowable = false) : CommonAsyncContext(napiEnv, isThrowable) {};
70     std::string name;
71     std::string owner;
72     std::string extraInfo;
73     std::string bundleName;
74     std::string credentialType;
75     std::string credential;
76     std::string key;
77     std::string value;
78     bool isAccessible = false;
79     bool isEnable = false;
80     bool result = false;
81 };
82 
83 struct JSAuthCallback {
84     napi_ref onResult = nullptr;
85     napi_ref onRequestRedirected = nullptr;
86     napi_ref onRequestContinued = nullptr;
87 };
88 
89 struct OAuthAsyncContext : public CommonAsyncContext {
90     OAuthAsyncContext(napi_env env, bool throwAble = false) : CommonAsyncContext(env, throwAble) {};
91     std::string name;
92     std::string owner;
93     std::string sessionId;
94     std::string bundleName;
95     std::string authType;
96     std::string token;
97     std::set<std::string> authList;
98     bool isVisible = false;
99     AAFwk::Want options;
100     AuthenticatorInfo authenticatorInfo;
101     std::vector<OAuthTokenInfo> oauthTokenInfos;
102     JSAuthCallback callback;
103     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
104     sptr<IRemoteObject> authenticatorCb = nullptr;
105 };
106 
107 struct VerifyCredentialContext : public CommonAsyncContext {
108     VerifyCredentialContext(napi_env env, bool throwAble = false) : CommonAsyncContext(env, throwAble) {};
109     std::string name;
110     std::string owner;
111     VerifyCredentialOptions options;
112     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
113     JSAuthCallback callback;
114 };
115 
116 struct SetPropertiesContext : public CommonAsyncContext {
117     SetPropertiesContext(napi_env env, bool throwAble = false) : CommonAsyncContext(env, throwAble) {};
118     std::string owner;
119     SetPropertiesOptions options;
120     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
121     JSAuthCallback callback;
122 };
123 
124 // when new PropertyType is added, new error message need to be added in ErrMsgList.
125 typedef enum PropertyType {
126     NAME = 0,
127     OWNER,
128     AUTH_TYPE,
129     BUNDLE_NAME,
130     SESSION_ID,
131     IS_VISIBLE,
132     TOKEN,
133     EXTRA_INFO,
134     CREDENTIAL_TYPE,
135     CREDENTIAL,
136     KEY,
137     VALUE,
138     IS_ACCESSIBLE,
139     IS_ENABLE,
140 } PropertyType;
141 
142 struct SelectAccountsContext : public CommonAsyncContext {
SelectAccountsContextSelectAccountsContext143     SelectAccountsContext(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
144     SelectAccountsOptions options;
145     std::vector<AppAccountInfo> appAccountInfos;
146     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
147 };
148 
149 struct CheckAccountLabelsContext : public CommonAsyncContext {
CheckAccountLabelsContextCheckAccountLabelsContext150     CheckAccountLabelsContext(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
151     std::string name;
152     std::string owner;
153     std::vector<std::string> labels;
154     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
155 };
156 
157 struct GetAccountsAsyncContext : public CommonAsyncContext {
GetAccountsAsyncContextGetAccountsAsyncContext158     GetAccountsAsyncContext(napi_env napiEnv, bool isThrowable) : CommonAsyncContext(napiEnv, isThrowable) {};
159     std::string owner;
160     std::vector<AppAccountInfo> appAccounts;
161 };
162 
163 struct CreateAccountContext : public CommonAsyncContext {
CreateAccountContextCreateAccountContext164     explicit CreateAccountContext(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
165     std::string name;
166     CreateAccountOptions options;
167     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
168 };
169 
170 struct CreateAccountImplicitlyContext : public CommonAsyncContext {
CreateAccountImplicitlyContextCreateAccountImplicitlyContext171     explicit CreateAccountImplicitlyContext(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
172     std::string owner;
173     CreateAccountImplicitlyOptions options;
174     JSAuthCallback callback;
175     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
176 };
177 
178 struct SubscriberAccountsWorker : public CommonAsyncContext {
SubscriberAccountsWorkerSubscriberAccountsWorker179     explicit SubscriberAccountsWorker(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
180     napi_ref ref = nullptr;
181     std::vector<AppAccountInfo> accounts;
182     int code = 0;
183     SubscriberPtr *subscriber = nullptr;
184 };
185 
186 struct AsyncContextForSubscribe : public CommonAsyncContext {
AsyncContextForSubscribeAsyncContextForSubscribe187     explicit AsyncContextForSubscribe(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
188     std::string type;
189     std::vector<std::string> owners;
190     AppAccountManager *appAccountManager = nullptr;
191     std::shared_ptr<SubscriberPtr> subscriber = nullptr;
192 };
193 
194 struct AsyncContextForUnsubscribe : public CommonAsyncContext {
AsyncContextForUnsubscribeAsyncContextForUnsubscribe195     explicit AsyncContextForUnsubscribe(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
196     std::string type;
197     std::vector<std::shared_ptr<SubscriberPtr>> subscribers = {nullptr};
198     AppAccountManager *appAccountManager = nullptr;
199     size_t argc = 0;
200 };
201 
202 struct AuthenticatorCallbackParam : public CommonAsyncContext {
AuthenticatorCallbackParamAuthenticatorCallbackParam203     explicit AuthenticatorCallbackParam(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
204     int32_t resultCode = -1;
205     AAFwk::Want result;
206     AAFwk::Want request;
207     JSAuthCallback callback;
208     CommonAsyncContext context;
209 };
210 
211 class AuthenticatorAsyncCallback : public AppAccountAuthenticatorCallbackStub {
212 public:
213     explicit AuthenticatorAsyncCallback(napi_env env, napi_ref ref, napi_deferred deferred, uv_after_work_cb workCb);
214     ~AuthenticatorAsyncCallback();
215 
216     void OnResult(int32_t resultCode, const AAFwk::Want &result) override;
217     void OnRequestRedirected(AAFwk::Want &request) override;
218     void OnRequestContinued() override;
219 
220 private:
221     std::mutex mutex_;
222     bool isDone = false;
223     napi_env env_ = nullptr;
224     napi_ref callbackRef_ = nullptr;
225     napi_deferred deferred_ = nullptr;
226     uv_after_work_cb workCb_ = nullptr;
227 };
228 
229 class AppAccountManagerCallback : public AppAccountAuthenticatorCallbackStub {
230 public:
231     explicit AppAccountManagerCallback(napi_env env, JSAuthCallback callback);
232     ~AppAccountManagerCallback();
233 
234     void OnResult(int32_t resultCode, const AAFwk::Want &result) override;
235     void OnRequestRedirected(AAFwk::Want &request) override;
236     void OnRequestContinued() override;
237 
238 private:
239     std::mutex mutex_;
240     bool isDone = false;
241     napi_env env_ = nullptr;
242     JSAuthCallback callback_;
243 };
244 
245 bool InitAuthenticatorWorkEnv(
246     napi_env env, uv_loop_s **loop, uv_work_t **work, AuthenticatorCallbackParam **param);
247 
248 napi_value NapiGetNull(napi_env env);
249 
250 std::string GetNamedProperty(napi_env env, napi_value obj);
251 
252 void SetNamedProperty(napi_env env, napi_value dstObj, const char *objName, const char *propName);
253 
254 void SetNamedProperty(napi_env env, napi_value dstObj, const int32_t objValue, const char *propName);
255 
256 napi_value GetErrorCodeValue(napi_env env, int errCode);
257 
258 bool GetArrayLength(napi_env env, napi_value value, uint32_t &length);
259 
260 void CheckAccountLabelsOnResultWork(uv_work_t *work, int status);
261 
262 void SelectAccountsOnResultWork(uv_work_t *work, int status);
263 
264 void GetAppAccountInfoForResult(napi_env env, const std::vector<AppAccountInfo> &info, napi_value &result);
265 
266 void GetAuthenticatorInfoForResult(napi_env env, const AuthenticatorInfo &info, napi_value &result);
267 
268 void GetOAuthTokenInfoForResult(napi_env env, const std::vector<OAuthTokenInfo> &info, napi_value result);
269 
270 void GetOAuthListForResult(napi_env env, const std::set<std::string> &info, napi_value result);
271 
272 void GetAuthenticatorCallbackForResult(napi_env env, sptr<IRemoteObject> callback, napi_value *result);
273 
274 bool ParseContextWithExInfo(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext);
275 
276 bool ParseContextForAuth(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext);
277 
278 void ParseContextForAuthenticate(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext, size_t argc);
279 
280 bool ParseContextForOAuth(napi_env env, napi_callback_info cbInfo,
281     OAuthAsyncContext *asyncContext, const std::vector<PropertyType> &propertyList, napi_value *result);
282 
283 bool ParseContextForAppAccount(napi_env env, napi_callback_info cbInfo,
284     AppAccountAsyncContext *asyncContext, const std::vector<PropertyType> &propertyList, napi_value *result);
285 
286 bool ParseContextCBArray(napi_env env, napi_callback_info cbInfo, GetAccountsAsyncContext *asyncContext);
287 
288 bool ParseContextWithStrCBArray(napi_env env, napi_callback_info cbInfo, GetAccountsAsyncContext *asyncContext);
289 
290 bool ParseContextForCreateAccount(napi_env env, napi_callback_info cbInfo, CreateAccountContext *context);
291 
292 bool ParseContextForCreateAccountImplicitly(
293     napi_env env, napi_callback_info cbInfo, CreateAccountImplicitlyContext *context);
294 
295 bool ParseParametersBySubscribe(const napi_env &env, napi_callback_info cbInfo, AsyncContextForSubscribe *context);
296 
297 bool ParseParametersByUnsubscribe(
298     const napi_env &env, napi_callback_info cbInfo, AsyncContextForUnsubscribe *context);
299 
300 napi_value GetSubscriberByUnsubscribe(const napi_env &env, std::vector<std::shared_ptr<SubscriberPtr>> &subscriber,
301     AsyncContextForUnsubscribe *asyncContextForOff, bool &isFind);
302 
303 bool ParseStringVector(napi_env env, napi_value value, std::vector<std::string> &strVec);
304 bool ParseAccountVector(napi_env env, napi_value value, std::vector<std::pair<std::string, std::string>> &accountVec);
305 bool ParseVerifyCredentialOptions(napi_env env, napi_value object, VerifyCredentialOptions &options);
306 bool ParseSelectAccountsOptions(napi_env env, napi_value object, SelectAccountsOptions &options);
307 bool ParseSetPropertiesOptions(napi_env env, napi_value object, SetPropertiesOptions &options);
308 bool ParseCreateAccountOptions(napi_env env, napi_value object, CreateAccountOptions &options);
309 bool GetNamedFunction(napi_env env, napi_value object, const std::string &name, napi_ref &funcRef);
310 bool ParseJSAuthCallback(napi_env env, napi_value object, JSAuthCallback &callback);
311 bool ParseContextForVerifyCredential(napi_env env, napi_callback_info info, VerifyCredentialContext *context);
312 bool ParseContextForSetProperties(napi_env env, napi_callback_info info, SetPropertiesContext *context);
313 bool ParseContextForSelectAccount(napi_env env, napi_callback_info info, SelectAccountsContext *context);
314 bool ParseContextForCheckAccountLabels(napi_env env, napi_callback_info info, CheckAccountLabelsContext *context);
315 
316 void UnsubscribeExecuteCB(napi_env env, void *data);
317 void UnsubscribeCallbackCompletedCB(napi_env env, napi_status status, void *data);
318 void VerifyCredCompleteCB(napi_env env, napi_status status, void *data);
319 void ProcessOnResultCallback(
320     napi_env env, JSAuthCallback &callback, int32_t resultCode, const AAFwk::WantParams &result);
321 bool GetAbilityName(napi_env env, std::string &abilityName);
322 }  // namespace AccountJsKit
323 }  // namespace OHOS
324 
325 #endif  // OS_ACCOUNT_INTERFACES_KITS_NAPI_APPACCOUNT_INCLUDE_NAPI_APP_ACCOUNT_COMMON_H
326