diff --git a/dotnet-authserver/src/TeacherIdentity.AuthServer/Infrastructure/Middleware/AppendAuthorizationInfoToAnalyticsEventsMiddleware.cs b/dotnet-authserver/src/TeacherIdentity.AuthServer/Infrastructure/Middleware/AppendAuthorizationInfoToAnalyticsEventsMiddleware.cs new file mode 100644 index 000000000..3354592ea --- /dev/null +++ b/dotnet-authserver/src/TeacherIdentity.AuthServer/Infrastructure/Middleware/AppendAuthorizationInfoToAnalyticsEventsMiddleware.cs @@ -0,0 +1,38 @@ +using Dfe.Analytics.AspNetCore; + +namespace TeacherIdentity.AuthServer.Infrastructure.Middleware; + +public class AppendAuthorizationInfoToAnalyticsEventsMiddleware +{ + private readonly RequestDelegate _next; + + public AppendAuthorizationInfoToAnalyticsEventsMiddleware(RequestDelegate next) + { + _next = next; + } + + public Task Invoke(HttpContext context) + { + var analyticsEvent = context.GetWebRequestEvent(); + var clientRedirectInfo = context.GetClientRedirectInfo(); + + if (context.TryGetAuthenticationState(out var authenticationState)) + { + if (!string.IsNullOrEmpty(authenticationState.SessionId)) + { + analyticsEvent.AddData("session_id", authenticationState.SessionId); + } + + if (authenticationState.TryGetOAuthState(out var oAuthState)) + { + analyticsEvent.AddData("client_id", oAuthState.ClientId); + } + } + else if (clientRedirectInfo is not null) + { + analyticsEvent.AddData("client_id", clientRedirectInfo.ClientId); + } + + return _next(context); + } +} diff --git a/dotnet-authserver/src/TeacherIdentity.AuthServer/Infrastructure/Middleware/AppendSessionIdToAnalyticsEventsMiddleware.cs b/dotnet-authserver/src/TeacherIdentity.AuthServer/Infrastructure/Middleware/AppendSessionIdToAnalyticsEventsMiddleware.cs deleted file mode 100644 index dc4280278..000000000 --- a/dotnet-authserver/src/TeacherIdentity.AuthServer/Infrastructure/Middleware/AppendSessionIdToAnalyticsEventsMiddleware.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Dfe.Analytics.AspNetCore; - -namespace TeacherIdentity.AuthServer.Infrastructure.Middleware; - -public class AppendSessionIdToAnalyticsEventsMiddleware -{ - private readonly RequestDelegate _next; - - public AppendSessionIdToAnalyticsEventsMiddleware(RequestDelegate next) - { - _next = next; - } - - public Task Invoke(HttpContext context) - { - if (context.TryGetAuthenticationState(out var authenticationState) && - !string.IsNullOrEmpty(authenticationState.SessionId)) - { - var analyticsEvent = context.GetWebRequestEvent(); - analyticsEvent.AddData("session_id", authenticationState.SessionId); - } - - return _next(context); - } -} diff --git a/dotnet-authserver/src/TeacherIdentity.AuthServer/Program.cs b/dotnet-authserver/src/TeacherIdentity.AuthServer/Program.cs index c24bd2d72..ed959fd94 100644 --- a/dotnet-authserver/src/TeacherIdentity.AuthServer/Program.cs +++ b/dotnet-authserver/src/TeacherIdentity.AuthServer/Program.cs @@ -608,7 +608,7 @@ public static async Task Main(string[] args) a => { a.UseDfeAnalytics(); - a.UseMiddleware(); + a.UseMiddleware(); }); }