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

Feature/kitosudv 3892 optional cors origins #920

Merged
merged 1 commit into from
Jan 16, 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
11 changes: 10 additions & 1 deletion Presentation.Web/App_Start/WebApiConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
using DataType = Core.DomainModel.ItSystem.DataType;
using HelpText = Core.DomainModel.HelpText;
using Core.DomainModel.Shared;
using Presentation.Web.Models.Application.Cors;

namespace Presentation.Web
{
public static class WebApiConfig
{
const string ControllerSuffix = "Controller";
private const string ControllerSuffix = "Controller";

public static void Register(HttpConfiguration config)
{
Expand Down Expand Up @@ -62,6 +63,14 @@ public static void Register(HttpConfiguration config)
config.Filters.Add(new ValidateActionParametersAttribute());
config.Filters.Add(new DenyRightsHoldersAccessAttribute()); //By default block all actions for users with rights holders access in one or more organizations
config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);

//Optionally enable CORS
var corsConfig = CorsConfiguration.FromConfiguration();
var globalCors = corsConfig.GlobalCorsSettings;
if (globalCors.HasValue)
{
config.EnableCors(globalCors.Value);
}
}

public static IEdmModel GetModel()
Expand Down
7 changes: 4 additions & 3 deletions Presentation.Web/Controllers/Web/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
using Core.ApplicationServices.Authentication;
using Core.ApplicationServices.SSO.Model;
using Core.DomainServices;

using Presentation.Web.Models.Application.FeatureToggle;
using Presentation.Web.Properties;
using Presentation.Web.Models.Application.RuntimeEnv;

namespace Presentation.Web.Controllers.Web
{
Expand All @@ -15,6 +14,7 @@ public class HomeController : Controller
{
private readonly IAuthenticationContext _userContext;
private readonly IUserRepository _userRepository;
private readonly bool _isProd;
private const string SsoErrorKey = "SSO_ERROR";
private const string FeatureToggleKey = "FEATURE_TOGGLE";
private const string SsoAuthenticationCompletedKey = "SSO_PREFERRED_START";
Expand All @@ -23,11 +23,12 @@ public HomeController(IAuthenticationContext userContext, IUserRepository userRe
{
_userContext = userContext;
_userRepository = userRepository;
_isProd = KitosEnvironmentConfiguration.FromConfiguration().Environment == KitosEnvironment.Production;
}

public ActionResult Index()
{
ViewBag.StylingScheme = Settings.Default.Environment?.ToLowerInvariant().Contains("prod") == true ? "PROD" : "TEST";
ViewBag.StylingScheme = _isProd ? "PROD" : "TEST";
AppendSsoError();
AppendFeatureToggles();
AppendSsoLoginInformation();
Expand Down
44 changes: 44 additions & 0 deletions Presentation.Web/Models/Application/Cors/CorsConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Web.Http.Cors;
using Core.Abstractions.Types;
using Presentation.Web.Models.Application.RuntimeEnv;
using Presentation.Web.Properties;

namespace Presentation.Web.Models.Application.Cors
{
public class CorsConfiguration
{
private const string WildCard = "*";
public Maybe<EnableCorsAttribute> GlobalCorsSettings { get; }

public static CorsConfiguration FromConfiguration()
{
var environmentConfiguration = KitosEnvironmentConfiguration.FromConfiguration();
var config = Maybe<EnableCorsAttribute>.None;

if (environmentConfiguration.Environment == KitosEnvironment.Dev)
{
var origins = Settings.Default.CorsOrigins;
if (!string.IsNullOrWhiteSpace(origins))
{
var configuredOrigins = origins.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();
var originsString = string.Join(",", configuredOrigins);
Trace.WriteLine($"CORS origins enabled:{originsString}");
if (originsString.Length > 0)
{
config = new EnableCorsAttribute(originsString, WildCard, WildCard);
}
}
}

return new CorsConfiguration(config);
}

public CorsConfiguration(Maybe<EnableCorsAttribute> globalCorsSettings)
{
GlobalCorsSettings = globalCorsSettings;
}
}
}
10 changes: 10 additions & 0 deletions Presentation.Web/Models/Application/RuntimeEnv/KitosEnvironment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Presentation.Web.Models.Application.RuntimeEnv
{
public enum KitosEnvironment
{
Dev = 0,
Integration = 1,
Staging = 2,
Production = 3
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Configuration;
using Presentation.Web.Properties;

namespace Presentation.Web.Models.Application.RuntimeEnv
{
public class KitosEnvironmentConfiguration
{
public KitosEnvironment Environment { get; }

public KitosEnvironmentConfiguration(KitosEnvironment environment)
{
Environment = environment;
}

public static KitosEnvironmentConfiguration FromConfiguration()
{
var environmentConf = Settings.Default.Environment?.ToLowerInvariant() ?? "";
var env = environmentConf switch
{
"dev" => KitosEnvironment.Dev,
"integration" => KitosEnvironment.Integration,
"staging" => KitosEnvironment.Staging,
"prod" => KitosEnvironment.Production,
_ => throw new ConfigurationErrorsException(
$"Invalid value of the Environment variable. Got:\"{environmentConf}\"")
};

return new KitosEnvironmentConfiguration(env);
}
}
}
3 changes: 3 additions & 0 deletions Presentation.Web/Presentation.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@
<Compile Include="Models\API\V1\BusinessRoleDTO.cs" />
<Compile Include="Models\API\V1\CreateItSystemDTO.cs" />
<Compile Include="Models\API\V1\CreateItsystemUsageDTO.cs" />
<Compile Include="Models\Application\Cors\CorsConfiguration.cs" />
<Compile Include="Models\Application\Csv\CsvColumnDefinition.cs" />
<Compile Include="Models\Application\Csv\CsvColumnIdentity.cs" />
<Compile Include="Models\Application\Csv\CsvResponseBuilder.cs" />
Expand Down Expand Up @@ -830,6 +831,8 @@
<Compile Include="Models\API\V1\UserWithEmailDTO.cs" />
<Compile Include="Models\API\V1\ValueWithOptionalDateAndRemark.cs" />
<Compile Include="Models\API\V1\ValueWithOptionalRemarkDTO.cs" />
<Compile Include="Models\Application\RuntimeEnv\KitosEnvironment.cs" />
<Compile Include="Models\Application\RuntimeEnv\KitosEnvironmentConfiguration.cs" />
<Compile Include="Ninject\HangfireNinjectResolutionScope.cs" />
<Compile Include="Ninject\BindingSyntaxExtensions.cs" />
<Compile Include="Ninject\KernelBuilder.cs" />
Expand Down
11 changes: 10 additions & 1 deletion Presentation.Web/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Presentation.Web/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@
<Setting Name="SsoServiceProviderId" Type="System.String" Scope="Application">
<Value Profile="(Default)">https://kitos-local.strongminds.dk</Value>
</Setting>
<Setting Name="CorsOrigins" Type="System.String" Scope="Application">
<Value Profile="(Default)">http://localhost:4200</Value>
</Setting>
</Settings>
</SettingsFile>
3 changes: 3 additions & 0 deletions Presentation.Web/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@
<setting name="SsoServiceProviderId" serializeAs="String">
<value>https://kitos-local.strongminds.dk</value>
</setting>
<setting name="CorsOrigins" serializeAs="String">
<value>http://localhost:4200</value>
</setting>
</Presentation.Web.Properties.Settings>
</applicationSettings>
<!--<system.diagnostics>
Expand Down