Skip to content

Commit

Permalink
fix(auth): add auth middlewares only once, use fallbackPolicy instead…
Browse files Browse the repository at this point in the history
… of custom middleware
  • Loading branch information
SonicGD committed Feb 8, 2024
1 parent 72795fd commit 069369e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 89 deletions.
7 changes: 2 additions & 5 deletions src/Sitko.Core.App.Web/IWebApplicationModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ void ConfigureAfterUseRouting(IApplicationContext applicationContext,
IApplicationBuilder appBuilder)
{
}

void ConfigureAuthMiddleware(IApplicationContext applicationContext,
IApplicationBuilder appBuilder)
{
}
}

public interface IWebApplicationModule<TModuleOptions> : IWebApplicationModule, IApplicationModule<TModuleOptions>
Expand All @@ -39,3 +34,5 @@ void ConfigureWebHost(IApplicationContext applicationContext, ConfigureWebHostBu
{
}
}

public interface IAuthApplicationModule : IWebApplicationModule;
19 changes: 13 additions & 6 deletions src/Sitko.Core.App.Web/WebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,31 @@ public static WebApplication MapSitkoCore(this WebApplication webApplication)
}
}

if (webOptions.EnableMvc)
{
webApplication.MapControllers();
}

var webModules =
ModulesHelper.GetEnabledModuleRegistrations(applicationContext, applicationModuleRegistrations)
.Select(r => r.GetInstance())
.OfType<IWebApplicationModule>()
.ToList();

var authModules = webModules.OfType<IAuthApplicationModule>().ToList();
if (authModules.Count != 0)
{
webApplication.UseAuthentication();
webApplication.UseAuthorization();
}

foreach (var webModule in webModules)
{
webModule.ConfigureBeforeUseRouting(applicationContext, webApplication);
webModule.ConfigureAfterUseRouting(applicationContext, webApplication);
webModule.ConfigureAuthMiddleware(applicationContext, webApplication);
webModule.ConfigureEndpoints(applicationContext, webApplication, webApplication);
}

if (webOptions.EnableMvc)
{
webApplication.MapControllers();
}

return webApplication;
}
}
35 changes: 12 additions & 23 deletions src/Sitko.Core.Auth/AuthModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,10 @@ namespace Sitko.Core.Auth;

public interface IAuthModule : IApplicationModule;

internal static class AuthMiddlewareState
{
public static bool IsConfigured { get; set; }
}

public abstract class AuthModule<TAuthOptions> : BaseApplicationModule<TAuthOptions>, IWebApplicationModule,
public abstract class AuthModule<TAuthOptions> : BaseApplicationModule<TAuthOptions>, IAuthApplicationModule,
IAuthModule
where TAuthOptions : AuthOptions, new()
{
public virtual void ConfigureAuthMiddleware(IApplicationContext applicationContext,
IApplicationBuilder appBuilder)
{
if (!AuthMiddlewareState.IsConfigured)
{
AuthMiddlewareState.IsConfigured = true;
appBuilder.UseAuthentication().UseAuthorization();
appBuilder.UseMiddleware<AuthorizationMiddleware<TAuthOptions>>();
}
}

public override void ConfigureServices(IApplicationContext applicationContext, IServiceCollection services,
TAuthOptions startupOptions)
{
Expand Down Expand Up @@ -65,15 +49,21 @@ public override void ConfigureServices(IApplicationContext applicationContext, I
{
options.AddPolicy(name, policy);
}

if (!string.IsNullOrEmpty(startupOptions.ForcePolicy))
{
options.FallbackPolicy = startupOptions.Policies
.FirstOrDefault(pair => pair.Key == startupOptions.ForcePolicy).Value;
}
});
if (startupOptions.EnableRedisDataProtection)
{
services.AddDataProtection().PersistKeysToStackExchangeRedis(() =>
{
var redis = ConnectionMultiplexer
.Connect($"{startupOptions.RedisHost}:{startupOptions.RedisPort}");
return redis.GetDatabase(startupOptions.RedisDb);
}, $"{applicationContext.Name}-DP")
{
var redis = ConnectionMultiplexer
.Connect($"{startupOptions.RedisHost}:{startupOptions.RedisPort}");
return redis.GetDatabase(startupOptions.RedisDb);
}, $"{applicationContext.Name}-DP")
.SetApplicationName(applicationContext.Name)
.SetDefaultKeyLifetime(TimeSpan.FromMinutes(startupOptions.DataProtectionLifeTimeInMinutes));
}
Expand All @@ -86,4 +76,3 @@ protected virtual void ConfigureCookieOptions(CookieAuthenticationOptions option
protected abstract void ConfigureAuthentication(AuthenticationBuilder authenticationBuilder,
TAuthOptions startupOptions);
}

46 changes: 0 additions & 46 deletions src/Sitko.Core.Auth/AuthorizationMiddleware.cs

This file was deleted.

10 changes: 1 addition & 9 deletions src/Sitko.Core.Identity/IdentityModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Sitko.Core.Identity;

public class IdentityModule<TUser, TRole, TPk, TDbContext> : BaseApplicationModule<IdentityModuleOptions>,
IWebApplicationModule
IAuthApplicationModule
where TUser : IdentityUser<TPk>
where TRole : IdentityRole<TPk>
where TDbContext : IdentityDbContext<TUser, TRole, TPk>
Expand All @@ -31,13 +31,6 @@ public void ConfigureEndpoints(IApplicationContext applicationContext,
}
}

public void ConfigureAfterUseRouting(IApplicationContext applicationContext,
IApplicationBuilder appBuilder)
{
appBuilder.UseAuthentication();
appBuilder.UseAuthorization();
}

public override void ConfigureServices(IApplicationContext applicationContext, IServiceCollection services,
IdentityModuleOptions startupOptions)
{
Expand Down Expand Up @@ -110,4 +103,3 @@ public class FakeEnv : IWebHostEnvironment
public string WebRootPath { get; set; } = "";
public IFileProvider WebRootFileProvider { get; set; } = null!;
}

0 comments on commit 069369e

Please sign in to comment.