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 OHOS_DM_CREDENTIAL_MANAGER_H
17 #define OHOS_DM_CREDENTIAL_MANAGER_H
18 
19 #include "device_auth.h"
20 #include "idevice_manager_service_listener.h"
21 #include "dm_timer.h"
22 #include "hichain_connector.h"
23 namespace OHOS {
24 namespace DistributedHardware {
25 typedef struct {
26     int32_t credentialType;
27     std::string credentialId;
28     std::string serverPk;
29     std::string pkInfoSignature;
30     std::string pkInfo;
31     std::string authCode;
32     std::string peerDeviceId;
33 } CredentialData;
34 class DmCredentialManager final : public IDmGroupResCallback,
35                                   public std::enable_shared_from_this<DmCredentialManager> {
36 public:
37     DmCredentialManager(std::shared_ptr<HiChainConnector> hiChainConnector,
38                         std::shared_ptr<IDeviceManagerServiceListener> listener);
39     ~DmCredentialManager();
40 
41     /**
42      * @tc.name: HiChainConnector::RegisterCredentialCallback
43      * @tc.desc: Register Credential Callback Info of the DmCredential Manager
44      * @tc.type: FUNC
45      */
46     int32_t RegisterCredentialCallback(const std::string &pkgName);
47 
48     /**
49      * @tc.name: HiChainConnector::UnRegisterCredentialCallback
50      * @tc.desc: UnRegister Credential Callback Info of the DmCredential Manager
51      * @tc.type: FUNC
52      */
53     int32_t UnRegisterCredentialCallback(const std::string &pkgName);
54 
55     /**
56      * @tc.name: HiChainConnector::RequestCredential
57      * @tc.desc: Request Credential Info of the DmCredential Manager
58      * @tc.type: FUNC
59      */
60     int32_t RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr);
61 
62     /**
63      * @tc.name: HiChainConnector::ImportCredential
64      * @tc.desc: Import Credential Info of the DmCredential Manager
65      * @tc.type: FUNC
66      */
67     int32_t ImportCredential(const std::string &pkgName, const std::string &credentialInfo);
68 
69     /**
70      * @tc.name: HiChainConnector::ImportLocalCredential
71      * @tc.desc: Import Nonsymmetry Credential Info of the DmCredential Manager
72      * @tc.type: FUNC
73      */
74     int32_t ImportLocalCredential(const std::string &credentialInfo);
75 
76     /**
77      * @tc.name: DmCredentialManager::ImportRemoteCredential
78      * @tc.desc: Import Symmetry Credential Info of the DmCredential Manager
79      * @tc.type: FUNC
80      */
81     int32_t ImportRemoteCredential(const std::string &credentialInfo);
82 
83     /**
84      * @tc.name: DmCredentialManager::DeleteRemoteCredential
85      * @tc.desc: delete Symmetry Credential Info of the DmCredential Manager
86      * @tc.type: FUNC
87      */
88     int32_t DeleteRemoteCredential(const std::string &deleteInfo);
89 
90     /**
91      * @tc.name: HiChainConnector::DeleteCredential
92      * @tc.desc: Delete Credential Info of the DmCredential Manager
93      * @tc.type: FUNC
94      */
95     int32_t DeleteCredential(const std::string &pkgName, const std::string &deleteInfo);
96 
97     /**
98      * @tc.name: HiChainConnector::OnCredentialResult
99      * @tc.desc: Credential Result of the DmCredential Manager
100      * @tc.type: FUNC
101      */
102     void OnGroupResult(int64_t requestId, int32_t action, const std::string &resultInfo);
103 
104     int32_t ImportRemoteCredentialExt(const std::string &credentialInfo);
105     void OnGroupResultExt(int32_t action, const std::string &resultInfo);
106 
107 private:
108     std::shared_ptr<HiChainConnector> hiChainConnector_;
109     std::shared_ptr<IDeviceManagerServiceListener> listener_;
110     std::vector<std::string> credentialVec_;
111     int64_t requestId_ = 0;
112     std::mutex locks_;
113     std::string pkgName_;
114 private:
115     int32_t GetCredentialData(const std::string &credentialInfo, const CredentialData &inputCreData,
116         nlohmann::json &jsonOutObj);
117     int32_t GetAddDeviceList(const nlohmann::json &jsonObject, nlohmann::json &jsonDeviceList);
118 };
119 } // namespace DistributedHardware
120 } // namespace OHOS
121 #endif // OHOS_DM_CREDENTIAL_MANAGER_H