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_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 "auth_ui_state_manager.h" 25 #include "authentication.h" 26 #include "idevice_manager_service_listener.h" 27 #include "dm_ability_manager.h" 28 #include "dm_adapter_manager.h" 29 #include "dm_constants.h" 30 #include "dm_device_info.h" 31 #include "dm_timer.h" 32 #include "hichain_connector.h" 33 #include "softbus_connector.h" 34 #include "softbus_session.h" 35 36 namespace OHOS { 37 namespace DistributedHardware { 38 typedef enum AuthState { 39 AUTH_REQUEST_INIT = 1, 40 AUTH_REQUEST_NEGOTIATE, 41 AUTH_REQUEST_NEGOTIATE_DONE, 42 AUTH_REQUEST_REPLY, 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 localDeviceName; 73 int32_t localDeviceTypeId; 74 std::string deviceId; 75 std::string deviceName; 76 std::string deviceTypeId; 77 int32_t sessionId; 78 int32_t groupVisibility; 79 bool cryptoSupport; 80 std::string cryptoName; 81 std::string cryptoVer; 82 std::string hostPkgName; 83 std::string targetPkgName; 84 std::string appOperation; 85 std::string appDesc; 86 std::string appName; 87 std::string customDesc; 88 std::string appThumbnail; 89 std::string token; 90 int32_t reason; 91 std::vector<std::string> syncGroupList; 92 } DmAuthRequestContext; 93 94 typedef struct DmAuthResponseContext { 95 int32_t authType; 96 std::string deviceId; 97 std::string localDeviceId; 98 std::string deviceName; 99 int32_t deviceTypeId; 100 int32_t msgType; 101 int32_t sessionId; 102 bool cryptoSupport; 103 bool isIdenticalAccount; 104 std::string cryptoName; 105 std::string cryptoVer; 106 int32_t reply; 107 std::string networkId; 108 std::string groupId; 109 std::string groupName; 110 std::string hostPkgName; 111 std::string targetPkgName; 112 std::string appOperation; 113 std::string appDesc; 114 std::string customDesc; 115 std::string appIcon; 116 std::string appThumbnail; 117 std::string token; 118 std::string authToken; 119 int32_t pageId; 120 int64_t requestId; 121 int32_t code; 122 int32_t state; 123 std::vector<std::string> syncGroupList; 124 } DmAuthResponseContext; 125 126 class AuthMessageProcessor; 127 128 class DmAuthManager final : public ISoftbusSessionCallback, 129 public IHiChainConnectorCallback, 130 public std::enable_shared_from_this<DmAuthManager> { 131 public: 132 DmAuthManager(std::shared_ptr<SoftbusConnector> softbusConnector, 133 std::shared_ptr<IDeviceManagerServiceListener> listener, 134 std::shared_ptr<HiChainConnector> hiChainConnector); 135 ~DmAuthManager(); 136 137 /** 138 * @tc.name: DmAuthManager::AuthenticateDevice 139 * @tc.desc: Authenticate Device of the DeviceManager Authenticate Manager 140 * @tc.type: FUNC 141 */ 142 int32_t AuthenticateDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId, 143 const std::string &extra); 144 145 /** 146 * @tc.name: DmAuthManager::UnAuthenticateDevice 147 * @tc.desc: UnAuthenticate Device of the DeviceManager Authenticate Manager 148 * @tc.type: FUNC 149 */ 150 int32_t UnAuthenticateDevice(const std::string &pkgName, const std::string &networkId); 151 152 /** 153 * @brief UnBind device. 154 * @param pkgName package name. 155 * @param deviceId device id. 156 * @return Return 0 if success. 157 */ 158 int32_t UnBindDevice(const std::string &pkgName, const std::string &udidHash); 159 160 /** 161 * @tc.name: DmAuthManager::VerifyAuthentication 162 * @tc.desc: Verify Authentication of the DeviceManager Authenticate Manager 163 * @tc.type: FUNC 164 */ 165 int32_t VerifyAuthentication(const std::string &authParam); 166 167 /** 168 * @tc.name: DmAuthManager::OnSessionOpened 169 * @tc.desc: Opened Session of the DeviceManager Authenticate Manager 170 * @tc.type: FUNC 171 */ 172 void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result); 173 174 /** 175 * @tc.name: DmAuthManager::OnSessionClosed 176 * @tc.desc: Closed Session of the DeviceManager Authenticate Manager 177 * @tc.type: FUNC 178 */ 179 void OnSessionClosed(const int32_t sessionId); 180 181 /** 182 * @tc.name: DmAuthManager::OnDataReceived 183 * @tc.desc: Received Data of the DeviceManager Authenticate Manager 184 * @tc.type: FUNC 185 */ 186 void OnDataReceived(const int32_t sessionId, const std::string message); 187 188 /** 189 * @tc.name: DmAuthManager::OnGroupCreated 190 * @tc.desc: Created Group of the DeviceManager Authenticate Manager 191 * @tc.type: FUNC 192 */ 193 void OnGroupCreated(int64_t requestId, const std::string &groupId); 194 195 /** 196 * @tc.name: DmAuthManager::OnMemberJoin 197 * @tc.desc: Join Member of the DeviceManager Authenticate Manager 198 * @tc.type: FUNC 199 */ 200 void OnMemberJoin(int64_t requestId, int32_t status); 201 202 /** 203 * @tc.name: DmAuthManager::EstablishAuthChannel 204 * @tc.desc: Establish Auth Channel of the DeviceManager Authenticate Manager, auth state machine 205 * @tc.type: FUNC 206 */ 207 int32_t EstablishAuthChannel(const std::string &deviceId); 208 209 /** 210 * @tc.name: DmAuthManager::StartNegotiate 211 * @tc.desc: Start Negotiate of the DeviceManager Authenticate Manager 212 * @tc.type: FUNC 213 */ 214 void StartNegotiate(const int32_t &sessionId); 215 216 /** 217 * @tc.name: DmAuthManager::RespNegotiate 218 * @tc.desc: Resp Negotiate of the DeviceManager Authenticate Manager 219 * @tc.type: FUNC 220 */ 221 void RespNegotiate(const int32_t &sessionId); 222 223 /** 224 * @tc.name: DmAuthManager::SendAuthRequest 225 * @tc.desc: Send Auth Request of the DeviceManager Authenticate Manager 226 * @tc.type: FUNC 227 */ 228 void SendAuthRequest(const int32_t &sessionId); 229 230 /** 231 * @tc.name: DmAuthManager::StartAuthProcess 232 * @tc.desc: Start Auth Process of the DeviceManager Authenticate Manager 233 * @tc.type: FUNC 234 */ 235 int32_t StartAuthProcess(const int32_t &action); 236 237 /** 238 * @tc.name: DmAuthManager::StartRespAuthProcess 239 * @tc.desc: Start Resp Auth Process of the DeviceManager Authenticate Manager 240 * @tc.type: FUNC 241 */ 242 void StartRespAuthProcess(); 243 244 /** 245 * @tc.name: DmAuthManager::CreateGroup 246 * @tc.desc: Create Group of the DeviceManager Authenticate Manager 247 * @tc.type: FUNC 248 */ 249 int32_t CreateGroup(); 250 251 /** 252 * @tc.name: DmAuthManager::AddMember 253 * @tc.desc: Add Member of the DeviceManager Authenticate Manager 254 * @tc.type: FUNC 255 */ 256 int32_t AddMember(int32_t pinCode); 257 258 /** 259 * @tc.name: DmAuthManager::GetConnectAddr 260 * @tc.desc: Get Connect Addr of the DeviceManager Authenticate Manager 261 * @tc.type: FUNC 262 */ 263 std::string GetConnectAddr(std::string deviceId); 264 265 /** 266 * @tc.name: DmAuthManager::JoinNetwork 267 * @tc.desc: Join Net work of the DeviceManager Authenticate Manager 268 * @tc.type: FUNC 269 */ 270 int32_t JoinNetwork(); 271 272 /** 273 * @tc.name: DmAuthManager::AuthenticateFinish 274 * @tc.desc: Finish Authenticate of the DeviceManager Authenticate Manager 275 * @tc.type: FUNC 276 */ 277 void AuthenticateFinish(); 278 279 /** 280 * @tc.name: DmAuthManager::GetIsCryptoSupport 281 * @tc.desc: Get Cryp to Support of the DeviceManager Authenticate Manager 282 * @tc.type: FUNC 283 */ 284 bool GetIsCryptoSupport(); 285 286 /** 287 * @tc.name: DmAuthManager::SetAuthRequestState 288 * @tc.desc: Set Auth Request State of the DeviceManager Authenticate Manager 289 * @tc.type: FUNC 290 */ 291 int32_t SetAuthRequestState(std::shared_ptr<AuthRequestState> authRequestState); 292 293 /** 294 * @tc.name: DmAuthManager::SetAuthResponseState 295 * @tc.desc: Set Auth Response State of the DeviceManager Authenticate Manager 296 * @tc.type: FUNC 297 */ 298 int32_t SetAuthResponseState(std::shared_ptr<AuthResponseState> authResponseState); 299 300 /** 301 * @tc.name: DmAuthManager::GetPinCode 302 * @tc.desc: Get Pin Code of the DeviceManager Authenticate Manager 303 * @tc.type: FUNC 304 */ 305 int32_t GetPinCode(); 306 307 /** 308 * @tc.name: DmAuthManager::GenerateGroupName 309 * @tc.desc: Generate Group Name of the DeviceManager Authenticate Manager 310 * @tc.type: FUNC 311 */ 312 std::string GenerateGroupName(); 313 314 /** 315 * @tc.name: DmAuthManager::HandleAuthenticateTimeout 316 * @tc.desc: Handle Authenticate Timeout of the DeviceManager Authenticate Manager 317 * @tc.type: FUNC 318 */ 319 void HandleAuthenticateTimeout(std::string name); 320 321 /** 322 * @tc.name: DmAuthManager::CancelDisplay 323 * @tc.desc: Cancel Display of the DeviceManager Authenticate Manager 324 * @tc.type: FUNC 325 */ 326 void CancelDisplay(); 327 328 /** 329 * @tc.name: DmAuthManager::GeneratePincode 330 * @tc.desc: Generate Pincode of the DeviceManager Authenticate Manager 331 * @tc.type: FUNC 332 */ 333 int32_t GeneratePincode(); 334 335 /** 336 * @tc.name: DmAuthManager::ShowConfigDialog 337 * @tc.desc: Show Config Dialog of the DeviceManager Authenticate Manager 338 * @tc.type: FUNC 339 */ 340 void ShowConfigDialog(); 341 342 /** 343 * @tc.name: DmAuthManager::ShowAuthInfoDialog 344 * @tc.desc: Show AuthInfo Dialog of the DeviceManager Authenticate Manager 345 * @tc.type: FUNC 346 */ 347 void ShowAuthInfoDialog(); 348 349 /** 350 * @tc.name: DmAuthManager::ShowStartAuthDialog 351 * @tc.desc: Show Start Auth Dialog of the DeviceManager Authenticate Manager 352 * @tc.type: FUNC 353 */ 354 void ShowStartAuthDialog(); 355 356 /** 357 * @tc.name: DmAuthManager::GetAuthenticationParam 358 * @tc.desc: Get Authentication Param of the DeviceManager Authenticate Manager 359 * @tc.type: FUNC 360 */ 361 int32_t GetAuthenticationParam(DmAuthParam &authParam); 362 363 /** 364 * @tc.name: DmAuthManager::OnUserOperation 365 * @tc.desc: User Operation of the DeviceManager Authenticate Manager 366 * @tc.type: FUNC 367 */ 368 int32_t OnUserOperation(int32_t action, const std::string ¶ms); 369 370 /** 371 * @tc.name: DmAuthManager::UserSwitchEventCallback 372 * @tc.desc: User Switch Event Callback of the DeviceManager Authenticate Manager 373 * @tc.type: FUNC 374 */ 375 void UserSwitchEventCallback(int32_t userId); 376 377 /** 378 * @tc.name: DmAuthManager::SetPageId 379 * @tc.desc: Set PageId of the DeviceManager Authenticate Manager 380 * @tc.type: FUNC 381 */ 382 int32_t SetPageId(int32_t pageId); 383 384 /** 385 * @tc.name: DmAuthManager::SetReasonAndFinish 386 * @tc.desc: Set Reason of the DeviceManager Authenticate Manager 387 * @tc.type: FUNC 388 */ 389 int32_t SetReasonAndFinish(int32_t reason, int32_t state); 390 391 /** 392 * @tc.name: DmAuthManager::IsIdenticalAccount 393 * @tc.desc: judge IdenticalAccount or not 394 * @tc.type: FUNC 395 */ 396 bool IsIdenticalAccount(); 397 398 /** 399 * @tc.name: DmAuthManager::RegisterUiStateCallback 400 * @tc.desc: Register ui state callback 401 * @tc.type: FUNC 402 */ 403 int32_t RegisterUiStateCallback(const std::string pkgName); 404 405 /** 406 * @tc.name: DmAuthManager::UnRegisterUiStateCallback 407 * @tc.desc: Unregister ui state callback 408 * @tc.type: FUNC 409 */ 410 int32_t UnRegisterUiStateCallback(const std::string pkgName); 411 private: 412 int32_t CheckAuthParamVaild(const std::string &pkgName, int32_t authType, const std::string &deviceId, 413 const std::string &extra); 414 void ProcessSourceMsg(); 415 void ProcessSinkMsg(); 416 private: 417 std::shared_ptr<SoftbusConnector> softbusConnector_; 418 std::shared_ptr<HiChainConnector> hiChainConnector_; 419 std::shared_ptr<AuthUiStateManager> authUiStateMgr_; 420 std::shared_ptr<IDeviceManagerServiceListener> listener_; 421 std::shared_ptr<DmAdapterManager> adapterMgr_; 422 std::map<int32_t, std::shared_ptr<IAuthentication>> authenticationMap_; 423 std::shared_ptr<AuthRequestState> authRequestState_ = nullptr; 424 std::shared_ptr<AuthResponseState> authResponseState_ = nullptr; 425 std::shared_ptr<DmAuthRequestContext> authRequestContext_; 426 std::shared_ptr<DmAuthResponseContext> authResponseContext_; 427 std::shared_ptr<AuthMessageProcessor> authMessageProcessor_; 428 std::shared_ptr<DmTimer> timer_; 429 std::shared_ptr<DmAbilityManager> dmAbilityMgr_; 430 bool isCryptoSupport_ = false; 431 bool isFinishOfLocal_ = true; 432 int32_t authTimes_ = 0; 433 int32_t action_ = USER_OPERATION_TYPE_CANCEL_AUTH; 434 std::shared_ptr<IAuthentication> authPtr_; 435 bool isAddingMember_ = false; 436 }; 437 } // namespace DistributedHardware 438 } // namespace OHOS 439 #endif // OHOS_DM_AUTH_MANAGER_H 440