-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f591d7
commit 7b0bd08
Showing
13 changed files
with
4,790 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Nuar.Cors.Core | ||
{ | ||
public class CorsOptionsBuilder : IOptionsBuilder<CorsOptions> | ||
{ | ||
private readonly CorsOptions _options; | ||
|
||
public CorsOptionsBuilder() | ||
{ | ||
_options = new CorsOptions | ||
{ | ||
AllowedOrigins = new List<string>(), | ||
AllowedMethods = new List<string>(), | ||
AllowedHeaders = new List<string>(), | ||
ExposedHeaders = new List<string>(), | ||
Environment = new Dictionary<string, CorsEnvironment>() | ||
}; | ||
} | ||
|
||
public CorsOptionsBuilder AllowCredentials(bool allow) | ||
{ | ||
_options.AllowCredentials = allow; | ||
return this; | ||
} | ||
|
||
public CorsOptionsBuilder WithAllowedOrigins(params string[] origins) | ||
{ | ||
_options.AllowedOrigins = origins; | ||
return this; | ||
} | ||
|
||
public CorsOptionsBuilder WithDeniedOrigins(params string[] deniedOrigins) | ||
{ | ||
_options.DeniedOrigins = deniedOrigins; | ||
return this; | ||
} | ||
|
||
public CorsOptionsBuilder WithAllowedMethods(params string[] methods) | ||
{ | ||
_options.AllowedMethods = methods; | ||
return this; | ||
} | ||
|
||
public CorsOptionsBuilder WithAllowedHeaders(params string[] headers) | ||
{ | ||
_options.AllowedHeaders = headers; | ||
return this; | ||
} | ||
|
||
public CorsOptionsBuilder WithExposedHeaders(params string[] headers) | ||
{ | ||
_options.ExposedHeaders = headers; | ||
return this; | ||
} | ||
|
||
public CorsOptionsBuilder WithMaxAge(int maxAge) | ||
{ | ||
_options.MaxAge = maxAge; | ||
return this; | ||
} | ||
|
||
public CorsOptionsBuilder EnableLogging(bool enable) | ||
{ | ||
_options.LoggingEnabled = enable; | ||
return this; | ||
} | ||
|
||
public CorsOptionsBuilder WithEnvironment(string environment, CorsEnvironment corsEnvironment) | ||
{ | ||
_options.Environment[environment] = corsEnvironment; | ||
return this; | ||
} | ||
|
||
public CorsOptions Build() => _options; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Nuar.Cors; | ||
using Nuar.Cors.Core; | ||
|
||
namespace Nuar.Extensions.Cors | ||
{ | ||
public class CorsExtension : IExtension | ||
{ | ||
public string Name => "cors"; | ||
public string Description => "Cross-Origin Resource Sharing"; | ||
public string Version => "1.0.0"; | ||
|
||
public void Add(IServiceCollection services, IOptionsProvider optionsProvider) | ||
{ | ||
var options = optionsProvider.GetForExtension<CorsOptions>(Name); // Ensure CorsOptions implements IOptions | ||
|
||
services.AddCors(cors => | ||
{ | ||
var allowedHeaders = options.AllowedHeaders ?? Enumerable.Empty<string>(); | ||
var allowedMethods = options.AllowedMethods ?? Enumerable.Empty<string>(); | ||
var allowedOrigins = options.AllowedOrigins ?? Enumerable.Empty<string>(); | ||
var exposedHeaders = options.ExposedHeaders ?? Enumerable.Empty<string>(); | ||
|
||
cors.AddPolicy("CorsPolicy", builder => | ||
{ | ||
var origins = allowedOrigins.ToArray(); | ||
if (options.AllowCredentials && origins.FirstOrDefault() != "*") | ||
{ | ||
builder.AllowCredentials(); | ||
} | ||
else | ||
{ | ||
builder.DisallowCredentials(); | ||
} | ||
|
||
// Handle Denied Origins | ||
if (options.DeniedOrigins != null && options.DeniedOrigins.Any()) | ||
{ | ||
origins = origins.Except(options.DeniedOrigins).ToArray(); | ||
} | ||
|
||
builder.WithHeaders(allowedHeaders.ToArray()) | ||
.WithMethods(allowedMethods.ToArray()) | ||
.WithOrigins(origins) | ||
.WithExposedHeaders(exposedHeaders.ToArray()); | ||
|
||
// Apply MaxAge | ||
if (options.MaxAge.HasValue) | ||
{ | ||
builder.SetPreflightMaxAge(TimeSpan.FromSeconds(options.MaxAge.Value)); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
public void Use(IApplicationBuilder app, IOptionsProvider optionsProvider) | ||
{ | ||
var options = optionsProvider.GetForExtension<CorsOptions>(Name); // Ensure CorsOptions implements IOptions | ||
|
||
// Handle Logging | ||
if (options.LoggingEnabled) | ||
{ | ||
var logger = app.ApplicationServices.GetRequiredService<ILogger<CorsExtension>>(); | ||
logger.LogInformation("CORS policy is applied."); | ||
} | ||
|
||
app.UseCors("CorsPolicy"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Nuar.Cors.Core | ||
{ | ||
public interface IOptionsBuilder<TOptions> | ||
{ | ||
TOptions Build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Nuar.Cors/src/Nuar.Cors/obj/Debug/net9.0/Nuar.Cors.AssemblyInfoInputs.cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
fb691fae5e8ff7ce111567c6ee56426d511e58d292f5e1863854ae2fe85b0dfc | ||
ea5bc4205fb4e0555c31b64def31f340295e00e222ccc672bbca57d617852012 |
Binary file modified
BIN
+43.5 KB
(29000%)
src/Nuar.Cors/src/Nuar.Cors/obj/Debug/net9.0/Nuar.Cors.assets.cache
Binary file not shown.
Binary file added
BIN
+4.74 KB
src/Nuar.Cors/src/Nuar.Cors/obj/Debug/net9.0/Nuar.Cors.csproj.AssemblyReference.cache
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 7 additions & 1 deletion
8
src/Nuar.Cors/src/Nuar.Cors/obj/Nuar.Cors.csproj.nuget.g.targets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8" standalone="no"?> | ||
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" /> | ||
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' "> | ||
<Import Project="$(NuGetPackageRoot)microsoft.extensions.options/8.0.0/buildTransitive/net6.0/Microsoft.Extensions.Options.targets" Condition="Exists('$(NuGetPackageRoot)microsoft.extensions.options/8.0.0/buildTransitive/net6.0/Microsoft.Extensions.Options.targets')" /> | ||
<Import Project="$(NuGetPackageRoot)microsoft.extensions.configuration.binder/8.0.0/buildTransitive/netstandard2.0/Microsoft.Extensions.Configuration.Binder.targets" Condition="Exists('$(NuGetPackageRoot)microsoft.extensions.configuration.binder/8.0.0/buildTransitive/netstandard2.0/Microsoft.Extensions.Configuration.Binder.targets')" /> | ||
<Import Project="$(NuGetPackageRoot)microsoft.extensions.logging.abstractions/8.0.0/buildTransitive/net6.0/Microsoft.Extensions.Logging.Abstractions.targets" Condition="Exists('$(NuGetPackageRoot)microsoft.extensions.logging.abstractions/8.0.0/buildTransitive/net6.0/Microsoft.Extensions.Logging.Abstractions.targets')" /> | ||
</ImportGroup> | ||
</Project> |
Oops, something went wrong.