From 5e62ec372841af6c6630669f0007f3d1fa9946ac Mon Sep 17 00:00:00 2001 From: Mark Ettema Date: Sun, 17 Oct 2021 16:31:28 +0700 Subject: [PATCH] Adding some logging Added additional claims --- src/Domains/IdentityServer/SteamClaims.cs | 7 ++++ src/Domains/Steam/SteamProfileService.cs | 16 ++++++++ src/Program.cs | 47 ++++++++++++++++------- src/Startup.cs | 4 ++ src/SteamOpenIdConnectProvider.csproj | 2 +- 5 files changed, 61 insertions(+), 15 deletions(-) create mode 100644 src/Domains/IdentityServer/SteamClaims.cs diff --git a/src/Domains/IdentityServer/SteamClaims.cs b/src/Domains/IdentityServer/SteamClaims.cs new file mode 100644 index 0000000..e557d46 --- /dev/null +++ b/src/Domains/IdentityServer/SteamClaims.cs @@ -0,0 +1,7 @@ +namespace SteamOpenIdConnectProvider.Domains.IdentityServer +{ + public static class SteamClaims + { + public static readonly string SteamId = "steam_id"; + } +} diff --git a/src/Domains/Steam/SteamProfileService.cs b/src/Domains/Steam/SteamProfileService.cs index 64650c0..a2f9465 100644 --- a/src/Domains/Steam/SteamProfileService.cs +++ b/src/Domains/Steam/SteamProfileService.cs @@ -8,6 +8,7 @@ using IdentityServer4.Extensions; using IdentityServer4.Models; using IdentityServer4.Services; using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using SteamOpenIdConnectProvider.Domains.IdentityServer; using SteamOpenIdConnectProvider.Domains.Steam; @@ -20,16 +21,19 @@ namespace SteamOpenIdConnectProvider.Services private readonly HttpClient _httpClient; private readonly SteamConfig _config; private readonly IUserClaimsPrincipalFactory _claimsFactory; + private readonly ILogger _logger; private readonly UserManager _userManager; public SteamProfileService( UserManager userManager, IUserClaimsPrincipalFactory claimsFactory, IOptions config, + ILogger logger, HttpClient httpClient) { _userManager = userManager; _claimsFactory = claimsFactory; + _logger = logger; _config = config.Value; _httpClient = httpClient; } @@ -44,6 +48,7 @@ namespace SteamOpenIdConnectProvider.Services claims = claims.Where(claim => context.RequestedClaimTypes.Contains(claim.Type)).ToList(); var steamId = sub.Substring(Constants.OpenIdUrl.Length); + AddClaim(claims, SteamClaims.SteamId, steamId); var userSummary = await GetPlayerSummariesAsync(new[] { steamId }); var player = userSummary.Players.FirstOrDefault(); @@ -57,6 +62,17 @@ namespace SteamOpenIdConnectProvider.Services AddClaim(claims, OpenIdStandardClaims.Website, player.ProfileUrl); } + if (_logger.IsEnabled(LogLevel.Debug)) + { + foreach (var claim in claims) + { + _logger.LogDebug("Issued claim {claim}:{value} for {principle}", + claim.Type, + claim.Value, + principal.Identity.Name); + } + } + context.IssuedClaims = claims; } diff --git a/src/Program.cs b/src/Program.cs index ed6d99a..c7cbf68 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,5 +1,7 @@ -using Microsoft.AspNetCore; +using System; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; using Serilog; using Serilog.Events; using Serilog.Sinks.SystemConsole.Themes; @@ -8,25 +10,42 @@ namespace SteamOpenIdConnectProvider { public class Program { - public static void Main(string[] args) + public static int Main(string[] args) { Log.Logger = new LoggerConfiguration() - .MinimumLevel.Debug() - .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) - .MinimumLevel.Override("System", LogEventLevel.Warning) - .MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information) - .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning) + .MinimumLevel.Override("Microsoft", LogEventLevel.Information) .Enrich.FromLogContext() .WriteTo.Console() - .CreateLogger(); + .CreateBootstrapLogger(); - CreateWebHostBuilder(args).Build().Run(); + try + { + Log.Information("Starting web host"); + CreateHostBuilder(args).Build().Run(); + return 0; + } + catch (Exception ex) + { + Log.Fatal(ex, "Host terminated unexpectedly"); + return 1; + } + finally + { + Log.CloseAndFlush(); + } } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseKestrel() - .UseSerilog() - .UseStartup(); + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .UseSerilog((context, services, configuration) => configuration + .ReadFrom.Configuration(context.Configuration) + .ReadFrom.Services(services) + .MinimumLevel.Is(context.HostingEnvironment.IsDevelopment() ? LogEventLevel.Debug : LogEventLevel.Information) + .Enrich.FromLogContext() + .WriteTo.Console(theme: AnsiConsoleTheme.Code)) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); } } diff --git a/src/Startup.cs b/src/Startup.cs index c115356..b8c16cd 100644 --- a/src/Startup.cs +++ b/src/Startup.cs @@ -19,6 +19,7 @@ using SteamOpenIdConnectProvider.Domains.Steam; using System.IO; using System.Text; using Serilog; +using Microsoft.Extensions.Logging; namespace SteamOpenIdConnectProvider { @@ -82,8 +83,11 @@ namespace SteamOpenIdConnectProvider public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { + var logger = app.ApplicationServices.GetRequiredService>(); + if (env.IsDevelopment()) { + logger.LogWarning("Starting up in development mode"); app.UseDeveloperExceptionPage(); } diff --git a/src/SteamOpenIdConnectProvider.csproj b/src/SteamOpenIdConnectProvider.csproj index 293fe6e..71c43d3 100644 --- a/src/SteamOpenIdConnectProvider.csproj +++ b/src/SteamOpenIdConnectProvider.csproj @@ -19,7 +19,7 @@ - +