mirror of
https://github.com/bitwarden/server.git
synced 2025-07-18 08:00:59 -05:00
[PM-5645] Cosmos DB Grant Storage (#3634)
* table storage grants * simple shard on storage accounts * use is not * cosmos grant repo * remove single storage connection string * some fixes to dapper grant repo * pattern matching * add fallback to base PersistedGrantStore * service collection extension cleanup * cleanup * remove unused Id * empty string rowkey * fix sharding method logic * ttl for cosmos * make ttl an int * fixes to cosmos implementation * fix partition key values * catch notfound exceptions * indenting * update grantitem with custom serialization * use new transform helpers * grantloader perf test tool * ref * remove grant loader project * remove table storage implementation * remove table storage stuff * all redis fallback to build to null * revert sln file change * EOF new line * remove trailing comma * lint fixes * add grant to names * move cosmos serilaizer to utils * add some .net 8 keyed service comments * EnableContentResponseOnWrite * Fix type in EF grant repository
This commit is contained in:
@ -338,16 +338,50 @@ public static class CoreHelpers
|
||||
return Encoding.UTF8.GetString(Base64UrlDecode(input));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encodes a Base64 URL formatted string.
|
||||
/// </summary>
|
||||
/// <param name="input">Byte data</param>
|
||||
/// <returns>Base64 URL formatted string</returns>
|
||||
public static string Base64UrlEncode(byte[] input)
|
||||
{
|
||||
var output = Convert.ToBase64String(input)
|
||||
// Standard base64 encoder
|
||||
var standardB64 = Convert.ToBase64String(input);
|
||||
return TransformToBase64Url(standardB64);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms a Base64 standard formatted string to a Base64 URL formatted string.
|
||||
/// </summary>
|
||||
/// <param name="input">Base64 standard formatted string</param>
|
||||
/// <returns>Base64 URL formatted string</returns>
|
||||
public static string TransformToBase64Url(string input)
|
||||
{
|
||||
var output = input
|
||||
.Replace('+', '-')
|
||||
.Replace('/', '_')
|
||||
.Replace("=", string.Empty);
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a Base64 URL formatted string.
|
||||
/// </summary>
|
||||
/// <param name="input">Base64 URL formatted string</param>
|
||||
/// <returns>Data as bytes</returns>
|
||||
public static byte[] Base64UrlDecode(string input)
|
||||
{
|
||||
var standardB64 = TransformFromBase64Url(input);
|
||||
// Standard base64 decoder
|
||||
return Convert.FromBase64String(standardB64);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms a Base64 URL formatted string to a Base64 standard formatted string.
|
||||
/// </summary>
|
||||
/// <param name="input">Base64 URL formatted string</param>
|
||||
/// <returns>Base64 standard formatted string</returns>
|
||||
public static string TransformFromBase64Url(string input)
|
||||
{
|
||||
var output = input;
|
||||
// 62nd char of encoding
|
||||
@ -370,8 +404,8 @@ public static class CoreHelpers
|
||||
throw new InvalidOperationException("Illegal base64url string!");
|
||||
}
|
||||
|
||||
// Standard base64 decoder
|
||||
return Convert.FromBase64String(output);
|
||||
// Standard base64 string output
|
||||
return output;
|
||||
}
|
||||
|
||||
public static string PunyEncode(string text)
|
||||
|
40
src/Core/Utilities/SystemTextJsonCosmosSerializer.cs
Normal file
40
src/Core/Utilities/SystemTextJsonCosmosSerializer.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using System.Text.Json;
|
||||
using Azure.Core.Serialization;
|
||||
using Microsoft.Azure.Cosmos;
|
||||
|
||||
namespace Bit.Core.Utilities;
|
||||
|
||||
// ref: https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs
|
||||
public class SystemTextJsonCosmosSerializer : CosmosSerializer
|
||||
{
|
||||
private readonly JsonObjectSerializer _systemTextJsonSerializer;
|
||||
|
||||
public SystemTextJsonCosmosSerializer(JsonSerializerOptions jsonSerializerOptions)
|
||||
{
|
||||
_systemTextJsonSerializer = new JsonObjectSerializer(jsonSerializerOptions);
|
||||
}
|
||||
|
||||
public override T FromStream<T>(Stream stream)
|
||||
{
|
||||
using (stream)
|
||||
{
|
||||
if (stream.CanSeek && stream.Length == 0)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
if (typeof(Stream).IsAssignableFrom(typeof(T)))
|
||||
{
|
||||
return (T)(object)stream;
|
||||
}
|
||||
return (T)_systemTextJsonSerializer.Deserialize(stream, typeof(T), default);
|
||||
}
|
||||
}
|
||||
|
||||
public override Stream ToStream<T>(T input)
|
||||
{
|
||||
var streamPayload = new MemoryStream();
|
||||
_systemTextJsonSerializer.Serialize(streamPayload, input, input.GetType(), default);
|
||||
streamPayload.Position = 0;
|
||||
return streamPayload;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user