Skip to content
Snippets Groups Projects
Commit ddd2cf08 authored by L. Ellenbeck's avatar L. Ellenbeck
Browse files

Merge branch 'Topic/798-tosMiddleware' into 'Product/708-tosProcess'

New: Implement TOSMiddleware

See merge request coscine/cs/apicommons!33
parents c7461b8d 7db0905b
No related branches found
No related tags found
3 merge requests!38Product/708-tosProcess,!36Sprint/2020-10,!33New: Implement TOSMiddleware
...@@ -283,7 +283,8 @@ Task("NugetPack") ...@@ -283,7 +283,8 @@ Task("NugetPack")
Properties = new Dictionary<string, string> Properties = new Dictionary<string, string>
{ {
{ "Configuration", configuration} { "Configuration", configuration}
} },
Symbols = !configuration.Equals("Release")
}; };
NuGetPack(project.ToString(), settings); NuGetPack(project.ToString(), settings);
} }
......
...@@ -9,8 +9,8 @@ using System.Reflection; ...@@ -9,8 +9,8 @@ using System.Reflection;
[assembly: AssemblyDescription("ApiCommons.Tests is a part of the CoScInE group.")] [assembly: AssemblyDescription("ApiCommons.Tests is a part of the CoScInE group.")]
[assembly: AssemblyCompany("IT Center, RWTH Aachen University")] [assembly: AssemblyCompany("IT Center, RWTH Aachen University")]
[assembly: AssemblyProduct("ApiCommons.Tests")] [assembly: AssemblyProduct("ApiCommons.Tests")]
[assembly: AssemblyVersion("1.7.0")] [assembly: AssemblyVersion("1.7.1")]
[assembly: AssemblyFileVersion("1.7.0")] [assembly: AssemblyFileVersion("1.7.1")]
[assembly: AssemblyInformationalVersion("1.7.0-sprint-2020-05-0008")] [assembly: AssemblyInformationalVersion("1.7.1-beta0001")]
[assembly: AssemblyCopyright("2020 IT Center, RWTH Aachen University")] [assembly: AssemblyCopyright("2020 IT Center, RWTH Aachen University")]
...@@ -52,6 +52,12 @@ namespace Coscine.ApiCommons ...@@ -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) public virtual void ConfigureExtensionLate(IApplicationBuilder app, IHostingEnvironment env)
{ {
...@@ -72,6 +78,8 @@ namespace Coscine.ApiCommons ...@@ -72,6 +78,8 @@ namespace Coscine.ApiCommons
app.UseAuthentication(); app.UseAuthentication();
ConfigureExtensionMiddleware(app, env);
app.UsePathBase(_basePath); app.UsePathBase(_basePath);
app.UseMvc(); app.UseMvc();
......
...@@ -30,7 +30,6 @@ namespace Coscine.ApiCommons ...@@ -30,7 +30,6 @@ namespace Coscine.ApiCommons
{ {
logger.Debug("Initialize Main Method"); logger.Debug("Initialize Main Method");
var host = new WebHostBuilder() var host = new WebHostBuilder()
.ConfigureServices(services => .ConfigureServices(services =>
{ {
...@@ -49,8 +48,6 @@ namespace Coscine.ApiCommons ...@@ -49,8 +48,6 @@ namespace Coscine.ApiCommons
.Build(); .Build();
host.Run(); host.Run();
} }
catch (Exception ex) catch (Exception ex)
{ {
......
using Coscine.ApiCommons.Utils; using Coscine.ApiCommons.Middleware;
using Coscine.ApiCommons.Utils;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
...@@ -46,5 +49,12 @@ namespace Coscine.ApiCommons ...@@ -46,5 +49,12 @@ namespace Coscine.ApiCommons
ConfigureServicesExtensionLate(services); ConfigureServicesExtensionLate(services);
} }
public override void ConfigureExtensionMiddleware(IApplicationBuilder app, IHostingEnvironment env)
{
base.ConfigureExtensionMiddleware(app, env);
app.UseMiddleware<TOSMiddleware>();
}
} }
} }
...@@ -51,8 +51,8 @@ ...@@ -51,8 +51,8 @@
<Reference Include="Coscine.Configuration, Version=1.5.0.0, Culture=neutral, PublicKeyToken=ce3d7a32d7dc1e5a, processorArchitecture=MSIL"> <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> <HintPath>..\packages\Coscine.Configuration.1.5.0\lib\net461\Coscine.Configuration.dll</HintPath>
</Reference> </Reference>
<Reference Include="Coscine.Database, Version=1.19.0.0, Culture=neutral, PublicKeyToken=767d77427707b70a, processorArchitecture=MSIL"> <Reference Include="Coscine.Database, Version=1.20.0.0, Culture=neutral, PublicKeyToken=767d77427707b70a, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.Database.1.19.0\lib\net461\Coscine.Database.dll</HintPath> <HintPath>..\packages\Coscine.Database.1.20.0-topic-798-tosmid0001\lib\net461\Coscine.Database.dll</HintPath>
</Reference> </Reference>
<Reference Include="Coscine.Logging, Version=1.2.0.0, Culture=neutral, PublicKeyToken=e1ed402bc3f6525e, processorArchitecture=MSIL"> <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> <HintPath>..\packages\Coscine.Logging.1.2.0\lib\net461\Coscine.Logging.dll</HintPath>
...@@ -386,6 +386,7 @@ ...@@ -386,6 +386,7 @@
<Compile Include="Exceptions\NotAuthorizedException.cs" /> <Compile Include="Exceptions\NotAuthorizedException.cs" />
<Compile Include="Factories\ObjectFactory.cs" /> <Compile Include="Factories\ObjectFactory.cs" />
<Compile Include="AbstractProgram.cs" /> <Compile Include="AbstractProgram.cs" />
<Compile Include="Middleware\TOSMiddleware.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="AbstractDefaultStartup.cs" /> <Compile Include="AbstractDefaultStartup.cs" />
<Compile Include="Utils\JWTHandler.cs" /> <Compile Include="Utils\JWTHandler.cs" />
......
...@@ -8,7 +8,7 @@ namespace Coscine.ApiCommons ...@@ -8,7 +8,7 @@ namespace Coscine.ApiCommons
public class Configurator public class Configurator
{ {
public ApplicationInformation ApplicationInformation { get; set; } = new ApplicationInformation(); public ApplicationInformation ApplicationInformation { get; set; } = new ApplicationInformation();
public IConfiguration Configuration { get; set; } public static IConfiguration Configuration { get; set; }
public Configurator(ApplicationInformation applicationInformation, IConfiguration configuration) public Configurator(ApplicationInformation applicationInformation, IConfiguration configuration)
{ {
......
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);
}
}
}
...@@ -9,8 +9,8 @@ using System.Reflection; ...@@ -9,8 +9,8 @@ using System.Reflection;
[assembly: AssemblyDescription("ApiCommons is a part of the CoScInE group.")] [assembly: AssemblyDescription("ApiCommons is a part of the CoScInE group.")]
[assembly: AssemblyCompany("IT Center, RWTH Aachen University")] [assembly: AssemblyCompany("IT Center, RWTH Aachen University")]
[assembly: AssemblyProduct("ApiCommons")] [assembly: AssemblyProduct("ApiCommons")]
[assembly: AssemblyVersion("1.7.0")] [assembly: AssemblyVersion("1.7.1")]
[assembly: AssemblyFileVersion("1.7.0")] [assembly: AssemblyFileVersion("1.7.1")]
[assembly: AssemblyInformationalVersion("1.7.0-sprint-2020-05-0008")] [assembly: AssemblyInformationalVersion("1.7.1-beta0001")]
[assembly: AssemblyCopyright("2020 IT Center, RWTH Aachen University")] [assembly: AssemblyCopyright("2020 IT Center, RWTH Aachen University")]
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<packages> <packages>
<package id="Consul" version="0.7.2.6" targetFramework="net472" /> <package id="Consul" version="0.7.2.6" targetFramework="net472" />
<package id="Coscine.Configuration" version="1.5.0" targetFramework="net461" /> <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="Coscine.Logging" version="1.2.0" targetFramework="net461" />
<package id="EntityFramework" version="6.2.0" targetFramework="net472" /> <package id="EntityFramework" version="6.2.0" targetFramework="net472" />
<package id="linq2db" version="2.6.4" targetFramework="net472" /> <package id="linq2db" version="2.6.4" targetFramework="net472" />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment