From 01d782c9fdd90a506e5492f32f1c0095947887f6 Mon Sep 17 00:00:00 2001 From: Raymen Scholten Date: Tue, 20 Feb 2024 14:31:30 +0100 Subject: [PATCH] Cleanup using new TimeProvider (#68) * Cleanup using new TimeProvider * Register timeprovider --- .../ApplicationInjection.cs | 2 +- .../SignatureVerificationMiddleware.cs | 9 ++++---- .../core/src/Core.Domain/DateTimeProvider.cs | 22 ------------------- .../tests/Core.APITests/Core.APITests.csproj | 1 + .../SignatureVerificationMiddlewareTests.cs | 6 ++--- 5 files changed, 9 insertions(+), 31 deletions(-) diff --git a/backend/core/src/Core.API/DependencyInjection/ApplicationInjection.cs b/backend/core/src/Core.API/DependencyInjection/ApplicationInjection.cs index 74ffb6f..bafb9e0 100644 --- a/backend/core/src/Core.API/DependencyInjection/ApplicationInjection.cs +++ b/backend/core/src/Core.API/DependencyInjection/ApplicationInjection.cs @@ -20,7 +20,7 @@ public static IServiceCollection AddApplication(this IServiceCollection services services.AddValidatorsFromAssembly(Application.AssemblyReference.Assembly); services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Application.AssemblyReference.Assembly)); - services.AddSingleton(); + services.AddSingleton(TimeProvider.System); return services; } diff --git a/backend/core/src/Core.API/ResponseHandling/SignatureVerificationMiddleware.cs b/backend/core/src/Core.API/ResponseHandling/SignatureVerificationMiddleware.cs index f6626f3..980339e 100644 --- a/backend/core/src/Core.API/ResponseHandling/SignatureVerificationMiddleware.cs +++ b/backend/core/src/Core.API/ResponseHandling/SignatureVerificationMiddleware.cs @@ -2,7 +2,6 @@ // under the Apache License, Version 2.0. See the NOTICE file at the root // of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 -using Core.Domain; using Core.Domain.Exceptions; using Core.Presentation.Models; using NSec.Cryptography; @@ -23,16 +22,16 @@ namespace Core.API.ResponseHandling public class SignatureVerificationMiddleware { private readonly RequestDelegate _next; - private readonly IDateTimeProvider _dateTimeProvider; + private readonly TimeProvider _timeProvider; private readonly ILogger _logger; public SignatureVerificationMiddleware( RequestDelegate next, - IDateTimeProvider dateTimeProvider, + TimeProvider timeProvider, ILogger logger) { _next = next; - _dateTimeProvider = dateTimeProvider; + _timeProvider = timeProvider; _logger = logger; } @@ -138,7 +137,7 @@ private static async Task GetPayloadStream(HttpContext context, st private bool IsWithinAllowedTime(long timestampHeaderLong) { var suppliedDateTime = DateTimeOffset.FromUnixTimeSeconds(timestampHeaderLong); - var dateDiff = _dateTimeProvider.UtcNow - suppliedDateTime; + var dateDiff = _timeProvider.GetUtcNow() - suppliedDateTime; long allowedDifference = 30; // 30 seconds return Math.Abs(dateDiff.TotalSeconds) <= allowedDifference; } diff --git a/backend/core/src/Core.Domain/DateTimeProvider.cs b/backend/core/src/Core.Domain/DateTimeProvider.cs index 340a42e..365635d 100644 --- a/backend/core/src/Core.Domain/DateTimeProvider.cs +++ b/backend/core/src/Core.Domain/DateTimeProvider.cs @@ -6,28 +6,6 @@ namespace Core.Domain { - public interface IDateTimeProvider - { - public DateTimeOffset UtcNow { get; } - } - - public class StaticDateTimeProvider : IDateTimeProvider - { - private readonly DateTimeOffset dateTimeOffset; - - public StaticDateTimeProvider(DateTimeOffset dateTimeOffset) - { - this.dateTimeOffset = dateTimeOffset; - } - - public DateTimeOffset UtcNow => dateTimeOffset; - } - - public class SystemDateTimeProvider : IDateTimeProvider - { - public DateTimeOffset UtcNow => DateTimeOffset.UtcNow; - } - // https://dvoituron.com/2020/01/22/UnitTest-DateTime/ public class DateTimeProvider { diff --git a/backend/core/tests/Core.APITests/Core.APITests.csproj b/backend/core/tests/Core.APITests/Core.APITests.csproj index 823f231..09b61ab 100644 --- a/backend/core/tests/Core.APITests/Core.APITests.csproj +++ b/backend/core/tests/Core.APITests/Core.APITests.csproj @@ -15,6 +15,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/backend/core/tests/Core.APITests/ResponseHandlingTests/SignatureVerificationMiddlewareTests.cs b/backend/core/tests/Core.APITests/ResponseHandlingTests/SignatureVerificationMiddlewareTests.cs index ac7442f..94f84c0 100644 --- a/backend/core/tests/Core.APITests/ResponseHandlingTests/SignatureVerificationMiddlewareTests.cs +++ b/backend/core/tests/Core.APITests/ResponseHandlingTests/SignatureVerificationMiddlewareTests.cs @@ -3,13 +3,13 @@ // of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 using Core.API.ResponseHandling; -using Core.Domain; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Time.Testing; using NSec.Cryptography; using System.Net; using System.Net.Http.Headers; @@ -33,8 +33,8 @@ public async Task Init() .UseTestServer() .ConfigureServices(services => { - services.AddSingleton( - new StaticDateTimeProvider( + services.AddSingleton( + new FakeTimeProvider( DateTimeOffset.FromUnixTimeSeconds(1577836800))); }) .Configure(app =>