From 2aba485263ec8b8cb0c3b9ee88925e6c536a3802 Mon Sep 17 00:00:00 2001 From: Chris R Date: Wed, 2 Sep 2015 15:07:26 -0700 Subject: [PATCH] Move Context objects to OIDC and JwtBearer, remove generics. --- .../Events/AuthenticationChallengeContext.cs | 5 +- .../Events/AuthenticationFailedContext.cs | 9 ++- .../Events/JwtBearerAuthenticationEvents.cs | 10 ++-- .../Events/MessageReceivedContext.cs | 9 ++- .../Events/SecurityTokenReceivedContext.cs | 9 ++- .../Events/SecurityTokenValidatedContext.cs | 9 ++- .../JwtBearerAuthenticationHandler.cs | 25 +++----- .../Events/IOAuthAuthenticationEvents.cs | 2 - .../Events/AuthenticationFailedContext.cs | 21 +++++++ .../AuthorizationCodeReceivedContext.cs | 7 ++- .../AuthorizationCodeRedeemedContext.cs | 3 +- .../Events/MessageReceivedContext.cs | 23 ++++++++ .../OpenIdConnectAuthenticationEvents.cs | 13 ++-- .../RedirectFromIdentityProviderContext.cs | 9 +-- .../RedirectToIdentityProviderContext.cs | 14 +++-- .../Events/SecurityTokenReceivedContext.cs | 20 +++++++ .../Events/SecurityTokenValidatedContext.cs | 18 ++++++ .../OpenIdConnectAuthenticationHandler.cs | 59 +++++++++---------- 18 files changed, 166 insertions(+), 99 deletions(-) rename src/{Microsoft.AspNet.Authentication => Microsoft.AspNet.Authentication.JwtBearer}/Events/AuthenticationFailedContext.cs (60%) rename src/{Microsoft.AspNet.Authentication => Microsoft.AspNet.Authentication.JwtBearer}/Events/MessageReceivedContext.cs (59%) rename src/{Microsoft.AspNet.Authentication => Microsoft.AspNet.Authentication.JwtBearer}/Events/SecurityTokenReceivedContext.cs (59%) rename src/{Microsoft.AspNet.Authentication => Microsoft.AspNet.Authentication.JwtBearer}/Events/SecurityTokenValidatedContext.cs (55%) create mode 100644 src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/AuthenticationFailedContext.cs create mode 100644 src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/MessageReceivedContext.cs rename src/{Microsoft.AspNet.Authentication => Microsoft.AspNet.Authentication.OpenIdConnect}/Events/RedirectFromIdentityProviderContext.cs (56%) rename src/{Microsoft.AspNet.Authentication => Microsoft.AspNet.Authentication.OpenIdConnect}/Events/RedirectToIdentityProviderContext.cs (61%) create mode 100644 src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/SecurityTokenReceivedContext.cs create mode 100644 src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/SecurityTokenValidatedContext.cs diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/AuthenticationChallengeContext.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/AuthenticationChallengeContext.cs index 58829ba879..702ccc4527 100644 --- a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/AuthenticationChallengeContext.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/AuthenticationChallengeContext.cs @@ -5,9 +5,10 @@ using Microsoft.AspNet.Http; namespace Microsoft.AspNet.Authentication.JwtBearer { - public class AuthenticationChallengeContext : BaseControlContext + public class AuthenticationChallengeContext : BaseControlContext { - public AuthenticationChallengeContext(HttpContext context, TOptions options) : base(context, options) + public AuthenticationChallengeContext(HttpContext context, JwtBearerAuthenticationOptions options) + : base(context, options) { } } diff --git a/src/Microsoft.AspNet.Authentication/Events/AuthenticationFailedContext.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/AuthenticationFailedContext.cs similarity index 60% rename from src/Microsoft.AspNet.Authentication/Events/AuthenticationFailedContext.cs rename to src/Microsoft.AspNet.Authentication.JwtBearer/Events/AuthenticationFailedContext.cs index 33e99076e6..f4301c4c6f 100644 --- a/src/Microsoft.AspNet.Authentication/Events/AuthenticationFailedContext.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/AuthenticationFailedContext.cs @@ -4,16 +4,15 @@ using System; using Microsoft.AspNet.Http; -namespace Microsoft.AspNet.Authentication +namespace Microsoft.AspNet.Authentication.JwtBearer { - public class AuthenticationFailedContext : BaseControlContext + public class AuthenticationFailedContext : BaseControlContext { - public AuthenticationFailedContext(HttpContext context, TOptions options) : base(context, options) + public AuthenticationFailedContext(HttpContext context, JwtBearerAuthenticationOptions options) + : base(context, options) { } public Exception Exception { get; set; } - - public TMessage ProtocolMessage { get; set; } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/JwtBearerAuthenticationEvents.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/JwtBearerAuthenticationEvents.cs index e2d1f500f3..a8d674d1e9 100644 --- a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/JwtBearerAuthenticationEvents.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/JwtBearerAuthenticationEvents.cs @@ -30,26 +30,26 @@ namespace Microsoft.AspNet.Authentication.JwtBearer /// /// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed. /// - public Func, Task> AuthenticationFailed { get; set; } + public Func AuthenticationFailed { get; set; } /// /// Invoked when a protocol message is first received. /// - public Func, Task> MessageReceived { get; set; } + public Func MessageReceived { get; set; } /// /// Invoked with the security token that has been extracted from the protocol message. /// - public Func, Task> SecurityTokenReceived { get; set; } + public Func SecurityTokenReceived { get; set; } /// /// Invoked after the security token has passed validation and a ClaimsIdentity has been generated. /// - public Func, Task> SecurityTokenValidated { get; set; } + public Func SecurityTokenValidated { get; set; } /// /// Invoked to apply a challenge sent back to the caller. /// - public Func, Task> ApplyChallenge { get; set; } + public Func ApplyChallenge { get; set; } } } diff --git a/src/Microsoft.AspNet.Authentication/Events/MessageReceivedContext.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/MessageReceivedContext.cs similarity index 59% rename from src/Microsoft.AspNet.Authentication/Events/MessageReceivedContext.cs rename to src/Microsoft.AspNet.Authentication.JwtBearer/Events/MessageReceivedContext.cs index 86babe7277..e9e8955786 100644 --- a/src/Microsoft.AspNet.Authentication/Events/MessageReceivedContext.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/MessageReceivedContext.cs @@ -3,16 +3,15 @@ using Microsoft.AspNet.Http; -namespace Microsoft.AspNet.Authentication +namespace Microsoft.AspNet.Authentication.JwtBearer { - public class MessageReceivedContext : BaseControlContext + public class MessageReceivedContext : BaseControlContext { - public MessageReceivedContext(HttpContext context, TOptions options) : base(context, options) + public MessageReceivedContext(HttpContext context, JwtBearerAuthenticationOptions options) + : base(context, options) { } - public TMessage ProtocolMessage { get; set; } - /// /// Bearer Token. This will give application an opportunity to retrieve token from an alternation location. /// diff --git a/src/Microsoft.AspNet.Authentication/Events/SecurityTokenReceivedContext.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/SecurityTokenReceivedContext.cs similarity index 59% rename from src/Microsoft.AspNet.Authentication/Events/SecurityTokenReceivedContext.cs rename to src/Microsoft.AspNet.Authentication.JwtBearer/Events/SecurityTokenReceivedContext.cs index 225f4fd6de..672021ca6b 100644 --- a/src/Microsoft.AspNet.Authentication/Events/SecurityTokenReceivedContext.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/SecurityTokenReceivedContext.cs @@ -3,16 +3,15 @@ using Microsoft.AspNet.Http; -namespace Microsoft.AspNet.Authentication +namespace Microsoft.AspNet.Authentication.JwtBearer { - public class SecurityTokenReceivedContext : BaseControlContext + public class SecurityTokenReceivedContext : BaseControlContext { - public SecurityTokenReceivedContext(HttpContext context, TOptions options) : base(context, options) + public SecurityTokenReceivedContext(HttpContext context, JwtBearerAuthenticationOptions options) + : base(context, options) { } public string SecurityToken { get; set; } - - public TMessage ProtocolMessage { get; set; } } } diff --git a/src/Microsoft.AspNet.Authentication/Events/SecurityTokenValidatedContext.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/SecurityTokenValidatedContext.cs similarity index 55% rename from src/Microsoft.AspNet.Authentication/Events/SecurityTokenValidatedContext.cs rename to src/Microsoft.AspNet.Authentication.JwtBearer/Events/SecurityTokenValidatedContext.cs index 4acf056a2e..383284e4ae 100644 --- a/src/Microsoft.AspNet.Authentication/Events/SecurityTokenValidatedContext.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/SecurityTokenValidatedContext.cs @@ -3,14 +3,13 @@ using Microsoft.AspNet.Http; -namespace Microsoft.AspNet.Authentication +namespace Microsoft.AspNet.Authentication.JwtBearer { - public class SecurityTokenValidatedContext : BaseControlContext + public class SecurityTokenValidatedContext : BaseControlContext { - public SecurityTokenValidatedContext(HttpContext context, TOptions options) : base(context, options) + public SecurityTokenValidatedContext(HttpContext context, JwtBearerAuthenticationOptions options) + : base(context, options) { } - - public TMessage ProtocolMessage { get; set; } } } diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerAuthenticationHandler.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerAuthenticationHandler.cs index 74edec3f67..b2e6cfbd8c 100644 --- a/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerAuthenticationHandler.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerAuthenticationHandler.cs @@ -27,11 +27,7 @@ namespace Microsoft.AspNet.Authentication.JwtBearer try { // Give application opportunity to find from a different location, adjust, or reject token - var messageReceivedContext = - new MessageReceivedContext(Context, Options) - { - ProtocolMessage = Context, - }; + var messageReceivedContext = new MessageReceivedContext(Context, Options); // event can set the token await Options.Events.MessageReceived(messageReceivedContext); @@ -71,10 +67,8 @@ namespace Microsoft.AspNet.Authentication.JwtBearer } // notify user token was received - var securityTokenReceivedContext = - new SecurityTokenReceivedContext(Context, Options) + var securityTokenReceivedContext = new SecurityTokenReceivedContext(Context, Options) { - ProtocolMessage = Context, SecurityToken = token, }; @@ -117,9 +111,8 @@ namespace Microsoft.AspNet.Authentication.JwtBearer { var principal = validator.ValidateToken(token, validationParameters, out validatedToken); var ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), Options.AuthenticationScheme); - var securityTokenValidatedContext = new SecurityTokenValidatedContext(Context, Options) + var securityTokenValidatedContext = new SecurityTokenValidatedContext(Context, Options) { - ProtocolMessage = Context, AuthenticationTicket = ticket }; @@ -150,12 +143,10 @@ namespace Microsoft.AspNet.Authentication.JwtBearer Options.ConfigurationManager.RequestRefresh(); } - var authenticationFailedContext = - new AuthenticationFailedContext(Context, Options) - { - ProtocolMessage = Context, - Exception = ex - }; + var authenticationFailedContext = new AuthenticationFailedContext(Context, Options) + { + Exception = ex + }; await Options.Events.AuthenticationFailed(authenticationFailedContext); if (authenticationFailedContext.HandledResponse) @@ -175,7 +166,7 @@ namespace Microsoft.AspNet.Authentication.JwtBearer protected override async Task HandleUnauthorizedAsync(ChallengeContext context) { Response.StatusCode = 401; - await Options.Events.ApplyChallenge(new AuthenticationChallengeContext(Context, Options)); + await Options.Events.ApplyChallenge(new AuthenticationChallengeContext(Context, Options)); return false; } diff --git a/src/Microsoft.AspNet.Authentication.OAuth/Events/IOAuthAuthenticationEvents.cs b/src/Microsoft.AspNet.Authentication.OAuth/Events/IOAuthAuthenticationEvents.cs index 5f402aa072..897b889206 100644 --- a/src/Microsoft.AspNet.Authentication.OAuth/Events/IOAuthAuthenticationEvents.cs +++ b/src/Microsoft.AspNet.Authentication.OAuth/Events/IOAuthAuthenticationEvents.cs @@ -1,9 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System.Security.Claims; using System.Threading.Tasks; -using Microsoft.AspNet.Http.Authentication; namespace Microsoft.AspNet.Authentication.OAuth { diff --git a/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/AuthenticationFailedContext.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/AuthenticationFailedContext.cs new file mode 100644 index 0000000000..7521ed7ce0 --- /dev/null +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/AuthenticationFailedContext.cs @@ -0,0 +1,21 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNet.Http; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; + +namespace Microsoft.AspNet.Authentication.OpenIdConnect +{ + public class AuthenticationFailedContext : BaseControlContext + { + public AuthenticationFailedContext(HttpContext context, OpenIdConnectAuthenticationOptions options) + : base(context, options) + { + } + + public Exception Exception { get; set; } + + public OpenIdConnectMessage ProtocolMessage { get; set; } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/AuthorizationCodeReceivedContext.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/AuthorizationCodeReceivedContext.cs index 5fdd57e6b8..22c00742de 100644 --- a/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/AuthorizationCodeReceivedContext.cs +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/AuthorizationCodeReceivedContext.cs @@ -1,10 +1,10 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using Microsoft.AspNet.Http; -using Microsoft.IdentityModel.Protocols.OpenIdConnect; using System.Diagnostics.CodeAnalysis; using System.IdentityModel.Tokens.Jwt; +using Microsoft.AspNet.Http; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; namespace Microsoft.AspNet.Authentication.OpenIdConnect { @@ -16,7 +16,8 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect /// /// Creates a /// - public AuthorizationCodeReceivedContext(HttpContext context, OpenIdConnectAuthenticationOptions options) : base(context, options) + public AuthorizationCodeReceivedContext(HttpContext context, OpenIdConnectAuthenticationOptions options) + : base(context, options) { } diff --git a/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/AuthorizationCodeRedeemedContext.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/AuthorizationCodeRedeemedContext.cs index 0b54f6ae8e..c511832cab 100644 --- a/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/AuthorizationCodeRedeemedContext.cs +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/AuthorizationCodeRedeemedContext.cs @@ -11,7 +11,8 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect /// /// Creates a /// - public AuthorizationCodeRedeemedContext(HttpContext context, OpenIdConnectAuthenticationOptions options) : base(context, options) + public AuthorizationCodeRedeemedContext(HttpContext context, OpenIdConnectAuthenticationOptions options) + : base(context, options) { } diff --git a/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/MessageReceivedContext.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/MessageReceivedContext.cs new file mode 100644 index 0000000000..ece53d3dda --- /dev/null +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/MessageReceivedContext.cs @@ -0,0 +1,23 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Http; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; + +namespace Microsoft.AspNet.Authentication.OpenIdConnect +{ + public class MessageReceivedContext : BaseControlContext + { + public MessageReceivedContext(HttpContext context, OpenIdConnectAuthenticationOptions options) + : base(context, options) + { + } + + public OpenIdConnectMessage ProtocolMessage { get; set; } + + /// + /// Bearer Token. This will give application an opportunity to retrieve token from an alternation location. + /// + public string Token { get; set; } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/OpenIdConnectAuthenticationEvents.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/OpenIdConnectAuthenticationEvents.cs index a084eb84c7..134ce48929 100644 --- a/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/OpenIdConnectAuthenticationEvents.cs +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/OpenIdConnectAuthenticationEvents.cs @@ -3,7 +3,6 @@ using System; using System.Threading.Tasks; -using Microsoft.IdentityModel.Protocols.OpenIdConnect; namespace Microsoft.AspNet.Authentication.OpenIdConnect { @@ -21,15 +20,15 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect AuthorizationCodeReceived = context => Task.FromResult(0); AuthorizationCodeRedeemed = context => Task.FromResult(0); MessageReceived = context => Task.FromResult(0); + RedirectToIdentityProvider = context => Task.FromResult(0); SecurityTokenReceived = context => Task.FromResult(0); SecurityTokenValidated = context => Task.FromResult(0); - RedirectToIdentityProvider = context => Task.FromResult(0); } /// /// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed. /// - public Func, Task> AuthenticationFailed { get; set; } + public Func AuthenticationFailed { get; set; } /// /// Invoked after security token validation if an authorization code is present in the protocol message. @@ -44,21 +43,21 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect /// /// Invoked when a protocol message is first received. /// - public Func, Task> MessageReceived { get; set; } + public Func MessageReceived { get; set; } /// /// Invoked to manipulate redirects to the identity provider for SignIn, SignOut, or Challenge. /// - public Func, Task> RedirectToIdentityProvider { get; set; } + public Func RedirectToIdentityProvider { get; set; } /// /// Invoked with the security token that has been extracted from the protocol message. /// - public Func, Task> SecurityTokenReceived { get; set; } + public Func SecurityTokenReceived { get; set; } /// /// Invoked after the security token has passed validation and a ClaimsIdentity has been generated. /// - public Func, Task> SecurityTokenValidated { get; set; } + public Func SecurityTokenValidated { get; set; } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Authentication/Events/RedirectFromIdentityProviderContext.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/RedirectFromIdentityProviderContext.cs similarity index 56% rename from src/Microsoft.AspNet.Authentication/Events/RedirectFromIdentityProviderContext.cs rename to src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/RedirectFromIdentityProviderContext.cs index 9de5cd1a00..fb55ec53e6 100644 --- a/src/Microsoft.AspNet.Authentication/Events/RedirectFromIdentityProviderContext.cs +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/RedirectFromIdentityProviderContext.cs @@ -2,12 +2,13 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNet.Http; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; -namespace Microsoft.AspNet.Authentication +namespace Microsoft.AspNet.Authentication.OpenIdConnect { - public class RedirectFromIdentityProviderContext : BaseControlContext + public class RedirectFromIdentityProviderContext : BaseControlContext { - public RedirectFromIdentityProviderContext(HttpContext context, TOptions options) + public RedirectFromIdentityProviderContext(HttpContext context, OpenIdConnectAuthenticationOptions options) : base(context, options) { } @@ -16,6 +17,6 @@ namespace Microsoft.AspNet.Authentication public bool IsRequestCompleted { get; set; } - public TMessage ProtocolMessage { get; set; } + public OpenIdConnectMessage ProtocolMessage { get; set; } } } diff --git a/src/Microsoft.AspNet.Authentication/Events/RedirectToIdentityProviderContext.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/RedirectToIdentityProviderContext.cs similarity index 61% rename from src/Microsoft.AspNet.Authentication/Events/RedirectToIdentityProviderContext.cs rename to src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/RedirectToIdentityProviderContext.cs index 9b95794745..705421bdbc 100644 --- a/src/Microsoft.AspNet.Authentication/Events/RedirectToIdentityProviderContext.cs +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/RedirectToIdentityProviderContext.cs @@ -4,25 +4,27 @@ using System; using Microsoft.AspNet.Http; using Microsoft.Framework.Internal; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; -namespace Microsoft.AspNet.Authentication +namespace Microsoft.AspNet.Authentication.OpenIdConnect { /// /// When a user configures the to be notified prior to redirecting to an IdentityProvider - /// an instance of is passed to the 'RedirectToIdentityProviderContext". + /// an instance of is passed to the 'RedirectToIdentityProviderContext". /// /// protocol specific message. /// protocol specific options. - public class RedirectToIdentityProviderContext : BaseControlContext + public class RedirectToIdentityProviderContext : BaseControlContext { - public RedirectToIdentityProviderContext([NotNull] HttpContext context, [NotNull] TOptions options) : base(context, options) + public RedirectToIdentityProviderContext([NotNull] HttpContext context, [NotNull] OpenIdConnectAuthenticationOptions options) + : base(context, options) { } /// - /// Gets or sets the . + /// Gets or sets the . /// /// if 'value' is null. - public TMessage ProtocolMessage { get; [param: NotNull] set; } + public OpenIdConnectMessage ProtocolMessage { get; [param: NotNull] set; } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/SecurityTokenReceivedContext.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/SecurityTokenReceivedContext.cs new file mode 100644 index 0000000000..0b960e033b --- /dev/null +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/SecurityTokenReceivedContext.cs @@ -0,0 +1,20 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Http; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; + +namespace Microsoft.AspNet.Authentication.OpenIdConnect +{ + public class SecurityTokenReceivedContext : BaseControlContext + { + public SecurityTokenReceivedContext(HttpContext context, OpenIdConnectAuthenticationOptions options) + : base(context, options) + { + } + + public string SecurityToken { get; set; } + + public OpenIdConnectMessage ProtocolMessage { get; set; } + } +} diff --git a/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/SecurityTokenValidatedContext.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/SecurityTokenValidatedContext.cs new file mode 100644 index 0000000000..e0d12c50a4 --- /dev/null +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/SecurityTokenValidatedContext.cs @@ -0,0 +1,18 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Http; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; + +namespace Microsoft.AspNet.Authentication.OpenIdConnect +{ + public class SecurityTokenValidatedContext : BaseControlContext + { + public SecurityTokenValidatedContext(HttpContext context, OpenIdConnectAuthenticationOptions options) + : base(context, options) + { + } + + public OpenIdConnectMessage ProtocolMessage { get; set; } + } +} diff --git a/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectAuthenticationHandler.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectAuthenticationHandler.cs index 8452f2fd26..8ef64f6046 100644 --- a/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectAuthenticationHandler.cs +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectAuthenticationHandler.cs @@ -89,7 +89,7 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect message.PostLogoutRedirectUri = Options.PostLogoutRedirectUri; } - var redirectToIdentityProviderContext = new RedirectToIdentityProviderContext(Context, Options) + var redirectToIdentityProviderContext = new RedirectToIdentityProviderContext(Context, Options) { ProtocolMessage = message }; @@ -220,11 +220,10 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect } } - var redirectToIdentityProviderContext = - new RedirectToIdentityProviderContext(Context, Options) - { - ProtocolMessage = message - }; + var redirectToIdentityProviderContext = new RedirectToIdentityProviderContext(Context, Options) + { + ProtocolMessage = message + }; await Options.Events.RedirectToIdentityProvider(redirectToIdentityProviderContext); if (redirectToIdentityProviderContext.HandledResponse) @@ -745,14 +744,13 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect } } - private async Task> RunMessageReceivedEventAsync(OpenIdConnectMessage message) + private async Task RunMessageReceivedEventAsync(OpenIdConnectMessage message) { Logger.LogDebug(Resources.OIDCH_0001_MessageReceived, message.BuildRedirectUrl()); - var messageReceivedContext = - new MessageReceivedContext(Context, Options) - { - ProtocolMessage = message - }; + var messageReceivedContext = new MessageReceivedContext(Context, Options) + { + ProtocolMessage = message + }; await Options.Events.MessageReceived(messageReceivedContext); if (messageReceivedContext.HandledResponse) @@ -818,14 +816,13 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect return authorizationCodeRedeemedContext; } - private async Task> RunSecurityTokenReceivedEventAsync(OpenIdConnectMessage message) + private async Task RunSecurityTokenReceivedEventAsync(OpenIdConnectMessage message) { Logger.LogDebug(Resources.OIDCH_0020_IdTokenReceived, message.IdToken); - var securityTokenReceivedContext = - new SecurityTokenReceivedContext(Context, Options) - { - ProtocolMessage = message, - }; + var securityTokenReceivedContext = new SecurityTokenReceivedContext(Context, Options) + { + ProtocolMessage = message, + }; await Options.Events.SecurityTokenReceived(securityTokenReceivedContext); if (securityTokenReceivedContext.HandledResponse) @@ -840,14 +837,13 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect return securityTokenReceivedContext; } - private async Task> RunSecurityTokenValidatedEventAsync(OpenIdConnectMessage message, AuthenticationTicket ticket) + private async Task RunSecurityTokenValidatedEventAsync(OpenIdConnectMessage message, AuthenticationTicket ticket) { - var securityTokenValidatedContext = - new SecurityTokenValidatedContext(Context, Options) - { - AuthenticationTicket = ticket, - ProtocolMessage = message - }; + var securityTokenValidatedContext = new SecurityTokenValidatedContext(Context, Options) + { + AuthenticationTicket = ticket, + ProtocolMessage = message + }; await Options.Events.SecurityTokenValidated(securityTokenValidatedContext); if (securityTokenValidatedContext.HandledResponse) @@ -862,14 +858,13 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect return securityTokenValidatedContext; } - private async Task> RunAuthenticationFailedEventAsync(OpenIdConnectMessage message, Exception exception) + private async Task RunAuthenticationFailedEventAsync(OpenIdConnectMessage message, Exception exception) { - var authenticationFailedContext = - new AuthenticationFailedContext(Context, Options) - { - ProtocolMessage = message, - Exception = exception - }; + var authenticationFailedContext = new AuthenticationFailedContext(Context, Options) + { + ProtocolMessage = message, + Exception = exception + }; await Options.Events.AuthenticationFailed(authenticationFailedContext); if (authenticationFailedContext.HandledResponse)