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
Expand Up @@ -213,6 +213,7 @@ public static IIdentityServerBuilder AddCoreServices(this IIdentityServerBuilder
builder.Services.AddSingleton<LicenseExpirationChecker>();

builder.Services.AddSingleton<IDiagnosticEntry, AssemblyInfoDiagnosticEntry>();
builder.Services.AddSingleton<IDiagnosticEntry, AuthSchemeInfoDiagnosticEntry>();
builder.Services.AddSingleton<DiagnosticSummary>();
builder.Services.AddHostedService<DiagnosticHostedService>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using System.Text.Json;
using Microsoft.AspNetCore.Authentication;

namespace Duende.IdentityServer.Licensing.V2.Diagnostics.DiagnosticEntries;

internal class AuthSchemeInfoDiagnosticEntry(IAuthenticationSchemeProvider authenticationSchemeProvider) : IDiagnosticEntry
{
public async Task WriteAsync(Utf8JsonWriter writer)
{
var schemes = await authenticationSchemeProvider.GetAllSchemesAsync();

writer.WriteStartObject("AuthSchemeInfo");
writer.WriteStartArray("Schemes");
foreach (var scheme in schemes)
{
writer.WriteStartObject();
writer.WriteString(scheme.Name, scheme.HandlerType.FullName ?? "Unknown");
writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WriteEndObject();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using System.Buffers;
using System.Text;
using System.Text.Json;
using Duende.IdentityServer.Licensing.V2.Diagnostics.DiagnosticEntries;

Expand All @@ -14,17 +12,10 @@ public class AssemblyInfoDiagnosticEntryTests
public async Task WriteAsync_ShouldWriteAssemblyInfo()
{
var subject = new AssemblyInfoDiagnosticEntry();
var bufferWriter = new ArrayBufferWriter<byte>();
await using var writer = new Utf8JsonWriter(bufferWriter, new JsonWriterOptions { Indented = false });
writer.WriteStartObject();

await subject.WriteAsync(writer);
var result = await DiagnosticEntryTestHelper.WriteEntryToJson(subject);

writer.WriteEndObject();
await writer.FlushAsync();
var json = Encoding.UTF8.GetString(bufferWriter.WrittenSpan);
var jsonDocument = JsonDocument.Parse(json);
var assemblyInfo = jsonDocument.RootElement.GetProperty("AssemblyInfo");
var assemblyInfo = result.RootElement.GetProperty("AssemblyInfo");
assemblyInfo.GetProperty("AssemblyCount").ValueKind.ShouldBe(JsonValueKind.Number);
var assemblies = assemblyInfo.GetProperty("Assemblies");
assemblies.ValueKind.ShouldBe(JsonValueKind.Array);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using Duende.IdentityServer.Licensing.V2.Diagnostics.DiagnosticEntries;
using Microsoft.AspNetCore.Authentication;
using UnitTests.Common;

namespace IdentityServer.UnitTests.Licensing.V2.DiagnosticEntries;

public class AuthSchemeInfoDiagnosticEntryTests
{

private readonly MockAuthenticationSchemeProvider _mockAuthenticationSchemeProvider;
private readonly AuthSchemeInfoDiagnosticEntry _subject;

public AuthSchemeInfoDiagnosticEntryTests()
{
_mockAuthenticationSchemeProvider = new MockAuthenticationSchemeProvider();
_subject = new AuthSchemeInfoDiagnosticEntry(_mockAuthenticationSchemeProvider);
}

[Fact]
public async Task WriteAsync_ShouldWriteAuthSchemeInfo()
{
var testAuthenticationScheme = new AuthenticationScheme("TestScheme", "Test Scheme", typeof(MockAuthenticationHandler));
_mockAuthenticationSchemeProvider.RemoveScheme("scheme");
_mockAuthenticationSchemeProvider.AddScheme(testAuthenticationScheme);

var result = await DiagnosticEntryTestHelper.WriteEntryToJson(_subject);

var authSchemeInfo = result.RootElement.GetProperty("AuthSchemeInfo");
var authSchemes = authSchemeInfo.GetProperty("Schemes");
var firstEntry = authSchemes.EnumerateArray().First();
firstEntry.GetProperty("TestScheme").GetString().ShouldBe("UnitTests.Common.MockAuthenticationHandler");
}

[Fact]
public async Task WriteAsync_ShouldWriteAllRegisteredAuthSchemes()
{
_mockAuthenticationSchemeProvider.RemoveScheme("scheme");
_mockAuthenticationSchemeProvider.AddScheme(new AuthenticationScheme("FirstTestScheme", "First Test Scheme", typeof(MockAuthenticationHandler)));
_mockAuthenticationSchemeProvider.AddScheme(new AuthenticationScheme("SecondTestScheme", "Second Test Scheme", typeof(MockAuthenticationHandler)));

var result = await DiagnosticEntryTestHelper.WriteEntryToJson(_subject);

var authSchemeInfo = result.RootElement.GetProperty("AuthSchemeInfo");
var authSchemes = authSchemeInfo.GetProperty("Schemes");
authSchemes.GetArrayLength().ShouldBe(2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using System.Buffers;
using System.Text;
using System.Text.Json;
using Duende.IdentityServer.Licensing.V2.Diagnostics;

namespace IdentityServer.UnitTests.Licensing.V2.DiagnosticEntries;

internal static class DiagnosticEntryTestHelper
{
public static async Task<JsonDocument> WriteEntryToJson(IDiagnosticEntry subject)
{
var bufferWriter = new ArrayBufferWriter<byte>();

await using var writer = new Utf8JsonWriter(bufferWriter, new JsonWriterOptions { Indented = false });
writer.WriteStartObject();

await subject.WriteAsync(writer);

writer.WriteEndObject();
await writer.FlushAsync();

var json = Encoding.UTF8.GetString(bufferWriter.WrittenSpan);

return JsonDocument.Parse(json);
}
}
Loading