Adding some logging

Added additional claims
This commit is contained in:
Mark Ettema 2021-10-17 16:31:28 +07:00
parent fdf472f13d
commit 5e62ec3728
5 changed files with 61 additions and 15 deletions

View file

@ -0,0 +1,7 @@
namespace SteamOpenIdConnectProvider.Domains.IdentityServer
{
public static class SteamClaims
{
public static readonly string SteamId = "steam_id";
}
}

View file

@ -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<IdentityUser> _claimsFactory;
private readonly ILogger<SteamProfileService> _logger;
private readonly UserManager<IdentityUser> _userManager;
public SteamProfileService(
UserManager<IdentityUser> userManager,
IUserClaimsPrincipalFactory<IdentityUser> claimsFactory,
IOptions<SteamConfig> config,
ILogger<SteamProfileService> 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;
}

View file

@ -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<Startup>();
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<Startup>();
});
}
}

View file

@ -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<ILogger<Startup>>();
if (env.IsDevelopment())
{
logger.LogWarning("Starting up in development mode");
app.UseDeveloperExceptionPage();
}

View file

@ -19,7 +19,7 @@
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
</ItemGroup>
</Project>