Add check of userId when calling addMultiMembers interface

Signed-off-by: fuzikun <fuzikun@huawei.com>
This commit is contained in:
fuzikun
2022-07-12 16:05:24 +08:00
parent 16b43f5942
commit c1928749ca
2 changed files with 107 additions and 21 deletions
@@ -256,14 +256,14 @@ static int32_t CheckCreateParams(int32_t osAccountId, const CJson *jsonParams)
return HC_SUCCESS;
}
static int32_t GenerateAddTokenParams(const CJson *jsonParams, CJson *addParams)
static int32_t GenerateAddTokenParams(const CJson *deviceInfo, CJson *addParams)
{
const char *userId = GetStringFromJson(jsonParams, FIELD_USER_ID);
const char *userId = GetStringFromJson(deviceInfo, FIELD_USER_ID);
if (userId == NULL) {
LOGE("Failed to get userId from json!");
return HC_ERR_JSON_GET;
}
const char *deviceId = GetStringFromJson(jsonParams, FIELD_DEVICE_ID);
const char *deviceId = GetStringFromJson(deviceInfo, FIELD_DEVICE_ID);
if (deviceId == NULL) {
LOGE("Failed to get deviceId from json!");
return HC_ERR_JSON_GET;
@@ -385,9 +385,9 @@ static int32_t GenerateTrustedDevParams(const CJson *jsonParams, const char *gro
return HC_SUCCESS;
}
static int32_t CheckPeerDeviceNotSelf(const CJson *jsonParams)
static int32_t CheckPeerDeviceNotSelf(const CJson *deviceInfo)
{
const char *udid = GetStringFromJson(jsonParams, FIELD_UDID);
const char *udid = GetStringFromJson(deviceInfo, FIELD_UDID);
if (udid == NULL) {
LOGE("Failed to get udid from json!");
return HC_ERR_JSON_GET;
@@ -395,13 +395,8 @@ static int32_t CheckPeerDeviceNotSelf(const CJson *jsonParams)
return AssertPeerDeviceNotSelf(udid);
}
static int32_t AddDeviceAndToken(int32_t osAccountId, CJson *jsonParams, CJson *deviceInfo)
static int32_t AddDeviceAndToken(int32_t osAccountId, const CJson *jsonParams, CJson *deviceInfo)
{
int32_t res = CheckPeerDeviceNotSelf(jsonParams);
if (res != HC_SUCCESS) {
LOGE("The peer device udid is equals to the local udid!");
return res;
}
const char *groupId = GetStringFromJson(jsonParams, FIELD_GROUP_ID);
if (groupId == NULL) {
LOGE("Failed to get groupId from json!");
@@ -412,7 +407,7 @@ static int32_t AddDeviceAndToken(int32_t osAccountId, CJson *jsonParams, CJson *
LOGE("Failed to get credential from json!");
return HC_ERR_JSON_GET;
}
res = GenerateAddTokenParams(deviceInfo, credential);
int32_t res = GenerateAddTokenParams(deviceInfo, credential);
if (res != HC_SUCCESS) {
return res;
}
@@ -485,6 +480,51 @@ static int32_t AddGroupAndLocalDev(int32_t osAccountId, CJson *jsonParams, const
return res;
}
static int32_t CheckUserIdValid(int32_t osAccountId, const CJson *jsonParams, const CJson *deviceInfo)
{
const char *userId = GetStringFromJson(deviceInfo, FIELD_USER_ID);
if (userId == NULL) {
LOGE("Failed to get userId from json!");
return HC_ERR_JSON_GET;
}
const char *groupId = GetStringFromJson(jsonParams, FIELD_GROUP_ID);
if (groupId == NULL) {
LOGE("Failed to get groupId from json!");
return HC_ERR_JSON_GET;
}
uint32_t index;
TrustedGroupEntry **entry = NULL;
GroupEntryVec groupEntryVec = CreateGroupEntryVec();
QueryGroupParams params = InitQueryGroupParams();
params.groupId = groupId;
params.groupType = ACROSS_ACCOUNT_AUTHORIZE_GROUP;
if (QueryGroups(osAccountId, &params, &groupEntryVec) != HC_SUCCESS) {
LOGE("Failed to query groups!");
ClearGroupEntryVec(&groupEntryVec);
return HC_ERR_DB;
}
FOR_EACH_HC_VECTOR(groupEntryVec, index, entry) {
if ((entry != NULL) && (*entry != NULL) && (strcmp(userId, StringGet(&(*entry)->sharedUserId)) == 0)) {
ClearGroupEntryVec(&groupEntryVec);
return HC_SUCCESS;
}
}
LOGE("The input userId is inconsistent with the sharedUserId!");
ClearGroupEntryVec(&groupEntryVec);
return HC_ERR_INVALID_PARAMS;
}
static int32_t CheckDeviceInfoValid(int32_t osAccountId, const CJson *jsonParams, const CJson *deviceInfo)
{
int32_t res = CheckPeerDeviceNotSelf(deviceInfo);
if (res != HC_SUCCESS) {
LOGE("The peer device udid is equals to the local udid!");
return res;
}
/* Across account group: input userId must be consistent with the sharedUserId. */
return CheckUserIdValid(osAccountId, jsonParams, deviceInfo);
}
static int32_t CreateGroup(int32_t osAccountId, CJson *jsonParams, char **returnJsonStr)
{
LOGI("[Start]: Start to create a across account group!");
@@ -550,6 +590,9 @@ static int32_t AddMultiMembersToGroup(int32_t osAccountId, const char *appId, CJ
LOGE("The deviceInfo is NULL!");
continue;
}
if (CheckDeviceInfoValid(osAccountId, jsonParams, deviceInfo) != HC_SUCCESS) {
continue;
}
if (AddDeviceAndToken(osAccountId, jsonParams, deviceInfo) == HC_SUCCESS) {
addedCount++;
}
@@ -354,9 +354,9 @@ static int32_t GenerateTrustedDevParams(const CJson *jsonParams, const char *gro
return HC_SUCCESS;
}
static int32_t CheckPeerDeviceNotSelf(const CJson *jsonParams)
static int32_t CheckPeerDeviceNotSelf(const CJson *deviceInfo)
{
const char *udid = GetStringFromJson(jsonParams, FIELD_UDID);
const char *udid = GetStringFromJson(deviceInfo, FIELD_UDID);
if (udid == NULL) {
LOGE("Failed to get udid from json!");
return HC_ERR_JSON_GET;
@@ -364,13 +364,8 @@ static int32_t CheckPeerDeviceNotSelf(const CJson *jsonParams)
return AssertPeerDeviceNotSelf(udid);
}
static int32_t AddDeviceAndToken(int32_t osAccountId, CJson *jsonParams, CJson *deviceInfo)
static int32_t AddDeviceAndToken(int32_t osAccountId, const CJson *jsonParams, CJson *deviceInfo)
{
int32_t res = CheckPeerDeviceNotSelf(jsonParams);
if (res != HC_SUCCESS) {
LOGE("The peer device udid is equals to the local udid!");
return res;
}
const char *groupId = GetStringFromJson(jsonParams, FIELD_GROUP_ID);
if (groupId == NULL) {
LOGE("Failed to get groupId from json!");
@@ -381,7 +376,7 @@ static int32_t AddDeviceAndToken(int32_t osAccountId, CJson *jsonParams, CJson *
LOGE("Failed to get credential from json!");
return HC_ERR_JSON_GET;
}
res = GenerateAddTokenParams(deviceInfo, credential);
int32_t res = GenerateAddTokenParams(deviceInfo, credential);
if (res != HC_SUCCESS) {
return res;
}
@@ -480,6 +475,51 @@ static int32_t AddGroupAndToken(int32_t osAccountId, CJson *jsonParams, const ch
return res;
}
static int32_t CheckUserIdValid(int32_t osAccountId, const CJson *jsonParams, const CJson *deviceInfo)
{
const char *userId = GetStringFromJson(deviceInfo, FIELD_USER_ID);
if (userId == NULL) {
LOGE("Failed to get userId from json!");
return HC_ERR_JSON_GET;
}
const char *groupId = GetStringFromJson(jsonParams, FIELD_GROUP_ID);
if (groupId == NULL) {
LOGE("Failed to get groupId from json!");
return HC_ERR_JSON_GET;
}
uint32_t index;
TrustedGroupEntry **entry = NULL;
GroupEntryVec groupEntryVec = CreateGroupEntryVec();
QueryGroupParams params = InitQueryGroupParams();
params.groupId = groupId;
params.groupType = IDENTICAL_ACCOUNT_GROUP;
if (QueryGroups(osAccountId, &params, &groupEntryVec) != HC_SUCCESS) {
LOGE("Failed to query groups!");
ClearGroupEntryVec(&groupEntryVec);
return HC_ERR_DB;
}
FOR_EACH_HC_VECTOR(groupEntryVec, index, entry) {
if ((entry != NULL) && (*entry != NULL) && (strcmp(userId, StringGet(&(*entry)->userId)) == 0)) {
ClearGroupEntryVec(&groupEntryVec);
return HC_SUCCESS;
}
}
LOGE("The input userId is inconsistent with the local userId!");
ClearGroupEntryVec(&groupEntryVec);
return HC_ERR_INVALID_PARAMS;
}
static int32_t CheckDeviceInfoValid(int32_t osAccountId, const CJson *jsonParams, const CJson *deviceInfo)
{
int32_t res = CheckPeerDeviceNotSelf(deviceInfo);
if (res != HC_SUCCESS) {
LOGE("The peer device udid is equals to the local udid!");
return res;
}
/* Identical account group: input userId must be consistent with the local userId. */
return CheckUserIdValid(osAccountId, jsonParams, deviceInfo);
}
static int32_t CreateGroup(int32_t osAccountId, CJson *jsonParams, char **returnJsonStr)
{
LOGI("[Start]: Start to create a identical account group!");
@@ -544,6 +584,9 @@ static int32_t AddMultiMembersToGroup(int32_t osAccountId, const char *appId, CJ
LOGE("The deviceInfo is NULL!");
continue;
}
if (CheckDeviceInfoValid(osAccountId, jsonParams, deviceInfo) != HC_SUCCESS) {
continue;
}
if (AddDeviceAndToken(osAccountId, jsonParams, deviceInfo) == HC_SUCCESS) {
addedCount++;
}