Skip to content

Commit

Permalink
Merge pull request #1239 from DuendeSoftware/joe/cors-policy-provider…
Browse files Browse the repository at this point in the history
…-di-cleanup

Remove IServiceProvider from CorsPolicyService
  • Loading branch information
brockallen committed Jun 1, 2023
2 parents 4ff67ad + 1db71a2 commit 1556ba8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
16 changes: 7 additions & 9 deletions src/EntityFramework/Services/CorsPolicyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ namespace Duende.IdentityServer.EntityFramework.Services;
public class CorsPolicyService : ICorsPolicyService
{
/// <summary>
/// The IServiceProvider.
/// The DbContext.
/// </summary>
protected readonly IServiceProvider Provider;
protected readonly IConfigurationDbContext DbContext;

/// <summary>
/// The CancellationToken provider.
Expand All @@ -33,17 +33,18 @@ public class CorsPolicyService : ICorsPolicyService
/// The logger.
/// </summary>
protected readonly ILogger<CorsPolicyService> Logger;


/// <summary>
/// Initializes a new instance of the <see cref="CorsPolicyService"/> class.
/// </summary>
/// <param name="provider">The provider.</param>
/// <param name="dbContext">The DbContext</param>
/// <param name="logger">The logger.</param>
/// <param name="cancellationTokenProvider"></param>
/// <exception cref="ArgumentNullException">context</exception>
public CorsPolicyService(IServiceProvider provider, ILogger<CorsPolicyService> logger, ICancellationTokenProvider cancellationTokenProvider)
public CorsPolicyService(IConfigurationDbContext dbContext, ILogger<CorsPolicyService> logger, ICancellationTokenProvider cancellationTokenProvider)
{
Provider = provider;
DbContext = dbContext;
Logger = logger;
CancellationTokenProvider = cancellationTokenProvider;
}
Expand All @@ -57,10 +58,7 @@ public async Task<bool> IsOriginAllowedAsync(string origin)
{
origin = origin.ToLowerInvariant();

// doing this here and not in the ctor because: https://github.com/aspnet/CORS/issues/105
var dbContext = Provider.GetRequiredService<IConfigurationDbContext>();

var query = from o in dbContext.ClientCorsOrigins
var query = from o in DbContext.ClientCorsOrigins
where o.Origin == origin
select o;

Expand Down
12 changes: 2 additions & 10 deletions test/EntityFramework.Tests/Services/CorsPolicyServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ public void IsOriginAllowedAsync_WhenOriginIsAllowed_ExpectTrue(DbContextOptions
bool result;
using (var context = new ConfigurationDbContext(options))
{
var svcs = new ServiceCollection();
svcs.AddSingleton<IConfigurationDbContext>(context);
var provider = svcs.BuildServiceProvider();

var service = new CorsPolicyService(provider, FakeLogger<CorsPolicyService>.Create(), new NoneCancellationTokenProvider());
var service = new CorsPolicyService(context, FakeLogger<CorsPolicyService>.Create(), new NoneCancellationTokenProvider());
result = service.IsOriginAllowedAsync(testCorsOrigin).Result;
}

Expand All @@ -82,11 +78,7 @@ public void IsOriginAllowedAsync_WhenOriginIsNotAllowed_ExpectFalse(DbContextOpt
bool result;
using (var context = new ConfigurationDbContext(options))
{
var svcs = new ServiceCollection();
svcs.AddSingleton<IConfigurationDbContext>(context);
var provider = svcs.BuildServiceProvider();

var service = new CorsPolicyService(provider, FakeLogger<CorsPolicyService>.Create(), new NoneCancellationTokenProvider());
var service = new CorsPolicyService(context, FakeLogger<CorsPolicyService>.Create(), new NoneCancellationTokenProvider());
result = service.IsOriginAllowedAsync("InvalidOrigin").Result;
}

Expand Down

0 comments on commit 1556ba8

Please sign in to comment.