diff --git a/build.cake b/build.cake index 81648ff61bbee492e1bc7b4390eb8e0cc33d3056..6ebee0ff2a2bfaf12194236edd3084cc16bcd2ab 100644 --- a/build.cake +++ b/build.cake @@ -283,8 +283,9 @@ Task("NugetPack") Properties = new Dictionary<string, string> { { "Configuration", configuration} - } - }; + }, + Symbols = !configuration.Equals("Release") + }; NuGetPack(project.ToString(), settings); } } diff --git a/src/ApiCommons.Tests/Properties/AssemblyInfo.cs b/src/ApiCommons.Tests/Properties/AssemblyInfo.cs index baff7cba2352b642c8dcb0fbb86b5ec2172a647e..1f389a73c1520a86b4f4925c97307bee053f2627 100644 --- a/src/ApiCommons.Tests/Properties/AssemblyInfo.cs +++ b/src/ApiCommons.Tests/Properties/AssemblyInfo.cs @@ -9,8 +9,8 @@ using System.Reflection; [assembly: AssemblyDescription("ApiCommons.Tests is a part of the CoScInE group.")] [assembly: AssemblyCompany("IT Center, RWTH Aachen University")] [assembly: AssemblyProduct("ApiCommons.Tests")] -[assembly: AssemblyVersion("1.7.0")] -[assembly: AssemblyFileVersion("1.7.0")] -[assembly: AssemblyInformationalVersion("1.7.0-sprint-2020-05-0008")] +[assembly: AssemblyVersion("1.7.1")] +[assembly: AssemblyFileVersion("1.7.1")] +[assembly: AssemblyInformationalVersion("1.7.1-beta0001")] [assembly: AssemblyCopyright("2020 IT Center, RWTH Aachen University")] diff --git a/src/ApiCommons/AbstractDefaultStartup.cs b/src/ApiCommons/AbstractDefaultStartup.cs index d2ccb8636f7c87a3e76f052f68fba972ffc61848..0b0be7b30e717b878dc2f32c9d5d45ec9fd96b00 100644 --- a/src/ApiCommons/AbstractDefaultStartup.cs +++ b/src/ApiCommons/AbstractDefaultStartup.cs @@ -52,6 +52,12 @@ namespace Coscine.ApiCommons } + // Add Middlewares which need the User to be existent + public virtual void ConfigureExtensionMiddleware(IApplicationBuilder app, IHostingEnvironment env) + { + + } + public virtual void ConfigureExtensionLate(IApplicationBuilder app, IHostingEnvironment env) { @@ -72,6 +78,8 @@ namespace Coscine.ApiCommons app.UseAuthentication(); + ConfigureExtensionMiddleware(app, env); + app.UsePathBase(_basePath); app.UseMvc(); diff --git a/src/ApiCommons/AbstractProgram.cs b/src/ApiCommons/AbstractProgram.cs index faf3c9c506293960c5bd058f925763a33bcac52b..8fe9094fbd2ff00469ffd9a9ad2f982716f6bbcf 100644 --- a/src/ApiCommons/AbstractProgram.cs +++ b/src/ApiCommons/AbstractProgram.cs @@ -30,7 +30,6 @@ namespace Coscine.ApiCommons { logger.Debug("Initialize Main Method"); - var host = new WebHostBuilder() .ConfigureServices(services => { @@ -49,8 +48,6 @@ namespace Coscine.ApiCommons .Build(); host.Run(); - - } catch (Exception ex) { diff --git a/src/ApiCommons/AbstractStartup.cs b/src/ApiCommons/AbstractStartup.cs index d5e5ceb253efd54f8266ac0d96ecabbffd1d5fa8..e69b05ca3028f60b85376820488944fbe5a7bea0 100644 --- a/src/ApiCommons/AbstractStartup.cs +++ b/src/ApiCommons/AbstractStartup.cs @@ -1,5 +1,8 @@ -using Coscine.ApiCommons.Utils; +using Coscine.ApiCommons.Middleware; +using Coscine.ApiCommons.Utils; using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; @@ -46,5 +49,12 @@ namespace Coscine.ApiCommons ConfigureServicesExtensionLate(services); } + public override void ConfigureExtensionMiddleware(IApplicationBuilder app, IHostingEnvironment env) + { + base.ConfigureExtensionMiddleware(app, env); + + app.UseMiddleware<TOSMiddleware>(); + } + } } diff --git a/src/ApiCommons/ApiCommons.csproj b/src/ApiCommons/ApiCommons.csproj index 9ef8a6629fadfc7de9f6a53ff2ea8b09527e3b26..b6c972db98f650cfe60c364cccc6c987a1de89eb 100644 --- a/src/ApiCommons/ApiCommons.csproj +++ b/src/ApiCommons/ApiCommons.csproj @@ -51,8 +51,8 @@ <Reference Include="Coscine.Configuration, Version=1.5.0.0, Culture=neutral, PublicKeyToken=ce3d7a32d7dc1e5a, processorArchitecture=MSIL"> <HintPath>..\packages\Coscine.Configuration.1.5.0\lib\net461\Coscine.Configuration.dll</HintPath> </Reference> - <Reference Include="Coscine.Database, Version=1.19.0.0, Culture=neutral, PublicKeyToken=767d77427707b70a, processorArchitecture=MSIL"> - <HintPath>..\packages\Coscine.Database.1.19.0\lib\net461\Coscine.Database.dll</HintPath> + <Reference Include="Coscine.Database, Version=1.20.0.0, Culture=neutral, PublicKeyToken=767d77427707b70a, processorArchitecture=MSIL"> + <HintPath>..\packages\Coscine.Database.1.20.0-topic-798-tosmid0001\lib\net461\Coscine.Database.dll</HintPath> </Reference> <Reference Include="Coscine.Logging, Version=1.2.0.0, Culture=neutral, PublicKeyToken=e1ed402bc3f6525e, processorArchitecture=MSIL"> <HintPath>..\packages\Coscine.Logging.1.2.0\lib\net461\Coscine.Logging.dll</HintPath> @@ -386,6 +386,7 @@ <Compile Include="Exceptions\NotAuthorizedException.cs" /> <Compile Include="Factories\ObjectFactory.cs" /> <Compile Include="AbstractProgram.cs" /> + <Compile Include="Middleware\TOSMiddleware.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="AbstractDefaultStartup.cs" /> <Compile Include="Utils\JWTHandler.cs" /> diff --git a/src/ApiCommons/Configurator.cs b/src/ApiCommons/Configurator.cs index 8b5cfe68f17edabc1732618455111a89217e2b8d..b60aac954fdebc33b3d95db2f318e765f19f9713 100644 --- a/src/ApiCommons/Configurator.cs +++ b/src/ApiCommons/Configurator.cs @@ -8,7 +8,7 @@ namespace Coscine.ApiCommons public class Configurator { public ApplicationInformation ApplicationInformation { get; set; } = new ApplicationInformation(); - public IConfiguration Configuration { get; set; } + public static IConfiguration Configuration { get; set; } public Configurator(ApplicationInformation applicationInformation, IConfiguration configuration) { diff --git a/src/ApiCommons/Middleware/TOSMiddleware.cs b/src/ApiCommons/Middleware/TOSMiddleware.cs new file mode 100644 index 0000000000000000000000000000000000000000..0a388e100175510c4d125715b920060206129888 --- /dev/null +++ b/src/ApiCommons/Middleware/TOSMiddleware.cs @@ -0,0 +1,44 @@ +using Coscine.Database.Models; +using Microsoft.AspNetCore.Http; +using Newtonsoft.Json; +using System; +using System.Linq; +using System.Net; +using System.Threading.Tasks; + +namespace Coscine.ApiCommons.Middleware +{ + public class TOSMiddleware + { + private readonly RequestDelegate _next; + + public TOSMiddleware(RequestDelegate next) + { + _next = next; + } + + public async Task Invoke(HttpContext context) + { + var userId = context.User?.FindFirst("UserID")?.Value; + if (userId != null && Guid.TryParse(userId, out Guid userIdGuid)) + { + TOSModel tosModel = new TOSModel(); + var tosAcceptedList = tosModel.GetAllWhere((entry) => entry.UserId == userIdGuid); + var currentTos = Configurator.Configuration.GetStringAndWait("coscine/global/tos/version"); + var tosAccepted = tosAcceptedList != null + && tosAcceptedList.Any((entry) => entry.Version == currentTos); + if (!tosAccepted) + { + var result = JsonConvert.SerializeObject( + new { error = $"The TOS of version {currentTos} have not been accepted!" }); + context.Response.ContentType = "application/json"; + context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; + await context.Response.WriteAsync(result); + return; + } + } + + await _next(context); + } + } +} diff --git a/src/ApiCommons/Properties/AssemblyInfo.cs b/src/ApiCommons/Properties/AssemblyInfo.cs index 0f2abcb96c10cbf4b5ca8abc403bdcc5cd15ab92..666443d55acc8992a5c8a76996723974990d45a4 100644 --- a/src/ApiCommons/Properties/AssemblyInfo.cs +++ b/src/ApiCommons/Properties/AssemblyInfo.cs @@ -9,8 +9,8 @@ using System.Reflection; [assembly: AssemblyDescription("ApiCommons is a part of the CoScInE group.")] [assembly: AssemblyCompany("IT Center, RWTH Aachen University")] [assembly: AssemblyProduct("ApiCommons")] -[assembly: AssemblyVersion("1.7.0")] -[assembly: AssemblyFileVersion("1.7.0")] -[assembly: AssemblyInformationalVersion("1.7.0-sprint-2020-05-0008")] +[assembly: AssemblyVersion("1.7.1")] +[assembly: AssemblyFileVersion("1.7.1")] +[assembly: AssemblyInformationalVersion("1.7.1-beta0001")] [assembly: AssemblyCopyright("2020 IT Center, RWTH Aachen University")] diff --git a/src/ApiCommons/packages.config b/src/ApiCommons/packages.config index fc50c2818e8f6f096138abc58f8705015c73654e..f1a3f9ba14d766615c0d27ff1142907940a698df 100644 --- a/src/ApiCommons/packages.config +++ b/src/ApiCommons/packages.config @@ -2,7 +2,7 @@ <packages> <package id="Consul" version="0.7.2.6" targetFramework="net472" /> <package id="Coscine.Configuration" version="1.5.0" targetFramework="net461" /> - <package id="Coscine.Database" version="1.19.0" targetFramework="net461" /> + <package id="Coscine.Database" version="1.20.0-topic-798-tosmid0001" targetFramework="net461" /> <package id="Coscine.Logging" version="1.2.0" targetFramework="net461" /> <package id="EntityFramework" version="6.2.0" targetFramework="net472" /> <package id="linq2db" version="2.6.4" targetFramework="net472" />