diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index 72ee5cbc..1ef1c554 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -152,6 +152,7 @@ const int32_t LOCAL_CREDENTIAL_DEAL_TYPE = 1; const int32_t REMOTE_CREDENTIAL_DEAL_TYPE = 2; const int32_t NONSYMMETRY_CREDENTIAL_TYPE = 2; const int32_t SYMMETRY_CREDENTIAL_TYPE = 1; +const int32_t UNKNOWN_CREDENTIAL_TYPE = 0; // Softbus const int32_t SOFTBUS_CHECK_INTERVAL = 100000; // 100ms const uint32_t SOFTBUS_SUBSCRIBE_ID_PREFIX_LEN = 16; @@ -222,6 +223,7 @@ const std::string FIELD_PROCESS_TYPE = "processType"; const std::string FIELD_AUTH_TYPE = "authType"; const std::string FIELD_CREDENTIAL_DATA = "credentialData"; const std::string FIELD_CREDENTIAL_ID = "credentialId"; +const std::string FIELD_PEER_CREDENTIAL_INFO = "peerCredentialInfo"; const int32_t SAME_ACCOUNT_TYPE = 1; const int32_t CROSS_ACCOUNT_TYPE = 2; const int32_t PIN_CODE_NETWORK = 0; diff --git a/services/implementation/include/credential/dm_credential_manager.h b/services/implementation/include/credential/dm_credential_manager.h index 0cab7bad..845ff6bb 100644 --- a/services/implementation/include/credential/dm_credential_manager.h +++ b/services/implementation/include/credential/dm_credential_manager.h @@ -74,12 +74,19 @@ public: int32_t ImportLocalCredential(const std::string &credentialInfo); /** - * @tc.name: HiChainConnector::ImportRemoteCredential + * @tc.name: DmCredentialManager::ImportRemoteCredential * @tc.desc: Import Symmetry Credential Info of the DmCredential Manager * @tc.type: FUNC */ int32_t ImportRemoteCredential(const std::string &credentialInfo); + /** + * @tc.name: DmCredentialManager::DeleteRemoteCredential + * @tc.desc: delete Symmetry Credential Info of the DmCredential Manager + * @tc.type: FUNC + */ + int32_t DeleteRemoteCredential(const std::string &credentialInfo); + /** * @tc.name: HiChainConnector::DeleteCredential * @tc.desc: Delete Credential Info of the DmCredential Manager @@ -103,6 +110,7 @@ private: private: int32_t GetCredentialData(const std::string &credentialInfo, const CredentialData &inputCreData, nlohmann::json &jsonOutObj); + int32_t GetAddDeviceList(const nlohmann::json &jsonObject, nlohmann::json &jsonDeviceList); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/include/dependency/hichain/hichain_connector.h b/services/implementation/include/dependency/hichain/hichain_connector.h index 522f8377..d3d9213f 100644 --- a/services/implementation/include/dependency/hichain/hichain_connector.h +++ b/services/implementation/include/dependency/hichain/hichain_connector.h @@ -175,6 +175,20 @@ public: */ int32_t getRegisterInfo(const std::string &queryParams, std::string &returnJsonStr); + /** + * @tc.name: HiChainConnector::addMultiMembers + * @tc.desc: Get RegisterInfo Info of the HiChain Connector + * @tc.type: FUNC + */ + int32_t addMultiMembers(const int32_t groupType, const std::string &userId, + const nlohmann::json &jsonDeviceList); + /** + * @tc.name: HiChainConnector::deleteMultiMembers + * @tc.desc: Get RegisterInfo Info of the HiChain Connector + * @tc.type: FUNC + */ + int32_t deleteMultiMembers(const int32_t groupType, const std::string &userId, + const nlohmann::json &jsonDeviceList); private: int64_t GenRequestId(); int32_t SyncGroups(std::string deviceId, std::vector &remoteGroupIdList); @@ -187,7 +201,7 @@ private: bool IsGroupInfoInvalid(GroupInfo &group); int32_t GetStrFieldByType(const std::string &reqJsonStr, const std::string &outField, int32_t type); int32_t GetNumsFieldByType(const std::string &reqJsonStr, int32_t &outField, int32_t type); - + int32_t GetGroupId(const std::string &userId, const int32_t groupType, std::string &groupId); private: const DeviceGroupManager *deviceGroupManager_ = nullptr; DeviceAuthCallback deviceAuthCallback_; diff --git a/services/implementation/src/credential/dm_credential_manager.cpp b/services/implementation/src/credential/dm_credential_manager.cpp index b6b7a3b7..b276ea20 100644 --- a/services/implementation/src/credential/dm_credential_manager.cpp +++ b/services/implementation/src/credential/dm_credential_manager.cpp @@ -24,6 +24,24 @@ namespace OHOS { namespace DistributedHardware { +struct CredentialDataInfo { + int32_t credentialType; + std::string credentailId; + std::string serverPk; + std::string pkInfoSignature; + std::string pkInfo; + std::string authCode; + std::string peerDeviceId; + std::string userId; + CredentialDataInfo() : credentialType(UNKNOWN_CREDENTIAL_TYPE) + { + } +}; + +struct PeerCredentialInfo { + std::string peerDeviceId; + std::string peerCredentialId; +}; void from_json(const nlohmann::json &jsonObject, CredentialData &credentialData) { if (!jsonObject.contains(FIELD_CREDENTIAL_TYPE) || !jsonObject.contains(FIELD_CREDENTIAL_ID) || @@ -100,7 +118,7 @@ int32_t DmCredentialManager::ImportCredential(const std::string &pkgName, const if (processType == LOCAL_CREDENTIAL_DEAL_TYPE) { return ImportLocalCredential(credentialInfo); } else if (processType == REMOTE_CREDENTIAL_DEAL_TYPE) { - return DM_OK; + return ImportRemoteCredential(credentialInfo); } else { LOGE("credential type error!"); } @@ -185,7 +203,7 @@ int32_t DmCredentialManager::DeleteCredential(const std::string &pkgName, const if (processType == LOCAL_CREDENTIAL_DEAL_TYPE) { return hiChainConnector_->DeleteGroup(requestId_, userId, authType); } else if (processType == REMOTE_CREDENTIAL_DEAL_TYPE) { - return DM_OK; + return DeleteRemoteCredential(deleteInfo); } else { LOGE("credential type error!"); } @@ -267,5 +285,193 @@ int32_t DmCredentialManager::GetCredentialData(const std::string &credentialInfo jsonOutObj = jsonCreObj; return DM_OK; } + +void from_json(const nlohmann::json &jsonObject, CredentialDataInfo &credentialDataInfo) +{ + if (!jsonObject.contains(FIELD_CREDENTIAL_TYPE)) { + LOGE("credentialType json key not exist"); + return; + } + jsonObject[FIELD_CREDENTIAL_TYPE].get_to(credentialDataInfo.credentialType); + if (jsonObject.contains(FIELD_CREDENTIAL_ID)) { + jsonObject[FIELD_CREDENTIAL_ID].get_to(credentialDataInfo.credentailId); + } + if (credentialDataInfo.credentialType == NONSYMMETRY_CREDENTIAL_TYPE) { + if (jsonObject.contains(FIELD_SERVER_PK)) { + jsonObject[FIELD_SERVER_PK].get_to(credentialDataInfo.serverPk); + } + if (jsonObject.contains(FIELD_PKINFO_SIGNATURE)) { + jsonObject[FIELD_PKINFO_SIGNATURE].get_to(credentialDataInfo.pkInfoSignature); + } + if (jsonObject.contains(FIELD_PKINFO)) { + nlohmann::json jsonPkInfo = jsonObject[FIELD_PKINFO]; + credentialDataInfo.pkInfo = jsonPkInfo.dump().c_str(); + } + } else if (credentialDataInfo.credentialType == SYMMETRY_CREDENTIAL_TYPE) { + if (jsonObject.contains(FIELD_AUTH_CODE)) { + jsonObject[FIELD_AUTH_CODE].get_to(credentialDataInfo.authCode); + } + } else { + LOGE("credentialType john key is unknown"); + return; + } + if (jsonObject.contains(FIELD_PEER_DEVICE_ID)) { + jsonObject[FIELD_PEER_DEVICE_ID].get_to(credentialDataInfo.peerDeviceId); + } +} + +void to_json(nlohmann::json &jsonObject, const CredentialDataInfo &credentialDataInfo) +{ + jsonObject[FIELD_DEVICE_ID] = credentialDataInfo.peerDeviceId; + jsonObject[FIELD_UDID] =credentialDataInfo.peerDeviceId; + jsonObject[FIELD_USER_ID] = credentialDataInfo.userId; + jsonObject[FIELD_CREDENTIAL_TYPE] = credentialDataInfo.credentialType; + jsonObject[FIELD_CREDENTIAL_ID] = atoi(credentialDataInfo.credentailId.c_str()); + if (credentialDataInfo.credentialType == NONSYMMETRY_CREDENTIAL_TYPE) { + jsonObject[FIELD_SERVER_PK] = credentialDataInfo.serverPk; + jsonObject[FIELD_PKINFO_SIGNATURE] = credentialDataInfo.pkInfoSignature; + jsonObject[FIELD_PKINFO] = credentialDataInfo.pkInfo; + } else if (credentialDataInfo.credentialType == SYMMETRY_CREDENTIAL_TYPE) { + jsonObject[FIELD_AUTH_CODE] = credentialDataInfo.authCode; + } +} + +int32_t DmCredentialManager::GetAddDeviceList(const nlohmann::json &jsonObject, nlohmann::json &jsonDeviceList) +{ + if (!jsonObject.contains(FIELD_CREDENTIAL_DATA) || !jsonObject.contains(FIELD_AUTH_TYPE)) { + LOGE("credentaildata or authType string key not exist!"); + return ERR_DM_FAILED; + } + nlohmann::json credentialJson = jsonObject[FIELD_CREDENTIAL_DATA]; + auto credentialDataList = credentialJson.get>(); + int32_t authType = jsonObject[FIELD_AUTH_TYPE]; + + for (auto &credentialData : credentialDataList) { + if (authType == SAME_ACCOUNT_TYPE) { + if (jsonObject.contains(FIELD_USER_ID)) { + credentialData.userId = jsonObject[FIELD_USER_ID]; + } + } else if (authType == CROSS_ACCOUNT_TYPE) { + if (jsonObject.contains(FIELD_PEER_USER_ID)) { + credentialData.userId = jsonObject[FIELD_PEER_USER_ID]; + } + } + } + for (size_t i = 0; i < credentialDataList.size(); i++) { + jsonDeviceList[FIELD_DEVICE_LIST][i] = credentialDataList.at(i); + } + return DM_OK; +} + +int32_t DmCredentialManager::ImportRemoteCredential(const std::string &credentialInfo) +{ + nlohmann::json jsonObject = nlohmann::json::parse(credentialInfo, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("credentialInfo string not a json type."); + return ERR_DM_FAILED; + } + if (!jsonObject.contains(FIELD_AUTH_TYPE) || !jsonObject.contains(FIELD_CREDENTIAL_DATA)) { + LOGE("auth type, credentialData string key not exist!"); + return ERR_DM_FAILED; + } + int32_t authType = jsonObject[FIELD_AUTH_TYPE]; + std::string userId; + int32_t groupType = 0; + if (authType == SAME_ACCOUNT_TYPE) { + groupType = IDENTICAL_ACCOUNT_GROUP; + if (!jsonObject.contains(FIELD_USER_ID)) { + LOGE("userId string key not exist!"); + return ERR_DM_FAILED; + } else { + userId = jsonObject[FIELD_USER_ID]; + } + } else if (authType == CROSS_ACCOUNT_TYPE) { + groupType = ACROSS_ACCOUNT_AUTHORIZE_GROUP; + if (!jsonObject.contains(FIELD_PEER_USER_ID)) { + LOGE("peerUserId string key not exist!"); + return ERR_DM_FAILED; + } else { + userId = jsonObject[FIELD_PEER_USER_ID]; + } + } + nlohmann::json jsonDeviceList; + if (GetAddDeviceList(jsonObject, jsonDeviceList) != DM_OK) { + LOGE("failed to get add DeviceList."); + return ERR_DM_FAILED; + } + if (hiChainConnector_->addMultiMembers(groupType, userId, jsonDeviceList) != DM_OK) { + LOGE("failed to add members to group."); + return ERR_DM_FAILED; + } + return DM_OK; +} + +void from_json(const nlohmann::json &jsonObject, PeerCredentialInfo &peerCredentialInfo) +{ + if (jsonObject.contains(FIELD_PEER_USER_ID)) { + jsonObject[FIELD_PEER_USER_ID].get_to(peerCredentialInfo.peerDeviceId); + } +} + +void to_json(nlohmann::json &jsonObject, const PeerCredentialInfo &peerCredentialInfo) +{ + jsonObject[FIELD_DEVICE_ID] = peerCredentialInfo.peerDeviceId; +} + +int32_t GetDeleteDeviceList(const nlohmann::json &jsonObject, nlohmann::json &deviceList) +{ + if (!jsonObject.contains(FIELD_PEER_CREDENTIAL_INFO)) { + LOGE("devicelist string key not exist!"); + return ERR_DM_FAILED; + } + auto peerCredentialInfo = jsonObject[FIELD_PEER_CREDENTIAL_INFO].get>(); + for (size_t i = 0; i < peerCredentialInfo.size(); i++) { + deviceList[FIELD_DEVICE_LIST][i] = peerCredentialInfo[i]; + } + return DM_OK; +} + +int32_t DmCredentialManager::DeleteRemoteCredential(const std::string &deleteInfo) +{ + nlohmann::json jsonObject = nlohmann::json::parse(deleteInfo, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("credentialInfo string not a json type."); + return ERR_DM_FAILED; + } + if (!jsonObject.contains(FIELD_AUTH_TYPE) || !jsonObject.contains(FIELD_PEER_CREDENTIAL_INFO)) { + LOGE("authType, peerCredential or peerUserId string key not exist!"); + return ERR_DM_FAILED; + } + int32_t authType = jsonObject[FIELD_AUTH_TYPE]; + std::string userId; + int32_t groupType = 0; + if (authType == SAME_ACCOUNT_TYPE) { + if (!jsonObject.contains(FIELD_USER_ID)) { + LOGE("userId string key not exist."); + return ERR_DM_FAILED; + } else { + userId = jsonObject[FIELD_USER_ID]; + } + groupType = IDENTICAL_ACCOUNT_GROUP; + } else if (authType == CROSS_ACCOUNT_TYPE) { + if (!jsonObject.contains(FIELD_PEER_USER_ID)) { + LOGE("peerUserId string key not exist."); + return ERR_DM_FAILED; + } else { + userId = jsonObject[FIELD_PEER_USER_ID]; + } + groupType = ACROSS_ACCOUNT_AUTHORIZE_GROUP; + } + nlohmann::json jsonDeviceList; + if (GetDeleteDeviceList(jsonObject, jsonDeviceList) != DM_OK) { + LOGE("failed to get delete DeviceList."); + return ERR_DM_FAILED; + } + if (hiChainConnector_->deleteMultiMembers(groupType, userId, jsonDeviceList) != DM_OK) { + LOGE("failed to delete members from group."); + return ERR_DM_FAILED; + } + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/dependency/hichain/hichain_connector.cpp b/services/implementation/src/dependency/hichain/hichain_connector.cpp index 9be5f55c..c84baca0 100644 --- a/services/implementation/src/dependency/hichain/hichain_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_connector.cpp @@ -753,5 +753,98 @@ int32_t HiChainConnector::getRegisterInfo(const std::string &queryParams, std::s LOGI("request hichain device registerinfo successfully."); return DM_OK; } + +int32_t HiChainConnector::GetGroupId(const std::string &userId, const int32_t groupType, std::string &groupId) +{ + nlohmann::json jsonObjGroup; + jsonObjGroup[FIELD_GROUP_TYPE] = groupType; + std::string queryParams = jsonObjGroup.dump(); + std::vector groupList; + + if (!GetGroupInfo(queryParams.c_str(), groupList)) { + LOGE("failed to get device join groups"); + return ERR_DM_FAILED; + } + for (auto &groupinfo : groupList) { + LOGI("groupinfo.groupId:%s", groupinfo.groupId.c_str()); + if (groupinfo.userId == userId) { + groupId = groupinfo.groupId; + break; + } + } + return DM_OK; +} + +int32_t HiChainConnector::addMultiMembers(const int32_t groupType, const std::string &userId, + const nlohmann::json &jsonDeviceList) +{ + if (deviceGroupManager_ == nullptr) { + LOGE("HiChainConnector::deviceGroupManager_ is nullptr."); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + if (userId.empty() || !jsonDeviceList.contains(FIELD_DEVICE_LIST)) { + LOGE("userId or deviceList is empty"); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + std::string groupId; + if (GetGroupId(userId, groupType, groupId) != DM_OK) { + LOGE("failed to get groupid"); + return ERR_DM_FAILED; + } + std::string appId = DM_PKG_NAME; + nlohmann::json jsonObj; + jsonObj[FIELD_GROUP_ID] = groupId; + jsonObj[FIELD_GROUP_TYPE] = groupType; + jsonObj[FIELD_DEVICE_LIST] = jsonDeviceList[FIELD_DEVICE_LIST]; + std::string addParams = jsonObj.dump(); + int32_t osAccountUserId = MultipleUserConnector::GetCurrentAccountUserID(); + if (osAccountUserId < 0) { + LOGE("get current process account user id failed"); + return ERR_DM_FAILED; + } + int32_t ret = deviceGroupManager_->addMultiMembersToGroup(osAccountUserId, + appId.c_str(), addParams.c_str()); + if (ret!= DM_OK) { + LOGE("HiChainConnector::addMultiMemberstoGroup failure! ret=%d", ret); + return ret; + } + return DM_OK; +} + +int32_t HiChainConnector::deleteMultiMembers(const int32_t groupType, const std::string &userId, + const nlohmann::json &jsonDeviceList) +{ + if (deviceGroupManager_ == nullptr) { + LOGE("HiChainConnector::deviceGroupManager_ is nullptr."); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + if (userId.empty() || !jsonDeviceList.contains(FIELD_DEVICE_LIST)) { + LOGE("jsonDeviceList userId or deleteInfo string is empty"); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + std::string groupId; + if (GetGroupId(userId, groupType, groupId) != DM_OK) { + LOGE("failed to get groupid"); + return ERR_DM_FAILED; + } + std::string appId = DM_PKG_NAME; + nlohmann::json jsonObj; + jsonObj[FIELD_GROUP_ID] = groupId; + jsonObj[FIELD_GROUP_TYPE] = groupType; + jsonObj[FIELD_DEVICE_LIST] = jsonDeviceList[FIELD_DEVICE_LIST]; + std::string deleteParams = jsonObj.dump(); + int32_t osAccountUserId = MultipleUserConnector::GetCurrentAccountUserID(); + if (osAccountUserId < 0) { + LOGE("get current process account user id failed"); + return ERR_DM_FAILED; + } + int32_t ret = deviceGroupManager_->delMultiMembersFromGroup(osAccountUserId, + appId.c_str(), deleteParams.c_str()); + if (ret != DM_OK) { + LOGE("HiChainConnector::deleteMultiMembers failure!, ret=%d", ret); + return ret; + } + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/mock/device_auth.h b/test/unittest/mock/device_auth.h index 5ca6d5c5..3d1a6a44 100644 --- a/test/unittest/mock/device_auth.h +++ b/test/unittest/mock/device_auth.h @@ -67,6 +67,7 @@ #define FIELD_IS_DELETE_ALL "isDeleteAll" #define FIELD_BLE_CHALLENGE "bleChallenge" #define FIELD_OS_ACCOUNT_ID "osAccountId" +#define FIELD_DEVICE_LIST "deviceList" using OsAccountEnum = enum _OsAccountEnum : int32_t { DEFAULT_OS_ACCOUNT = 0, @@ -167,6 +168,8 @@ using DeviceGroupManager = struct _DeviceGroupManager { int32_t (*deleteMemberFromGroup)(int32_t osAccountId, int64_t requestId, const char *appId, const char *deleteParams); int32_t (*processData)(int64_t requestId, const uint8_t *data, uint32_t dataLen); + int32_t (*addMultiMembersToGroup)(int32_t osAccountId, const char *appId, const char *addParams); + int32_t (*delMultiMembersFromGroup)(int32_t osAccountId, const char *appId, const char *deleteParams); int32_t (*confirmRequest)(int32_t osAccountId, int64_t requestId, const char *appId, const char *confirmParams); int32_t (*bindPeer)(int64_t requestId, const char *appId, const char *bindParams); int32_t (*unbindPeer)(int64_t requestId, const char *appId, const char *unbindParams);