1 /* 2 * Copyright (c) 2022-2024 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 USER_AUTH_CLIENT_IMPL_H 17 #define USER_AUTH_CLIENT_IMPL_H 18 19 #include <mutex> 20 #include <optional> 21 22 #include "nocopyable.h" 23 24 #include "event_listener_callback_service.h" 25 #include "iuser_auth.h" 26 #include "iuser_auth_widget_callback.h" 27 #include "user_auth_client.h" 28 #include "user_auth_types.h" 29 #include "user_auth_common_defines.h" 30 31 namespace OHOS { 32 namespace UserIam { 33 namespace UserAuth { 34 class UserAuthClientImpl final : public UserAuthClient, NoCopyable { 35 public: 36 static UserAuthClientImpl& Instance(); 37 int32_t GetAvailableStatus(AuthType authType, AuthTrustLevel authTrustLevel); 38 int32_t GetNorthAvailableStatus(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel); 39 int32_t GetAvailableStatus(int32_t userId, AuthType authType, AuthTrustLevel authTrustLevel) override; 40 void GetProperty(int32_t userId, const GetPropertyRequest &request, 41 const std::shared_ptr<GetPropCallback> &callback) override; 42 void GetPropertyById(uint64_t credentialId, const std::vector<Attributes::AttributeKey> &keys, 43 const std::shared_ptr<GetPropCallback> &callback) override; 44 void SetProperty(int32_t userId, const SetPropertyRequest &request, 45 const std::shared_ptr<SetPropCallback> &callback) override; 46 uint64_t BeginAuthentication(const AuthParam &authParam, 47 const std::shared_ptr<AuthenticationCallback> &callback) override; 48 uint64_t BeginNorthAuthentication(int32_t apiVersion, const std::vector<uint8_t> &challenge, AuthType authType, 49 AuthTrustLevel atl, const std::shared_ptr<AuthenticationCallback> &callback); 50 int32_t CancelAuthentication(uint64_t contextId) override; 51 uint64_t BeginIdentification(const std::vector<uint8_t> &challenge, AuthType authType, 52 const std::shared_ptr<IdentificationCallback> &callback) override; 53 int32_t CancelIdentification(uint64_t contextId) override; 54 int32_t GetVersion(int32_t &version); 55 uint64_t BeginWidgetAuth(const WidgetAuthParam &authParam, const WidgetParam &widgetParam, 56 const std::shared_ptr<AuthenticationCallback> &callback) override; 57 uint64_t BeginWidgetAuth(int32_t apiVersion, const WidgetAuthParam &authParam, const WidgetParam &widgetParam, 58 const std::shared_ptr<AuthenticationCallback> &callback); 59 int32_t SetWidgetCallback(int32_t version, const std::shared_ptr<IUserAuthWidgetCallback> &callback); 60 int32_t Notice(NoticeType noticeType, const std::string &eventData); 61 int32_t GetEnrolledState(int32_t apiVersion, AuthType authType, EnrolledState &enrolledState); 62 int32_t RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authTypes, 63 const std::shared_ptr<AuthSuccessEventListener> &listener) override; 64 int32_t UnRegistUserAuthSuccessEventListener( 65 const std::shared_ptr<AuthSuccessEventListener> &listener) override; 66 int32_t SetGlobalConfigParam(const GlobalConfigParam ¶m) override; 67 int32_t PrepareRemoteAuth(const std::string &networkId, 68 const std::shared_ptr<PrepareRemoteAuthCallback> &callback) override; 69 int32_t QueryReusableAuthResult(const WidgetAuthParam &authParam, std::vector<uint8_t> &token) override; 70 void CleanUpResource(); 71 72 private: 73 ResultCode SetPropertyInner(int32_t userId, const SetPropertyRequest &request, 74 const std::shared_ptr<SetPropCallback> &callback); 75 uint64_t BeginWidgetAuthInner(int32_t apiVersion, const AuthParamInner &authParam, 76 const WidgetParamInner &widgetParam, const std::shared_ptr<AuthenticationCallback> &callback); 77 void InitIpcRemoteAuthParam(const std::optional<RemoteAuthParam> &remoteAuthParam, 78 IpcRemoteAuthParam &ipcRemoteAuthParam); 79 void InitIpcGlobalConfigParam(const GlobalConfigParam &globalConfigParam, 80 IpcGlobalConfigParam &ipcGlobalConfigParam); 81 void InitIpcAuthParam(const AuthParamInner &authParam, IpcAuthParamInner &ipcAuthParam); 82 void InitIpcWidgetParam(const WidgetParamInner &widgetParam, IpcWidgetParamInner &ipcWidgetParam); 83 84 friend class UserAuthClient; 85 UserAuthClientImpl() = default; 86 ~UserAuthClientImpl() override; 87 class UserAuthImplDeathRecipient : public IRemoteObject::DeathRecipient, public NoCopyable { 88 public: 89 UserAuthImplDeathRecipient() = default; 90 ~UserAuthImplDeathRecipient() override = default; 91 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 92 }; 93 void ResetProxy(const wptr<IRemoteObject> &remote); 94 sptr<IUserAuth> GetProxy(); 95 sptr<IUserAuth> proxy_ {nullptr}; 96 sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr}; 97 constexpr static int32_t MINIMUM_VERSION {0}; 98 std::mutex mutex_; 99 }; 100 } // namespace UserAuth 101 } // namespace UserIam 102 } // namespace OHOS 103 #endif // USER_AUTH_CLIENT_IMPL_H