Skip to content

Commit 7b69f54

Browse files
iamcarbonabergs
authored andcommitted
Strongly type AaGuid
1 parent 3044795 commit 7b69f54

File tree

8 files changed

+38
-34
lines changed

8 files changed

+38
-34
lines changed

Src/Fido2.AspNet/DistributedCacheMetadataService.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,16 @@ protected virtual async Task<MetadataBLOBPayload> GetMemoryCachedPayload(IMetada
182182

183183
public async Task<MetadataBLOBPayloadEntry> GetEntryAsync(Guid aaguid, CancellationToken cancellationToken = default)
184184
{
185-
var aaguidComparisonString = aaguid.ToString("D");
186-
187185
var memCacheEntry = await _memoryCache.GetOrCreateAsync<MetadataBLOBPayloadEntry>(
188-
$"{CACHE_PREFIX}:{aaguidComparisonString}",
186+
$"{CACHE_PREFIX}:{aaguid}",
189187
async entry =>
190188
{
191189
foreach (var repo in _repositories)
192190
{
193191
var cachedPayload = await GetMemoryCachedPayload(repo, cancellationToken);
194192
if (cachedPayload != null)
195193
{
196-
var matchingEntry = cachedPayload.Entries?.FirstOrDefault(o => o.AaGuid == aaguidComparisonString);
194+
var matchingEntry = cachedPayload.Entries?.FirstOrDefault(o => o.AaGuid == aaguid);
197195
if (matchingEntry != null)
198196
{
199197
entry.AbsoluteExpiration = GetMemoryCacheAbsoluteExpiryTime(GetNextUpdateTimeFromPayload(cachedPayload));

Src/Fido2.Models/Metadata/MetadataBLOBPayloadEntry.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel.DataAnnotations;
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
23
using System.Linq;
34
using System.Text.Json.Serialization;
45

@@ -24,7 +25,7 @@ public sealed class MetadataBLOBPayloadEntry
2425
/// <para>The Authenticator Attestation GUID.</para>
2526
/// </summary>
2627
[JsonPropertyName("aaguid")]
27-
public string AaGuid { get; set; }
28+
public Guid? AaGuid { get; set; }
2829

2930
/// <summary>
3031
/// Gets or sets a list of the attestation certificate public key identifiers encoded as hex string.

Src/Fido2.Models/Metadata/MetadataStatement.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel.DataAnnotations;
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
23
using System.Text.Json.Serialization;
34

45
namespace Fido2NetLib;
@@ -34,7 +35,7 @@ public class MetadataStatement
3435
/// <para>Note: FIDO 2 Authenticators support AAGUID, but they don't support AAID.</para>
3536
/// </remarks>
3637
[JsonPropertyName("aaguid")]
37-
public string AaGuid { get; set; }
38+
public Guid? AaGuid { get; set; }
3839

3940
/// <summary>
4041
/// Gets or sets a list of the attestation certificate public key identifiers encoded as hex string.

Src/Fido2.Models/Objects/AttestationVerificationSuccess.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Security.Cryptography.X509Certificates;
1+
using System;
2+
using System.Security.Cryptography.X509Certificates;
23
using System.Text.Json.Serialization;
34

45
namespace Fido2NetLib.Objects;
@@ -12,10 +13,14 @@ public class AttestationVerificationSuccess : AssertionVerificationResult
1213
public byte[] PublicKey { get; set; }
1314

1415
public Fido2User User { get; set; }
16+
1517
public string CredType { get; set; }
16-
public System.Guid Aaguid { get; set; }
18+
19+
public Guid AaGuid { get; set; }
20+
1721
#nullable enable
1822
public X509Certificate2? AttestationCertificate { get; set; }
1923
#nullable disable
24+
2025
public X509Certificate2[] AttestationCertificateChain { get; set; }
2126
}

Src/Fido2/ConformanceMetadataService.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ public bool ConformanceTesting()
4848

4949
protected virtual async Task LoadEntryStatementAsync(IMetadataRepository repository, MetadataBLOBPayload blob, MetadataBLOBPayloadEntry entry, CancellationToken cancellationToken)
5050
{
51-
if (entry.AaGuid != null)
51+
if (entry.AaGuid.HasValue)
5252
{
5353
var statement = await repository.GetMetadataStatementAsync(blob, entry, cancellationToken);
5454

55-
if (!string.IsNullOrWhiteSpace(statement?.AaGuid))
55+
if (statement?.AaGuid is Guid aaGuid)
5656
{
57-
_metadataStatements.TryAdd(Guid.Parse(statement.AaGuid), statement);
57+
_metadataStatements.TryAdd(aaGuid, statement);
5858
}
5959
}
6060
}
@@ -65,9 +65,9 @@ protected virtual async Task InitializeRepositoryAsync(IMetadataRepository repos
6565

6666
foreach (var entry in blob.Entries)
6767
{
68-
if (!string.IsNullOrEmpty(entry.AaGuid))
68+
if (entry.AaGuid is Guid aaGuid)
6969
{
70-
if (_entries.TryAdd(Guid.Parse(entry.AaGuid), entry))
70+
if (_entries.TryAdd(aaGuid, entry))
7171
{
7272
//Load if it doesn't already exist
7373
await LoadEntryStatementAsync(repository, blob, entry, cancellationToken);

Src/Fido2/Metadata/FileSystemMetadataRepository.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ public FileSystemMetadataRepository(string path)
2727
if (_blob is null)
2828
await GetBLOBAsync(cancellationToken);
2929

30-
if (!string.IsNullOrEmpty(entry.AaGuid) && Guid.TryParse(entry.AaGuid, out Guid parsedAaGuid))
30+
if (entry.AaGuid is Guid aaGuid && _entries.TryGetValue(aaGuid, out var found))
3131
{
32-
if (_entries.ContainsKey(parsedAaGuid))
33-
return _entries[parsedAaGuid].MetadataStatement;
32+
return found.MetadataStatement;
3433
}
3534

3635
return null;
@@ -56,7 +55,7 @@ public async Task<MetadataBLOBPayload> GetBLOBAsync(CancellationToken cancellati
5655
}
5756
}
5857
};
59-
if (null != conformanceEntry.AaGuid) _entries.Add(new Guid(conformanceEntry.AaGuid), conformanceEntry);
58+
if (null != conformanceEntry.AaGuid) _entries.Add(conformanceEntry.AaGuid.Value, conformanceEntry);
6059
}
6160
}
6261

Test/Fido2Tests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,13 +503,13 @@ public void MetadataBLOBPayloadEntry_Can_Be_JSON_Roundtripped()
503503
{
504504
var input = new MetadataBLOBPayloadEntry()
505505
{
506-
AaGuid = Guid.NewGuid().ToString(),
506+
AaGuid = Guid.NewGuid(),
507507
MetadataStatement = new MetadataStatement(),
508508
StatusReports = Array.Empty<StatusReport>(),
509509
TimeOfLastStatusChange = DateTime.UtcNow.ToString("o")
510510
};
511511

512-
input.MetadataStatement.AaGuid = Guid.NewGuid().ToString();
512+
input.MetadataStatement.AaGuid = Guid.NewGuid();
513513
input.MetadataStatement.Description = "Test entry";
514514
input.MetadataStatement.AuthenticatorVersion = 1;
515515
input.MetadataStatement.Upv = new UafVersion[] { new UafVersion

Test/MetadataServiceTests.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ public Task<MetadataBLOBPayload> GetBLOBAsync(CancellationToken cancellationToke
6262
Number = _number,
6363
Entries = new MetadataBLOBPayloadEntry[]
6464
{
65-
new MetadataBLOBPayloadEntry()
65+
new MetadataBLOBPayloadEntry
6666
{
67-
AaGuid = "6d44ba9b-f6ec-2e49-b930-0c8fe920cb73",
68-
MetadataStatement = new MetadataStatement()
67+
AaGuid = Guid.Parse("6d44ba9b-f6ec-2e49-b930-0c8fe920cb73"),
68+
MetadataStatement = new MetadataStatement
6969
{
7070
Description = "Security Key by Yubico with NFC"
7171
}
@@ -137,13 +137,13 @@ public async Task DistributeCacheMetadataService_Cache_Rollover_Works()
137137
currentTimeClock
138138
);
139139

140-
var entryIdString = "6d44ba9b-f6ec-2e49-b930-0c8fe920cb73";
140+
var entryIdGuid = Guid.Parse("6d44ba9b-f6ec-2e49-b930-0c8fe920cb73");
141141

142-
var entry = await serviceInstance1.GetEntryAsync(Guid.Parse(entryIdString));
142+
var entry = await serviceInstance1.GetEntryAsync(entryIdGuid);
143143

144144
for (int x = 0; x < 10; x++)
145145
{
146-
await serviceInstance1.GetEntryAsync(Guid.Parse(entryIdString));
146+
await serviceInstance1.GetEntryAsync(entryIdGuid);
147147
}
148148

149149
Assert.Equal(1, staticClient.GetBLOBAsyncCount);
@@ -152,36 +152,36 @@ public async Task DistributeCacheMetadataService_Cache_Rollover_Works()
152152

153153
var blobEntry = await distributedCache.GetStringAsync("DistributedCacheMetadataService:V2:" + staticClient.GetType().Name + ":TOC");
154154

155-
var itemEntry = memCache.Get<MetadataBLOBPayloadEntry>("DistributedCacheMetadataService:V2:" + entryIdString);
155+
var itemEntry = memCache.Get<MetadataBLOBPayloadEntry>($"DistributedCacheMetadataService:V2:{entryIdGuid}");
156156

157157
Assert.NotNull(blobEntry);
158158

159-
Assert.Equal(itemEntry.AaGuid, entryIdString);
159+
Assert.Equal(itemEntry.AaGuid, entryIdGuid);
160160

161161
currentTimeClock.UtcNow = DateTimeOffset.Parse("2021-11-30 23:59:59.999Z"); //Before next update
162162

163-
await serviceInstance1.GetEntryAsync(Guid.Parse(entryIdString));
163+
await serviceInstance1.GetEntryAsync(entryIdGuid);
164164

165165
Assert.Equal(1, staticClient.GetBLOBAsyncCount);
166166

167167
currentTimeClock.UtcNow = DateTimeOffset.Parse("2021-12-02 00:59:59.999Z"); //Before buffer period (25 hours)
168168

169-
await serviceInstance1.GetEntryAsync(Guid.Parse(entryIdString));
170-
await serviceInstance1.GetEntryAsync(Guid.Parse(entryIdString));
169+
await serviceInstance1.GetEntryAsync(entryIdGuid);
170+
await serviceInstance1.GetEntryAsync(entryIdGuid);
171171

172172
Assert.Equal(1, staticClient.GetBLOBAsyncCount);
173173

174174
currentTimeClock.UtcNow = DateTimeOffset.Parse("2021-12-02 01:00:00.001Z"); //After buffer period (25 hours)
175175

176176
staticClient.NextUpdate = "2021-12-30";
177177

178-
await serviceInstance1.GetEntryAsync(Guid.Parse(entryIdString));
178+
await serviceInstance1.GetEntryAsync(entryIdGuid);
179179

180180
Assert.Equal(2, staticClient.GetBLOBAsyncCount);
181181

182182
currentTimeClock.UtcNow = DateTimeOffset.Parse("2021-12-29 01:00:00.001Z");
183183

184-
await serviceInstance1.GetEntryAsync(Guid.Parse(entryIdString));
184+
await serviceInstance1.GetEntryAsync(entryIdGuid);
185185

186186
Assert.Equal(2, staticClient.GetBLOBAsyncCount);
187187
}

0 commit comments

Comments
 (0)