Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove IServiceProvider from CorsPolicyService #1239

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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