• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 const std::int32_t STR_MAX_SIZE = 256;
35 constexpr std::int32_t ARGS_SIZE_ONE = 1;
36 constexpr std::int32_t ARGS_SIZE_TWO = 2;
37 constexpr std::int32_t ARGS_SIZE_THREE = 3;
38 constexpr std::int32_t ARGS_SIZE_FOUR = 4;
39 constexpr std::int32_t ARGS_SIZE_FIVE = 5;
40 constexpr std::int32_t ARGS_SIZE_SIX = 6;
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 const std::string APP_ACCOUNT_CLASS_NAME = "AppAccountManager";
49 const std::string TYPE_CHANGE = "change";
50 
51 static const std::int32_t SUBSCRIBE_MAX_PARA = 3;
52 static const std::int32_t UNSUBSCRIBE_MAX_PARA = 2;
53 
54 class AppAccountManagerCallback;
55 struct AsyncContextForSubscribe;
56 extern std::mutex g_lockForAppAccountSubscribers;
57 extern std::map<AppAccountManager *, std::vector<AsyncContextForSubscribe *>> g_AppAccountSubscribers;
58 
59 class SubscriberPtr : public AppAccountSubscriber {
60 public:
61     explicit SubscriberPtr(const AppAccountSubscribeInfo &subscribeInfo);
62     ~SubscriberPtr();
63 
64     void OnAccountsChanged(const std::vector<AppAccountInfo> &accounts) override;
65 
66     void SetEnv(const napi_env &env);
67     void SetCallbackRef(const napi_ref &ref);
68 
69 private:
70     napi_env env_ = nullptr;
71     napi_ref ref_ = nullptr;
72 };
73 
74 struct AppAccountAsyncContext : public CommonAsyncContext {
75     std::string name;
76     std::string owner;
77     std::string extraInfo;
78     std::string bundleName;
79     std::string credentialType;
80     std::string credential;
81     std::string key;
82     std::string value;
83     std::string subscribeType;
84     std::string unSubscribeType;
85     bool isAccessible = false;
86     bool isEnable = false;
87     bool result = false;
88 };
89 
90 struct JSAuthCallback {
91     napi_ref onResult = nullptr;
92     napi_ref onRequestRedirected = nullptr;
93     napi_ref onRequestContinued = nullptr;
94 };
95 
96 struct OAuthAsyncContext : public CommonAsyncContext {
97     std::string name;
98     std::string owner;
99     std::string sessionId;
100     std::string bundleName;
101     std::string authType;
102     std::string token;
103     std::set<std::string> authList;
104     bool isVisible = false;
105     AAFwk::Want options;
106     AuthenticatorInfo authenticatorInfo;
107     std::vector<OAuthTokenInfo> oauthTokenInfos;
108     JSAuthCallback callback;
109     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
110     sptr<IRemoteObject> authenticatorCb = nullptr;
111 };
112 
113 struct VerifyCredentialContext : public CommonAsyncContext {
114     std::string name;
115     std::string owner;
116     VerifyCredentialOptions options;
117     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
118     JSAuthCallback callback;
119 };
120 
121 struct SetPropertiesContext : public CommonAsyncContext {
122     std::string owner;
123     SetPropertiesOptions options;
124     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
125     JSAuthCallback callback;
126 };
127 
128 struct SelectAccountsContext : public CommonAsyncContext {
129     SelectAccountsOptions options;
130     std::vector<AppAccountInfo> appAccountInfos;
131     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
132 };
133 
134 struct CheckAccountLabelsContext : public CommonAsyncContext {
135     std::string name;
136     std::string owner;
137     std::vector<std::string> labels;
138     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
139 };
140 
141 struct GetAccountsAsyncContext : public CommonAsyncContext {
142     std::string owner;
143     std::vector<AppAccountInfo> appAccounts;
144 };
145 
146 struct CreateAccountContext : public CommonAsyncContext {
147 public:
CreateAccountContextCreateAccountContext148     explicit CreateAccountContext(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
149     std::string name;
150     CreateAccountOptions options;
151     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
152 };
153 
154 struct CreateAccountImplicitlyContext : public CommonAsyncContext {
CreateAccountImplicitlyContextCreateAccountImplicitlyContext155     explicit CreateAccountImplicitlyContext(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
156     std::string owner;
157     CreateAccountImplicitlyOptions options;
158     JSAuthCallback callback;
159     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
160 };
161 
162 struct SubscriberAccountsWorker : public CommonAsyncContext {
SubscriberAccountsWorkerSubscriberAccountsWorker163     explicit SubscriberAccountsWorker(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
164     napi_ref ref = nullptr;
165     std::vector<AppAccountInfo> accounts;
166     int code = 0;
167     SubscriberPtr *subscriber = nullptr;
168 };
169 
170 struct AsyncContextForSubscribe : public CommonAsyncContext {
AsyncContextForSubscribeAsyncContextForSubscribe171     explicit AsyncContextForSubscribe(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
172     std::string type;
173     std::vector<std::string> owners;
174     AppAccountManager *appAccountManager = nullptr;
175     std::shared_ptr<SubscriberPtr> subscriber = nullptr;
176 };
177 
178 struct AsyncContextForUnsubscribe : public CommonAsyncContext {
AsyncContextForUnsubscribeAsyncContextForUnsubscribe179     explicit AsyncContextForUnsubscribe(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
180     std::string type;
181     std::vector<std::shared_ptr<SubscriberPtr>> subscribers = {nullptr};
182     AppAccountManager *appAccountManager = nullptr;
183     size_t argc = 0;
184 };
185 
186 struct AuthenticatorCallbackParam : public CommonAsyncContext {
AuthenticatorCallbackParamAuthenticatorCallbackParam187     explicit AuthenticatorCallbackParam(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
188     int32_t resultCode;
189     AAFwk::Want result;
190     AAFwk::Want request;
191     JSAuthCallback callback;
192     CommonAsyncContext context;
193 };
194 
195 class AuthenticatorAsyncCallback : public AppAccountAuthenticatorCallbackStub {
196 public:
197     explicit AuthenticatorAsyncCallback(const CommonAsyncContext &context, uv_after_work_cb workCb);
198     ~AuthenticatorAsyncCallback();
199 
200     void OnResult(int32_t resultCode, const AAFwk::Want &result) override;
201     void OnRequestRedirected(AAFwk::Want &request) override;
202     void OnRequestContinued() override;
203 
204 private:
205     std::mutex mutex_;
206     bool isDone = false;
207     CommonAsyncContext context_;
208     uv_after_work_cb workCb_;
209 };
210 
211 class AppAccountManagerCallback : public AppAccountAuthenticatorCallbackStub {
212 public:
213     explicit AppAccountManagerCallback(napi_env env, JSAuthCallback callback);
214     ~AppAccountManagerCallback();
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     napi_env env_ = nullptr;
222     JSAuthCallback callback_;
223 };
224 
225 bool InitAuthenticatorWorkEnv(
226     napi_env env, uv_loop_s **loop, uv_work_t **work, AuthenticatorCallbackParam **param);
227 
228 napi_value NapiGetNull(napi_env env);
229 
230 std::string GetNamedProperty(napi_env env, napi_value obj);
231 
232 void SetNamedProperty(napi_env env, napi_value dstObj, const char *objName, const char *propName);
233 
234 void SetNamedProperty(napi_env env, napi_value dstObj, const int32_t objValue, const char *propName);
235 
236 napi_value GetErrorCodeValue(napi_env env, int errCode);
237 
238 bool GetArrayLength(napi_env env, napi_value value, uint32_t &length);
239 
240 void CheckAccountLabelsOnResultWork(uv_work_t *work, int status);
241 
242 void SelectAccountsOnResultWork(uv_work_t *work, int status);
243 
244 void GetAppAccountInfoForResult(napi_env env, const std::vector<AppAccountInfo> &info, napi_value &result);
245 
246 void GetAuthenticatorInfoForResult(napi_env env, const AuthenticatorInfo &info, napi_value &result);
247 
248 void GetOAuthTokenInfoForResult(napi_env env, const std::vector<OAuthTokenInfo> &info, napi_value result);
249 
250 void GetOAuthListForResult(napi_env env, const std::set<std::string> &info, napi_value result);
251 
252 void GetAuthenticatorCallbackForResult(napi_env env, sptr<IRemoteObject> callback, napi_value *result);
253 
254 bool ParseContextWithExInfo(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext);
255 
256 bool ParseContextForSetExInfo(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext);
257 
258 bool ParseContextForAuth(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext);
259 
260 void ParseContextForAuthenticate(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext, size_t argc);
261 
262 bool ParseContextForDeleteOAuthToken(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext);
263 
264 bool ParseContextForGetOAuthToken(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext);
265 
266 bool ParseContextForSetOAuthTokenVisibility(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext);
267 
268 bool ParseContextForCheckOAuthTokenVisibility(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext);
269 
270 bool ParseContextForGetAuthenticatorInfo(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext);
271 
272 bool ParseContextForGetAllOAuthTokens(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext);
273 
274 bool ParseContextForGetOAuthList(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext);
275 
276 bool ParseContextForGetAuthenticatorCallback(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext);
277 
278 bool ParseContextForSetOAuthToken(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext);
279 
280 bool ParseContextWithBdName(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext);
281 
282 bool ParseContextForSetAppAccess(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext);
283 
284 bool ParseContextWithIsEnable(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext);
285 
286 bool ParseContextWithTwoPara(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext);
287 
288 bool ParseContextToSetCredential(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext);
289 
290 bool ParseContextForAssociatedData(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext);
291 
292 bool ParseContextToGetData(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext);
293 
294 bool ParseContextCBArray(napi_env env, napi_callback_info cbInfo, GetAccountsAsyncContext *asyncContext);
295 
296 bool ParseContextWithCredentialType(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext);
297 
298 bool ParseContextWithStrCBArray(napi_env env, napi_callback_info cbInfo, GetAccountsAsyncContext *asyncContext);
299 
300 bool ParseContextForCreateAccount(napi_env env, napi_callback_info cbInfo, CreateAccountContext *context);
301 
302 bool ParseContextForCreateAccountImplicitly(
303     napi_env env, napi_callback_info cbInfo, CreateAccountImplicitlyContext *context);
304 
305 bool ParseParametersBySubscribe(const napi_env &env, napi_callback_info cbInfo, AsyncContextForSubscribe *context);
306 
307 bool ParseParametersByUnsubscribe(
308     const napi_env &env, napi_callback_info cbInfo, AsyncContextForUnsubscribe *context);
309 
310 napi_value GetSubscriberByUnsubscribe(const napi_env &env, std::vector<std::shared_ptr<SubscriberPtr>> &subscriber,
311     AsyncContextForUnsubscribe *asyncContextForOff, bool &isFind);
312 
313 bool ParseStringVector(napi_env env, napi_value value, std::vector<std::string> &strVec);
314 bool ParseAccountVector(napi_env env, napi_value value, std::vector<std::pair<std::string, std::string>> &accountVec);
315 bool ParseVerifyCredentialOptions(napi_env env, napi_value object, VerifyCredentialOptions &options);
316 bool ParseSelectAccountsOptions(napi_env env, napi_value object, SelectAccountsOptions &options);
317 bool ParseSetPropertiesOptions(napi_env env, napi_value object, SetPropertiesOptions &options);
318 bool ParseCreateAccountOptions(napi_env env, napi_value object, CreateAccountOptions &options);
319 bool GetNamedFunction(napi_env env, napi_value object, const std::string &name, napi_ref &funcRef);
320 bool ParseJSAuthCallback(napi_env env, napi_value object, JSAuthCallback &callback);
321 bool ParseContextForVerifyCredential(napi_env env, napi_callback_info info, VerifyCredentialContext *context);
322 bool ParseContextForSetProperties(napi_env env, napi_callback_info info, SetPropertiesContext *context);
323 bool ParseContextForSelectAccount(napi_env env, napi_callback_info info, SelectAccountsContext *context);
324 bool ParseContextForCheckAccountLabels(napi_env env, napi_callback_info info, CheckAccountLabelsContext *context);
325 
326 void UnsubscribeExecuteCB(napi_env env, void *data);
327 void UnsubscribeCallbackCompletedCB(napi_env env, napi_status status, void *data);
328 void VerifyCredCompleteCB(napi_env env, napi_status status, void *data);
329 void ProcessOnResultCallback(
330     napi_env env, JSAuthCallback &callback, int32_t resultCode, const AAFwk::WantParams &result);
331 bool GetAbilityName(napi_env env, std::string &abilityName);
332 }  // namespace AccountJsKit
333 }  // namespace OHOS
334 
335 #endif  // OS_ACCOUNT_INTERFACES_KITS_NAPI_APPACCOUNT_INCLUDE_NAPI_APP_ACCOUNT_COMMON_H
336