• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_MANAGER_H
17 #define OHOS_DM_AUTH_MANAGER_H
18 
19 #include <map>
20 #include <string>
21 
22 #include "auth_request_state.h"
23 #include "auth_response_state.h"
24 #include "authentication.h"
25 #include "device_manager_service_listener.h"
26 #include "dm_ability_manager.h"
27 #include "dm_adapter_manager.h"
28 #include "dm_constants.h"
29 #include "dm_device_info.h"
30 #include "dm_timer.h"
31 #include "hichain_connector.h"
32 #include "softbus_connector.h"
33 #include "softbus_session.h"
34 
35 namespace OHOS {
36 namespace DistributedHardware {
37 typedef enum AuthState {
38     AUTH_REQUEST_INIT = 1,
39     AUTH_REQUEST_NEGOTIATE,
40     AUTH_REQUEST_NEGOTIATE_DONE,
41     AUTH_REQUEST_REPLY,
42     AUTH_REQUEST_INPUT,
43     AUTH_REQUEST_JOIN,
44     AUTH_REQUEST_NETWORK,
45     AUTH_REQUEST_FINISH,
46     AUTH_RESPONSE_INIT = 20,
47     AUTH_RESPONSE_NEGOTIATE,
48     AUTH_RESPONSE_CONFIRM,
49     AUTH_RESPONSE_GROUP,
50     AUTH_RESPONSE_SHOW,
51     AUTH_RESPONSE_FINISH,
52 } AuthState;
53 
54 enum DmMsgType : int32_t {
55     MSG_TYPE_UNKNOWN = 0,
56     MSG_TYPE_NEGOTIATE = 80,
57     MSG_TYPE_RESP_NEGOTIATE = 90,
58     MSG_TYPE_REQ_AUTH = 100,
59     MSG_TYPE_INVITE_AUTH_INFO = 102,
60     MSG_TYPE_REQ_AUTH_TERMINATE = 104,
61     MSG_TYPE_RESP_AUTH = 200,
62     MSG_TYPE_JOIN_AUTH_INFO = 201,
63     MSG_TYPE_RESP_AUTH_TERMINATE = 205,
64     MSG_TYPE_CHANNEL_CLOSED = 300,
65     MSG_TYPE_SYNC_GROUP = 400,
66     MSG_TYPE_AUTH_BY_PIN = 500,
67 };
68 
69 typedef struct DmAuthRequestContext {
70     int32_t authType;
71     std::string localDeviceId;
72     std::string deviceId;
73     std::string deviceName;
74     std::string deviceTypeId;
75     int32_t sessionId;
76     int32_t groupVisibility;
77     bool cryptoSupport;
78     std::string cryptoName;
79     std::string cryptoVer;
80     std::string hostPkgName;
81     std::string targetPkgName;
82     std::string appName;
83     std::string appDesc;
84     std::string appIcon;
85     std::string appThumbnail;
86     std::string token;
87     int32_t reason;
88     std::vector<std::string> syncGroupList;
89 } DmAuthRequestContext;
90 
91 typedef struct DmAuthResponseContext {
92     int32_t authType;
93     std::string deviceId;
94     std::string localDeviceId;
95     int32_t msgType;
96     int32_t sessionId;
97     bool cryptoSupport;
98     std::string cryptoName;
99     std::string cryptoVer;
100     int32_t reply;
101     std::string networkId;
102     std::string groupId;
103     std::string groupName;
104     std::string hostPkgName;
105     std::string targetPkgName;
106     std::string appName;
107     std::string appDesc;
108     std::string appIcon;
109     std::string appThumbnail;
110     std::string token;
111     std::string authToken;
112     int32_t pageId;
113     int64_t requestId;
114     int32_t code;
115     int32_t state;
116     std::vector<std::string> syncGroupList;
117 } DmAuthResponseContext;
118 
119 class AuthMessageProcessor;
120 
121 class DmAuthManager final : public ISoftbusSessionCallback,
122                             public IHiChainConnectorCallback,
123                             public std::enable_shared_from_this<DmAuthManager> {
124 public:
125     DmAuthManager(std::shared_ptr<SoftbusConnector> softbusConnector,
126                   std::shared_ptr<DeviceManagerServiceListener> listener,
127                   std::shared_ptr<HiChainConnector> hiChainConnector_);
128     ~DmAuthManager();
129     int32_t AuthenticateDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId,
130                                const std::string &extra);
131     int32_t UnAuthenticateDevice(const std::string &pkgName, const std::string &deviceId);
132     int32_t VerifyAuthentication(const std::string &authParam);
133     void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result);
134     void OnSessionClosed(int32_t sessionId);
135     void OnDataReceived(int32_t sessionId, std::string message);
136     void OnGroupCreated(int64_t requestId, const std::string &groupId);
137     void OnMemberJoin(int64_t requestId, int32_t status);
138 
139     // auth state machine
140     int32_t EstablishAuthChannel(const std::string &deviceId);
141     void StartNegotiate(const int32_t &sessionId);
142     void RespNegotiate(const int32_t &sessionId);
143     void SendAuthRequest(const int32_t &sessionId);
144     int32_t StartAuthProcess(const int32_t &authType);
145     void StartRespAuthProcess();
146     int32_t CreateGroup();
147     int32_t AddMember(int32_t pinCode);
148     std::string GetConnectAddr(std::string deviceId);
149     int32_t JoinNetwork();
150     void AuthenticateFinish();
151     bool GetIsCryptoSupport();
152     int32_t SetAuthRequestState(std::shared_ptr<AuthRequestState> authRequestState);
153     int32_t SetAuthResponseState(std::shared_ptr<AuthResponseState> authResponseState);
154     int32_t GetPinCode();
155     std::string GenerateGroupName();
156     void HandleAuthenticateTimeout(std::string name);
157     void CancelDisplay();
158 
159     /**
160      * @tc.name: DmAuthManager::UpdateInputDialogDisplay
161      * @tc.desc: Update InputDialog Display of the DeviceManager Authenticate Manager
162      * @tc.type: FUNC
163      */
164     void UpdateInputDialogDisplay(bool isShow);
165 
166     /**
167      * @tc.name: DmAuthManager::GeneratePincode
168      * @tc.desc: Generate Pincode of the DeviceManager Authenticate Manager
169      * @tc.type: FUNC
170      */
171     int32_t GeneratePincode();
172     void ShowConfigDialog();
173     void ShowAuthInfoDialog();
174     void ShowStartAuthDialog();
175     int32_t GetAuthenticationParam(DmAuthParam &authParam);
176     /**
177      * @tc.name: DmAuthManager::OnUserOperation
178      * @tc.desc: User Operation of the DeviceManager Authenticate Manager
179      * @tc.type: FUNC
180      */
181     int32_t OnUserOperation(int32_t action, const std::string &params);
182     void UserSwitchEventCallback(int32_t userId);
183     int32_t SetPageId(int32_t pageId);
184     int32_t SetReasonAndFinish(int32_t reason, int32_t state);
185 
186 private:
187     std::shared_ptr<SoftbusConnector> softbusConnector_;
188     std::shared_ptr<HiChainConnector> hiChainConnector_;
189     std::shared_ptr<DeviceManagerServiceListener> listener_;
190     std::shared_ptr<DmAdapterManager> adapterMgr_;
191     std::map<int32_t, std::shared_ptr<IAuthentication>> authenticationMap_;
192     std::shared_ptr<AuthRequestState> authRequestState_ = nullptr;
193     std::shared_ptr<AuthResponseState> authResponseState_ = nullptr;
194     std::shared_ptr<DmAuthRequestContext> authRequestContext_;
195     std::shared_ptr<DmAuthResponseContext> authResponseContext_;
196     std::shared_ptr<AuthMessageProcessor> authMessageProcessor_;
197     std::shared_ptr<DmTimer> timer_;
198     std::shared_ptr<DmAbilityManager> dmAbilityMgr_;
199     bool isCryptoSupport_ = false;
200     bool isFinishOfLocal_ = true;
201     int32_t authTimes_ = 0;
202     std::shared_ptr<IAuthentication> authPtr_;
203 };
204 } // namespace DistributedHardware
205 } // namespace OHOS
206 #endif // OHOS_DM_AUTH_MANAGER_H
207