mirror of
https://github.com/bitwarden/server.git
synced 2025-07-05 18:12:48 -05:00
Start Migration from Newtonsoft.Json to System.Text.Json (#1803)
* Start switch to System.Text.Json * Work on switching to System.Text.Json * Main work on STJ refactor * Fix build errors * Run formatting * Delete unused file * Use legacy for two factor providers * Run formatter * Add TokenProviderTests * Run formatting * Fix merge issues * Switch to use JsonSerializer * Address PR feedback * Fix formatting * Ran formatter * Switch to async * Ensure Enums are serialized as strings * Fix formatting * Enqueue single items as arrays * Remove CreateAsync method on AzureQueueService
This commit is contained in:
@ -1,11 +1,12 @@
|
||||
using System.Diagnostics;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Admin.Models;
|
||||
using Bit.Core.Settings;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Bit.Admin.Controllers
|
||||
{
|
||||
@ -37,20 +38,21 @@ namespace Bit.Admin.Controllers
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IActionResult> GetLatestDockerHubVersion(string repository)
|
||||
public async Task<IActionResult> GetLatestDockerHubVersion(string repository, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.GetAsync(
|
||||
$"https://hub.docker.com/v2/repositories/bitwarden/{repository}/tags/");
|
||||
$"https://hub.docker.com/v2/repositories/bitwarden/{repository}/tags/", cancellationToken);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var data = JObject.Parse(json);
|
||||
var results = data["results"] as JArray;
|
||||
foreach (var result in results)
|
||||
using var jsonDocument = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync(cancellationToken), cancellationToken: cancellationToken);
|
||||
var root = jsonDocument.RootElement;
|
||||
|
||||
var results = root.GetProperty("results");
|
||||
foreach (var result in results.EnumerateArray())
|
||||
{
|
||||
var name = result["name"].ToString();
|
||||
var name = result.GetProperty("name").GetString();
|
||||
if (!string.IsNullOrWhiteSpace(name) && name.Length > 0 && char.IsNumber(name[0]))
|
||||
{
|
||||
return new JsonResult(name);
|
||||
@ -63,17 +65,17 @@ namespace Bit.Admin.Controllers
|
||||
return new JsonResult("-");
|
||||
}
|
||||
|
||||
public async Task<IActionResult> GetInstalledWebVersion()
|
||||
public async Task<IActionResult> GetInstalledWebVersion(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.GetAsync(
|
||||
$"{_globalSettings.BaseServiceUri.InternalVault}/version.json");
|
||||
$"{_globalSettings.BaseServiceUri.InternalVault}/version.json", cancellationToken);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var data = JObject.Parse(json);
|
||||
return new JsonResult(data["version"].ToString());
|
||||
using var jsonDocument = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync(cancellationToken), cancellationToken: cancellationToken);
|
||||
var root = jsonDocument.RootElement;
|
||||
return new JsonResult(root.GetProperty("version").GetString());
|
||||
}
|
||||
}
|
||||
catch (HttpRequestException) { }
|
||||
|
@ -3,9 +3,9 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Admin.Models;
|
||||
using Bit.Core;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
@ -14,7 +14,6 @@ using Bit.Core.Utilities;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Admin.Controllers
|
||||
{
|
||||
@ -264,14 +263,16 @@ namespace Bit.Admin.Controllers
|
||||
{
|
||||
var license = await _organizationService.GenerateLicenseAsync(organization,
|
||||
model.InstallationId.Value, model.Version);
|
||||
return File(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(license, Formatting.Indented)),
|
||||
"text/plain", "bitwarden_organization_license.json");
|
||||
var ms = new MemoryStream();
|
||||
await JsonSerializer.SerializeAsync(ms, license, JsonHelpers.Indented);
|
||||
return File(ms, "text/plain", "bitwarden_organization_license.json");
|
||||
}
|
||||
else if (user != null)
|
||||
{
|
||||
var license = await _userService.GenerateLicenseAsync(user, null, model.Version);
|
||||
return File(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(license, Formatting.Indented)),
|
||||
"text/plain", "bitwarden_premium_license.json");
|
||||
var ms = new MemoryStream();
|
||||
await JsonSerializer.SerializeAsync(ms, license, JsonHelpers.Indented);
|
||||
return File(ms, "text/plain", "bitwarden_premium_license.json");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Reference in New Issue
Block a user