Skip to content

Commit 891b7dd

Browse files
Fix ValidateIdToken not being raised with EventsType
Fix the OnValidateIdToken event callback not running when the EventsType property is used with the Apple provider.
1 parent 9ff8009 commit 891b7dd

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

src/AspNet.Security.OAuth.Apple/AppleAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
9696
if (Options.ValidateTokens)
9797
{
9898
var validateIdContext = new AppleValidateIdTokenContext(Context, Scheme, Options, idToken);
99-
await Options.Events.ValidateIdToken(validateIdContext);
99+
await Events.ValidateIdToken(validateIdContext);
100100
}
101101

102102
var tokenClaims = ExtractClaimsFromToken(idToken);

test/AspNet.Security.OAuth.Providers.Tests/Apple/AppleTests.cs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.AspNetCore.Authentication;
1313
using Microsoft.AspNetCore.Http;
1414
using Microsoft.Extensions.DependencyInjection;
15+
using Microsoft.Extensions.DependencyInjection.Extensions;
1516
using Microsoft.Extensions.Options;
1617
using Microsoft.IdentityModel.Logging;
1718
using Microsoft.IdentityModel.Tokens;
@@ -436,5 +437,119 @@ static void ConfigureServices(IServiceCollection services)
436437
second.ShouldNotBeEmpty();
437438
first.ShouldNotBeSameAs(second);
438439
}
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+
}
439554
}
440555
}

0 commit comments

Comments
 (0)