Skip to content

Commit 6424715

Browse files
mackie1001abergs
andauthored
Refactored IMetadataService to be async and removed init-related methods (#266)
* Refactored IMetadataService and implemented a new "on demand" implementation of DistributedCacheMetadataService that uses a combination of in-memory and distributed cache implementations * Renamed SimpleMetadataService to ConformanceMetadataService since that's now its only purpose * Updated Fido2MetadataServiceRepository to use IHttpClientFactory and a named client rather than using the typed client registration mechanism * Changed the origin Co-authored-by: Anders Åberg <[email protected]>
1 parent db931bd commit 6424715

15 files changed

+476
-346
lines changed

Demo/ConformanceTesting.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ public static IMetadataService MetadataServiceInstance(string cacheDir, string o
2222
new ConformanceMetadataRepository(null, origin),
2323
new FileSystemMetadataRepository(cacheDir)
2424
};
25-
_instance = new SimpleMetadataService(repos);
26-
_instance.InitializeAsync().Wait();
25+
var simpleService = new ConformanceMetadataService(repos);
26+
simpleService.InitializeAsync().Wait();
27+
_instance = simpleService;
2728
}
2829
}
2930
}

Demo/Pages/dashboard.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
if (_mds != null)
5353
{
54-
var entry = _mds.GetEntry(item.AaGuid);
54+
var entry = await _mds.GetEntryAsync(item.AaGuid);
5555
desc = entry.MetadataStatement.Description.ToString();
5656
icon = entry.MetadataStatement.Icon.ToString();
5757
}

Demo/Startup.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public void ConfigureServices(IServiceCollection services)
3030
});
3131

3232
// Use the in-memory implementation of IDistributedCache.
33+
services.AddMemoryCache();
3334
services.AddDistributedMemoryCache();
35+
3436
services.AddSession(options =>
3537
{
3638
// Set a short timeout for easy testing.
@@ -52,7 +54,10 @@ public void ConfigureServices(IServiceCollection services)
5254
})
5355
.AddCachedMetadataService(config =>
5456
{
55-
config.AddFidoMetadataRepository();
57+
config.AddFidoMetadataRepository(httpClientBuilder =>
58+
{
59+
//TODO: any specific config you want for accessing the MDS
60+
});
5661
});
5762
}
5863

Demo/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"fido2": {
33
"serverDomain": "localhost",
44
"origin": "https://localhost:44329",
5-
"timestampDriftTolerance": 300000,
5+
"timestampDriftTolerance": 300000
66
},
77
"Logging": {
88
"IncludeScopes": false,

Src/Fido2.AspNet/DateTimeUtilities.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Fido2NetLib
8+
{
9+
internal static class DateTimeUtilities
10+
{
11+
/// <summary>
12+
/// Finds the nearest future point in time that aligns with the increment provided
13+
/// e.g. 12:58:23 -> 13:00 if the increment provided is 2 minutes
14+
/// </summary>
15+
/// <param name="startTime">The time from which to calculated the next increment</param>
16+
/// <param name="increment">The increment used to calculate the new time</param>
17+
/// <returns></returns>
18+
public static DateTimeOffset GetNextIncrement(this DateTimeOffset startTime, TimeSpan increment)
19+
{
20+
//Find next increment
21+
var nextIncrementTicks = (long)(Math.Ceiling((decimal)startTime.Ticks / (decimal)increment.Ticks) * (decimal)increment.Ticks);
22+
23+
//Find the difference between the start time and the target time
24+
var timeSpanDiff = TimeSpan.FromTicks(nextIncrementTicks).Subtract(TimeSpan.FromTicks(startTime.Ticks));
25+
26+
//If the calculated difference is 0 then make it the increment value
27+
if (timeSpanDiff.Ticks == 0)
28+
timeSpanDiff = TimeSpan.FromTicks(increment.Ticks);
29+
30+
//Add the difference to the normalised time
31+
return startTime.Add(timeSpanDiff);
32+
}
33+
34+
}
35+
}

0 commit comments

Comments
 (0)