1 /*
2  * Copyright (c) 2022-2023 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_IDM_SERVICE_H
17 #define USER_IDM_SERVICE_H
18 
19 #include "user_idm_stub.h"
20 
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include "system_ability.h"
26 #include "system_ability_definition.h"
27 
28 #include "context.h"
29 #include "context_factory.h"
30 #include "credential_info_interface.h"
31 
32 namespace OHOS {
33 namespace UserIam {
34 namespace UserAuth {
35 class UserIdmService : public SystemAbility, public UserIdmStub, public NoCopyable {
36 public:
37     DECLARE_SYSTEM_ABILITY(UserIdmService);
38     explicit UserIdmService(int32_t systemAbilityId, bool runOnCreate = false);
39     ~UserIdmService() override = default;
40     int Dump(int fd, const std::vector<std::u16string> &args) override;
41     int32_t OpenSession(int32_t userId, std::vector<uint8_t> &challenge) override;
42     void CloseSession(int32_t userId) override;
43     int32_t GetCredentialInfo(int32_t userId, AuthType authType,
44         const sptr<IdmGetCredInfoCallbackInterface> &callback) override;
45     int32_t GetSecInfo(int32_t userId, const sptr<IdmGetSecureUserInfoCallbackInterface> &callback) override;
46     void AddCredential(int32_t userId, const CredentialPara &credPara,
47         const sptr<IdmCallbackInterface> &callback, bool isUpdate) override;
48     void UpdateCredential(int32_t userId, const CredentialPara &credPara,
49         const sptr<IdmCallbackInterface> &callback) override;
50     int32_t Cancel(int32_t userId) override;
51     int32_t EnforceDelUser(int32_t userId, const sptr<IdmCallbackInterface> &callback) override;
52     void DelUser(int32_t userId, const std::vector<uint8_t> authToken,
53         const sptr<IdmCallbackInterface> &callback) override;
54     void DelCredential(int32_t userId, uint64_t credentialId, const std::vector<uint8_t> &authToken,
55         const sptr<IdmCallbackInterface> &callback) override;
56     void ClearRedundancyCredential(const sptr<IdmCallbackInterface> &callback) override;
57 
58 protected:
59     void OnStart() override;
60     void OnStop() override;
61 
62 private:
63     bool CheckEnrollPermissionAndEnableStatus(
64         const std::shared_ptr<ContextCallback> &contextCallback, AuthType authType);
65     int32_t CancelCurrentEnroll();
66     void CancelCurrentEnrollIfExist();
67     int32_t GetSecInfoInner(int32_t userId, SecUserInfo &secUserInfo);
68     int32_t GetCredentialInfoInner(int32_t userId, AuthType authType, std::vector<CredentialInfo> &credInfoList);
69     int32_t EnforceDelUserInner(int32_t userId, std::shared_ptr<ContextCallback> callbackForTrace,
70         std::string changeReasonTrace);
71     void ClearRedundancyCredentialInner();
72     void SetAuthTypeTrace(const std::vector<std::shared_ptr<CredentialInfoInterface>> &credInfos,
73         const std::shared_ptr<ContextCallback> &contextCallback);
74     void StartEnroll(Enrollment::EnrollmentPara &para,
75         const std::shared_ptr<ContextCallback> &contextCallback, Attributes &extraInfo);
76     void PublishCommonEvent(int32_t userId, uint64_t credentialId, AuthType authType);
77     std::mutex mutex_;
78 };
79 } // namespace UserAuth
80 } // namespace UserIam
81 } // namespace OHOS
82 #endif // USER_IDM_SERVICE_H