Skip to content

Commit

Permalink
Use TimeProvider instead of ISystemClock (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
trejjam authored May 7, 2024
1 parent e5cb022 commit 84c8cb5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ IServiceCollection serviceCollection;

// you may need to add these dependencies
serviceCollection.AddMemoryCache();
using Microsoft.Extensions.Internal;
serviceCollection.TryAddSingleton<ISystemClock, SystemClock>();
using System;
serviceCollection.TryAddSingleton<TimeProvider>(TimeProvider.System);

// configure AppStoreConnect services
serviceCollection.AddAppleAppStoreConnect(optionsBuilder => optionsBuilder
Expand Down
2 changes: 2 additions & 0 deletions src/Apple.AppStoreConnect/Apple.AppStoreConnect.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
<PackageReference Include="H.NSwag.Generator" Version="14.0.7.76" PrivateAssets="All" />
<PackageReference Include="Microsoft.Bcl.TimeProvider" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
Expand All @@ -41,6 +42,7 @@

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="H.NSwag.Generator" Version="14.0.7.76" PrivateAssets="All" />
<PackageReference Include="Microsoft.Bcl.TimeProvider" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
Expand Down
9 changes: 4 additions & 5 deletions src/Apple.AppStoreConnect/DefaultJwtGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Apple.AppStoreConnect.Interfaces;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
Expand All @@ -14,19 +13,19 @@ namespace Apple.AppStoreConnect;
public partial class DefaultJwtGenerator : IJwtGenerator
{
private readonly IMemoryCache _cache;
private readonly ISystemClock _clock;
private readonly TimeProvider _timeProvider;
private readonly IOptions<AppleAuthenticationOptions> _appleAuthenticationOptions;
private readonly ILogger _logger;

public DefaultJwtGenerator(
IMemoryCache cache,
ISystemClock clock,
TimeProvider timeProvider,
IOptions<AppleAuthenticationOptions> appleAuthenticationOptions,
ILogger<DefaultJwtGenerator> logger
)
{
_cache = cache;
_clock = clock;
_timeProvider = timeProvider;
_appleAuthenticationOptions = appleAuthenticationOptions;
_logger = logger;
}
Expand Down Expand Up @@ -78,7 +77,7 @@ AppleAuthenticationOptions options
AppleAuthenticationOptions appleAuthenticationOptions, CancellationToken cancellationToken
)
{
var now = _clock.UtcNow;
var now = _timeProvider.GetUtcNow();
var expiresAt = now.Add(appleAuthenticationOptions.JwtExpiresAfter);

Log.GeneratingNewJwtToken(_logger, appleAuthenticationOptions.KeyId, expiresAt);
Expand Down
7 changes: 4 additions & 3 deletions src/Test.Apple.AppStoreConnect/DefaultJwtGeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Time.Testing;
using System;
using System.IO;
using System.Threading.Tasks;
Expand Down Expand Up @@ -43,7 +43,7 @@ public async Task GenerateJwtTokenWorks()
.AddConsole()
.AddDebug()
)
.AddSingleton<ISystemClock, SystemClock>();
.AddSingleton<TimeProvider, FakeTimeProvider>();

serviceCollection.TryAddEnumerable(ServiceDescriptor
.Singleton<IPostConfigureOptions<AppleAuthenticationOptions>, AppleAuthenticationPostConfigure>()
Expand Down Expand Up @@ -90,7 +90,8 @@ public async Task TwoGenerateJwtTokenWorks(int delay)
.AddConsole()
.AddDebug()
)
.AddSingleton<ISystemClock, SystemClock>();
.AddSingleton<FakeTimeProvider>()
.AddSingleton<TimeProvider>(s => s.GetRequiredService<FakeTimeProvider>());

serviceCollection.TryAddEnumerable(ServiceDescriptor
.Singleton<IPostConfigureOptions<AppleAuthenticationOptions>, AppleAuthenticationPostConfigure>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="8.4.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="8.4.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="8.0.4" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="8.0.0" />
Expand Down

0 comments on commit 84c8cb5

Please sign in to comment.