1
0
mirror of https://github.com/bitwarden/server.git synced 2025-01-21 21:41:21 +01:00

Fix organization_license not reading camelCase (#1832)

* Fix organization_license not reading camelCase

* Fix formatting
This commit is contained in:
Justin Baur 2022-02-01 12:26:50 -05:00 committed by GitHub
parent 1b233370eb
commit b47c30d4f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 9 deletions

View File

@ -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<T>(s);
}
}
using var stream = file.OpenReadStream();
obj = await JsonSerializer.DeserializeAsync<T>(stream, JsonHelpers.IgnoreCase);
}
catch { }
}

View File

@ -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,

View File

@ -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<HttpContext>();
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<OrganizationLicense>(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\"}";
}
}