Skip to content

Commit

Permalink
Merge branch 'main' into update-msw
Browse files Browse the repository at this point in the history
  • Loading branch information
raymens authored Feb 20, 2024
2 parents c7e2c1c + 01d782c commit 4350597
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDateTimeProvider, SystemDateTimeProvider>();
services.AddSingleton(TimeProvider.System);

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<SignatureVerificationMiddleware> _logger;

public SignatureVerificationMiddleware(
RequestDelegate next,
IDateTimeProvider dateTimeProvider,
TimeProvider timeProvider,
ILogger<SignatureVerificationMiddleware> logger)
{
_next = next;
_dateTimeProvider = dateTimeProvider;
_timeProvider = timeProvider;
_logger = logger;
}

Expand Down Expand Up @@ -138,7 +137,7 @@ private static async Task<MemoryStream> 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;
}
Expand Down
22 changes: 0 additions & 22 deletions backend/core/src/Core.Domain/DateTimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions backend/core/tests/Core.APITests/Core.APITests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.1" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="8.0.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,8 +33,8 @@ public async Task Init()
.UseTestServer()
.ConfigureServices(services =>
{
services.AddSingleton<IDateTimeProvider>(
new StaticDateTimeProvider(
services.AddSingleton<TimeProvider>(
new FakeTimeProvider(
DateTimeOffset.FromUnixTimeSeconds(1577836800)));
})
.Configure(app =>
Expand Down

0 comments on commit 4350597

Please sign in to comment.