From 1920937eaec94499a974f70e194c94f580588844 Mon Sep 17 00:00:00 2001 From: "joel.thoms" Date: Tue, 16 Aug 2016 15:49:56 -0700 Subject: [PATCH] Added OnAuthenticated handler to KeycloakAuthenticationOptions. --- .../KeycloakIdentityModel.csproj | 1 + .../Models/EventArgs/OnAuthenticatedEventArgs.cs | 7 +++++++ .../Configuration/KeycloakAuthenticationOptions.cs | 7 +++++++ .../Middleware/KeycloakAuthenticationHandler.cs | 14 ++++++++++---- 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 src/KeycloakIdentityModel/Models/EventArgs/OnAuthenticatedEventArgs.cs diff --git a/src/KeycloakIdentityModel/KeycloakIdentityModel.csproj b/src/KeycloakIdentityModel/KeycloakIdentityModel.csproj index 6e81d1f..d11d778 100644 --- a/src/KeycloakIdentityModel/KeycloakIdentityModel.csproj +++ b/src/KeycloakIdentityModel/KeycloakIdentityModel.csproj @@ -69,6 +69,7 @@ + diff --git a/src/KeycloakIdentityModel/Models/EventArgs/OnAuthenticatedEventArgs.cs b/src/KeycloakIdentityModel/Models/EventArgs/OnAuthenticatedEventArgs.cs new file mode 100644 index 0000000..c0b86e9 --- /dev/null +++ b/src/KeycloakIdentityModel/Models/EventArgs/OnAuthenticatedEventArgs.cs @@ -0,0 +1,7 @@ +namespace KeycloakIdentityModel.Models.EventArgs +{ + public class OnAuthenticatedEventArgs: System.EventArgs + { + public string RedirectUri { get; set; } + } +} diff --git a/src/Owin.Security.Keycloak/Configuration/KeycloakAuthenticationOptions.cs b/src/Owin.Security.Keycloak/Configuration/KeycloakAuthenticationOptions.cs index f691c11..17523c6 100644 --- a/src/Owin.Security.Keycloak/Configuration/KeycloakAuthenticationOptions.cs +++ b/src/Owin.Security.Keycloak/Configuration/KeycloakAuthenticationOptions.cs @@ -1,5 +1,7 @@ using System; using KeycloakIdentityModel.Models.Configuration; +using KeycloakIdentityModel.Models.EventArgs; +using Microsoft.Owin; using Microsoft.Owin.Security; namespace Owin.Security.Keycloak @@ -191,5 +193,10 @@ public KeycloakAuthenticationOptions() public string CallbackPath { get; set; } public string ResponseType { get; set; } + + /// + /// OPTIONAL: Triggers to fire after a successful authentication. + /// + public Action OnAuthenticated { get; set; } } } \ No newline at end of file diff --git a/src/Owin.Security.Keycloak/Middleware/KeycloakAuthenticationHandler.cs b/src/Owin.Security.Keycloak/Middleware/KeycloakAuthenticationHandler.cs index 9e24efb..cdfc737 100644 --- a/src/Owin.Security.Keycloak/Middleware/KeycloakAuthenticationHandler.cs +++ b/src/Owin.Security.Keycloak/Middleware/KeycloakAuthenticationHandler.cs @@ -8,6 +8,7 @@ using System.Security.Claims; using System.Threading.Tasks; using KeycloakIdentityModel; +using KeycloakIdentityModel.Models.EventArgs; using KeycloakIdentityModel.Models.Responses; using Microsoft.Owin; using Microsoft.Owin.Security; @@ -79,14 +80,19 @@ stateData[Constants.CacheTypes.AuthenticationProperties] as AuthenticationProper var kcIdentity = await KeycloakIdentity.ConvertFromAuthResponseAsync(Options, authResult, Request.Uri); var identity = await kcIdentity.ToClaimsIdentityAsync(); - Context.Authentication.User.AddIdentity(identity); + + Context.Authentication.User = new ClaimsPrincipal(identity); SignInAsAuthentication(identity, properties, Options.SignInAsAuthenticationType); + // Trigger OnAuthenticated? + var eventArgs = new OnAuthenticatedEventArgs { RedirectUri = properties.RedirectUri }; + Options.OnAuthenticated?.Invoke(Context, eventArgs); + // Redirect back to the original secured resource, if any - if (!string.IsNullOrWhiteSpace(properties.RedirectUri) && - Uri.IsWellFormedUriString(properties.RedirectUri, UriKind.Absolute)) + if (!string.IsNullOrWhiteSpace(eventArgs.RedirectUri) && + Uri.IsWellFormedUriString(eventArgs.RedirectUri, UriKind.Absolute)) { - Response.Redirect(properties.RedirectUri); + Response.Redirect(eventArgs.RedirectUri); return true; } }