Skip to content

Commit e481fa4

Browse files
committed
Add ClaimResult with headers and cookies
1 parent 7f69ebb commit e481fa4

4 files changed

Lines changed: 95 additions & 9 deletions

File tree

Directory.Packages.props

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<PackageVersion Include="MailKit" Version="4.17.0" />
3232
<PackageVersion Include="MediatR" Version="14.2.0" />
3333
<PackageVersion Include="MessagePack" Version="3.1.8" />
34-
<PackageVersion Include="Meziantou.Analyzer" Version="3.0.190" />
34+
<PackageVersion Include="Meziantou.Analyzer" Version="3.0.194" />
3535
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly" Version="10.0.11" />
3636
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="10.0.11" />
3737
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="10.0.11" />
@@ -70,21 +70,21 @@
7070
<PackageVersion Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.18.0" />
7171
<PackageVersion Include="Riok.Mapperly" Version="4.3.1" />
7272
<PackageVersion Include="Rocks" Version="10.3.0" />
73-
<PackageVersion Include="Scalar.AspNetCore" Version="2.17.1" />
73+
<PackageVersion Include="Scalar.AspNetCore" Version="2.17.2" />
7474
<PackageVersion Include="SendGrid.Extensions.DependencyInjection" Version="1.0.1" />
7575
<PackageVersion Include="Serilog.AspNetCore" Version="10.0.0" />
7676
<PackageVersion Include="SimpleBase" Version="5.6.4" />
7777
<PackageVersion Include="starkbank-ecdsa" Version="1.3.3" />
7878
<PackageVersion Include="System.IO.Hashing" Version="10.0.11" />
79-
<PackageVersion Include="System.Linq.Dynamic.Core" Version="1.7.3" />
79+
<PackageVersion Include="System.Linq.Dynamic.Core" Version="1.7.4" />
8080
<PackageVersion Include="System.Text.Json" Version="10.0.4" />
8181
<PackageVersion Include="SystemTextJsonPatch" Version="5.0.0" />
8282
<PackageVersion Include="TestHost.Abstracts" Version="2.0.1" />
8383
<PackageVersion Include="Testcontainers.MongoDb" Version="4.14.0" />
8484
<PackageVersion Include="Testcontainers.MsSql" Version="4.14.0" />
8585
<PackageVersion Include="TUnit" Version="1.65.68" />
86-
<PackageVersion Include="Twilio" Version="7.14.9" />
87-
<PackageVersion Include="Verify.TUnit" Version="31.28.0" />
86+
<PackageVersion Include="Twilio" Version="8.0.0" />
87+
<PackageVersion Include="Verify.TUnit" Version="32.0.0" />
8888
<PackageVersion Include="YamlDotNet" Version="18.1.0" />
8989
</ItemGroup>
9090
<!-- TFM-specific overrides -->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Arbiter.CommandQuery.Models;
2+
3+
using MessagePack;
4+
5+
namespace Arbiter.CommandQuery.Endpoints;
6+
7+
/// <summary>
8+
/// Represents claim-related endpoint output including headers and claims.
9+
/// </summary>
10+
[MessagePackObject(true)]
11+
public sealed class ClaimResult
12+
{
13+
/// <summary>
14+
/// Gets or sets the request headers.
15+
/// </summary>
16+
public IList<KeyValue<string, string>>? Headers { get; set; }
17+
18+
/// <summary>
19+
/// Gets or sets the request cookies.
20+
/// </summary>
21+
public IList<KeyValue<string, string>>? Cookies { get; set; }
22+
23+
/// <summary>
24+
/// Gets or sets the resolved claims.
25+
/// </summary>
26+
public IList<ClaimType>? Claims { get; set; }
27+
28+
}

src/Arbiter.CommandQuery.Endpoints/DiagnosticsEndpoint.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,24 +183,45 @@ private async Task<Results<Ok<HealthReportModel>, ProblemHttpResult>> HealthChec
183183
}
184184

185185
/// <summary>
186-
/// Returns the current user's claims.
186+
/// Returns request headers and the current user's claims.
187187
/// </summary>
188+
/// <param name="httpContext">The HTTP context containing the incoming request headers.</param>
188189
/// <param name="user">The current user principal.</param>
189190
/// <param name="cancellationToken">The cancellation token.</param>
190-
/// <returns>A list of claim type and value pairs.</returns>
191-
private async Task<Results<Ok<List<ClaimType>>, ProblemHttpResult>> ClaimsCheck(
191+
/// <returns>A claim result containing request headers and claim type/value pairs, or problem details when an error occurs.</returns>
192+
private async Task<Results<Ok<ClaimResult>, ProblemHttpResult>> ClaimsCheck(
193+
HttpContext httpContext,
192194
ClaimsPrincipal? user = default,
193195
CancellationToken cancellationToken = default)
194196
{
195197
LogClaimsCheckRequested(_logger, user?.Identity?.Name ?? "anonymous");
196198

197199
try
198200
{
201+
var headers = httpContext.Request.Headers
202+
.SelectMany(h => h.Value, (h, v) => new KeyValue<string, string> { Key = h.Key, Value = v })
203+
.OrderBy(c => c.Key, StringComparer.Ordinal)
204+
.ThenBy(c => c.Value, StringComparer.Ordinal)
205+
.ToList();
206+
207+
var cookies = httpContext.Request.Cookies
208+
.Select(c => new KeyValue<string, string> { Key = c.Key, Value = c.Value })
209+
.OrderBy(c => c.Key, StringComparer.Ordinal)
210+
.ToList();
211+
199212
var claims = user?.Claims
200213
.Select(c => new ClaimType { Type = c.Type, Value = c.Value })
214+
.OrderBy(c => c.Type, StringComparer.Ordinal)
201215
.ToList();
202216

203-
return TypedResults.Ok(claims);
217+
var results = new ClaimResult
218+
{
219+
Headers = headers,
220+
Cookies = cookies,
221+
Claims = claims,
222+
};
223+
224+
return TypedResults.Ok(results);
204225
}
205226
catch (Exception ex)
206227
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Text.Json.Serialization;
2+
3+
using MessagePack;
4+
5+
namespace Arbiter.CommandQuery.Models;
6+
7+
/// <summary>
8+
/// Represents a key and associated value.
9+
/// </summary>
10+
/// <typeparam name="TKey">The type of the key.</typeparam>
11+
/// <typeparam name="TValue">The type of the value associated with the key.</typeparam>
12+
[Equatable]
13+
[MessagePackObject(true)]
14+
public partial class KeyValue<TKey, TValue>
15+
{
16+
/// <summary>
17+
/// Gets or sets the key.
18+
/// </summary>
19+
[JsonPropertyName("key")]
20+
public TKey Key { get; set; } = default!;
21+
22+
/// <summary>
23+
/// Gets or sets the value associated with the key.
24+
/// </summary>
25+
[JsonPropertyName("value")]
26+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
27+
public TValue? Value { get; set; }
28+
29+
/// <summary>
30+
/// Returns a string representation of the key-value pair.
31+
/// </summary>
32+
/// <returns>A string representation of the key-value pair.</returns>
33+
public override string ToString()
34+
{
35+
return $"KeyValue: Key = {Key}, Value = {Value}";
36+
}
37+
}

0 commit comments

Comments
 (0)