mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
Fix OneLogin Import (#1899)
* Add PermissiveStringConverter * Formatting * Add value check * Fix PR feedback * Run formatter
This commit is contained in:
@ -1,8 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.Json.Serialization;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Models.Business;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Api.Models.Public.Request
|
||||
{
|
||||
@ -41,10 +43,12 @@ namespace Bit.Api.Models.Public.Request
|
||||
/// <example>external_id_123456</example>
|
||||
[Required]
|
||||
[StringLength(300)]
|
||||
[JsonConverter(typeof(PermissiveStringConverter))]
|
||||
public string ExternalId { get; set; }
|
||||
/// <summary>
|
||||
/// The associated external ids for members in this group.
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(PermissiveStringEnumerableConverter))]
|
||||
public IEnumerable<string> MemberExternalIds { get; set; }
|
||||
|
||||
public ImportedGroup ToImportedGroup(Guid organizationId)
|
||||
@ -79,6 +83,7 @@ namespace Bit.Api.Models.Public.Request
|
||||
/// <example>external_id_123456</example>
|
||||
[Required]
|
||||
[StringLength(300)]
|
||||
[JsonConverter(typeof(PermissiveStringConverter))]
|
||||
public string ExternalId { get; set; }
|
||||
/// <summary>
|
||||
/// Determines if this member should be removed from the organization during import.
|
||||
|
@ -1,4 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using NS = Newtonsoft.Json;
|
||||
@ -129,4 +132,72 @@ namespace Bit.Core.Utilities
|
||||
writer.WriteStringValue(CoreHelpers.ToEpocMilliseconds(value.Value).ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allows reading a string from a JSON number or string, should only be used on <see cref="string" /> properties
|
||||
/// </summary>
|
||||
public class PermissiveStringConverter : JsonConverter<string>
|
||||
{
|
||||
internal static PermissiveStringConverter Instance = new();
|
||||
|
||||
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return reader.TokenType switch
|
||||
{
|
||||
JsonTokenType.String => reader.GetString(),
|
||||
JsonTokenType.Number => reader.GetDecimal().ToString(),
|
||||
JsonTokenType.True => bool.TrueString,
|
||||
JsonTokenType.False => bool.FalseString,
|
||||
_ => throw new JsonException($"Unsupported TokenType: {reader.TokenType}"),
|
||||
};
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allows reading a JSON array of number or string, should only be used on <see cref="IEnumerable{T}" /> whose generic type is <see cref="string" />
|
||||
/// </summary>
|
||||
public class PermissiveStringEnumerableConverter : JsonConverter<IEnumerable<string>>
|
||||
{
|
||||
public override IEnumerable<string> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var stringList = new List<string>();
|
||||
|
||||
// Handle special cases or throw
|
||||
if (reader.TokenType != JsonTokenType.StartArray)
|
||||
{
|
||||
// An array was expected but to be extra permissive allow reading from anything other than an object
|
||||
if (reader.TokenType == JsonTokenType.StartObject)
|
||||
{
|
||||
throw new JsonException("Cannot read JSON Object to an IEnumerable<string>.");
|
||||
}
|
||||
|
||||
stringList.Add(PermissiveStringConverter.Instance.Read(ref reader, typeof(string), options));
|
||||
return stringList;
|
||||
}
|
||||
|
||||
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
|
||||
{
|
||||
stringList.Add(PermissiveStringConverter.Instance.Read(ref reader, typeof(string), options));
|
||||
}
|
||||
|
||||
return stringList;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, IEnumerable<string> value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStartArray();
|
||||
|
||||
foreach (var str in value)
|
||||
{
|
||||
PermissiveStringConverter.Instance.Write(writer, str, options);
|
||||
}
|
||||
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user