From ccc2d7930db546fc179917cda677c8571de57bfd Mon Sep 17 00:00:00 2001 From: winnieHu Date: Sat, 16 Oct 2021 20:00:48 +0800 Subject: [PATCH] fix auth bug when return error to peer Signed-off-by: winnieHu --- .../src/auth_session/auth_session_client.c | 2 +- .../src/auth_session/auth_session_common.c | 66 +++++++++++++++++-- .../src/auth_session/auth_session_server.c | 8 +-- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/services/session/src/auth_session/auth_session_client.c b/services/session/src/auth_session/auth_session_client.c index 9b7b02a..49ac5ef 100644 --- a/services/session/src/auth_session/auth_session_client.c +++ b/services/session/src/auth_session/auth_session_client.c @@ -151,8 +151,8 @@ static int32_t ProcessClientAuthSession(Session *session, CJson *in) CJson *out = CreateJson(); if (out == NULL) { LOGE("Failed to create json for out!"); - InformLocalAuthError(paramInSession, realSession->base.callback); InformPeerAuthError(paramInSession, realSession->base.callback); + InformLocalAuthError(paramInSession, realSession->base.callback); return HC_ERR_ALLOC_MEMORY; } res = ProcessClientAuthTask(realSession, GetAuthModuleType(paramInSession), in, out); diff --git a/services/session/src/auth_session/auth_session_common.c b/services/session/src/auth_session/auth_session_common.c index cc7f5db..f3e525b 100644 --- a/services/session/src/auth_session/auth_session_common.c +++ b/services/session/src/auth_session/auth_session_common.c @@ -24,6 +24,7 @@ #include "hc_log.h" #include "json_utils.h" +#define MIN_PROTOCOL_VERSION "1.0.0" IMPLEMENT_HC_VECTOR(ParamsVec, void *, 1) static bool IsOldFormatParams(const CJson *param) @@ -499,6 +500,64 @@ static int32_t ReturnErrorToLocalBySession(const AuthSession *session, int error return res; } +static int32_t AddVersionMsgToPeer(CJson *errorToPeer) +{ + CJson *version = CreateJson(); + if (version == NULL) { + LOGE("Failed to create json for version!"); + return HC_ERR_JSON_CREATE; + } + CJson *payload = CreateJson(); + if (payload == NULL) { + LOGE("Failed to create json for payload!"); + FreeJson(version); + return HC_ERR_JSON_CREATE; + } + int32_t res = HC_SUCCESS; + do { + if (AddStringToJson(version, FIELD_MIN_VERSION, MIN_PROTOCOL_VERSION) != HC_SUCCESS) { + LOGE("Failed to add min version to json!"); + res = HC_ERR_JSON_ADD; + break; + } + if (AddStringToJson(version, FIELD_CURRENT_VERSION, MIN_PROTOCOL_VERSION) != HC_SUCCESS) { + LOGE("Failed to add max version to json!"); + res = HC_ERR_JSON_ADD; + break; + } + if (AddObjToJson(payload, FIELD_VERSION, version) != HC_SUCCESS) { + LOGE("Add version object to errorToPeer failed."); + res = HC_ERR_JSON_ADD; + break; + } + if (AddIntToJson(payload, FIELD_ERROR_CODE, -1) != HC_SUCCESS) { + LOGE("Failed to add errorCode for peer!"); + res = HC_ERR_JSON_ADD; + break; + } + if (AddObjToJson(errorToPeer, FIELD_PAYLOAD, payload) != HC_SUCCESS) { + res = HC_ERR_JSON_ADD; + break; + } + } while (0); + FreeJson(version); + FreeJson(payload); + return res; +} + +static int32_t PrepareErrorMsgToPeer(CJson *errorToPeer) +{ + if (AddIntToJson(errorToPeer, FIELD_GROUP_ERROR_MSG, GROUP_ERR_MSG) != HC_SUCCESS) { + LOGE("Failed to add groupErrorMsg for peer!"); + return HC_ERR_JSON_FAIL; + } + if (AddIntToJson(errorToPeer, FIELD_MESSAGE, GROUP_ERR_MSG) != HC_SUCCESS) { + LOGE("Failed to add message for peer!"); + return HC_ERR_JSON_FAIL; + } + return AddVersionMsgToPeer(errorToPeer); +} + static int32_t ReturnErrorToPeerBySession(const CJson *authParam, const DeviceAuthCallback *callback) { int64_t requestId = 0; @@ -511,10 +570,10 @@ static int32_t ReturnErrorToPeerBySession(const CJson *authParam, const DeviceAu LOGE("Failed to allocate memory for errorToPeer!"); return HC_ERR_ALLOC_MEMORY; } - if (AddIntToJson(errorToPeer, FIELD_GROUP_ERROR_MSG, GROUP_ERR_MSG) != HC_SUCCESS) { - LOGE("Failed to add err message to return data!"); + int32_t res = PrepareErrorMsgToPeer(errorToPeer); + if (res != HC_SUCCESS) { FreeJson(errorToPeer); - return HC_ERR_JSON_FAIL; + return res; } char *errorToPeerStr = PackJsonToString(errorToPeer); FreeJson(errorToPeer); @@ -523,7 +582,6 @@ static int32_t ReturnErrorToPeerBySession(const CJson *authParam, const DeviceAu return HC_ERR_ALLOC_MEMORY; } - int32_t res = HC_SUCCESS; do { if ((callback == NULL) || (callback->onTransmit == NULL)) { LOGE("The callback of onTransmit is null!"); diff --git a/services/session/src/auth_session/auth_session_server.c b/services/session/src/auth_session/auth_session_server.c index c50dd87..b8cc7de 100644 --- a/services/session/src/auth_session/auth_session_server.c +++ b/services/session/src/auth_session/auth_session_server.c @@ -187,8 +187,8 @@ static int32_t StartServerAuthTask(AuthSession *session, const CJson *receivedDa CJson *out = CreateJson(); if (out == NULL) { LOGE("Failed to create json!"); - InformLocalAuthError(receivedData, session->base.callback); InformPeerAuthError(receivedData, session->base.callback); + InformLocalAuthError(receivedData, session->base.callback); return HC_ERR_ALLOC_MEMORY; } int32_t status = 0; @@ -238,8 +238,8 @@ static int ProcessServerAuthSession(Session *session, CJson *in) CJson *out = CreateJson(); if (out == NULL) { LOGE("Failed to create json for out!"); - InformLocalAuthError(paramInSession, realSession->base.callback); InformPeerAuthError(paramInSession, realSession->base.callback); + InformLocalAuthError(paramInSession, realSession->base.callback); return HC_ERR_ALLOC_MEMORY; } int32_t moduleType = GetAuthModuleType(paramInSession); @@ -288,8 +288,8 @@ static AuthSession *CreateServerAuthSessionInner(CJson *param, const DeviceAuthC } return (AuthSession *)session; err: - InformLocalAuthError(param, callback); InformPeerAuthError(param, callback); + InformLocalAuthError(param, callback); return NULL; } @@ -298,8 +298,8 @@ Session *CreateServerAuthSession(CJson *param, const DeviceAuthCallback *callbac AuthSession *session = NULL; if (AddIntToJson(param, FIELD_OPERATION_CODE, AUTHENTICATE) != HC_SUCCESS) { LOGE("Failed to add operation code to json!"); - InformLocalAuthError(param, callback); InformPeerAuthError(param, callback); + InformLocalAuthError(param, callback); return NULL; } session = CreateServerAuthSessionInner(param, callback);