1 /*
2  * Copyright (c) 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 OHOS_HICHAIN_AUTH_CONNECTOR_H
17 #define OHOS_HICHAIN_AUTH_CONNECTOR_H
18 
19 #include "device_auth.h"
20 #include "device_auth_defines.h"
21 #include "dm_constants.h"
22 #include "hichain_connector_callback.h"
23 #include "nlohmann/json.hpp"
24 
25 namespace OHOS {
26 namespace DistributedHardware {
27 
28 enum {
29     /** invalid operationCode for initialize */
30     CRED_OP_INVALID = -1,
31     /** operationCode for ProcessCredential to query credential */
32     CRED_OP_QUERY,
33     /** operationCode for ProcessCredential to create credential */
34     CRED_OP_CREATE,
35     /** operationCode for ProcessCredential to import credential */
36     CRED_OP_IMPORT,
37     /** operationCode for ProcessCredential to delete credential */
38     CRED_OP_DELETE,
39 };
40 
41 class HiChainAuthConnector {
42 public:
43     static bool onTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen);
44     static void onFinish(int64_t requestId, int operationCode, const char *returnData);
45     static void onError(int64_t requestId, int operationCode, int errorCode, const char *errorReturn);
46     static char *onRequest(int64_t requestId, int operationCode, const char *reqParams);
47     static void onSessionKeyReturned(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen);
48 
49 public:
50     HiChainAuthConnector();
51     ~HiChainAuthConnector();
52     int32_t AuthDevice(int32_t pinCode, int32_t osAccountId, std::string udid, int64_t requestId);
53     int32_t ProcessAuthData(int64_t requestId, std::string authData, int32_t osAccountId);
54     int32_t GenerateCredential(std::string &localUdid, int32_t osAccountId, std::string &publicKey);
55     bool QueryCredential(std::string &localUdid, int32_t osAccountId);
56     int32_t ImportCredential(int32_t osAccountId, std::string deviceId, std::string publicKey);
57     int32_t DeleteCredential(const std::string &deviceId, int32_t userId);
58     int32_t RegisterHiChainAuthCallback(std::shared_ptr<IDmDeviceAuthCallback> callback);
59     int32_t GetCredential(std::string &localUdid, int32_t osAccountId, std::string &publicKey);
60 
61 private:
62     DeviceAuthCallback deviceAuthCallback_;
63     static std::shared_ptr<IDmDeviceAuthCallback> dmDeviceAuthCallback_;
64 };
65 } // namespace DistributedHardware
66 } // namespace OHOS
67 #endif // OHOS_HICHAIN_AUTH_CONNECTOR_H
68