diff --git a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventBinding.cs b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventBinding.cs index c7b5676b7e30..b0315cf7b114 100644 --- a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventBinding.cs +++ b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventBinding.cs @@ -248,10 +248,7 @@ internal static AuthenticationEventMetadata GetEventAndValidateSchema(string bod { try { - if (!Helpers.IsJson(body)) - { - throw new RequestValidationException(AuthenticationEventResource.Ex_Invalid_JsonPayload); - } + Helpers.ValidateJson(body); } catch (JsonException ex) { diff --git a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventResource.Designer.cs b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventResource.Designer.cs index b4f5496093b2..4a1cbc426aad 100644 --- a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventResource.Designer.cs +++ b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventResource.Designer.cs @@ -79,6 +79,15 @@ internal static string Ex_Context_Null { } } + /// + /// Looks up a localized string similar to JSON is null or empty.. + /// + internal static string Ex_Empty_Json { + get { + return ResourceManager.GetString("Ex_Empty_Json", resourceCulture); + } + } + /// /// Looks up a localized string similar to Cannot determine the event from payload, please check that the incoming payload is a valid JSON string and contains the event type.. /// @@ -188,7 +197,7 @@ internal static string Ex_Invalid_Response { } /// - /// Looks up a localized string similar to Return type is invalid, please return either an AuthEventResponse, HttpResponse, HttpResponseMessage or string in your function return.. + /// Looks up a localized string similar to Return type is invalid, please return either an AuthEventResponse, HttpResponse, HttpResponseMessage or string in your function return. /// internal static string Ex_Invalid_Return { get { diff --git a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventResource.resx b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventResource.resx index d957f840d895..31fe2ad8dbd4 100644 --- a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventResource.resx +++ b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventResource.resx @@ -123,6 +123,9 @@ ListenerFactoryContext is null + + JSON is null or empty. + Cannot determine the event from payload, please check that the incoming payload is a valid JSON string and contains the event type. @@ -160,7 +163,7 @@ Response validation failed, see inner exceptions. - Return type is invalid, please return either an AuthEventResponse, HttpResponse, HttpResponseMessage or string in your function return. + Return type is invalid, please return either an AuthEventResponse, HttpResponse, HttpResponseMessage or string in your function return Invalid version on Schema diff --git a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventResponseHandler.cs b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventResponseHandler.cs index 97f76bddd09e..7dd18e19f5c9 100644 --- a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventResponseHandler.cs +++ b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/AuthenticationEventResponseHandler.cs @@ -227,10 +227,7 @@ internal static AuthenticationEventJsonElement GetJsonObjectFromString(string re { try { - if (!Helpers.IsJson(result)) - { - throw new ResponseValidationException(AuthenticationEventResource.Ex_Invalid_Return); - } + Helpers.ValidateJson(result); } catch (JsonException ex) { diff --git a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/Helpers.cs b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/Helpers.cs index 0bfcafc94b30..5d6c0949eeb6 100644 --- a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/Helpers.cs +++ b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/src/Helpers.cs @@ -108,19 +108,16 @@ internal static HttpResponseMessage HttpJsonResponse(AuthenticationEventJsonElem /// Determines whether the specified input is json. /// The input. - /// - /// true if the specified input is json; otherwise, false. - internal static bool IsJson(string input) + internal static void ValidateJson(string input) { if (string.IsNullOrEmpty(input)) { - return false; + throw new JsonException(AuthenticationEventResource.Ex_Empty_Json); } - // try parsing input to json object - using var _ = JsonDocument.Parse(input); - return true; - } + // try parsing input to json object + using var _ = JsonDocument.Parse(input); + } internal static AuthenticationEventAction GetEventActionForActionType(string actionType) { diff --git a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/AuthenticationEventBindingTests.cs b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/AuthenticationEventBindingTests.cs index ae13eda877ea..4dda66d0a13e 100644 --- a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/AuthenticationEventBindingTests.cs +++ b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/AuthenticationEventBindingTests.cs @@ -33,7 +33,7 @@ private static IEnumerable TestPayloadScenarios() { Test = string.Empty, Message = "Testing request without payload throws an error", - ExceptionMessage = "Invalid Json Payload" + ExceptionMessage = "Invalid Json Payload: JSON is null or empty." }.ToArray; yield return new TestCaseStructure() { diff --git a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/PayloadTests.cs b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/PayloadTests.cs index d015017052da..f742fe2d0f4c 100644 --- a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/PayloadTests.cs +++ b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/PayloadTests.cs @@ -64,7 +64,7 @@ public async Task Tests(TestTypes testType) case TestTypes.NoAction: return (Payloads.TokenIssuanceStart.TokenIssuanceStart.NoActionResponse, @"{'errors':['No Actions Found. Please supply atleast one action.']}", HttpStatusCode.InternalServerError); case TestTypes.Empty: - return (string.Empty, @"{'errors':['Return type is invalid, please return either an AuthEventResponse, HttpResponse, HttpResponseMessage or string in your function return.']}", HttpStatusCode.InternalServerError); + return (string.Empty, @"{'errors':['Return type is invalid, please return either an AuthEventResponse, HttpResponse, HttpResponseMessage or string in your function return: JSON is null or empty.']}", HttpStatusCode.InternalServerError); case TestTypes.ValidCloudEvent: return (Payloads.TokenIssuanceStart.TokenIssuanceStart.ActionResponse, Payloads.TokenIssuanceStart.TokenIssuanceStart.ExpectedPayload, HttpStatusCode.OK); default: diff --git a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/Payloads/TokenIssuanceStart/NullResponse.json b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/Payloads/TokenIssuanceStart/NullResponse.json index bfa3fc136c4a..485c54513fcb 100644 --- a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/Payloads/TokenIssuanceStart/NullResponse.json +++ b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/Payloads/TokenIssuanceStart/NullResponse.json @@ -1,3 +1,3 @@ { - "errors": [ "Return type is invalid, please return either an AuthEventResponse, HttpResponse, HttpResponseMessage or string in your function return." ] + "errors": [ "Return type is invalid, please return either an AuthEventResponse, HttpResponse, HttpResponseMessage or string in your function return" ] } diff --git a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/ResponseTypesTests.cs b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/ResponseTypesTests.cs index e752b0ef7a00..3bca59bf1117 100644 --- a/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/ResponseTypesTests.cs +++ b/sdk/entra/Microsoft.Azure.WebJobs.Extensions.AuthenticationEvents/tests/ResponseTypesTests.cs @@ -98,7 +98,7 @@ private object GetResponseTypeObject(ResponseTypes responseType, StreamWriter st case ResponseTypes.AuthEventResponse: return (code: HttpStatusCode.OK, Payloads.TokenIssuanceStart.TokenIssuanceStart.ActionResponse); case ResponseTypes.Unknown: - return (code: HttpStatusCode.InternalServerError, @"{'errors':['Return type is invalid, please return either an AuthEventResponse, HttpResponse, HttpResponseMessage or string in your function return.']}"); + return (code: HttpStatusCode.InternalServerError, @"{'errors':['Return type is invalid, please return either an AuthEventResponse, HttpResponse, HttpResponseMessage or string in your function return']}"); default: return (code: HttpStatusCode.BadRequest, string.Empty); };