Merge pull request #6 from neothor/feature/add-more-claims

Feature/add more claims
This commit is contained in:
Mark Ettema 2021-10-17 16:38:09 +07:00 committed by GitHub
commit 62ab2f2667
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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.Models;
using IdentityServer4.Services; using IdentityServer4.Services;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using SteamOpenIdConnectProvider.Domains.IdentityServer; using SteamOpenIdConnectProvider.Domains.IdentityServer;
using SteamOpenIdConnectProvider.Domains.Steam; using SteamOpenIdConnectProvider.Domains.Steam;
@ -20,16 +21,19 @@ namespace SteamOpenIdConnectProvider.Services
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
private readonly SteamConfig _config; private readonly SteamConfig _config;
private readonly IUserClaimsPrincipalFactory<IdentityUser> _claimsFactory; private readonly IUserClaimsPrincipalFactory<IdentityUser> _claimsFactory;
private readonly ILogger<SteamProfileService> _logger;
private readonly UserManager<IdentityUser> _userManager; private readonly UserManager<IdentityUser> _userManager;
public SteamProfileService( public SteamProfileService(
UserManager<IdentityUser> userManager, UserManager<IdentityUser> userManager,
IUserClaimsPrincipalFactory<IdentityUser> claimsFactory, IUserClaimsPrincipalFactory<IdentityUser> claimsFactory,
IOptions<SteamConfig> config, IOptions<SteamConfig> config,
ILogger<SteamProfileService> logger,
HttpClient httpClient) HttpClient httpClient)
{ {
_userManager = userManager; _userManager = userManager;
_claimsFactory = claimsFactory; _claimsFactory = claimsFactory;
_logger = logger;
_config = config.Value; _config = config.Value;
_httpClient = httpClient; _httpClient = httpClient;
} }
@ -44,6 +48,7 @@ namespace SteamOpenIdConnectProvider.Services
claims = claims.Where(claim => context.RequestedClaimTypes.Contains(claim.Type)).ToList(); claims = claims.Where(claim => context.RequestedClaimTypes.Contains(claim.Type)).ToList();
var steamId = sub.Substring(Constants.OpenIdUrl.Length); var steamId = sub.Substring(Constants.OpenIdUrl.Length);
AddClaim(claims, SteamClaims.SteamId, steamId);
var userSummary = await GetPlayerSummariesAsync(new[] { steamId }); var userSummary = await GetPlayerSummariesAsync(new[] { steamId });
var player = userSummary.Players.FirstOrDefault(); var player = userSummary.Players.FirstOrDefault();
@ -57,6 +62,17 @@ namespace SteamOpenIdConnectProvider.Services
AddClaim(claims, OpenIdStandardClaims.Website, player.ProfileUrl); 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; context.IssuedClaims = claims;
} }

View file

@ -1,5 +1,7 @@
using Microsoft.AspNetCore; using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Serilog; using Serilog;
using Serilog.Events; using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes; using Serilog.Sinks.SystemConsole.Themes;
@ -8,25 +10,42 @@ namespace SteamOpenIdConnectProvider
{ {
public class Program public class Program
{ {
public static void Main(string[] args) public static int Main(string[] args)
{ {
Log.Logger = new LoggerConfiguration() Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug() .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.Enrich.FromLogContext() .Enrich.FromLogContext()
.WriteTo.Console() .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) => public static IHostBuilder CreateHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args) Host.CreateDefaultBuilder(args)
.UseKestrel() .UseSerilog((context, services, configuration) => configuration
.UseSerilog() .ReadFrom.Configuration(context.Configuration)
.UseStartup<Startup>(); .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.IO;
using System.Text; using System.Text;
using Serilog; using Serilog;
using Microsoft.Extensions.Logging;
namespace SteamOpenIdConnectProvider namespace SteamOpenIdConnectProvider
{ {
@ -82,8 +83,11 @@ namespace SteamOpenIdConnectProvider
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ {
var logger = app.ApplicationServices.GetRequiredService<ILogger<Startup>>();
if (env.IsDevelopment()) if (env.IsDevelopment())
{ {
logger.LogWarning("Starting up in development mode");
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }

View file

@ -19,7 +19,7 @@
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
<PackageReference Include="Serilog" Version="2.10.0" /> <PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.AspNetCore" Version="4.1.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> </ItemGroup>
</Project> </Project>