• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OHOS_DM_AUTH_MESSAGE_PROCESSOR_H
17 #define OHOS_DM_AUTH_MESSAGE_PROCESSOR_H
18 
19 #include <memory>
20 #include <vector>
21 
22 #include "crypto_adapter.h"
23 #include "dm_auth_manager.h"
24 #include "nlohmann/json.hpp"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 constexpr const char* TAG_REPLY = "REPLY";
29 constexpr const char* TAG_NET_ID = "NETID";
30 constexpr const char* TAG_TARGET = "TARGET";
31 constexpr const char* TAG_APP_OPERATION = "APPOPERATION";
32 constexpr const char* TAG_APP_NAME = "APPNAME";
33 constexpr const char* TAG_APP_DESCRIPTION = "APPDESC";
34 constexpr const char* TAG_GROUPIDS = "GROUPIDLIST";
35 constexpr const char* TAG_CUSTOM_DESCRIPTION = "CUSTOMDESC";
36 constexpr const char* TAG_DEVICE_TYPE = "DEVICETYPE";
37 constexpr const char* TAG_REQUESTER = "REQUESTER";
38 constexpr const char* TAG_LOCAL_DEVICE_ID = "LOCALDEVICEID";
39 constexpr const char* TAG_LOCAL_DEVICE_TYPE = "LOCALDEVICETYPE";
40 constexpr const char* TAG_INDEX = "INDEX";
41 constexpr const char* TAG_SLICE_NUM = "SLICE";
42 constexpr const char* TAG_TOKEN = "TOKEN";
43 constexpr const char* TAG_CRYPTO_NAME = "CRYPTONAME";
44 constexpr const char* TAG_CRYPTO_VERSION = "CRYPTOVERSION";
45 constexpr const char* TAG_IDENTICAL_ACCOUNT = "IDENTICALACCOUNT";
46 constexpr const char* APP_THUMBNAIL = "appThumbnail";
47 constexpr const char* QR_CODE_KEY = "qrCode";
48 constexpr const char* TAG_AUTH_TOKEN = "authToken";
49 constexpr const char* NFC_CODE_KEY = "nfcCode";
50 
51 class DmAuthManager;
52 struct DmAuthRequestContext;
53 struct DmAuthResponseContext;
54 class ICryptoAdapter;
55 class AuthMessageProcessor {
56 public:
57     explicit AuthMessageProcessor(std::shared_ptr<DmAuthManager> authMgr);
58     ~AuthMessageProcessor();
59     std::vector<std::string> CreateAuthRequestMessage();
60     std::string CreateSimpleMessage(int32_t msgType);
61     int32_t ParseMessage(const std::string &message);
62     void SetRequestContext(std::shared_ptr<DmAuthRequestContext> authRequestContext);
63     void SetResponseContext(std::shared_ptr<DmAuthResponseContext> authResponseContext);
64     std::shared_ptr<DmAuthResponseContext> GetResponseContext();
65     std::shared_ptr<DmAuthRequestContext> GetRequestContext();
66 
67 private:
68     std::string CreateRequestAuthMessage(nlohmann::json &json);
69     void CreateNegotiateMessage(nlohmann::json &json);
70     void CreateSyncGroupMessage(nlohmann::json &json);
71     void CreateResponseAuthMessage(nlohmann::json &json);
72     void ParseAuthResponseMessage(nlohmann::json &json);
73     int32_t ParseAuthRequestMessage(nlohmann::json &json);
74     void ParseNegotiateMessage(const nlohmann::json &json);
75     void ParseRespNegotiateMessage(const nlohmann::json &json);
76     void CreateResponseFinishMessage(nlohmann::json &json);
77     void ParseResponseFinishMessage(nlohmann::json &json);
78     void GetAuthReqMessage(nlohmann::json &json);
79 
80 private:
81     std::weak_ptr<DmAuthManager> authMgr_;
82     std::shared_ptr<ICryptoAdapter> cryptoAdapter_;
83     std::shared_ptr<DmAuthRequestContext> authRequestContext_;
84     std::shared_ptr<DmAuthResponseContext> authResponseContext_;
85     std::vector<nlohmann::json> authSplitJsonList_;
86 };
87 } // namespace DistributedHardware
88 } // namespace OHOS
89 #endif // OHOS_DM_AUTH_MESSAGE_PROCESSOR_H
90