|
12 | 12 | using Microsoft.AspNetCore.Authentication;
|
13 | 13 | using Microsoft.AspNetCore.Http;
|
14 | 14 | using Microsoft.Extensions.DependencyInjection;
|
| 15 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
15 | 16 | using Microsoft.Extensions.Options;
|
16 | 17 | using Microsoft.IdentityModel.Logging;
|
17 | 18 | using Microsoft.IdentityModel.Tokens;
|
@@ -436,5 +437,119 @@ static void ConfigureServices(IServiceCollection services)
|
436 | 437 | second.ShouldNotBeEmpty();
|
437 | 438 | first.ShouldNotBeSameAs(second);
|
438 | 439 | }
|
| 440 | + |
| 441 | + [Fact] |
| 442 | + public async Task Custom_Events_Are_Raised_By_Handler() |
| 443 | + { |
| 444 | + // Arrange |
| 445 | + bool onGenerateClientSecretEventRaised = false; |
| 446 | + bool onValidateIdTokenEventRaised = false; |
| 447 | + |
| 448 | + void ConfigureServices(IServiceCollection services) |
| 449 | + { |
| 450 | + services.PostConfigureAll<AppleAuthenticationOptions>((options) => |
| 451 | + { |
| 452 | + var onGenerateClientSecret = options.Events.OnGenerateClientSecret; |
| 453 | + |
| 454 | + options.Events.OnGenerateClientSecret = async (context) => |
| 455 | + { |
| 456 | + await onGenerateClientSecret(context); |
| 457 | + onGenerateClientSecretEventRaised = true; |
| 458 | + }; |
| 459 | + |
| 460 | + var onValidateIdToken = options.Events.OnValidateIdToken; |
| 461 | + |
| 462 | + options.Events.OnValidateIdToken = async (context) => |
| 463 | + { |
| 464 | + await onValidateIdToken(context); |
| 465 | + onValidateIdTokenEventRaised = true; |
| 466 | + }; |
| 467 | + |
| 468 | + options.ClientSecret = string.Empty; |
| 469 | + options.GenerateClientSecret = true; |
| 470 | + options.JwtSecurityTokenHandler = new FrozenJwtSecurityTokenHandler(); |
| 471 | + options.KeyId = "my-key-id"; |
| 472 | + options.TeamId = "my-team-id"; |
| 473 | + options.ValidateTokens = true; |
| 474 | + options.PrivateKeyBytes = async (keyId) => |
| 475 | + { |
| 476 | + Assert.Equal("my-key-id", keyId); |
| 477 | + return await TestKeys.GetPrivateKeyBytesAsync(); |
| 478 | + }; |
| 479 | + }); |
| 480 | + } |
| 481 | + |
| 482 | + using var server = CreateTestServer(ConfigureServices); |
| 483 | + |
| 484 | + // Act |
| 485 | + var claims = await AuthenticateUserAsync(server); |
| 486 | + |
| 487 | + // Assert |
| 488 | + onGenerateClientSecretEventRaised.ShouldBeTrue(); |
| 489 | + onValidateIdTokenEventRaised.ShouldBeTrue(); |
| 490 | + } |
| 491 | + |
| 492 | + [Fact] |
| 493 | + public async Task Custom_Events_Are_Raised_By_Handler_Using_Custom_Events_Type() |
| 494 | + { |
| 495 | + // Arrange |
| 496 | + bool onGenerateClientSecretEventRaised = false; |
| 497 | + bool onValidateIdTokenEventRaised = false; |
| 498 | + |
| 499 | + void ConfigureServices(IServiceCollection services) |
| 500 | + { |
| 501 | + services.TryAddScoped((_) => |
| 502 | + { |
| 503 | + var events = new CustomAppleAuthenticationEvents(); |
| 504 | + |
| 505 | + var onGenerateClientSecret = events.OnGenerateClientSecret; |
| 506 | + |
| 507 | + events.OnGenerateClientSecret = async (context) => |
| 508 | + { |
| 509 | + await onGenerateClientSecret(context); |
| 510 | + onGenerateClientSecretEventRaised = true; |
| 511 | + }; |
| 512 | + |
| 513 | + var onValidateIdToken = events.OnValidateIdToken; |
| 514 | + |
| 515 | + events.OnValidateIdToken = async (context) => |
| 516 | + { |
| 517 | + await onValidateIdToken(context); |
| 518 | + onValidateIdTokenEventRaised = true; |
| 519 | + }; |
| 520 | + |
| 521 | + return events; |
| 522 | + }); |
| 523 | + |
| 524 | + services.PostConfigureAll<AppleAuthenticationOptions>((options) => |
| 525 | + { |
| 526 | + options.ClientSecret = string.Empty; |
| 527 | + options.EventsType = typeof(CustomAppleAuthenticationEvents); |
| 528 | + options.GenerateClientSecret = true; |
| 529 | + options.JwtSecurityTokenHandler = new FrozenJwtSecurityTokenHandler(); |
| 530 | + options.KeyId = "my-key-id"; |
| 531 | + options.TeamId = "my-team-id"; |
| 532 | + options.ValidateTokens = true; |
| 533 | + options.PrivateKeyBytes = async (keyId) => |
| 534 | + { |
| 535 | + Assert.Equal("my-key-id", keyId); |
| 536 | + return await TestKeys.GetPrivateKeyBytesAsync(); |
| 537 | + }; |
| 538 | + }); |
| 539 | + } |
| 540 | + |
| 541 | + using var server = CreateTestServer(ConfigureServices); |
| 542 | + |
| 543 | + // Act |
| 544 | + var claims = await AuthenticateUserAsync(server); |
| 545 | + |
| 546 | + // Assert |
| 547 | + onGenerateClientSecretEventRaised.ShouldBeTrue(); |
| 548 | + onValidateIdTokenEventRaised.ShouldBeTrue(); |
| 549 | + } |
| 550 | + |
| 551 | + private sealed class CustomAppleAuthenticationEvents : AppleAuthenticationEvents |
| 552 | + { |
| 553 | + } |
439 | 554 | }
|
440 | 555 | }
|
0 commit comments