Skip to content

Commit

Permalink
Merge pull request #138 from TheButlah/thebutlah/remove-ms-ident-tokens
Browse files Browse the repository at this point in the history
feat(did): remove dependency on Microsoft.IdentityModel.Tokens
  • Loading branch information
dooly123 authored Feb 9, 2025
2 parents 9c427ca + 71a97a7 commit 6182f31
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 10 deletions.
34 changes: 34 additions & 0 deletions Contrib/Auth/Did.Tests/Base64UrlSafeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Diagnostics;
using System.Linq;
using Xunit;

namespace Basis.Contrib.Auth.DecentralizedIds
{
public class Base64UrlSafeTests
{
[Fact]
public void TestEncode()
{
byte[] bytes = new byte[] { 0xDE, 0xAD, 0xBE, 0xEF };
string base64 = "3q2-7w";

Debug.Assert(
Base64UrlSafe.Encode(bytes).Equals(base64),
"base64 encoding did not match expected value"
);
}

[Fact]
public void TestDecode()
{
byte[] bytes = new byte[] { 0xDE, 0xAD, 0xBE, 0xEF };
string base64 = "3q2-7w";

Debug.Assert(
Base64UrlSafe.Decode(base64).SequenceEqual(bytes),
"base64 decoding was did not match expected value"
);
}
}
}
2 changes: 0 additions & 2 deletions Contrib/Auth/Did.Tests/DidKeyTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Basis.Contrib.Auth.DecentralizedIds.Newtypes;
using Xunit;
using JsonWebKey = Microsoft.IdentityModel.Tokens.JsonWebKey;

namespace Basis.Contrib.Auth.DecentralizedIds
{
Expand Down
40 changes: 40 additions & 0 deletions Contrib/Auth/Did/Base64UrlSafe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Yes, this whole approach is cursed and inefficient. YOLOSWAG.

namespace Basis.Contrib.Auth.DecentralizedIds
{
/// Base64 url-safe encode and decode.
public class Base64UrlSafe
{
public static string Encode(byte[] bytes)
{
string base64 = System.Convert.ToBase64String(bytes);
return base64
.TrimEnd('=') // Remove padding
.Replace('+', '-') // Convert + to -
.Replace('/', '_'); // Convert / to _
}

public static byte[] Decode(string str)
{
string base64 = str.Replace('-', '+') // Restore + from -
.Replace('_', '/'); // Restore / from _

// Add padding if needed
switch (base64.Length % 4)
{
case 0:
break; // No padding needed
case 2:
base64 += "==";
break;
case 3:
base64 += "=";
break;
default:
throw new System.FormatException("Invalid base64url string length");
}

return System.Convert.FromBase64String(base64);
}
}
}
4 changes: 2 additions & 2 deletions Contrib/Auth/Did/Did.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

<!--Third-party dependencies-->
<ItemGroup>
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.3.0" />
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageReference Include="BouncyCastle.Cryptography" Version="2.5.0" />
<PackageReference Include="SimpleBase" Version="4.0.2" />
<PackageReference Include="System.Text.Json" Version="9.0.1" />
<PackageReference Include="VarInt" Version="1.2.2" />
</ItemGroup>
</Project>
1 change: 0 additions & 1 deletion Contrib/Auth/Did/DidDocument.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.ObjectModel;
using DidUrlFragment = Basis.Contrib.Auth.DecentralizedIds.Newtypes.DidUrlFragment;
using JsonWebKey = Microsoft.IdentityModel.Tokens.JsonWebKey;

namespace Basis.Contrib.Auth.DecentralizedIds
{
Expand Down
6 changes: 1 addition & 5 deletions Contrib/Auth/Did/DidKeyResolver.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Tokens;
using Base128 = WojciechMikołajewicz.Base128;
using Base58 = SimpleBase.Base58;
using Debug = System.Diagnostics.Debug;
using Did = Basis.Contrib.Auth.DecentralizedIds.Newtypes.Did;
using DidUrlFragment = Basis.Contrib.Auth.DecentralizedIds.Newtypes.DidUrlFragment;
using Ed25519 = Org.BouncyCastle.Math.EC.Rfc8032.Ed25519;
using JsonWebKey = Microsoft.IdentityModel.Tokens.JsonWebKey;
using StringSplitOptions = System.StringSplitOptions;

namespace Basis.Contrib.Auth.DecentralizedIds
Expand Down Expand Up @@ -91,7 +87,7 @@ private static JsonWebKey CreateEd25519Jwk(byte[] pubkeyBytes)
{
Kty = "OKP",
Crv = "Ed25519",
X = Base64UrlEncoder.Encode(pubkeyBytes),
X = Base64UrlSafe.Encode(pubkeyBytes),
};
return key;
}
Expand Down
58 changes: 58 additions & 0 deletions Contrib/Auth/Did/JsonWebKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System; // ReadOnlySpan
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Basis.Contrib.Auth.DecentralizedIds
{
public class JsonWebKey
{
[JsonPropertyName("kty")]
public string? Kty { get; set; }

[JsonPropertyName("kid")]
public string? Kid { get; set; }

[JsonPropertyName("alg")]
public string? Alg { get; set; }

[JsonPropertyName("use")]
public string? Use { get; set; }

// Ed25519 parameters
[JsonPropertyName("x")]
public string? X { get; set; }

[JsonPropertyName("d")]
public string? D { get; set; }

[JsonPropertyName("crv")]
public string? Crv { get; set; }

// Symmetric key parameter
[JsonPropertyName("k")]
public string? K { get; set; }

// Helper method to exclude null values during serialization
public static JsonSerializerOptions SerializerOptions =>
new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = true,
};

public string Serialize()
{
return JsonSerializer.Serialize(this, SerializerOptions);
}

public static JsonWebKey? Deserialize(string json)
{
return JsonSerializer.Deserialize<JsonWebKey>(json, SerializerOptions);
}

public static JsonWebKey? Deserialize(ReadOnlySpan<byte> json)
{
return JsonSerializer.Deserialize<JsonWebKey>(json, SerializerOptions);
}
}
}

0 comments on commit 6182f31

Please sign in to comment.