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 #ifndef OS_ACCOUNT_INTERFACES_KITS_NAPI_APPACCOUNT_INCLUDE_NAPI_APP_ACCOUNT_AUTHENTICATOR_H 17 #define OS_ACCOUNT_INTERFACES_KITS_NAPI_APPACCOUNT_INCLUDE_NAPI_APP_ACCOUNT_AUTHENTICATOR_H 18 19 #include <mutex> 20 #include <uv.h> 21 22 #include "account_error_no.h" 23 #include "app_account_authenticator_stub.h" 24 #include "iapp_account_authenticator_callback.h" 25 #include "iremote_object.h" 26 #include "napi/native_api.h" 27 #include "napi_account_common.h" 28 #include "napi_app_account_common.h" 29 #include "refbase.h" 30 31 namespace OHOS { 32 namespace AccountJsKit { 33 struct JsAuthenticator { 34 napi_ref addAccountImplicitly = nullptr; 35 napi_ref authenticate = nullptr; 36 napi_ref verifyCredential = nullptr; 37 napi_ref checkAccountLabels = nullptr; 38 napi_ref setProperties = nullptr; 39 napi_ref isAccountRemovable = nullptr; 40 napi_ref createAccountImplicitly = nullptr; 41 napi_ref auth = nullptr; 42 }; 43 44 struct JsAuthenticatorParam : public CommonAsyncContext { 45 JsAuthenticator jsAuthenticator; 46 std::string name; 47 std::string authType; 48 std::string callerBundleName; 49 AAFwk::WantParams options; 50 std::vector<std::string> labels; 51 VerifyCredentialOptions verifyCredOptions; 52 SetPropertiesOptions setPropOptions; 53 CreateAccountImplicitlyOptions createOptions; 54 sptr<IRemoteObject> callback; 55 }; 56 57 class NapiAppAccountAuthenticator : public AccountSA::AppAccountAuthenticatorStub { 58 public: 59 NapiAppAccountAuthenticator(napi_env env, JsAuthenticator &jsAuthenticator); 60 ~NapiAppAccountAuthenticator() override; 61 bool CheckObjectLegality() const override; 62 int GetObjectType() const override; 63 void SetEnv(napi_env env); 64 static napi_value Init(napi_env env, napi_value exports); 65 ErrCode AddAccountImplicitly(const std::string &authType, const std::string &callerBundleName, 66 const AAFwk::WantParams &options, const sptr<IRemoteObject> &callback) override; 67 ErrCode Authenticate( 68 const std::string &name, const std::string &authType, const std::string &callerBundleName, 69 const AAFwk::WantParams &options, const sptr<IRemoteObject> &callback) override; 70 ErrCode CreateAccountImplicitly( 71 const CreateAccountImplicitlyOptions &options, const sptr<IRemoteObject> &callback) override; 72 ErrCode Auth( 73 const std::string &name, const std::string &authType, const AAFwk::WantParams &options, 74 const sptr<IRemoteObject> &callback) override; 75 ErrCode VerifyCredential( 76 const std::string &name, const VerifyCredentialOptions &options, const sptr<IRemoteObject> &callback) override; 77 ErrCode CheckAccountLabels( 78 const std::string &name, const std::vector<std::string> &labels, const sptr<IRemoteObject> &callback) override; 79 ErrCode SetProperties(const SetPropertiesOptions &options, const sptr<IRemoteObject> &callback) override; 80 ErrCode IsAccountRemovable(const std::string &name, const sptr<IRemoteObject> &callback) override; 81 ErrCode InitWorkEnv(uv_loop_s **loop, uv_work_t **work, JsAuthenticatorParam **param); 82 napi_value GetJsRemoteObject(); 83 void SetJsRemoteObject(napi_value remoteObject); 84 85 private: 86 static napi_value JsConstructor(napi_env env, napi_callback_info info); 87 static napi_value GetRemoteObject(napi_env env, napi_callback_info cbInfo); 88 static napi_value CreateAuthenticatorCallback(napi_env env, sptr<IRemoteObject> nativeCallback); 89 static napi_status GetNamedFunction(napi_env env, napi_value value, const std::string &name, napi_ref *result); 90 static void CallJsFunction(napi_env env, napi_ref funcRef, napi_value *argv, size_t argc); 91 static void CreateJsVerifyCredentialOptions(napi_env env, VerifyCredentialOptions &options, napi_value *jsOptions); 92 static void CreateJsSetPropertiesOptions(napi_env env, SetPropertiesOptions &options, napi_value *jsOptions); 93 static void AddAccountImplicitlyWork(uv_work_t *work, int status); 94 static void AuthenticateWork(uv_work_t *work, int status); 95 static void VerifyCredentialWork(uv_work_t *work, int status); 96 static void CheckAccountLabelsWork(uv_work_t *work, int status); 97 static void SetPropertiesWork(uv_work_t *work, int status); 98 static void IsAccountRemovableWork(uv_work_t *work, int status); 99 static void CreateAccountImplicitlyWork(uv_work_t *work, int status); 100 static void AuthWork(uv_work_t *work, int status); 101 102 private: 103 napi_env env_ = nullptr; 104 JsAuthenticator jsAuthenticator_; 105 napi_value remoteObject_ = nullptr; 106 }; 107 } // namespace AccountJsKit 108 } // namespace OHOS 109 110 #endif // OS_ACCOUNT_INTERFACES_KITS_NAPI_APPACCOUNT_INCLUDE_NAPI_APP_ACCOUNT_AUTHENTICATOR_H