From b47c30d4f4088817ab91761afbc4cdeb4cdc6679 Mon Sep 17 00:00:00 2001 From: Justin Baur Date: Tue, 1 Feb 2022 12:26:50 -0500 Subject: [PATCH] Fix organization_license not reading camelCase (#1832) * Fix organization_license not reading camelCase * Fix formatting --- src/Api/Utilities/ApiHelpers.cs | 11 ++------ src/Core/Utilities/JsonHelpers.cs | 6 +++++ test/Api.Test/Utilities/ApiHelpersTests.cs | 30 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 test/Api.Test/Utilities/ApiHelpersTests.cs diff --git a/src/Api/Utilities/ApiHelpers.cs b/src/Api/Utilities/ApiHelpers.cs index e9ce5b0939..c704aff39c 100644 --- a/src/Api/Utilities/ApiHelpers.cs +++ b/src/Api/Utilities/ApiHelpers.cs @@ -21,15 +21,8 @@ namespace Bit.Api.Utilities { try { - using (var stream = file.OpenReadStream()) - using (var reader = new StreamReader(stream)) - { - var s = await reader.ReadToEndAsync(); - if (!string.IsNullOrWhiteSpace(s)) - { - obj = JsonSerializer.Deserialize(s); - } - } + using var stream = file.OpenReadStream(); + obj = await JsonSerializer.DeserializeAsync(stream, JsonHelpers.IgnoreCase); } catch { } } diff --git a/src/Core/Utilities/JsonHelpers.cs b/src/Core/Utilities/JsonHelpers.cs index 10c1652562..69bd30ad1e 100644 --- a/src/Core/Utilities/JsonHelpers.cs +++ b/src/Core/Utilities/JsonHelpers.cs @@ -9,6 +9,7 @@ namespace Bit.Core.Utilities { public static JsonSerializerOptions Default { get; } public static JsonSerializerOptions Indented { get; } + public static JsonSerializerOptions IgnoreCase { get; } public static JsonSerializerOptions IgnoreWritingNull { get; } public static JsonSerializerOptions CamelCase { get; } public static JsonSerializerOptions IgnoreWritingNullAndCamelCase { get; } @@ -22,6 +23,11 @@ namespace Bit.Core.Utilities WriteIndented = true, }; + IgnoreCase = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + }; + IgnoreWritingNull = new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, diff --git a/test/Api.Test/Utilities/ApiHelpersTests.cs b/test/Api.Test/Utilities/ApiHelpersTests.cs new file mode 100644 index 0000000000..3e2181c8d1 --- /dev/null +++ b/test/Api.Test/Utilities/ApiHelpersTests.cs @@ -0,0 +1,30 @@ +using System.IO; +using System.Text; +using System.Threading.Tasks; +using Bit.Api.Models.Request; +using Bit.Api.Utilities; +using Bit.Core.Models.Business; +using Microsoft.AspNetCore.Http; +using NSubstitute; +using Xunit; + +namespace Bit.Api.Test.Utilities +{ + public class ApiHelpersTests + { + [Fact] + public async Task ReadJsonFileFromBody_Success() + { + var context = Substitute.For(); + context.Request.ContentLength.Returns(200); + var bytes = Encoding.UTF8.GetBytes(testFile); + var formFile = new FormFile(new MemoryStream(bytes), 0, bytes.Length, "bitwarden_organization_license", "bitwarden_organization_license.json"); + + + var license = await ApiHelpers.ReadJsonFileFromBody(context, formFile); + Assert.Equal(8, license.Version); + } + + const string testFile = "{\"licenseKey\": \"licenseKey\", \"installationId\": \"6285f891-b2ec-4047-84c5-2eb7f7747e74\", \"id\": \"1065216d-5854-4326-838d-635487f30b43\",\"name\": \"Test Org\",\"billingEmail\": \"test@email.com\",\"businessName\": null,\"enabled\": true, \"plan\": \"Enterprise (Annually)\",\"planType\": 11,\"seats\": 6,\"maxCollections\": null,\"usePolicies\": true,\"useSso\": true,\"useKeyConnector\": false,\"useGroups\": true,\"useEvents\": true,\"useDirectory\": true,\"useTotp\": true,\"use2fa\": true,\"useApi\": true,\"useResetPassword\": true,\"maxStorageGb\": 1,\"selfHost\": true,\"usersGetPremium\": true,\"version\": 8,\"issued\": \"2022-01-25T21:58:38.9454581Z\",\"refresh\": \"2022-01-28T14:26:31Z\",\"expires\": \"2022-01-28T14:26:31Z\",\"trial\": true,\"hash\": \"testvalue\",\"signature\": \"signature\"}"; + } +}