Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json;
using Fhi.HelseIdSelvbetjening.CLI.Services;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -25,7 +26,7 @@ public static string ResolveKeyValuePathOrString(
if (!string.IsNullOrWhiteSpace(directValue))
{
logger.LogInformation("{keyLabel} provided directly.", keyLabel);
return directValue;
return UnescapeJson(directValue.Trim());
}

if (!string.IsNullOrWhiteSpace(filePath))
Expand All @@ -37,5 +38,21 @@ public static string ResolveKeyValuePathOrString(
logger.LogWarning("{keyLabel} not provided.", keyLabel);
return string.Empty;
}

/// <summary>
/// Unescapes JSON string escape sequences by deserializing the input as a JSON string value.
/// <see href="https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/character-encoding"/>
/// </summary>
private static string UnescapeJson(string input)
{
try
{
return JsonSerializer.Deserialize<string>($"\"{input}\"")!;
}
catch (JsonException)
{
return input;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ namespace Fhi.HelseIdSelvbetjening.CLI.IntegrationTests
{
public class ReadClientSecretExpirationTests
{
/// <summary>
/// Unicode escape sequence for the double quote character (").
/// The first JWKs from HelseId contained unicode escaped quotes.
/// </summary>
private const string UnicodeEscapedQuote = "\\u0022";

[TestCase("{\n \"kid\": \"test-kid\",\n \"kty\": \"RSA\",\n \"d\": \"test-d-value\",\n \"n\": \"test-n-value\",\n \"e\": \"AQAB\"\n}")]
[TestCase("{\"d\":\"test-kid\",\"e\":\"AQAB\",\"kid\":\"test-kid\",\"kty\":\"RSA\",\"n\":\"test-n-value\"}")]
[TestCase("{ \"kid\": \"test-kid\", \"kty\": \"RSA\", \"d\": \"test-data\", \"n\": \"test-modulus\", \"e\": \"AQAB\" }")]
// [TestCase(@"{""kid"":""test-with-special-chars-!@#$%^&*()"",""d"":""data-with-quotes-\""and\""-backslashes-\\"",""n"":""modulus"",""e"":""AQAB""}")]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lagde en ny test case med innholdet fra kid flyttet til d fordi vi asserter i testen at kid skal være test-kid.

[TestCase(@"{""kid"":""test-kid"",""kty"":""RSA"",""d"":""test-with-special-chars-!@#$%^&*()"",""n"":""test-n-value"",""e"":""AQAB""}")]
[TestCase(@"{""kid"":""test-kid"",""kty"":""RSA"",""d"":""data-with-quotes-\""and\""-backslashes-\\"",""n"":""test-n-value"",""e"":""AQAB""}")]
[TestCase("{\\\"kid\\\":\\\"test-kid\\\",\\\"kty\\\":\\\"RSA\\\",\\\"d\\\":\\\"test-d-value\\\",\\\"n\\\":\\\"test-n-value\\\",\\\"e\\\":\\\"AQAB\\\"}")]
[TestCase("{" + UnicodeEscapedQuote + "kid" + UnicodeEscapedQuote + ":" + UnicodeEscapedQuote + "test-kid" + UnicodeEscapedQuote + "," + UnicodeEscapedQuote + "kty" + UnicodeEscapedQuote + ":" + UnicodeEscapedQuote + "RSA" + UnicodeEscapedQuote + "," + UnicodeEscapedQuote + "d" + UnicodeEscapedQuote + ":" + UnicodeEscapedQuote + "test-d-value" + UnicodeEscapedQuote + "," + UnicodeEscapedQuote + "n" + UnicodeEscapedQuote + ":" + UnicodeEscapedQuote + "test-n-value" + UnicodeEscapedQuote + "," + UnicodeEscapedQuote + "e" + UnicodeEscapedQuote + ":" + UnicodeEscapedQuote + "AQAB" + UnicodeEscapedQuote + "}")]
public async Task ReadClientSecretExpiration_ValidDirectJwkArgument_ExitCode0(string jwk)
{
var fakeLogProvider = new FakeLoggerProvider();
Expand All @@ -40,7 +49,7 @@ public async Task ReadClientSecretExpiration_ValidDirectJwkArgument_ExitCode0(st

using (Assert.EnterMultipleScope())
{
Assert.That(exitCode, Is.EqualTo(0));
Assert.That(exitCode, Is.Zero);
Assert.That(fakeLogProvider.Collector?.LatestRecord.Message, Does.Contain(((DateTimeOffset)clientSecrets.FirstOrDefault()!.Expiration!).ToUnixTimeSeconds().ToString()));
var logs = fakeLogProvider.Collector?.GetSnapshot().Select(x => x.Message).ToList();
Assert.That(logs!, Does.Contain("Kid: test-kid"));
Expand Down