1 /* 2 * Copyright (c) 2021-2025 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 { CommonAsyncContextAppAccountAsyncContext69 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 std::shared_ptr<NapiCallbackRef> onResult = nullptr; 85 std::shared_ptr<NapiCallbackRef> onRequestRedirected = nullptr; 86 std::shared_ptr<NapiCallbackRef> onRequestContinued = nullptr; 87 }; 88 89 struct OAuthAsyncContext : public CommonAsyncContext { CommonAsyncContextOAuthAsyncContext90 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 sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr; 103 sptr<IRemoteObject> authenticatorCb = nullptr; 104 }; 105 106 struct VerifyCredentialContext : public CommonAsyncContext { CommonAsyncContextVerifyCredentialContext107 VerifyCredentialContext(napi_env env, bool throwAble = false) : CommonAsyncContext(env, throwAble) {}; 108 std::string name; 109 std::string owner; 110 VerifyCredentialOptions options; 111 sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr; 112 JSAuthCallback callback; 113 }; 114 115 struct SetPropertiesContext : public CommonAsyncContext { CommonAsyncContextSetPropertiesContext116 SetPropertiesContext(napi_env env, bool throwAble = false) : CommonAsyncContext(env, throwAble) {}; 117 std::string owner; 118 SetPropertiesOptions options; 119 sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr; 120 JSAuthCallback callback; 121 }; 122 123 // when new PropertyType is added, new error message need to be added in ErrMsgList. 124 typedef enum PropertyType { 125 NAME = 0, 126 OWNER, 127 AUTH_TYPE, 128 BUNDLE_NAME, 129 SESSION_ID, 130 IS_VISIBLE, 131 TOKEN, 132 EXTRA_INFO, 133 CREDENTIAL_TYPE, 134 CREDENTIAL, 135 KEY, 136 VALUE, 137 IS_ACCESSIBLE, 138 IS_ENABLE, 139 } PropertyType; 140 141 struct SelectAccountsContext : public NapiAsyncContext { SelectAccountsContextSelectAccountsContext142 SelectAccountsContext(napi_env napiEnv) : NapiAsyncContext(napiEnv) {}; 143 SelectAccountsOptions options; 144 std::vector<AppAccountInfo> appAccountInfos; 145 sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr; 146 }; 147 148 struct CheckAccountLabelsContext : public NapiAsyncContext { CheckAccountLabelsContextCheckAccountLabelsContext149 CheckAccountLabelsContext(napi_env napiEnv) : NapiAsyncContext(napiEnv) {}; 150 std::string name; 151 std::string owner; 152 std::vector<std::string> labels; 153 sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr; 154 }; 155 156 struct GetAccountsAsyncContext : public CommonAsyncContext { GetAccountsAsyncContextGetAccountsAsyncContext157 GetAccountsAsyncContext(napi_env napiEnv, bool isThrowable) : CommonAsyncContext(napiEnv, isThrowable) {}; 158 std::string owner; 159 std::vector<AppAccountInfo> appAccounts; 160 }; 161 162 struct CreateAccountContext : public CommonAsyncContext { CreateAccountContextCreateAccountContext163 explicit CreateAccountContext(napi_env napiEnv) : CommonAsyncContext(napiEnv) {}; 164 std::string name; 165 CreateAccountOptions options; 166 sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr; 167 }; 168 169 struct CreateAccountImplicitlyContext : public CommonAsyncContext { CreateAccountImplicitlyContextCreateAccountImplicitlyContext170 explicit CreateAccountImplicitlyContext(napi_env napiEnv) : CommonAsyncContext(napiEnv) {}; 171 std::string owner; 172 CreateAccountImplicitlyOptions options; 173 JSAuthCallback callback; 174 sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr; 175 }; 176 177 struct SubscriberAccountsWorker : public CommonAsyncContext { SubscriberAccountsWorkerSubscriberAccountsWorker178 explicit SubscriberAccountsWorker(napi_env napiEnv) : CommonAsyncContext(napiEnv) {}; 179 napi_ref ref = nullptr; 180 std::vector<AppAccountInfo> accounts; 181 int code = 0; 182 SubscriberPtr *subscriber = nullptr; 183 }; 184 185 struct AsyncContextForSubscribe : public CommonAsyncContext { AsyncContextForSubscribeAsyncContextForSubscribe186 explicit AsyncContextForSubscribe(napi_env napiEnv) : CommonAsyncContext(napiEnv) {}; 187 std::string type; 188 std::vector<std::string> owners; 189 AppAccountManager *appAccountManager = nullptr; 190 std::shared_ptr<SubscriberPtr> subscriber = nullptr; 191 }; 192 193 struct AsyncContextForUnsubscribe : public CommonAsyncContext { AsyncContextForUnsubscribeAsyncContextForUnsubscribe194 explicit AsyncContextForUnsubscribe(napi_env napiEnv) : CommonAsyncContext(napiEnv) {}; 195 std::string type; 196 std::vector<std::shared_ptr<SubscriberPtr>> subscribers = {nullptr}; 197 AppAccountManager *appAccountManager = nullptr; 198 size_t argc = 0; 199 }; 200 201 struct AuthenticatorCallbackParam : public NapiAsyncContext { AuthenticatorCallbackParamAuthenticatorCallbackParam202 explicit AuthenticatorCallbackParam(JSAuthCallback &authCallback) 203 : NapiAsyncContext(), authCallback(authCallback) {}; AuthenticatorCallbackParamAuthenticatorCallbackParam204 explicit AuthenticatorCallbackParam(std::shared_ptr<NapiCallbackRef> callbackRef) 205 : NapiAsyncContext(callbackRef) {}; 206 int32_t resultCode = -1; 207 AAFwk::Want result; 208 AAFwk::Want request; 209 JSAuthCallback authCallback; 210 }; 211 212 class AuthenticatorAsyncCallback : public AppAccountAuthenticatorCallbackStub { 213 public: 214 AuthenticatorAsyncCallback(napi_env env, std::shared_ptr<NapiCallbackRef> callbackRef, napi_deferred deferred, 215 std::function<std::function<void()>(const std::shared_ptr<AuthenticatorCallbackParam> &)> workCb); 216 ~AuthenticatorAsyncCallback(); 217 218 ErrCode OnResult(int32_t resultCode, const AAFwk::Want &result) override; 219 ErrCode OnRequestRedirected(const AAFwk::Want &request) override; 220 ErrCode OnRequestContinued() override; 221 ErrCode CallbackEnter([[maybe_unused]] uint32_t code) override; 222 ErrCode CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result) override; 223 224 private: 225 std::mutex mutex_; 226 bool isDone = false; 227 napi_env env_ = nullptr; 228 std::shared_ptr<NapiCallbackRef> callbackRef_ = nullptr; 229 napi_deferred deferred_ = nullptr; 230 std::function<std::function<void()>(const std::shared_ptr<AuthenticatorCallbackParam> &)> workCb_; 231 }; 232 233 class AppAccountManagerCallback : public AppAccountAuthenticatorCallbackStub { 234 public: 235 explicit AppAccountManagerCallback(napi_env env, JSAuthCallback callback); 236 ~AppAccountManagerCallback(); 237 238 ErrCode OnResult(int32_t resultCode, const AAFwk::Want &result) override; 239 ErrCode OnRequestRedirected(const AAFwk::Want &request) override; 240 ErrCode OnRequestContinued() override; 241 ErrCode CallbackEnter([[maybe_unused]] uint32_t code) override; 242 ErrCode CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result) override; 243 244 private: 245 std::mutex mutex_; 246 bool isDone = false; 247 napi_env env_ = nullptr; 248 JSAuthCallback callback_; 249 }; 250 251 napi_value NapiGetNull(napi_env env); 252 253 std::string GetNamedProperty(napi_env env, napi_value obj); 254 255 void SetNamedProperty(napi_env env, napi_value dstObj, const char *objName, const char *propName); 256 257 void SetNamedProperty(napi_env env, napi_value dstObj, const int32_t objValue, const char *propName); 258 259 napi_value GetErrorCodeValue(napi_env env, int errCode); 260 261 bool GetArrayLength(napi_env env, napi_value value, uint32_t &length); 262 263 std::function<void()> CheckAccountLabelsOnResultWork(const std::shared_ptr<AuthenticatorCallbackParam> ¶m); 264 265 std::function<void()> SelectAccountsOnResultWork(const std::shared_ptr<AuthenticatorCallbackParam> ¶m); 266 267 void GetAppAccountInfoForResult(napi_env env, const std::vector<AppAccountInfo> &info, napi_value &result); 268 269 void GetAuthenticatorInfoForResult(napi_env env, const AuthenticatorInfo &info, napi_value &result); 270 271 void GetOAuthTokenInfoForResult(napi_env env, const std::vector<OAuthTokenInfo> &info, napi_value result); 272 273 void GetOAuthListForResult(napi_env env, const std::set<std::string> &info, napi_value result); 274 275 void GetAuthenticatorCallbackForResult(napi_env env, sptr<IRemoteObject> callback, napi_value *result); 276 277 bool ParseContextWithExInfo(napi_env env, napi_callback_info cbInfo, AppAccountAsyncContext *asyncContext); 278 279 bool ParseContextForAuth(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext); 280 281 void ParseContextForAuthenticate(napi_env env, napi_callback_info cbInfo, OAuthAsyncContext *asyncContext, size_t argc); 282 283 bool ParseContextForOAuth(napi_env env, napi_callback_info cbInfo, 284 OAuthAsyncContext *asyncContext, const std::vector<PropertyType> &propertyList, napi_value *result); 285 286 bool ParseContextForAppAccount(napi_env env, napi_callback_info cbInfo, 287 AppAccountAsyncContext *asyncContext, const std::vector<PropertyType> &propertyList, napi_value *result); 288 289 bool ParseContextCBArray(napi_env env, napi_callback_info cbInfo, GetAccountsAsyncContext *asyncContext); 290 291 bool ParseContextWithStrCBArray(napi_env env, napi_callback_info cbInfo, GetAccountsAsyncContext *asyncContext); 292 293 bool ParseContextForCreateAccount(napi_env env, napi_callback_info cbInfo, CreateAccountContext *context); 294 295 bool ParseContextForCreateAccountImplicitly( 296 napi_env env, napi_callback_info cbInfo, CreateAccountImplicitlyContext *context); 297 298 bool ParseParametersBySubscribe(const napi_env &env, napi_callback_info cbInfo, AsyncContextForSubscribe *context); 299 300 bool ParseParametersByUnsubscribe( 301 const napi_env &env, napi_callback_info cbInfo, AsyncContextForUnsubscribe *context); 302 303 napi_value GetSubscriberByUnsubscribe(const napi_env &env, std::vector<std::shared_ptr<SubscriberPtr>> &subscriber, 304 AsyncContextForUnsubscribe *asyncContextForOff, bool &isFind); 305 306 bool ParseStringVector(napi_env env, napi_value value, std::vector<std::string> &strVec); 307 bool ParseAccountVector(napi_env env, napi_value value, std::vector<std::pair<std::string, std::string>> &accountVec); 308 bool ParseVerifyCredentialOptions(napi_env env, napi_value object, VerifyCredentialOptions &options); 309 bool ParseSelectAccountsOptions(napi_env env, napi_value object, SelectAccountsOptions &options); 310 bool ParseSetPropertiesOptions(napi_env env, napi_value object, SetPropertiesOptions &options); 311 bool ParseCreateAccountOptions(napi_env env, napi_value object, CreateAccountOptions &options); 312 bool GetNamedFunction(napi_env env, napi_value object, const std::string &name, napi_ref &funcRef); 313 bool ParseJSAuthCallback(napi_env env, napi_value object, JSAuthCallback &callback); 314 bool ParseContextForVerifyCredential(napi_env env, napi_callback_info info, VerifyCredentialContext *context); 315 bool ParseContextForSetProperties(napi_env env, napi_callback_info info, SetPropertiesContext *context); 316 bool ParseContextForSelectAccount(napi_env env, napi_callback_info info, SelectAccountsContext *context); 317 bool ParseContextForCheckAccountLabels(napi_env env, napi_callback_info info, CheckAccountLabelsContext *context); 318 319 void UnsubscribeExecuteCB(napi_env env, void *data); 320 void UnsubscribeCallbackCompletedCB(napi_env env, napi_status status, void *data); 321 void VerifyCredCompleteCB(napi_env env, napi_status status, void *data); 322 void ProcessOnResultCallback( 323 napi_env env, JSAuthCallback &callback, int32_t resultCode, const AAFwk::WantParams &result); 324 } // namespace AccountJsKit 325 } // namespace OHOS 326 327 #endif // OS_ACCOUNT_INTERFACES_KITS_NAPI_APPACCOUNT_INCLUDE_NAPI_APP_ACCOUNT_COMMON_H 328