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 "user_auth_client.h"
25 #include "user_auth_interface.h"
26 #include "iuser_auth_widget_callback.h"
27 
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
31 class UserAuthClientImpl final : public UserAuthClient, NoCopyable {
32 public:
33     static UserAuthClientImpl& Instance();
34     int32_t GetAvailableStatus(AuthType authType, AuthTrustLevel authTrustLevel);
35     int32_t GetNorthAvailableStatus(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel);
36     int32_t GetAvailableStatus(int32_t userId, AuthType authType, AuthTrustLevel authTrustLevel) override;
37     void GetProperty(int32_t userId, const GetPropertyRequest &request,
38         const std::shared_ptr<GetPropCallback> &callback) override;
39     void GetPropertyById(uint64_t credentialId, const std::vector<Attributes::AttributeKey> &keys,
40         const std::shared_ptr<GetPropCallback> &callback) override;
41     void SetProperty(int32_t userId, const SetPropertyRequest &request,
42         const std::shared_ptr<SetPropCallback> &callback) override;
43     uint64_t BeginAuthentication(const AuthParam &authParam,
44         const std::shared_ptr<AuthenticationCallback> &callback) override;
45     uint64_t BeginNorthAuthentication(int32_t apiVersion, const std::vector<uint8_t> &challenge, AuthType authType,
46         AuthTrustLevel atl, const std::shared_ptr<AuthenticationCallback> &callback);
47     int32_t CancelAuthentication(uint64_t contextId) override;
48     uint64_t BeginIdentification(const std::vector<uint8_t> &challenge, AuthType authType,
49         const std::shared_ptr<IdentificationCallback> &callback) override;
50     int32_t CancelIdentification(uint64_t contextId) override;
51     int32_t GetVersion(int32_t &version);
52     uint64_t BeginWidgetAuth(const WidgetAuthParam &authParam, const WidgetParam &widgetParam,
53         const std::shared_ptr<AuthenticationCallback> &callback) override;
54     uint64_t BeginWidgetAuth(int32_t apiVersion, const WidgetAuthParam &authParam, const WidgetParam &widgetParam,
55         const std::shared_ptr<AuthenticationCallback> &callback);
56     int32_t SetWidgetCallback(int32_t version, const std::shared_ptr<IUserAuthWidgetCallback> &callback);
57     int32_t Notice(NoticeType noticeType, const std::string &eventData);
58     int32_t GetEnrolledState(int32_t apiVersion, AuthType authType, EnrolledState &enrolledState);
59     int32_t RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
60         const sptr<AuthEventListenerInterface> &listener) override;
61     int32_t UnRegistUserAuthSuccessEventListener(
62         const sptr<AuthEventListenerInterface> &listener) override;
63     int32_t SetGlobalConfigParam(const GlobalConfigParam &param) override;
64     int32_t PrepareRemoteAuth(const std::string &networkId,
65         const std::shared_ptr<PrepareRemoteAuthCallback> &callback) override;
66 
67 private:
68     ResultCode SetPropertyInner(int32_t userId, const SetPropertyRequest &request,
69         const std::shared_ptr<SetPropCallback> &callback);
70     uint64_t BeginWidgetAuthInner(int32_t apiVersion, const AuthParamInner &authParam,
71         const WidgetParam &widgetParam, const std::shared_ptr<AuthenticationCallback> &callback);
72 
73     friend class UserAuthClient;
74     UserAuthClientImpl() = default;
75     ~UserAuthClientImpl() override = default;
76     class UserAuthImplDeathRecipient : public IRemoteObject::DeathRecipient, public NoCopyable {
77     public:
78         UserAuthImplDeathRecipient() = default;
79         ~UserAuthImplDeathRecipient() override = default;
80         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
81     };
82     void ResetProxy(const wptr<IRemoteObject> &remote);
83     sptr<UserAuthInterface> GetProxy();
84     sptr<UserAuthInterface> proxy_ {nullptr};
85     sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr};
86     constexpr static int32_t MINIMUM_VERSION {0};
87     std::mutex mutex_;
88 };
89 } // namespace UserAuth
90 } // namespace UserIam
91 } // namespace OHOS
92 #endif // USER_AUTH_CLIENT_IMPL_H