|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
| 4 | +using System.Security.Claims; |
| 5 | +using System.Text.Json; |
4 | 6 | using Aspire.Dashboard.Configuration; |
5 | 7 | using Aspire.Hosting; |
| 8 | +using Microsoft.AspNetCore.Authentication.OpenIdConnect; |
| 9 | +using Microsoft.Extensions.Configuration; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using Microsoft.Extensions.Options; |
6 | 12 | using Xunit; |
| 13 | +using OpenIdConnectOptions = Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectOptions; |
7 | 14 |
|
8 | 15 | namespace Aspire.Dashboard.Tests; |
9 | 16 |
|
@@ -275,5 +282,168 @@ public void OpenIdConnectOptions_NoUserNameClaimType() |
275 | 282 | Assert.Equal("OpenID Connect claim type for username not configured. Specify a Dashboard:Frontend:OpenIdConnect:UsernameClaimType value.", result.FailureMessage); |
276 | 283 | } |
277 | 284 |
|
| 285 | + [Fact] |
| 286 | + public void OpenIdConnectOptions_ClaimActions_MapJsonKeyTest() |
| 287 | + { |
| 288 | + var app = new DashboardWebApplication(builder => builder.Configuration.AddInMemoryCollection( |
| 289 | + [ |
| 290 | + new("ASPNETCORE_URLS", "http://localhost:8000/"), |
| 291 | + new("ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL", "http://localhost:4319/"), |
| 292 | + new("Authentication:Schemes:OpenIdConnect:Authority", "https://id.aspire.dev/"), |
| 293 | + new("Authentication:Schemes:OpenIdConnect:ClientId", "aspire-dashboard"), |
| 294 | + new("Dashboard:Frontend:AuthMode", "OpenIdConnect"), |
| 295 | + new("Dashboard:Frontend:OpenIdConnect:ClaimActions:0:ClaimType", "role"), |
| 296 | + new("Dashboard:Frontend:OpenIdConnect:ClaimActions:0:JsonKey", "role"), |
| 297 | + new("Dashboard:Frontend:OpenIdConnect:RequiredClaimType", "role") |
| 298 | + ])); |
| 299 | + var openIdConnectAuthOptions = app.Services.GetService<IOptionsMonitor<OpenIdConnectOptions>>()?.Get(OpenIdConnectDefaults.AuthenticationScheme); |
| 300 | + Assert.NotNull(openIdConnectAuthOptions); |
| 301 | + Assert.NotEmpty(openIdConnectAuthOptions.ClaimActions); |
| 302 | + var claimAction = openIdConnectAuthOptions.ClaimActions.FirstOrDefault(x => x.ClaimType == "role"); |
| 303 | + Assert.NotNull(claimAction); |
| 304 | + Assert.Equal("role", claimAction.ClaimType); |
| 305 | + var jsonElement = JsonDocument.Parse(""" |
| 306 | + { |
| 307 | + "role": ["admin", "test"] |
| 308 | + } |
| 309 | + """).RootElement.Clone(); |
| 310 | + var claimIdentity = new ClaimsIdentity(); |
| 311 | + claimAction.Run(jsonElement, claimIdentity, "test"); |
| 312 | + Assert.Equal(2, claimIdentity.Claims.Count()); |
| 313 | + Assert.True(claimIdentity.HasClaim("role", "admin")); |
| 314 | + Assert.True(claimIdentity.HasClaim("role", "test")); |
| 315 | + } |
| 316 | + |
| 317 | + [Fact] |
| 318 | + public void GetOidcClaimActionConfigure_MapJsonKeyTest() |
| 319 | + { |
| 320 | + var claimAction = new ClaimAction |
| 321 | + { |
| 322 | + ClaimType = "role", |
| 323 | + JsonKey = "role" |
| 324 | + }; |
| 325 | + var oidcOption = new OpenIdConnectOptions(); |
| 326 | + oidcOption.ClaimActions.Clear(); |
| 327 | + var configure = DashboardWebApplication.GetOidcClaimActionConfigure(claimAction); |
| 328 | + configure(oidcOption); |
| 329 | + Assert.Single(oidcOption.ClaimActions); |
| 330 | + Assert.Contains(oidcOption.ClaimActions, x => x.ClaimType == claimAction.ClaimType && x.ValueType == ClaimValueTypes.String); |
| 331 | + var action = oidcOption.ClaimActions.FirstOrDefault(x => x.ClaimType == claimAction.ClaimType); |
| 332 | + Assert.NotNull(action); |
| 333 | + var jsonElement = JsonDocument.Parse(""" |
| 334 | + { |
| 335 | + "role": ["admin", "test"] |
| 336 | + } |
| 337 | + """).RootElement.Clone(); |
| 338 | + var claimIdentity = new ClaimsIdentity(); |
| 339 | + action.Run(jsonElement, claimIdentity, "test"); |
| 340 | + Assert.Equal(2, claimIdentity.Claims.Count()); |
| 341 | + Assert.True(claimIdentity.HasClaim("role", "admin")); |
| 342 | + Assert.True(claimIdentity.HasClaim("role", "test")); |
| 343 | + } |
| 344 | + |
| 345 | + [Fact] |
| 346 | + public void GetOidcClaimActionConfigure_MapUniqueJsonKeyTest() |
| 347 | + { |
| 348 | + var claimAction = new ClaimAction |
| 349 | + { |
| 350 | + ClaimType = "name", |
| 351 | + JsonKey = "name", |
| 352 | + IsUnique = true |
| 353 | + }; |
| 354 | + var oidcOption = new OpenIdConnectOptions(); |
| 355 | + oidcOption.ClaimActions.Clear(); |
| 356 | + var configure = DashboardWebApplication.GetOidcClaimActionConfigure(claimAction); |
| 357 | + configure(oidcOption); |
| 358 | + Assert.Single(oidcOption.ClaimActions); |
| 359 | + Assert.Contains(oidcOption.ClaimActions, x => x.ClaimType == claimAction.ClaimType && x.ValueType == ClaimValueTypes.String); |
| 360 | + var action = oidcOption.ClaimActions.FirstOrDefault(x => x.ClaimType == claimAction.ClaimType); |
| 361 | + Assert.NotNull(action); |
| 362 | + var jsonElement = JsonDocument.Parse(""" |
| 363 | + { |
| 364 | + "name": "test" |
| 365 | + } |
| 366 | + """).RootElement.Clone(); |
| 367 | + var claimIdentity = new ClaimsIdentity( |
| 368 | + [ |
| 369 | + new Claim("name", "test") |
| 370 | + ]); |
| 371 | + action.Run(jsonElement, claimIdentity, "test"); |
| 372 | + Assert.Single(claimIdentity.Claims); |
| 373 | + Assert.True(claimIdentity.HasClaim("name", "test")); |
| 374 | + |
| 375 | + var emptyClaimIdentity = new ClaimsIdentity(); |
| 376 | + action.Run(jsonElement, emptyClaimIdentity, "test"); |
| 377 | + Assert.Single(emptyClaimIdentity.Claims); |
| 378 | + Assert.True(emptyClaimIdentity.HasClaim("name", "test")); |
| 379 | + } |
| 380 | + |
| 381 | + [Fact] |
| 382 | + public void GetOidcClaimActionConfigure_MapJsonSubKeyTest() |
| 383 | + { |
| 384 | + var claimAction = new ClaimAction |
| 385 | + { |
| 386 | + ClaimType = "name", |
| 387 | + JsonKey = "profile", |
| 388 | + SubKey = "name" |
| 389 | + }; |
| 390 | + var oidcOption = new OpenIdConnectOptions(); |
| 391 | + oidcOption.ClaimActions.Clear(); |
| 392 | + var configure = DashboardWebApplication.GetOidcClaimActionConfigure(claimAction); |
| 393 | + configure(oidcOption); |
| 394 | + Assert.Single(oidcOption.ClaimActions); |
| 395 | + Assert.Contains(oidcOption.ClaimActions, x => x.ClaimType == claimAction.ClaimType && x.ValueType == ClaimValueTypes.String); |
| 396 | + var action = oidcOption.ClaimActions.FirstOrDefault(x => x.ClaimType == claimAction.ClaimType); |
| 397 | + Assert.NotNull(action); |
| 398 | + var jsonElement = JsonDocument.Parse(""" |
| 399 | + { |
| 400 | + "profile": { |
| 401 | + "name": "test" |
| 402 | + } |
| 403 | + } |
| 404 | + """).RootElement.Clone(); |
| 405 | + var claimIdentity = new ClaimsIdentity( |
| 406 | + [ |
| 407 | + new Claim("name", "test") |
| 408 | + ]); |
| 409 | + action.Run(jsonElement, claimIdentity, "test"); |
| 410 | + Assert.Equal(2, claimIdentity.Claims.Count()); |
| 411 | + Assert.True(claimIdentity.HasClaim("name", "test")); |
| 412 | + |
| 413 | + var emptyClaimIdentity = new ClaimsIdentity(); |
| 414 | + action.Run(jsonElement, emptyClaimIdentity, "test"); |
| 415 | + Assert.Single(emptyClaimIdentity.Claims); |
| 416 | + Assert.True(emptyClaimIdentity.HasClaim("name", "test")); |
| 417 | + } |
| 418 | + |
| 419 | + [Fact] |
| 420 | + public void GetOidcClaimActionConfigure_MapJsonKey_ValueTypeTest() |
| 421 | + { |
| 422 | + var claimAction = new ClaimAction |
| 423 | + { |
| 424 | + ClaimType = "sub", |
| 425 | + JsonKey = "userId", |
| 426 | + ValueType = ClaimValueTypes.Integer, |
| 427 | + IsUnique = true |
| 428 | + }; |
| 429 | + var oidcOption = new OpenIdConnectOptions(); |
| 430 | + oidcOption.ClaimActions.Clear(); |
| 431 | + var configure = DashboardWebApplication.GetOidcClaimActionConfigure(claimAction); |
| 432 | + configure(oidcOption); |
| 433 | + Assert.Single(oidcOption.ClaimActions); |
| 434 | + Assert.Contains(oidcOption.ClaimActions, x => x.ClaimType == claimAction.ClaimType && x.ValueType == claimAction.ValueType); |
| 435 | + var action = oidcOption.ClaimActions.FirstOrDefault(x => x.ClaimType == claimAction.ClaimType); |
| 436 | + Assert.NotNull(action); |
| 437 | + var jsonElement = JsonDocument.Parse(""" |
| 438 | + { |
| 439 | + "userId": "1" |
| 440 | + } |
| 441 | + """).RootElement.Clone(); |
| 442 | + var claimIdentity = new ClaimsIdentity(); |
| 443 | + action.Run(jsonElement, claimIdentity, "test"); |
| 444 | + Assert.NotEmpty(claimIdentity.Claims); |
| 445 | + Assert.True(claimIdentity.HasClaim("sub", "1")); |
| 446 | + } |
| 447 | + |
278 | 448 | #endregion |
279 | 449 | } |
0 commit comments