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_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 "idevice_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_JOIN, 43 AUTH_REQUEST_NETWORK, 44 AUTH_REQUEST_FINISH, 45 AUTH_RESPONSE_INIT = 20, 46 AUTH_RESPONSE_NEGOTIATE, 47 AUTH_RESPONSE_CONFIRM, 48 AUTH_RESPONSE_GROUP, 49 AUTH_RESPONSE_SHOW, 50 AUTH_RESPONSE_FINISH, 51 } AuthState; 52 53 enum DmMsgType : int32_t { 54 MSG_TYPE_UNKNOWN = 0, 55 MSG_TYPE_NEGOTIATE = 80, 56 MSG_TYPE_RESP_NEGOTIATE = 90, 57 MSG_TYPE_REQ_AUTH = 100, 58 MSG_TYPE_INVITE_AUTH_INFO = 102, 59 MSG_TYPE_REQ_AUTH_TERMINATE = 104, 60 MSG_TYPE_RESP_AUTH = 200, 61 MSG_TYPE_JOIN_AUTH_INFO = 201, 62 MSG_TYPE_RESP_AUTH_TERMINATE = 205, 63 MSG_TYPE_CHANNEL_CLOSED = 300, 64 MSG_TYPE_SYNC_GROUP = 400, 65 MSG_TYPE_AUTH_BY_PIN = 500, 66 }; 67 68 typedef struct DmAuthRequestContext { 69 int32_t authType; 70 std::string localDeviceId; 71 std::string deviceId; 72 std::string deviceName; 73 std::string deviceTypeId; 74 int32_t sessionId; 75 int32_t groupVisibility; 76 bool cryptoSupport; 77 std::string cryptoName; 78 std::string cryptoVer; 79 std::string hostPkgName; 80 std::string targetPkgName; 81 std::string appName; 82 std::string appDesc; 83 std::string appIcon; 84 std::string appThumbnail; 85 std::string token; 86 int32_t reason; 87 std::vector<std::string> syncGroupList; 88 } DmAuthRequestContext; 89 90 typedef struct DmAuthResponseContext { 91 int32_t authType; 92 std::string deviceId; 93 std::string localDeviceId; 94 int32_t msgType; 95 int32_t sessionId; 96 bool cryptoSupport; 97 bool isIdenticalAccount; 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<IDeviceManagerServiceListener> listener, 127 std::shared_ptr<HiChainConnector> hiChainConnector_); 128 ~DmAuthManager(); 129 130 /** 131 * @tc.name: DmAuthManager::AuthenticateDevice 132 * @tc.desc: Authenticate Device of the DeviceManager Authenticate Manager 133 * @tc.type: FUNC 134 */ 135 int32_t AuthenticateDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId, 136 const std::string &extra); 137 138 /** 139 * @tc.name: DmAuthManager::UnAuthenticateDevice 140 * @tc.desc: UnAuthenticate Device of the DeviceManager Authenticate Manager 141 * @tc.type: FUNC 142 */ 143 int32_t UnAuthenticateDevice(const std::string &pkgName, const std::string &deviceId); 144 145 /** 146 * @tc.name: DmAuthManager::VerifyAuthentication 147 * @tc.desc: Verify Authentication of the DeviceManager Authenticate Manager 148 * @tc.type: FUNC 149 */ 150 int32_t VerifyAuthentication(const std::string &authParam); 151 152 /** 153 * @tc.name: DmAuthManager::OnSessionOpened 154 * @tc.desc: Opened Session of the DeviceManager Authenticate Manager 155 * @tc.type: FUNC 156 */ 157 void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result); 158 159 /** 160 * @tc.name: DmAuthManager::OnSessionClosed 161 * @tc.desc: Closed Session of the DeviceManager Authenticate Manager 162 * @tc.type: FUNC 163 */ 164 void OnSessionClosed(const int32_t sessionId); 165 166 /** 167 * @tc.name: DmAuthManager::OnDataReceived 168 * @tc.desc: Received Data of the DeviceManager Authenticate Manager 169 * @tc.type: FUNC 170 */ 171 void OnDataReceived(const int32_t sessionId, const std::string message); 172 173 /** 174 * @tc.name: DmAuthManager::OnGroupCreated 175 * @tc.desc: Created Group of the DeviceManager Authenticate Manager 176 * @tc.type: FUNC 177 */ 178 void OnGroupCreated(int64_t requestId, const std::string &groupId); 179 180 /** 181 * @tc.name: DmAuthManager::OnMemberJoin 182 * @tc.desc: Join Member of the DeviceManager Authenticate Manager 183 * @tc.type: FUNC 184 */ 185 void OnMemberJoin(int64_t requestId, int32_t status); 186 187 /** 188 * @tc.name: DmAuthManager::EstablishAuthChannel 189 * @tc.desc: Establish Auth Channel of the DeviceManager Authenticate Manager, auth state machine 190 * @tc.type: FUNC 191 */ 192 int32_t EstablishAuthChannel(const std::string &deviceId); 193 194 /** 195 * @tc.name: DmAuthManager::StartNegotiate 196 * @tc.desc: Start Negotiate of the DeviceManager Authenticate Manager 197 * @tc.type: FUNC 198 */ 199 void StartNegotiate(const int32_t &sessionId); 200 201 /** 202 * @tc.name: DmAuthManager::RespNegotiate 203 * @tc.desc: Resp Negotiate of the DeviceManager Authenticate Manager 204 * @tc.type: FUNC 205 */ 206 void RespNegotiate(const int32_t &sessionId); 207 208 /** 209 * @tc.name: DmAuthManager::SendAuthRequest 210 * @tc.desc: Send Auth Request of the DeviceManager Authenticate Manager 211 * @tc.type: FUNC 212 */ 213 void SendAuthRequest(const int32_t &sessionId); 214 215 /** 216 * @tc.name: DmAuthManager::StartAuthProcess 217 * @tc.desc: Start Auth Process of the DeviceManager Authenticate Manager 218 * @tc.type: FUNC 219 */ 220 int32_t StartAuthProcess(const int32_t &authType); 221 222 /** 223 * @tc.name: DmAuthManager::StartRespAuthProcess 224 * @tc.desc: Start Resp Auth Process of the DeviceManager Authenticate Manager 225 * @tc.type: FUNC 226 */ 227 void StartRespAuthProcess(); 228 229 /** 230 * @tc.name: DmAuthManager::CreateGroup 231 * @tc.desc: Create Group of the DeviceManager Authenticate Manager 232 * @tc.type: FUNC 233 */ 234 int32_t CreateGroup(); 235 236 /** 237 * @tc.name: DmAuthManager::AddMember 238 * @tc.desc: Add Member of the DeviceManager Authenticate Manager 239 * @tc.type: FUNC 240 */ 241 int32_t AddMember(int32_t pinCode); 242 243 /** 244 * @tc.name: DmAuthManager::GetConnectAddr 245 * @tc.desc: Get Connect Addr of the DeviceManager Authenticate Manager 246 * @tc.type: FUNC 247 */ 248 std::string GetConnectAddr(std::string deviceId); 249 250 /** 251 * @tc.name: DmAuthManager::JoinNetwork 252 * @tc.desc: Join Net work of the DeviceManager Authenticate Manager 253 * @tc.type: FUNC 254 */ 255 int32_t JoinNetwork(); 256 257 /** 258 * @tc.name: DmAuthManager::AuthenticateFinish 259 * @tc.desc: Finish Authenticate of the DeviceManager Authenticate Manager 260 * @tc.type: FUNC 261 */ 262 void AuthenticateFinish(); 263 264 /** 265 * @tc.name: DmAuthManager::GetIsCryptoSupport 266 * @tc.desc: Get Cryp to Support of the DeviceManager Authenticate Manager 267 * @tc.type: FUNC 268 */ 269 bool GetIsCryptoSupport(); 270 271 /** 272 * @tc.name: DmAuthManager::SetAuthRequestState 273 * @tc.desc: Set Auth Request State of the DeviceManager Authenticate Manager 274 * @tc.type: FUNC 275 */ 276 int32_t SetAuthRequestState(std::shared_ptr<AuthRequestState> authRequestState); 277 278 /** 279 * @tc.name: DmAuthManager::SetAuthResponseState 280 * @tc.desc: Set Auth Response State of the DeviceManager Authenticate Manager 281 * @tc.type: FUNC 282 */ 283 int32_t SetAuthResponseState(std::shared_ptr<AuthResponseState> authResponseState); 284 285 /** 286 * @tc.name: DmAuthManager::GetPinCode 287 * @tc.desc: Get Pin Code of the DeviceManager Authenticate Manager 288 * @tc.type: FUNC 289 */ 290 int32_t GetPinCode(); 291 292 /** 293 * @tc.name: DmAuthManager::GenerateGroupName 294 * @tc.desc: Generate Group Name of the DeviceManager Authenticate Manager 295 * @tc.type: FUNC 296 */ 297 std::string GenerateGroupName(); 298 299 /** 300 * @tc.name: DmAuthManager::HandleAuthenticateTimeout 301 * @tc.desc: Handle Authenticate Timeout of the DeviceManager Authenticate Manager 302 * @tc.type: FUNC 303 */ 304 void HandleAuthenticateTimeout(std::string name); 305 306 /** 307 * @tc.name: DmAuthManager::CancelDisplay 308 * @tc.desc: Cancel Display of the DeviceManager Authenticate Manager 309 * @tc.type: FUNC 310 */ 311 void CancelDisplay(); 312 313 /** 314 * @tc.name: DmAuthManager::UpdateInputDialogDisplay 315 * @tc.desc: Update InputDialog Display of the DeviceManager Authenticate Manager 316 * @tc.type: FUNC 317 */ 318 void UpdateInputDialogDisplay(bool isShow); 319 320 /** 321 * @tc.name: DmAuthManager::GeneratePincode 322 * @tc.desc: Generate Pincode of the DeviceManager Authenticate Manager 323 * @tc.type: FUNC 324 */ 325 int32_t GeneratePincode(); 326 327 /** 328 * @tc.name: DmAuthManager::ShowConfigDialog 329 * @tc.desc: Show Config Dialog of the DeviceManager Authenticate Manager 330 * @tc.type: FUNC 331 */ 332 void ShowConfigDialog(); 333 334 /** 335 * @tc.name: DmAuthManager::ShowAuthInfoDialog 336 * @tc.desc: Show AuthInfo Dialog of the DeviceManager Authenticate Manager 337 * @tc.type: FUNC 338 */ 339 void ShowAuthInfoDialog(); 340 341 /** 342 * @tc.name: DmAuthManager::ShowStartAuthDialog 343 * @tc.desc: Show Start Auth Dialog of the DeviceManager Authenticate Manager 344 * @tc.type: FUNC 345 */ 346 void ShowStartAuthDialog(); 347 348 /** 349 * @tc.name: DmAuthManager::GetAuthenticationParam 350 * @tc.desc: Get Authentication Param of the DeviceManager Authenticate Manager 351 * @tc.type: FUNC 352 */ 353 int32_t GetAuthenticationParam(DmAuthParam &authParam); 354 355 /** 356 * @tc.name: DmAuthManager::OnUserOperation 357 * @tc.desc: User Operation of the DeviceManager Authenticate Manager 358 * @tc.type: FUNC 359 */ 360 int32_t OnUserOperation(int32_t action, const std::string ¶ms); 361 362 /** 363 * @tc.name: DmAuthManager::UserSwitchEventCallback 364 * @tc.desc: User Switch Event Callback of the DeviceManager Authenticate Manager 365 * @tc.type: FUNC 366 */ 367 void UserSwitchEventCallback(int32_t userId); 368 369 /** 370 * @tc.name: DmAuthManager::SetPageId 371 * @tc.desc: Set PageId of the DeviceManager Authenticate Manager 372 * @tc.type: FUNC 373 */ 374 int32_t SetPageId(int32_t pageId); 375 376 /** 377 * @tc.name: DmAuthManager::SetReasonAndFinish 378 * @tc.desc: Set Reason of the DeviceManager Authenticate Manager 379 * @tc.type: FUNC 380 */ 381 int32_t SetReasonAndFinish(int32_t reason, int32_t state); 382 383 /** 384 * @tc.name: DmAuthManager::IsIdenticalAccount 385 * @tc.desc: judge IdenticalAccount or not 386 * @tc.type: FUNC 387 */ 388 bool IsIdenticalAccount(); 389 private: 390 std::shared_ptr<SoftbusConnector> softbusConnector_; 391 std::shared_ptr<HiChainConnector> hiChainConnector_; 392 std::shared_ptr<IDeviceManagerServiceListener> listener_; 393 std::shared_ptr<DmAdapterManager> adapterMgr_; 394 std::map<int32_t, std::shared_ptr<IAuthentication>> authenticationMap_; 395 std::shared_ptr<AuthRequestState> authRequestState_ = nullptr; 396 std::shared_ptr<AuthResponseState> authResponseState_ = nullptr; 397 std::shared_ptr<DmAuthRequestContext> authRequestContext_; 398 std::shared_ptr<DmAuthResponseContext> authResponseContext_; 399 std::shared_ptr<AuthMessageProcessor> authMessageProcessor_; 400 std::shared_ptr<DmTimer> timer_; 401 std::shared_ptr<DmAbilityManager> dmAbilityMgr_; 402 bool isCryptoSupport_ = false; 403 bool isFinishOfLocal_ = true; 404 int32_t authTimes_ = 0; 405 std::shared_ptr<IAuthentication> authPtr_; 406 }; 407 } // namespace DistributedHardware 408 } // namespace OHOS 409 #endif // OHOS_DM_AUTH_MANAGER_H 410