• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SERVICE_H
17 #define USER_AUTH_SERVICE_H
18 
19 #include "user_auth_stub.h"
20 
21 #include <string>
22 #include <system_ability.h>
23 #include <system_ability_definition.h>
24 
25 #include "context_callback.h"
26 #include "context_factory.h"
27 #include "context_pool.h"
28 #include "resource_node_pool.h"
29 #include "user_idm_database.h"
30 #include "attributes.h"
31 
32 namespace OHOS {
33 namespace UserIam {
34 namespace UserAuth {
35 class UserAuthService : public SystemAbility, public UserAuthStub, public NoCopyable {
36 public:
37     DECLARE_SYSTEM_ABILITY(UserAuthService);
38     static std::shared_ptr<UserAuthService> GetInstance();
39 
40     UserAuthService();
41     ~UserAuthService() override = default;
42     int32_t GetAvailableStatus(int32_t apiVersion, int32_t userId, AuthType authType,
43         AuthTrustLevel authTrustLevel) override;
44     int32_t GetAvailableStatus(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel) override;
45     void GetProperty(int32_t userId, AuthType authType,
46         const std::vector<Attributes::AttributeKey> &keys,
47         sptr<GetExecutorPropertyCallbackInterface> &callback) override;
48     void GetPropertyById(uint64_t credentialId, const std::vector<Attributes::AttributeKey> &keys,
49         sptr<GetExecutorPropertyCallbackInterface> &callback) override;
50     void SetProperty(int32_t userId, AuthType authType, const Attributes &attributes,
51         sptr<SetExecutorPropertyCallbackInterface> &callback) override;
52     uint64_t AuthUser(AuthParamInner &param, std::optional<RemoteAuthParam> &remoteAuthParam,
53         sptr<UserAuthCallbackInterface> &callback) override;
54     uint64_t Auth(int32_t apiVersion, const std::vector<uint8_t> &challenge, AuthType authType,
55         AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback) override;
56     uint64_t AuthWidget(int32_t apiVersion, const AuthParamInner &authParam, const WidgetParamInner &widgetParam,
57         sptr<UserAuthCallbackInterface> &callback, sptr<ModalCallbackInterface> &modalCallback) override;
58     uint64_t Identify(const std::vector<uint8_t> &challenge, AuthType authType,
59         sptr<UserAuthCallbackInterface> &callback) override;
60     int32_t CancelAuthOrIdentify(uint64_t contextId, int32_t cancelReason) override;
61     int32_t GetVersion(int32_t &version) override;
62     int32_t Notice(NoticeType noticeType, const std::string &eventData) override;
63     int32_t RegisterWidgetCallback(int32_t version, sptr<WidgetCallbackInterface> &callback) override;
64     int32_t GetEnrolledState(int32_t apiVersion, AuthType authType, EnrolledState &enrolledState) override;
65     int32_t RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
66         const sptr<AuthEventListenerInterface> &listener) override;
67     int32_t UnRegistUserAuthSuccessEventListener(const sptr<AuthEventListenerInterface> &listener) override;
68     int32_t SetGlobalConfigParam(const GlobalConfigParam &param) override;
69     int32_t PrepareRemoteAuth(const std::string &networkId, sptr<UserAuthCallbackInterface> &callback) override;
70     void VerifyAuthToken(const std::vector<uint8_t> &tokenIn, uint64_t allowableDuration,
71         const sptr<VerifyTokenCallbackInterface> &callback) override;
72 
73 protected:
74     void OnStart() override;
75     void OnStop() override;
76 
77 private:
78     std::shared_ptr<ContextCallback> GetAuthContextCallback(int32_t apiVersion,
79         const std::vector<uint8_t> &challenge, AuthType authType, AuthTrustLevel authTrustLevel,
80         sptr<UserAuthCallbackInterface> &callback);
81     std::shared_ptr<ContextCallback> GetAuthContextCallback(int32_t apiVersion, const AuthParamInner &authParam,
82         const WidgetParamInner &widgetParam, sptr<UserAuthCallbackInterface> &callback);
83     bool CheckAuthTrustLevel(AuthTrustLevel authTrustLevel);
84     bool CheckSingeFaceOrFinger(const std::vector<AuthType> &authType);
85     bool CheckPrivatePinEnroll(const std::vector<AuthType> &authType, std::vector<AuthType> &validType);
86     int32_t CheckAuthWidgetType(const std::vector<AuthType> &authType);
87     int32_t CheckCallerPermissionForUserId(const AuthParamInner &authParam);
88     int32_t CheckAuthPermissionAndParam(const AuthParamInner &authParam, const WidgetParamInner &widgetParam,
89         bool isBackgroundApplication);
90     uint64_t StartWidgetContext(const std::shared_ptr<ContextCallback> &contextCallback,
91         const AuthParamInner &authParam, const WidgetParamInner &widgetParam, std::vector<AuthType> &validType,
92         ContextFactory::AuthWidgetContextPara &para);
93     uint64_t StartAuthContext(int32_t apiVersion, Authentication::AuthenticationPara para,
94         const std::shared_ptr<ContextCallback> &contextCallback, bool needSubscribeAppState);
95     uint64_t AuthRemoteUser(AuthParamInner &authParam, Authentication::AuthenticationPara &para,
96         RemoteAuthParam &remoteAuthParam, const std::shared_ptr<ContextCallback> &contextCallback,
97         ResultCode &failReason);
98     uint64_t StartRemoteAuthInvokerContext(AuthParamInner authParam,
99         RemoteAuthInvokerContextParam &param, const std::shared_ptr<ContextCallback> &contextCallback);
100     bool Insert2ContextPool(const std::shared_ptr<Context> &context);
101     bool CheckCallerIsSystemApp();
102     int32_t CheckAuthPermissionAndParam(int32_t authType, const int32_t &callerType, const std::string &callerName,
103         AuthTrustLevel authTrustLevel);
104     bool CheckAuthPermissionAndParam(AuthType authType, AuthTrustLevel authTrustLevel,
105         const std::shared_ptr<ContextCallback> &contextCallback, Attributes &extraInfo);
106     bool CheckAuthTypeIsValid(std::vector<AuthType> authType);
107     int32_t CheckWindowMode(const WidgetParamInner &widgetParam);
108     int32_t CheckValidSolution(int32_t userId, const AuthParamInner &authParam, const WidgetParamInner &widgetParam,
109         std::vector<AuthType> &validType);
110     int32_t GetCallerInfo(bool isUserIdSpecified, int32_t userId, ContextFactory::AuthWidgetContextPara &para,
111         std::shared_ptr<ContextCallback> &contextCallback);
112     int32_t CheckCallerPermissionForPrivatePin(const AuthParamInner &authParam);
113     void FillGetPropertyKeys(AuthType authType, const std::vector<Attributes::AttributeKey> &keys,
114         std::vector<uint32_t> &uint32Keys);
115     void FillGetPropertyValue(AuthType authType, const std::vector<Attributes::AttributeKey> &keys, Attributes &values);
116     bool CompleteRemoteAuthParam(RemoteAuthParam &remoteAuthParam, const std::string &localNetworkId);
117     int32_t PrepareRemoteAuthInner(const std::string &networkId, sptr<UserAuthCallbackInterface> &callback);
118     int32_t DoPrepareRemoteAuth(const std::string &networkId);
119     int32_t GetAvailableStatusInner(int32_t apiVersion, int32_t userId, AuthType authType,
120         AuthTrustLevel authTrustLevel);
121     bool GetAuthTokenAttr(const HdiUserAuthTokenPlain &tokenPlain, const std::vector<uint8_t> &rootSecret,
122         Attributes &extraInfo);
123     std::shared_ptr<ResourceNode> GetResourseNode(AuthType authType);
124     void ProcessPinExpired(int32_t ret, const AuthParamInner &authParam, std::vector<AuthType> &validType,
125         ContextFactory::AuthWidgetContextPara &para);
126     void ProcessWidgetSessionExclusive();
127     void GetPropertyInner(AuthType authType, const std::vector<Attributes::AttributeKey> &keys,
128         sptr<GetExecutorPropertyCallbackInterface> &callback, std::vector<uint64_t> &templateIds);
129     static std::mutex mutex_;
130     static std::shared_ptr<UserAuthService> instance_;
131 };
132 } // namespace UserAuth
133 } // namespace UserIam
134 } // namespace OHOS
135 #endif // USER_AUTH_SERVICE_H