-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from TheButlah/thebutlah/remove-ms-ident-tokens
feat(did): remove dependency on Microsoft.IdentityModel.Tokens
- Loading branch information
Showing
7 changed files
with
135 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |