Skip to content
Snippets Groups Projects
Select Git revision
  • ddd2cf0811f955fbe64e288499a81483f139bb1a
  • master default protected
  • gitkeep
  • dev protected
  • Issue/2309-docs
  • Issue/2373-fixPagination
  • Heinrichs-master-patch-93799
  • devops-aczepiel
  • Hotfix/2097-fixTimeFormat
  • Hotfix/2087-efNet6
  • Issue/1910-MigrationtoNET6.0
  • Issue/1964-tokenExpiryUIv2
  • Sprint/2022-01
  • Sprint/2021-11
  • Sprint/2021-08
  • Hotfix/1262-fixStatuscode
  • Sprint/2021-05
  • Product/1100-fdsS3
  • Topic/1292-FdsS3
  • Product/1188-LoggingExtended
  • Topic/1221-LogginExtendedNew
  • v2.2.3
  • v2.2.2
  • v2.2.1
  • v2.2.0
  • v2.1.3
  • v2.1.2
  • v2.1.1
  • v2.1.0
  • v2.0.2
  • v2.0.1
  • v2.0.0
  • v1.12.0
  • v1.11.0
  • v1.10.0
  • v1.9.1
  • v1.9.0
  • v1.8.0
  • v1.7.3
  • v1.7.2
  • v1.7.1
41 results

TOSMiddleware.cs

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    TOSMiddleware.cs 1.53 KiB
    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);
            }
        }
    }