Add configurations and update packages
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace GerstITS.Examples.Api.Configurations
|
||||
{
|
||||
public static class IConfigurationExtensions
|
||||
{
|
||||
#region Methods
|
||||
|
||||
public static TEnum GetFlaggedEnum<TEnum>(this IConfiguration configuration, string key)
|
||||
where TEnum : struct, IConvertible
|
||||
{
|
||||
ThrowsAnExceptionIfTypeIsNotAnEnumeration<TEnum>();
|
||||
|
||||
return (TEnum)(object)(configuration.GetSection(key)?
|
||||
.Get<TEnum[]>() ?? Enumerable.Empty<TEnum>())
|
||||
.Sum(i => (int)(object)i);
|
||||
}
|
||||
|
||||
private static void ThrowsAnExceptionIfTypeIsNotAnEnumeration<TEnum>() where TEnum : struct, IConvertible
|
||||
{
|
||||
if (!typeof(TEnum).IsEnum)
|
||||
throw new InvalidOperationException($"{typeof(TEnum)} is not supported.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using GerstITS.Data.EntityFramework;
|
||||
using GerstITS.System.Configurations;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace GerstITS.Examples.Api.Configurations
|
||||
{
|
||||
internal sealed class EntityFrameworkMigrationConfiguration : IEntityFrameworkMigrationConfiguration
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
public EntityFrameworkMigrationConfiguration(string parentPrefix,
|
||||
Microsoft.Extensions.Configuration.IConfiguration configuration)
|
||||
{
|
||||
var prefix = $"{parentPrefix}:{this.ToConfigurationPrefix()}";
|
||||
|
||||
AutoMigrate = configuration.GetValue<bool>($"{prefix}:{nameof(AutoMigrate)}");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IMigrationConfiguration
|
||||
|
||||
public bool AutoMigrate { get; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using GerstITS.Web.Api.Swagger;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using Microsoft.OpenApi.Models;
|
||||
|
||||
namespace BakeTronic.Web.Api.Configurations
|
||||
{
|
||||
internal sealed class OpenApiSecuritySchemeConfiguration : IOpenApiSecuritySchemeConfiguration
|
||||
{
|
||||
#region Properties
|
||||
|
||||
public bool AllowAnonymous { get; }
|
||||
public string HttpHeaderKey { get; }
|
||||
public string Scheme { get; }
|
||||
public SecuritySchemeType SchemeType { get; }
|
||||
public ParameterLocation ParameterLocation { get; }
|
||||
public string Description { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public OpenApiSecuritySchemeConfiguration()
|
||||
{
|
||||
AllowAnonymous = false;
|
||||
HttpHeaderKey = HeaderNames.Authorization;
|
||||
Scheme = "Bearer";
|
||||
SchemeType = SecuritySchemeType.Http;
|
||||
ParameterLocation = ParameterLocation.Header;
|
||||
Description = "Authorization header using the Bearer scheme (Value: Bearer {{access_token}}).";
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using GerstITS.Authentication.OpenId;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace BakeTronic.Web.Api.Configurations
|
||||
{
|
||||
internal class OpenIdValidationConfiguration : IOpenIdValidationConfiguration
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
public OpenIdValidationConfiguration(string parentPrefix,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
var prefix = $"{parentPrefix}:Validate";
|
||||
|
||||
Issuer = configuration.GetValue<string>($"{prefix}:{nameof(Issuer)}");
|
||||
Audience = configuration.GetValue<string>($"{prefix}:{nameof(Audience)}");
|
||||
RsaPublicKey = configuration.GetValue<string>($"{prefix}:{nameof(RsaPublicKey)}");
|
||||
RequiredClaimType = configuration.GetValue<string>($"{prefix}:{nameof(RequiredClaimType)}");
|
||||
ValidateLifetime = configuration.GetValue<bool>($"{prefix}:{nameof(ValidateLifetime)}");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IOpenIdValidationConfiguration
|
||||
|
||||
public string Issuer { get; }
|
||||
public string Audience { get; }
|
||||
public string RsaPublicKey { get; }
|
||||
public string RequiredClaimType { get; }
|
||||
public bool ValidateLifetime { get; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using BakeTronic.Web.Api.Configurations;
|
||||
using GerstITS.Authentication.OpenId;
|
||||
using GerstITS.System.Configurations;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace GerstITS.Examples.Api.Configurations
|
||||
{
|
||||
internal class OpenIdConfiguration : IOpenIdConfiguration
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
public OpenIdConfiguration(string parentPrefix,
|
||||
Microsoft.Extensions.Configuration.IConfiguration configuration)
|
||||
{
|
||||
var prefix = $"{parentPrefix}:{this.ToConfigurationPrefix()}";
|
||||
|
||||
Authority = configuration.GetValue<string>($"{prefix}:{nameof(Authority)}");
|
||||
RequireHttpsMetadata = configuration.GetValue<bool>($"{prefix}:{nameof(RequireHttpsMetadata)}");
|
||||
SaveToken = configuration.GetValue<bool>($"{prefix}:{nameof(SaveToken)}");
|
||||
|
||||
Validate = new OpenIdValidationConfiguration(prefix, configuration);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IOpenIdConfiguration
|
||||
|
||||
public string Authority { get; }
|
||||
public bool RequireHttpsMetadata { get; }
|
||||
public bool SaveToken { get; }
|
||||
|
||||
public IOpenIdValidationConfiguration Validate { get; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using GerstITS.System.Configurations;
|
||||
using GerstITS.Web.Api.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace BakeTronic.Web.Api.Configurations
|
||||
{
|
||||
internal sealed class CorsPolicyConfiguration : ICorsPolicy, GerstITS.System.Configurations.IConfiguration
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
public CorsPolicyConfiguration(string parentPrefix, Microsoft.Extensions.Configuration.IConfiguration configuration)
|
||||
{
|
||||
var prefix = $"{parentPrefix}:{this.ToConfigurationPrefix()}";
|
||||
|
||||
AllowCredentials = configuration.GetValue<bool>($"{prefix}:{nameof(AllowCredentials)}", false);
|
||||
AllowedOrigins = configuration.GetSection($"{prefix}:{nameof(AllowedOrigins)}").Get<string[]>() ?? new string[0];
|
||||
AllowedHeaders = configuration.GetSection($"{prefix}:{nameof(AllowedHeaders)}").Get<string[]>() ?? new string[0];
|
||||
AllowedMethods = configuration.GetSection($"{prefix}:{nameof(AllowedMethods)}").Get<string[]>() ?? new string[0];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ICorsPolicy
|
||||
|
||||
public bool AllowCredentials { get; }
|
||||
public string[] AllowedOrigins { get; set; }
|
||||
public string[] AllowedHeaders { get; set; }
|
||||
public string[] AllowedMethods { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using GerstITS.System.Configurations;
|
||||
using GerstITS.Web.Api.Builder;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
|
||||
namespace GerstITS.Examples.Api.Configurations
|
||||
{
|
||||
internal sealed class WebApiHeadersConfiguration : IHeaderConfiguration, IConfiguration
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
public WebApiHeadersConfiguration(string parentPrefix, Microsoft.Extensions.Configuration.IConfiguration configuration)
|
||||
{
|
||||
var prefix = $"{parentPrefix}:{this.ToConfigurationPrefix()}";
|
||||
|
||||
ForwardedHeaders = new ForwardedHeadersOptions
|
||||
{
|
||||
ForwardedHeaders = configuration.GetFlaggedEnum<ForwardedHeaders>($"{prefix}:{nameof(ForwardedHeadersOptions)}:{nameof(ForwardedHeadersOptions.ForwardedHeaders)}")
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IHeaderConfiguration
|
||||
|
||||
public ForwardedHeadersOptions ForwardedHeaders { get; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using GerstITS.Examples.Api.Configurations;
|
||||
using GerstITS.System.Configurations;
|
||||
using GerstITS.Web.Api.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace BakeTronic.Web.Api.Configurations
|
||||
{
|
||||
internal sealed class ServerConfiguration : IServerConfiguration
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
public ServerConfiguration(string parentPrefix, Microsoft.Extensions.Configuration.IConfiguration configuration)
|
||||
{
|
||||
var prefix = $"{parentPrefix}:{this.ToConfigurationPrefix()}";
|
||||
|
||||
UseHttpsRedirection = configuration.GetValue<bool>($"{prefix}:{nameof(UseHttpsRedirection)}");
|
||||
UseHsts = configuration.GetValue<bool>($"{prefix}:{nameof(UseHsts)}");
|
||||
UseCors = configuration.GetValue<bool>($"{prefix}:{nameof(UseCors)}");
|
||||
|
||||
Headers = new WebApiHeadersConfiguration(prefix, configuration);
|
||||
CorsPolicy = new CorsPolicyConfiguration(prefix, configuration);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IServerConfiguration
|
||||
|
||||
public bool UseHttpsRedirection { get; }
|
||||
public bool UseHsts { get; }
|
||||
public bool UseCors { get; }
|
||||
public IHeaderConfiguration Headers { get; }
|
||||
public ICorsPolicy CorsPolicy { get; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using GerstITS.System.Configurations;
|
||||
using GerstITS.Web.Api.Swagger;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace BakeTronic.Web.Api.Configurations
|
||||
{
|
||||
internal sealed class LicenseConfiguration : ILicense, GerstITS.System.Configurations.IConfiguration
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
public LicenseConfiguration(string parentPrefix, Microsoft.Extensions.Configuration.IConfiguration configuration)
|
||||
{
|
||||
var prefix = $"{parentPrefix}:{this.ToConfigurationPrefix()}";
|
||||
|
||||
Name = configuration.GetValue<string>($"{prefix}:{nameof(Name)}");
|
||||
Url = configuration.GetValue<Uri>($"{prefix}:{nameof(Url)}");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ILicence
|
||||
|
||||
public string Name { get; }
|
||||
public Uri Url { get; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,49 +1,46 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using GerstITS.Examples.Api.Configurations;
|
||||
using GerstITS.System.Configurations;
|
||||
using GerstITS.Web.Api.Swagger;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace GerstITS.Examples.Api.Swagger
|
||||
namespace BakeTronic.Web.Api.Configurations
|
||||
{
|
||||
internal class WebApiConfiguration : ISwaggerConfiguration, IConfiguration
|
||||
internal sealed class SwaggerConfiguration : ISwaggerConfiguration
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
public WebApiConfiguration()
|
||||
public SwaggerConfiguration(string parentPrefix,
|
||||
Microsoft.Extensions.Configuration.IConfiguration configuration)
|
||||
{
|
||||
var prefix = $"{parentPrefix}:{this.ToConfigurationPrefix()}";
|
||||
|
||||
var currentAssembly = typeof(WebApiConfiguration).Assembly;
|
||||
var fileVersionInfo = FileVersionInfo.GetVersionInfo(currentAssembly.Location);
|
||||
|
||||
Company = fileVersionInfo.CompanyName;
|
||||
|
||||
Name = fileVersionInfo.ProductName;
|
||||
Company = fileVersionInfo.CompanyName;
|
||||
SupportEMail = configuration.GetValue<string>($"{prefix}:{nameof(SupportEMail)}");
|
||||
TermsOfService = configuration.GetValue<Uri>($"{prefix}:{nameof(TermsOfService)}");
|
||||
|
||||
Release = currentAssembly.GetName().Version;
|
||||
|
||||
TermsOfService = new Uri("https://en.wikipedia.org/wiki/Terms_of_service");
|
||||
License = new LicenseConfiguration
|
||||
{
|
||||
Name = "MIT",
|
||||
Url = new Uri("https://opensource.org/licenses/MIT")
|
||||
};
|
||||
SupportEMail = "info@examples.net";
|
||||
|
||||
Security = new SwaggerSecurityConfiguration
|
||||
{
|
||||
AllowAnonymous = true
|
||||
};
|
||||
License = new LicenseConfiguration(prefix, configuration);
|
||||
Security = new OpenApiSecuritySchemeConfiguration();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region ISwaggerConfiguration
|
||||
|
||||
public string Name { get; }
|
||||
public string Company { get; }
|
||||
public string SupportEMail { get; }
|
||||
public Uri TermsOfService { get; }
|
||||
public ILicense License { get; }
|
||||
public Version Release { get; }
|
||||
public IOpenApiSecuritySchemeConfiguration Security { get; }
|
||||
|
||||
public ILicense License { get; }
|
||||
public IOpenApiSecuritySchemeConfiguration Security { get; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using BakeTronic.Web.Api.Configurations;
|
||||
using GerstITS.Authentication.OpenId;
|
||||
using GerstITS.Data.EntityFramework;
|
||||
using GerstITS.System.Configurations;
|
||||
using GerstITS.Web.Api.Builder;
|
||||
using GerstITS.Web.Api.Swagger;
|
||||
|
||||
namespace GerstITS.Examples.Api.Configurations
|
||||
{
|
||||
internal sealed class WebApiConfiguration : IConfiguration
|
||||
{
|
||||
#region Properties
|
||||
|
||||
public IServerConfiguration Server { get; }
|
||||
public IOpenIdConfiguration OpenId { get; }
|
||||
public ISwaggerConfiguration Swagger { get; set; }
|
||||
public IEntityFrameworkMigrationConfiguration EntityFrameworkMigration { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public WebApiConfiguration(Microsoft.Extensions.Configuration.IConfiguration configuration)
|
||||
{
|
||||
var prefix = this.ToConfigurationPrefix();
|
||||
|
||||
EntityFrameworkMigration = new EntityFrameworkMigrationConfiguration(prefix, configuration);
|
||||
OpenId = new OpenIdConfiguration(prefix, configuration);
|
||||
Server = new ServerConfiguration(prefix, configuration);
|
||||
Swagger = new SwaggerConfiguration(prefix, configuration);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using System;
|
||||
using GerstITS.Web.Api.Swagger;
|
||||
|
||||
namespace GerstITS.Examples.Api.Swagger
|
||||
{
|
||||
internal class LicenseConfiguration : ILicense
|
||||
{
|
||||
#region Properties
|
||||
|
||||
public string Name { get; set; }
|
||||
public Uri Url { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
using GerstITS.Web.Api.Swagger;
|
||||
using Microsoft.OpenApi.Models;
|
||||
|
||||
namespace GerstITS.Examples.Api.Swagger
|
||||
{
|
||||
internal sealed class SwaggerSecurityConfiguration : IOpenApiSecuritySchemeConfiguration
|
||||
{
|
||||
#region Properties
|
||||
|
||||
public bool AllowAnonymous { get; set; }
|
||||
public string HttpHeaderKey { get; set; }
|
||||
public string Scheme { get; set; }
|
||||
public SecuritySchemeType SchemeType { get; set; }
|
||||
public ParameterLocation ParameterLocation { get; set; }
|
||||
public string Description { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user