diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index fd401fdc..3ead6bab 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -75,6 +75,7 @@ enum { ERR_DM_AUTH_MESSAGE_INCOMPLETE = -20027, ERR_DM_CREATE_GROUP_FAILED = -20028, ERR_DM_IPC_READ_TOKEN_FAILED = -20029, + ERR_DM_AUTH_INPUT_PARAMETER_FAILED = -20030, }; const std::string TARGET_PKG_NAME_KEY = "targetPkgName"; @@ -136,6 +137,7 @@ const std::string PIN_CODE_KEY = "pinCode"; const std::string NFC_CODE_KEY = "nfcCode"; const std::string QR_CODE_KEY = "qrCode"; const std::string TAG_AUTH_TOKEN = "authToken"; +const std::string VERIFY_FAILED = "verifyFailed"; const int32_t AUTH_TYPE_PIN = 1; const int32_t AUTH_TYPE_SCAN = 2; const int32_t AUTH_TYPE_TOUCH = 3; @@ -172,8 +174,8 @@ const std::string ETH_PORT = "ETH_PORT"; // ACE const int32_t ACE_X = 50; const int32_t ACE_Y = 300; -const int32_t ACE_WIDTH = 450; -const int32_t ACE_HEIGHT = 400; +const int32_t ACE_WIDTH = 580; +const int32_t ACE_HEIGHT = 520; const std::string EVENT_CONFIRM = "EVENT_CONFIRM"; const std::string EVENT_CANCEL = "EVENT_CANCEL"; const std::string EVENT_INIT = "EVENT_INIT"; diff --git a/ext/no_interaction_auth/include/no_interaction_auth.h b/ext/no_interaction_auth/include/no_interaction_auth.h index 3675a065..30d91dab 100644 --- a/ext/no_interaction_auth/include/no_interaction_auth.h +++ b/ext/no_interaction_auth/include/no_interaction_auth.h @@ -33,6 +33,7 @@ public: int32_t StartAuth(std::string &authToken, std::shared_ptr authManager) override; int32_t VerifyAuthentication(std::string &authToken, const std::string &authParam) override; int32_t CloseAuthInfo(const int32_t &pageId, std::shared_ptr authManager) override; + int32_t UpdateAuthInfo(const int32_t &pageId) override; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/ext/no_interaction_auth/src/no_interaction_auth.cpp b/ext/no_interaction_auth/src/no_interaction_auth.cpp index 93b78304..2dff1431 100644 --- a/ext/no_interaction_auth/src/no_interaction_auth.cpp +++ b/ext/no_interaction_auth/src/no_interaction_auth.cpp @@ -52,6 +52,11 @@ int32_t NoInteractionAuth::CloseAuthInfo(const int32_t &pageId, std::shared_ptr< return DM_OK; } +int32_t NoInteractionAuth::UpdateAuthInfo(const int32_t &pageId) +{ + return DM_OK; +} + extern "C" IAuthentication *CreatePinAuthObject(void) { return new NoInteractionAuth; diff --git a/ext/pin_auth/include/lite/pin_auth_ui.h b/ext/pin_auth/include/lite/pin_auth_ui.h index aad43c50..7ee07129 100644 --- a/ext/pin_auth/include/lite/pin_auth_ui.h +++ b/ext/pin_auth/include/lite/pin_auth_ui.h @@ -42,7 +42,7 @@ public: * @tc.desc: Show Input PinDialog * @tc.type: FUNC */ - int32_t InputPinDialog(int32_t code, std::shared_ptr authManager); + int32_t InputPinDialog(std::shared_ptr authManager); /** * @tc.name: PinAuthUi::ClosePage * @tc.desc: Close Page @@ -50,6 +50,13 @@ public: */ int32_t ClosePage(const int32_t &pageId, std::shared_ptr authManager); + /** + * @tc.name: PinAuth::UpdateAuthInfo + * @tc.desc: Update the page information + * @tc.type: FUNC + */ + int32_t UpdatePinDialog(int32_t pageId); + private: int32_t StartFaUiService(std::shared_ptr dmAbilityManager); }; diff --git a/ext/pin_auth/include/pin_auth.h b/ext/pin_auth/include/pin_auth.h index 13afc8cd..57807dad 100644 --- a/ext/pin_auth/include/pin_auth.h +++ b/ext/pin_auth/include/pin_auth.h @@ -58,6 +58,13 @@ public: * @tc.type: FUNC */ int32_t CloseAuthInfo(const int32_t &pageId, std::shared_ptr authManager) override; + + /** + * @tc.name: PinAuth::UpdateAuthInfo + * @tc.desc: Update Auth Info of the PinAuth + * @tc.type: FUNC + */ + int32_t UpdateAuthInfo(const int32_t &pageId) override; private: int32_t times_ = 0; std::shared_ptr pinAuthUi_; diff --git a/ext/pin_auth/include/standard/pin_auth_ui.h b/ext/pin_auth/include/standard/pin_auth_ui.h index 7c594152..bb9e2742 100644 --- a/ext/pin_auth/include/standard/pin_auth_ui.h +++ b/ext/pin_auth/include/standard/pin_auth_ui.h @@ -27,9 +27,9 @@ class PinAuthUi { public: PinAuthUi(); int32_t ShowPinDialog(int32_t code, std::shared_ptr authManager); - int32_t InputPinDialog(int32_t code, std::shared_ptr authManager); + int32_t InputPinDialog(std::shared_ptr authManager); int32_t ClosePage(const int32_t &pageId, std::shared_ptr authManager); - + int32_t UpdatePinDialog(int32_t pageId); private: int32_t StartFaUiService(std::shared_ptr dmAbilityManager); }; diff --git a/ext/pin_auth/input_pin_dialog/dialog_ui/js/pages/index/index.hml b/ext/pin_auth/input_pin_dialog/dialog_ui/js/pages/index/index.hml index 0d045101..85825a76 100644 --- a/ext/pin_auth/input_pin_dialog/dialog_ui/js/pages/index/index.hml +++ b/ext/pin_auth/input_pin_dialog/dialog_ui/js/pages/index/index.hml @@ -22,7 +22,7 @@ 请输入另一个设备显示的PIN码进行验证 - + PIN码输入错误,请重新输入(3次:还有{{isTimes}}次机会) diff --git a/ext/pin_auth/input_pin_dialog/dialog_ui/js/pages/index/index.js b/ext/pin_auth/input_pin_dialog/dialog_ui/js/pages/index/index.js index 48a242f2..63d0ae1e 100644 --- a/ext/pin_auth/input_pin_dialog/dialog_ui/js/pages/index/index.js +++ b/ext/pin_auth/input_pin_dialog/dialog_ui/js/pages/index/index.js @@ -15,9 +15,10 @@ import router from '@ohos.router'; -var numbs = 0; -var code = 0; -var inputVal = 0; +var inputVal; +var inputValue; +var isShow = false; +var isTimes = 3; var EVENT_CONFIRM = "EVENT_CONFIRM"; var EVENT_CANCEL = "EVENT_CANCEL"; var EVENT_INIT = "EVENT_INIT"; @@ -26,37 +27,19 @@ var EVENT_CANCEL_CODE = "1"; var EVENT_INIT_CODE = "2"; export default { data: { - pincode: router.getParams().pinCode, - isShow:false, - isTimes:3, + isShow: router.getParams().verifyFailed, + inputValue: "", }, onInit() { - code = router.getParams().pinCode; callNativeHandler(EVENT_INIT, EVENT_INIT_CODE); }, onChange(e){ - inputVal = e.value; + inputVal = e.value; }, onConfirm() { - numbs = numbs + 1; - if(numbs <= 3){ - console.info('click confirm numbs < 3 '); - if(code == inputVal){ - console.info('click confirm code == inputVal'); - callNativeHandler(EVENT_CONFIRM, EVENT_CONFIRM_CODE); - }else{ - if(numbs == 3){ - console.info('click confirm code != inputVal and numbs == 3'); - callNativeHandler(EVENT_CANCEL, EVENT_CANCEL_CODE); - } - console.info('click confirm code != inputVal'); - this.isShow = true; - this.isTimes = 3 - numbs; - } - }else{ - console.info('click confirm numbs > 3 '); - callNativeHandler(EVENT_CANCEL, EVENT_CANCEL_CODE); - } + isTimes--; + callNativeHandler(EVENT_CONFIRM, inputVal); + inputValue = ""; }, onCancel() { console.info('click cancel'); diff --git a/ext/pin_auth/src/lite/pin_auth_ui.cpp b/ext/pin_auth/src/lite/pin_auth_ui.cpp index 205d3276..fbc08a26 100644 --- a/ext/pin_auth/src/lite/pin_auth_ui.cpp +++ b/ext/pin_auth/src/lite/pin_auth_ui.cpp @@ -33,7 +33,7 @@ int32_t PinAuthUi::ShowPinDialog(int32_t code, std::shared_ptr au return DM_OK; } -int32_t PinAuthUi::InputPinDialog(int32_t code, std::shared_ptr authManager) +int32_t PinAuthUi::InputPinDialog(std::shared_ptr authManager) { LOGI("InputPinDialog start"); std::shared_ptr dmAbilityManager_ = std::make_shared(); @@ -66,5 +66,10 @@ int32_t PinAuthUi::StartFaUiService(std::shared_ptr dmAbilityM } return DM_OK; } + +int32_t PinAuthUi::UpdatePinDialog((int32_t pageId) +{ + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/ext/pin_auth/src/pin_auth.cpp b/ext/pin_auth/src/pin_auth.cpp index a746406f..5ae4f09a 100644 --- a/ext/pin_auth/src/pin_auth.cpp +++ b/ext/pin_auth/src/pin_auth.cpp @@ -51,16 +51,7 @@ int32_t PinAuth::ShowAuthInfo(std::string &authToken, std::shared_ptr authManager) { - nlohmann::json jsonObject = nlohmann::json::parse(authToken, nullptr, false); - if (jsonObject.is_discarded()) { - LOGE("DecodeRequestAuth jsonStr error"); - return ERR_DM_FAILED; - } - if (!jsonObject.contains(PIN_CODE_KEY)) { - LOGE("err json string, first time"); - return ERR_DM_FAILED; - } - return pinAuthUi_->InputPinDialog(jsonObject[PIN_CODE_KEY], authManager); + return pinAuthUi_->InputPinDialog(authManager); } int32_t PinAuth::VerifyAuthentication(std::string &authToken, const std::string &authParam) @@ -106,6 +97,11 @@ int32_t PinAuth::CloseAuthInfo(const int32_t &pageId, std::shared_ptrClosePage(pageId, authManager); } +int32_t PinAuth::UpdateAuthInfo(const int32_t &pageId) +{ + return pinAuthUi_->UpdatePinDialog(pageId); +} + extern "C" IAuthentication *CreatePinAuthObject(void) { return new PinAuth; diff --git a/ext/pin_auth/src/standard/pin_auth_ui.cpp b/ext/pin_auth/src/standard/pin_auth_ui.cpp index 6e349747..09da2278 100644 --- a/ext/pin_auth/src/standard/pin_auth_ui.cpp +++ b/ext/pin_auth/src/standard/pin_auth_ui.cpp @@ -58,7 +58,7 @@ int32_t PinAuthUi::ShowPinDialog(int32_t code, std::shared_ptr au return DM_OK; } -int32_t PinAuthUi::InputPinDialog(int32_t code, std::shared_ptr authManager) +int32_t PinAuthUi::InputPinDialog(std::shared_ptr authManager) { LOGI("InputPinDialog start"); if (authManager == nullptr) { @@ -66,7 +66,7 @@ int32_t PinAuthUi::InputPinDialog(int32_t code, std::shared_ptr a return ERR_DM_FAILED; } nlohmann::json jsonObj; - jsonObj[PIN_CODE_KEY] = code; + jsonObj[VERIFY_FAILED] = false; jsonObj.dump(); const std::string params = jsonObj.dump(); @@ -76,13 +76,13 @@ int32_t PinAuthUi::InputPinDialog(int32_t code, std::shared_ptr a OHOS::Rosen::WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW, ACE_X, ACE_Y, ACE_WIDTH, ACE_HEIGHT, [authManager](int32_t id, const std::string& event, const std::string& params) { - if (params == EVENT_INIT_CODE) { + if (event == EVENT_INIT) { authManager->SetPageId(id); - } - if (params == EVENT_CANCEL_CODE || params == EVENT_CONFIRM_CODE) { - Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id); + } else if (event == EVENT_CONFIRM) { LOGI("CancelDialog start id:%d,event:%s,parms:%s", id, event.c_str(), params.c_str()); - authManager->VerifyAuthentication(params.c_str()); + authManager->AddMember(std::stoi(params)); + } else { + authManager->SetReasonAndFinish(ERR_DM_AUTH_INPUT_PARAMETER_FAILED, AuthState::AUTH_REQUEST_JOIN); } }); LOGI("ShowConfigDialog end"); @@ -97,6 +97,18 @@ int32_t PinAuthUi::ClosePage(const int32_t &pageId, std::shared_ptrUpdateDialog(pageId, params); + LOGI("UpdatePinDialog end"); + return DM_OK; +} + int32_t PinAuthUi::StartFaUiService(std::shared_ptr dmAbilityManager) { AbilityStatus status = dmAbilityManager->StartAbility(AbilityRole::ABILITY_ROLE_INITIATIVE); diff --git a/services/devicemanagerservice/include/authentication/auth_request_state.h b/services/devicemanagerservice/include/authentication/auth_request_state.h index 35d5cfde..647ce7db 100644 --- a/services/devicemanagerservice/include/authentication/auth_request_state.h +++ b/services/devicemanagerservice/include/authentication/auth_request_state.h @@ -131,23 +131,6 @@ public: int32_t Enter() override; }; -class AuthRequestInputState : public AuthRequestState { -public: - /** - * @tc.name: AuthRequestInputState::GetStateType - * @tc.desc: Get State Type of the AuthRequest Input State - * @tc.type: FUNC - */ - int32_t GetStateType() override; - - /** - * @tc.name: AuthRequestInputState::Enter - * @tc.desc: Enter of the AuthRequest Input State - * @tc.type: FUNC - */ - int32_t Enter() override; -}; - class AuthRequestJoinState : public AuthRequestState { public: /** diff --git a/services/devicemanagerservice/include/authentication/authentication.h b/services/devicemanagerservice/include/authentication/authentication.h index b5f8db04..30176837 100644 --- a/services/devicemanagerservice/include/authentication/authentication.h +++ b/services/devicemanagerservice/include/authentication/authentication.h @@ -28,6 +28,7 @@ public: virtual int32_t StartAuth(std::string &authToken, std::shared_ptr authManager) = 0; virtual int32_t VerifyAuthentication(std::string &authToken, const std::string &authParam) = 0; virtual int32_t CloseAuthInfo(const int32_t &pageId, std::shared_ptr authManager) = 0; + virtual int32_t UpdateAuthInfo(const int32_t &pageId) = 0; }; using CreateIAuthAdapterFuncPtr = IAuthentication *(*)(void); diff --git a/services/devicemanagerservice/include/authentication/dm_auth_manager.h b/services/devicemanagerservice/include/authentication/dm_auth_manager.h index c5f69a69..83726a5f 100644 --- a/services/devicemanagerservice/include/authentication/dm_auth_manager.h +++ b/services/devicemanagerservice/include/authentication/dm_auth_manager.h @@ -39,7 +39,6 @@ typedef enum AuthState { AUTH_REQUEST_NEGOTIATE, AUTH_REQUEST_NEGOTIATE_DONE, AUTH_REQUEST_REPLY, - AUTH_REQUEST_INPUT, AUTH_REQUEST_JOIN, AUTH_REQUEST_NETWORK, AUTH_REQUEST_FINISH, @@ -238,7 +237,7 @@ public: * @tc.desc: Add Member of the DeviceManager Authenticate Manager * @tc.type: FUNC */ - int32_t AddMember(const std::string &deviceId); + int32_t AddMember(int32_t pinCode); /** * @tc.name: DmAuthManager::GetConnectAddr @@ -367,11 +366,11 @@ public: int32_t SetPageId(int32_t pageId); /** - * @tc.name: DmAuthManager::SetReason + * @tc.name: DmAuthManager::SetReasonAndFinish * @tc.desc: Set Reason of the DeviceManager Authenticate Manager * @tc.type: FUNC */ - int32_t SetReason(int32_t reason, int32_t state); + int32_t SetReasonAndFinish(int32_t reason, int32_t state); private: std::shared_ptr softbusConnector_; @@ -388,6 +387,8 @@ private: std::shared_ptr dmAbilityMgr_; bool isCryptoSupport_ = false; bool isFinishOfLocal_ = true; + int32_t authTimes_ = 0; + std::shared_ptr authPtr_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/src/authentication/auth_request_state.cpp b/services/devicemanagerservice/src/authentication/auth_request_state.cpp index 71636718..94ea2544 100644 --- a/services/devicemanagerservice/src/authentication/auth_request_state.cpp +++ b/services/devicemanagerservice/src/authentication/auth_request_state.cpp @@ -122,23 +122,6 @@ int32_t AuthRequestReplyState::Enter() return DM_OK; } -int32_t AuthRequestInputState::GetStateType() -{ - return AuthState::AUTH_REQUEST_INPUT; -} - -int32_t AuthRequestInputState::Enter() -{ - LOGI("DmAuthManager::AuthRequestInputState"); - std::shared_ptr stateAuthManager = authManager_.lock(); - if (stateAuthManager == nullptr) { - LOGE("AuthRequestState::authManager_ null"); - return ERR_DM_FAILED; - } - stateAuthManager->ShowStartAuthDialog(); - return DM_OK; -} - int32_t AuthRequestJoinState::GetStateType() { return AuthState::AUTH_REQUEST_JOIN; @@ -152,7 +135,7 @@ int32_t AuthRequestJoinState::Enter() LOGE("AuthRequestState::authManager_ null"); return ERR_DM_FAILED; } - stateAuthManager->AddMember(context_->deviceId); + stateAuthManager->ShowStartAuthDialog(); return DM_OK; } diff --git a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp index d72dcab4..68653f69 100644 --- a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp +++ b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp @@ -40,6 +40,7 @@ const int32_t WAIT_NEGOTIATE_TIMEOUT = 10; const int32_t WAIT_REQUEST_TIMEOUT = 10; const int32_t CANCEL_PIN_CODE_DISPLAY = 1; const int32_t DEVICE_ID_HALF = 2; +const int32_t MAX_AUTH_TIMES = 3; DmAuthManager::DmAuthManager(std::shared_ptr softbusConnector, std::shared_ptr listener, @@ -84,6 +85,7 @@ int32_t DmAuthManager::AuthenticateDevice(const std::string &pkgName, int32_t au return ERR_DM_INPUT_PARAMETER_EMPTY; } + authPtr_ = authenticationMap_[authType]; if (timer_ == nullptr) { timer_ = std::make_shared(); } @@ -160,14 +162,8 @@ int32_t DmAuthManager::VerifyAuthentication(const std::string &authParam) LOGE("authResponseContext_ is not init"); return ERR_DM_AUTH_NOT_START; } - std::shared_ptr ptr; - if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end()) { - LOGE("DmAuthManager::authenticationMap_ is null"); - return ERR_DM_FAILED; - } timer_->DeleteTimer(INPUT_TIMEOUT_TASK); - ptr = authenticationMap_[authResponseContext_->authType]; - int32_t ret = ptr->VerifyAuthentication(authResponseContext_->authToken, authParam); + int32_t ret = authPtr_->VerifyAuthentication(authResponseContext_->authToken, authParam); switch (ret) { case DM_OK: authRequestState_->TransitionTo(std::make_shared()); @@ -314,7 +310,7 @@ void DmAuthManager::OnDataReceived(const int32_t sessionId, const std::string me void DmAuthManager::OnGroupCreated(int64_t requestId, const std::string &groupId) { - LOGI("DmAuthManager::OnGroupCreated start"); + LOGI("DmAuthManager::OnGroupCreated start group id %s", groupId.c_str()); if (authResponseState_ == nullptr) { LOGE("DmAuthManager::AuthenticateDevice end"); return; @@ -329,11 +325,11 @@ void DmAuthManager::OnGroupCreated(int64_t requestId, const std::string &groupId int32_t pinCode = GeneratePincode(); nlohmann::json jsonObj; - jsonObj[PIN_CODE_KEY] = pinCode; jsonObj[PIN_TOKEN] = authResponseContext_->token; jsonObj[QR_CODE_KEY] = GenerateGroupName(); jsonObj[NFC_CODE_KEY] = GenerateGroupName(); authResponseContext_->authToken = jsonObj.dump(); + LOGI("DmAuthManager::OnGroupCreated start group id %s", groupId.c_str()); authResponseContext_->groupId = groupId; authResponseContext_->code = pinCode; authMessageProcessor_->SetResponseContext(authResponseContext_); @@ -344,24 +340,31 @@ void DmAuthManager::OnGroupCreated(int64_t requestId, const std::string &groupId void DmAuthManager::OnMemberJoin(int64_t requestId, int32_t status) { - LOGI("DmAuthManager OnMemberJoin start"); + LOGI("DmAuthManager OnMemberJoin start authTimes %d", authTimes_); if (authRequestState_ != nullptr) { + authTimes_++; timer_->DeleteTimer(ADD_TIMEOUT_TASK); if (status != DM_OK || authResponseContext_->requestId != requestId) { - if (authRequestState_ != nullptr) { + if (authRequestState_ != nullptr && authTimes_ >= MAX_AUTH_TIMES) { authResponseContext_->state = AuthState::AUTH_REQUEST_JOIN; authRequestContext_->reason = ERR_DM_INPUT_PARAMETER_EMPTY; authRequestState_->TransitionTo(std::make_shared()); - return; + } else { + timer_->StartTimer(INPUT_TIMEOUT_TASK, INPUT_TIMEOUT, + [this] (std::string name) { + DmAuthManager::HandleAuthenticateTimeout(name); + }); + authPtr_->UpdateAuthInfo(authResponseContext_->pageId); } + } else { + authRequestState_->TransitionTo(std::make_shared()); } - authRequestState_->TransitionTo(std::make_shared()); } } void DmAuthManager::HandleAuthenticateTimeout(std::string name) { - LOGI("DmAuthManager::HandleAuthenticateTimeout start"); + LOGI("DmAuthManager::HandleAuthenticateTimeout start timer name %s", name.c_str()); if (authRequestState_ != nullptr && authRequestState_->GetStateType() != AuthState::AUTH_REQUEST_FINISH) { if (authResponseContext_ == nullptr) { authResponseContext_ = std::make_shared(); @@ -427,6 +430,8 @@ void DmAuthManager::RespNegotiate(const int32_t &sessionId) if (authentication == nullptr) { LOGE("DmAuthManager::AuthenticateDevice authType %d not support.", authResponseContext_->authType); authResponseContext_->reply = ERR_DM_UNSUPPORTED_AUTH_TYPE; + } else { + authPtr_ = authenticationMap_[authResponseContext_->authType]; } std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_RESP_NEGOTIATE); @@ -435,7 +440,7 @@ void DmAuthManager::RespNegotiate(const int32_t &sessionId) softbusConnector_->GetSoftbusSession()->SendData(sessionId, message); } authResponseContext_ = authResponseState_->GetAuthContext(); - if ((jsonObject[TAG_CRYPTO_SUPPORT] == true) && authResponseContext_->cryptoSupport) { + if (jsonObject[TAG_CRYPTO_SUPPORT] == true && authResponseContext_->cryptoSupport) { if (jsonObject[TAG_CRYPTO_NAME] == authResponseContext_->cryptoName && jsonObject[TAG_CRYPTO_VERSION] == authResponseContext_->cryptoVer) { isCryptoSupport_ = true; @@ -502,7 +507,7 @@ void DmAuthManager::StartRespAuthProcess() [this] (std::string name) { DmAuthManager::HandleAuthenticateTimeout(name); }); - authRequestState_->TransitionTo(std::make_shared()); + authRequestState_->TransitionTo(std::make_shared()); } else { LOGE("do not accept"); authResponseContext_->state = AuthState::AUTH_REQUEST_REPLY; @@ -520,19 +525,15 @@ int32_t DmAuthManager::CreateGroup() return DM_OK; } -int32_t DmAuthManager::AddMember(const std::string &deviceId) +int32_t DmAuthManager::AddMember(int32_t pinCode) { - LOGI("DmAuthManager::AddMember start"); - nlohmann::json jsonObj = nlohmann::json::parse(authResponseContext_->authToken, nullptr, false); - if (jsonObj.is_discarded()) { - LOGE("DecodeRequestAuth jsonStr error"); - return ERR_DM_FAILED; - } - + LOGI("DmAuthManager::AddMember start pin code %d", pinCode); + LOGI("DmAuthManager::AddMember start group id %s", authResponseContext_->groupId.c_str()); + timer_->DeleteTimer(INPUT_TIMEOUT_TASK); nlohmann::json jsonObject; jsonObject[TAG_GROUP_ID] = authResponseContext_->groupId; jsonObject[TAG_GROUP_NAME] = authResponseContext_->groupName; - jsonObject[PIN_CODE_KEY] = jsonObj[PIN_CODE_KEY]; + jsonObject[PIN_CODE_KEY] = pinCode; jsonObject[TAG_REQUEST_ID] = authResponseContext_->requestId; jsonObject[TAG_DEVICE_ID] = authResponseContext_->deviceId; std::string connectInfo = jsonObject.dump(); @@ -540,18 +541,10 @@ int32_t DmAuthManager::AddMember(const std::string &deviceId) [this] (std::string name) { DmAuthManager::HandleAuthenticateTimeout(name); }); - int32_t ret = hiChainConnector_->AddMember(deviceId, connectInfo); + int32_t ret = hiChainConnector_->AddMember(authRequestContext_->deviceId, connectInfo); if (ret != 0) { return ERR_DM_FAILED; } - LOGI("DmAuthManager::authRequestContext CancelDisplay start"); - std::shared_ptr ptr; - if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end()) { - LOGE("DmAuthManager::authenticationMap_ is null"); - return ERR_DM_FAILED; - } - ptr = authenticationMap_[authResponseContext_->authType]; - ptr->CloseAuthInfo(authResponseContext_->pageId, shared_from_this()); return DM_OK; } @@ -578,13 +571,7 @@ void DmAuthManager::AuthenticateFinish() LOGI("DmAuthManager::AuthenticateFinish start"); if (authResponseState_ != nullptr) { if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_FINISH) { - std::shared_ptr ptr; - if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end()) { - LOGE("DmAuthManager::authenticationMap_ is null"); - return ; - } - ptr = authenticationMap_[authResponseContext_->authType]; - ptr->CloseAuthInfo(authResponseContext_->pageId, shared_from_this()); + authPtr_->CloseAuthInfo(authResponseContext_->pageId, shared_from_this()); } if (isFinishOfLocal_) { authMessageProcessor_->SetResponseContext(authResponseContext_); @@ -596,6 +583,7 @@ void DmAuthManager::AuthenticateFinish() authResponseContext_ = nullptr; authResponseState_ = nullptr; authMessageProcessor_ = nullptr; + authPtr_ = nullptr; } else if (authRequestState_ != nullptr) { if (isFinishOfLocal_) { authMessageProcessor_->SetResponseContext(authResponseContext_); @@ -604,14 +592,8 @@ void DmAuthManager::AuthenticateFinish() } else { authRequestContext_->reason = authResponseContext_->reply; } - if (authResponseContext_->state == AuthState::AUTH_REQUEST_INPUT) { - std::shared_ptr ptr; - if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end()) { - LOGE("DmAuthManager::authenticationMap_ is null"); - return ; - } - ptr = authenticationMap_[authResponseContext_->authType]; - ptr->CloseAuthInfo(authResponseContext_->pageId, shared_from_this()); + if (authResponseContext_->state == AuthState::AUTH_REQUEST_JOIN) { + authPtr_->CloseAuthInfo(authResponseContext_->pageId, shared_from_this()); } listener_->OnAuthResult(authRequestContext_->hostPkgName, authRequestContext_->deviceId, authRequestContext_->token, authResponseContext_->state, authRequestContext_->reason); @@ -622,6 +604,8 @@ void DmAuthManager::AuthenticateFinish() authResponseContext_ = nullptr; authRequestState_ = nullptr; authMessageProcessor_ = nullptr; + authPtr_ = nullptr; + authTimes_ = 0; } LOGI("DmAuthManager::AuthenticateFinish complete"); } @@ -686,12 +670,8 @@ int32_t DmAuthManager::SetAuthResponseState(std::shared_ptr a int32_t DmAuthManager::GetPinCode() { - nlohmann::json jsonObj = nlohmann::json::parse(authResponseContext_->authToken, nullptr, false); - if (jsonObj.is_discarded()) { - LOGE("DecodeRequestAuth jsonStr error"); - return ERR_DM_FAILED; - } - return jsonObj[PIN_CODE_KEY]; + LOGI("ShowConfigDialog start add member pin code %d", authResponseContext_->code); + return authResponseContext_->code; } void DmAuthManager::ShowConfigDialog() @@ -711,27 +691,17 @@ void DmAuthManager::ShowConfigDialog() void DmAuthManager::ShowAuthInfoDialog() { - LOGI("DmAuthManager::ShowAuthInfoDialog start"); - std::shared_ptr ptr; - if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end()) { - LOGE("DmAuthManager::authenticationMap_ is null"); - return; - } - ptr = authenticationMap_[authResponseContext_->authType]; - LOGI("ShowAuthInfoDialog authToken:%s", authResponseContext_->authToken.c_str()); - ptr->ShowAuthInfo(authResponseContext_->authToken, shared_from_this()); + LOGI("DmAuthManager::ShowAuthInfoDialog start %d", authResponseContext_->code); + nlohmann::json jsonObj; + jsonObj[PIN_CODE_KEY] = authResponseContext_->code; + std::string authParam = jsonObj.dump(); + authPtr_->ShowAuthInfo(authParam, shared_from_this()); } void DmAuthManager::ShowStartAuthDialog() { LOGI("DmAuthManager::ShowStartAuthDialog start"); - std::shared_ptr ptr; - if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end()) { - LOGE("DmAuthManager::authenticationMap_ is null"); - return; - } - ptr = authenticationMap_[authResponseContext_->authType]; - ptr->StartAuth(authResponseContext_->authToken, shared_from_this()); + authPtr_->StartAuth(authResponseContext_->authToken, shared_from_this()); } int32_t DmAuthManager::GetAuthenticationParam(DmAuthParam &authParam) @@ -745,7 +715,7 @@ int32_t DmAuthManager::GetAuthenticationParam(DmAuthParam &authParam) LOGE("Authenticate is not start"); return ERR_DM_AUTH_NOT_START; } - + dmAbilityMgr_->StartAbilityDone(); AbilityRole role = dmAbilityMgr_->GetAbilityRole(); authParam.direction = (int32_t)role; @@ -835,17 +805,20 @@ int32_t DmAuthManager::SetPageId(int32_t pageId) return DM_OK; } -int32_t DmAuthManager::SetReason(int32_t reason, int32_t state) +int32_t DmAuthManager::SetReasonAndFinish(int32_t reason, int32_t state) { if (authResponseContext_ == nullptr) { LOGE("Authenticate is not start"); return ERR_DM_AUTH_NOT_START; } - if (state < AuthState::AUTH_REQUEST_FINISH) { - authRequestContext_->reason = reason; - } authResponseContext_->state = state; authResponseContext_->reply = reason; + if (authRequestState_ != nullptr && authRequestState_->GetStateType() != AuthState::AUTH_REQUEST_FINISH) { + authRequestContext_->reason = reason; + authRequestState_->TransitionTo(std::make_shared()); + } else if (authResponseState_ != nullptr && authResponseState_->GetStateType() != AuthState::AUTH_RESPONSE_FINISH) { + authResponseState_->TransitionTo(std::make_shared()); + } return DM_OK; } } // namespace DistributedHardware diff --git a/test/unittest/UTTest_auth_request_state.cpp b/test/unittest/UTTest_auth_request_state.cpp index 9aad1816..65bcc212 100644 --- a/test/unittest/UTTest_auth_request_state.cpp +++ b/test/unittest/UTTest_auth_request_state.cpp @@ -439,57 +439,6 @@ HWTEST_F(AuthRequestStateTest, Enter_008, testing::ext::TestSize.Level0) ASSERT_EQ(ret, DM_OK); } -/** - * @tc.name: AuthRequestInputState::GetStateType_005 - * @tc.desc: 1 call AuthRequestInputState::GetStateType - * 2 check ret is AuthState::AUTH_REQUEST_INPUT - * @tc.type: FUNC - * @tc.require: AR000GHSJK - */ -HWTEST_F(AuthRequestStateTest, GetStateType_005, testing::ext::TestSize.Level0) -{ - std::shared_ptr authRequestState = std::make_shared(); - int32_t ret = authRequestState->GetStateType(); - ASSERT_EQ(ret, AuthState::AUTH_REQUEST_INPUT); -} - -/** - * @tc.name: AuthRequestInputState::Enter_009 - * @tc.desc: 1 set authManager to null - * 2 call AuthRequestInputState::Enter with authManager = null - * 3 check ret is ERR_DM_FAILED - * @tc.type: FUNC - * @tc.require: AR000GHSJK - */ -HWTEST_F(AuthRequestStateTest, Enter_009, testing::ext::TestSize.Level0) -{ - std::shared_ptr authManager = - std::make_shared(softbusConnector, listener, hiChainConnector); - std::shared_ptr authRequestState = std::make_shared(); - authRequestState->SetAuthManager(nullptr); - int32_t ret = authRequestState->Enter(); - ASSERT_EQ(ret, ERR_DM_FAILED); -} - -/** - * @tc.name: AuthRequestInputState::Enter_010 - * @tc.desc: 1 set authManager not null - * 2 call AuthRequestInputState::Enter with authManager != null - * 3 check ret is DM_OK - * @tc.type: FUNC - * @tc.require: AR000GHSJK - */ -HWTEST_F(AuthRequestStateTest, Enter_010, testing::ext::TestSize.Level0) -{ - std::shared_ptr authManager = - std::make_shared(softbusConnector, listener, hiChainConnector); - authManager->authResponseContext_ = std::make_shared(); - std::shared_ptr authRequestState = std::make_shared(); - authRequestState->SetAuthManager(authManager); - int32_t ret = authRequestState->Enter(); - ASSERT_EQ(ret, DM_OK); -} - /** * @tc.name: AuthRequestJoinState::GetStateType_006 * @tc.desc: 1 call AuthRequestJoinState::GetStateType diff --git a/test/unittest/UTTest_dm_auth_manager.cpp b/test/unittest/UTTest_dm_auth_manager.cpp index 764c8322..a6c76070 100644 --- a/test/unittest/UTTest_dm_auth_manager.cpp +++ b/test/unittest/UTTest_dm_auth_manager.cpp @@ -208,10 +208,10 @@ HWTEST_F(DmAuthManagerTest, AddMember_001, testing::ext::TestSize.Level0) authManager->authResponseContext_->code = 123; authManager->authResponseContext_->requestId = 234; authManager->authResponseContext_->deviceId = "234"; - std::string deviceId = "44444"; + int32_t pinCode = 444444; authManager->hiChainConnector_->RegisterHiChainCallback(authManager); authManager->SetAuthResponseState(authResponseState); - int32_t ret = authManager->AddMember(deviceId); + int32_t ret = authManager->AddMember(pinCode); ASSERT_EQ(ret, ERR_DM_FAILED); } diff --git a/test/unittest/UTTest_pin_auth_ui.cpp b/test/unittest/UTTest_pin_auth_ui.cpp index 448854f0..b1055473 100644 --- a/test/unittest/UTTest_pin_auth_ui.cpp +++ b/test/unittest/UTTest_pin_auth_ui.cpp @@ -86,8 +86,7 @@ HWTEST_F(PinAuthUiTest, ShowPinDialog_002, testing::ext::TestSize.Level0) HWTEST_F(PinAuthUiTest, InputPinDialog_001, testing::ext::TestSize.Level0) { std::shared_ptr pinAuthUi = std::make_shared(); - int32_t code = 123456; - int32_t ret = pinAuthUi->InputPinDialog(code, nullptr); + int32_t ret = pinAuthUi->InputPinDialog(nullptr); ASSERT_EQ(ret, DM_OK); } @@ -100,8 +99,7 @@ HWTEST_F(PinAuthUiTest, InputPinDialog_001, testing::ext::TestSize.Level0) HWTEST_F(PinAuthUiTest, InputPinDialog_002, testing::ext::TestSize.Level0) { std::shared_ptr pinAuthUi = std::make_shared(); - int32_t code = 123456; - int32_t ret = pinAuthUi->InputPinDialog(code, authManager); + int32_t ret = pinAuthUi->InputPinDialog(authManager); ASSERT_EQ(ret, DM_OK); } } diff --git a/test/unittest/auth_request_state.cpp b/test/unittest/auth_request_state.cpp index 71636718..d3fdd78e 100644 --- a/test/unittest/auth_request_state.cpp +++ b/test/unittest/auth_request_state.cpp @@ -122,23 +122,6 @@ int32_t AuthRequestReplyState::Enter() return DM_OK; } -int32_t AuthRequestInputState::GetStateType() -{ - return AuthState::AUTH_REQUEST_INPUT; -} - -int32_t AuthRequestInputState::Enter() -{ - LOGI("DmAuthManager::AuthRequestInputState"); - std::shared_ptr stateAuthManager = authManager_.lock(); - if (stateAuthManager == nullptr) { - LOGE("AuthRequestState::authManager_ null"); - return ERR_DM_FAILED; - } - stateAuthManager->ShowStartAuthDialog(); - return DM_OK; -} - int32_t AuthRequestJoinState::GetStateType() { return AuthState::AUTH_REQUEST_JOIN;