Skip to content

Commit 9ccc79b

Browse files
committed
feat: add static class for Fido2 extensions in Extensions.cs
1 parent 2d3de7c commit 9ccc79b

File tree

6 files changed

+65
-56
lines changed

6 files changed

+65
-56
lines changed

Yubico.YubiKey/src/Yubico/YubiKey/Fido2/AuthenticatorData.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public AuthenticatorData(ReadOnlyMemory<byte> encodedData)
227227
/// </returns>
228228
public byte[] GetCredBlobExtension()
229229
{
230-
if (!TryGetExtensionData(Fido2ExtensionKeys.CredBlob, out var encodedData))
230+
if (!TryGetExtensionData(Fido2.Extensions.CredBlob, out var encodedData))
231231
{
232232
return Array.Empty<byte>();
233233
}
@@ -250,7 +250,7 @@ public byte[] GetCredBlobExtension()
250250
/// </returns>
251251
public bool GetThirdPartyPaymentExtension()
252252
{
253-
if (!TryGetExtensionData(Fido2ExtensionKeys.ThirdPartyPayment, out var encodedData))
253+
if (!TryGetExtensionData(Fido2.Extensions.ThirdPartyPayment, out var encodedData))
254254
{
255255
return false;
256256
}
@@ -276,7 +276,7 @@ public bool GetThirdPartyPaymentExtension()
276276
/// </returns>
277277
public int? GetMinPinLengthExtension()
278278
{
279-
if (!TryGetExtensionData(Fido2ExtensionKeys.MinPinLength, out var encodedData))
279+
if (!TryGetExtensionData(Fido2.Extensions.MinPinLength, out var encodedData))
280280
{
281281
return null;
282282
}
@@ -339,13 +339,13 @@ public byte[] GetHmacSecretExtension(PinUvAuthProtocolBase authProtocol)
339339
{
340340
Guard.IsNotNull(authProtocol, nameof(authProtocol));
341341

342-
if (!TryGetExtensionData(Fido2ExtensionKeys.HmacSecret, out var encodedData))
342+
if (!TryGetExtensionData(Fido2.Extensions.HmacSecret, out var encodedData))
343343
{
344344
return Array.Empty<byte>();
345345
}
346346

347347
bool hasHmacMcSecret = encodedData.Span[0] == CborHelpers.True;
348-
if (hasHmacMcSecret && !TryGetExtensionData(Fido2ExtensionKeys.HmacSecretMc, out encodedData))
348+
if (hasHmacMcSecret && !TryGetExtensionData(Fido2.Extensions.HmacSecretMc, out encodedData))
349349
{
350350
return Array.Empty<byte>();
351351
}
@@ -374,7 +374,7 @@ public byte[] GetHmacSecretExtension(PinUvAuthProtocolBase authProtocol)
374374
/// </exception>
375375
public CredProtectPolicy GetCredProtectExtension()
376376
{
377-
if (!TryGetExtensionData(Fido2ExtensionKeys.CredProtect, out var encodedValue))
377+
if (!TryGetExtensionData(Fido2.Extensions.CredProtect, out var encodedValue))
378378
{
379379
return CredProtectPolicy.None;
380380
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace Yubico.YubiKey.Fido2;
2+
3+
/// <summary>
4+
/// Contains constant strings for FIDO2 extension identifiers.
5+
/// </summary>
6+
public static class Extensions
7+
{
8+
/// <summary>
9+
/// The credential protection extension identifier.
10+
/// </summary>
11+
public const string CredProtect = "credProtect";
12+
13+
/// <summary>
14+
/// The credential blob extension identifier.
15+
/// </summary>
16+
public const string CredBlob = "credBlob";
17+
18+
/// <summary>
19+
/// The large blob key extension identifier.
20+
/// </summary>
21+
public const string LargeBlobKey = "largeBlobKey";
22+
23+
/// <summary>
24+
/// The minimum PIN length extension identifier.
25+
/// </summary>
26+
public const string MinPinLength = "minPinLength";
27+
28+
/// <summary>
29+
/// The HMAC secret extension identifier.
30+
/// </summary>
31+
public const string HmacSecret = "hmac-secret";
32+
33+
/// <summary>
34+
/// The HMAC secret multi-credential extension identifier.
35+
/// </summary>
36+
public const string HmacSecretMc = "hmac-secret-mc";
37+
38+
/// <summary>
39+
/// The third party payment extension identifier.
40+
/// </summary>
41+
public const string ThirdPartyPayment = "thirdPartyPayment";
42+
}

Yubico.YubiKey/src/Yubico/YubiKey/Fido2/Fido2ExtensionKeys.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

Yubico.YubiKey/src/Yubico/YubiKey/Fido2/GetAssertionParameters.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414

1515
using System;
1616
using System.Collections.Generic;
17-
using CommunityToolkit.Diagnostics;
1817
using Yubico.YubiKey.Fido2.Cbor;
1918
using Yubico.YubiKey.Fido2.PinProtocols;
20-
using Yubico.YubiKey.Utilities;
2119

2220
namespace Yubico.YubiKey.Fido2
2321
{
@@ -86,7 +84,7 @@ public class GetAssertionParameters : AuthenticatorOperationParameters<GetAssert
8684
/// </summary>
8785
/// <remarks>
8886
/// If you are getting assertions using
89-
/// <see cref="Fido2Session.GetAssertions"/>, you do NOT need to set this
87+
/// <see cref="Fido2Session.GetAssertions"/>, you do not need to set this
9088
/// property, the SDK will take care of it. But if you are getting
9189
/// assertions using the <see cref="Commands.GetAssertionCommand"/>, then
9290
/// you must set this property.
@@ -191,7 +189,7 @@ public void AllowCredential(CredentialId credentialId) => _allowList =
191189
/// </para>
192190
/// </remarks>
193191
public void RequestCredBlobExtension() =>
194-
AddExtension(Fido2ExtensionKeys.CredBlob, true);
192+
AddExtension(Fido2.Extensions.CredBlob, true);
195193

196194
/// <summary>
197195
/// Requests the third-party payment status of a credential during an assertion.
@@ -201,7 +199,7 @@ public void RequestCredBlobExtension() =>
201199
/// will return `true` for this extension in the assertion response. Otherwise, it will return `false`.
202200
/// </remarks>
203201
public void RequestThirdPartyPayment() =>
204-
AddExtension(Fido2ExtensionKeys.ThirdPartyPayment, true);
202+
AddExtension(Fido2.Extensions.ThirdPartyPayment, true);
205203

206204
/// <summary>
207205
/// Specify that the YubiKey should return the "hmac-secret" with the
@@ -393,7 +391,7 @@ public void EncodeHmacSecretExtension(PinUvAuthProtocolBase authProtocol)
393391
}
394392

395393
_hmacSecretEncoding = HmacSecretExtension.Encode(authProtocol, _salt1.Value, _salt2);
396-
AddExtension(Fido2ExtensionKeys.HmacSecret, _hmacSecretEncoding);
394+
AddExtension(Fido2.Extensions.HmacSecret, _hmacSecretEncoding);
397395
}
398396

399397
/// <inheritdoc/>

Yubico.YubiKey/src/Yubico/YubiKey/Fido2/MakeCredentialParameters.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
using System.Collections.Generic;
1717
using System.Formats.Cbor;
1818
using System.Globalization;
19-
using System.Linq;
2019
using CommunityToolkit.Diagnostics;
2120
using Yubico.YubiKey.Fido2.Cbor;
2221
using Yubico.YubiKey.Fido2.Cose;
2322
using Yubico.YubiKey.Fido2.PinProtocols;
24-
using Yubico.YubiKey.Utilities;
2523

2624
namespace Yubico.YubiKey.Fido2
2725
{
@@ -302,7 +300,7 @@ public void ExcludeCredential(CredentialId credentialId) =>
302300
/// as defined by standards like W3C's Secure Payment Confirmation.
303301
/// </remarks>
304302
public void AddThirdPartyPaymentExtension() =>
305-
AddExtension(Fido2ExtensionKeys.ThirdPartyPayment, true);
303+
AddExtension(Fido2.Extensions.ThirdPartyPayment, true);
306304

307305
/// <summary>
308306
/// Specify that the YubiKey should return the minimum PIN length with
@@ -347,12 +345,12 @@ public void AddThirdPartyPaymentExtension() =>
347345
public void AddMinPinLengthExtension(AuthenticatorInfo authenticatorInfo)
348346
{
349347
Guard.IsNotNull(authenticatorInfo);
350-
if (!authenticatorInfo.IsExtensionSupported(Fido2ExtensionKeys.MinPinLength))
348+
if (!authenticatorInfo.IsExtensionSupported(Fido2.Extensions.MinPinLength))
351349
{
352350
throw new NotSupportedException(ExceptionMessages.NotSupportedByYubiKeyVersion);
353351
}
354352

355-
AddExtension(Fido2ExtensionKeys.MinPinLength, true);
353+
AddExtension(Fido2.Extensions.MinPinLength, true);
356354
}
357355

358356
/// <summary>
@@ -414,7 +412,7 @@ public void AddCredBlobExtension(byte[] credBlobValue, AuthenticatorInfo authent
414412
Guard.IsNotNull(authenticatorInfo, nameof(authenticatorInfo));
415413
Guard.IsNotNull(credBlobValue, nameof(credBlobValue));
416414

417-
if (!authenticatorInfo.IsExtensionSupported(Fido2ExtensionKeys.CredBlob))
415+
if (!authenticatorInfo.IsExtensionSupported(Fido2.Extensions.CredBlob))
418416
{
419417
throw new NotSupportedException(ExceptionMessages.NotSupportedByYubiKeyVersion);
420418
}
@@ -429,7 +427,7 @@ public void AddCredBlobExtension(byte[] credBlobValue, AuthenticatorInfo authent
429427
}
430428

431429
var credBlob = new CredBlob(credBlobValue);
432-
AddExtension(Fido2ExtensionKeys.CredBlob, credBlob);
430+
AddExtension(Fido2.Extensions.CredBlob, credBlob);
433431
}
434432

435433
/// <summary>
@@ -478,12 +476,12 @@ public void AddCredBlobExtension(byte[] credBlobValue, AuthenticatorInfo authent
478476
public void AddHmacSecretExtension(AuthenticatorInfo authenticatorInfo)
479477
{
480478
Guard.IsNotNull(authenticatorInfo, nameof(authenticatorInfo));
481-
if (!authenticatorInfo.IsExtensionSupported(Fido2ExtensionKeys.HmacSecret))
479+
if (!authenticatorInfo.IsExtensionSupported(Fido2.Extensions.HmacSecret))
482480
{
483481
throw new NotSupportedException(ExceptionMessages.NotSupportedByYubiKeyVersion);
484482
}
485483

486-
AddExtension(Fido2ExtensionKeys.HmacSecret, true);
484+
AddExtension(Fido2.Extensions.HmacSecret, true);
487485
}
488486

489487
/// <summary>
@@ -535,7 +533,7 @@ public void AddHmacSecretMcExtension(
535533
{
536534
Guard.IsNotNull(authenticatorInfo, nameof(authenticatorInfo));
537535

538-
if (!authenticatorInfo.IsExtensionSupported(Fido2ExtensionKeys.HmacSecretMc))
536+
if (!authenticatorInfo.IsExtensionSupported(Fido2.Extensions.HmacSecretMc))
539537
{
540538
throw new NotSupportedException(ExceptionMessages.NotSupportedByYubiKeyVersion);
541539
}
@@ -597,8 +595,8 @@ public void EncodeHmacSecretExtension(PinUvAuthProtocolBase authProtocol)
597595

598596
_hmacSecretEncoding = HmacSecretExtension.Encode(authProtocol, _salt1.Value, _salt2);
599597

600-
AddExtension(Fido2ExtensionKeys.HmacSecret, true);
601-
AddExtension(Fido2ExtensionKeys.HmacSecretMc, _hmacSecretEncoding);
598+
AddExtension(Fido2.Extensions.HmacSecret, true);
599+
AddExtension(Fido2.Extensions.HmacSecretMc, _hmacSecretEncoding);
602600
}
603601

604602
/// <summary>
@@ -706,7 +704,7 @@ public void AddCredProtectExtension(
706704
}
707705

708706
Guard.IsNotNull(authenticatorInfo, nameof(authenticatorInfo));
709-
if (!authenticatorInfo.IsExtensionSupported(Fido2ExtensionKeys.CredProtect))
707+
if (!authenticatorInfo.IsExtensionSupported(Fido2.Extensions.CredProtect))
710708
{
711709
if (enforceCredProtectPolicy && credProtectPolicy != CredProtectPolicy.UserVerificationOptional)
712710
{
@@ -719,7 +717,7 @@ public void AddCredProtectExtension(
719717
// The encoding is key/value where the key is "credProtect" and the
720718
// value is an unsigned int (major type 0). The only three possible
721719
// values are 1, 2, or 3, so the encoding is simply 0x01, 02,or 03.
722-
AddExtension(Fido2ExtensionKeys.CredProtect, (byte)credProtectPolicy);
720+
AddExtension(Fido2.Extensions.CredProtect, (byte)credProtectPolicy);
723721
}
724722

725723
/// <inheritdoc cref="AddCredProtectExtension(CredProtectPolicy,bool,AuthenticatorInfo)"/>

Yubico.YubiKey/tests/integration/Yubico/YubiKey/Fido2/LargeBlobTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void SetLargeBlob_Succeeds()
6161
{
6262
ClientDataHash = _clientDataHash
6363
};
64-
mcParams1.AddExtension("largeBlobKey", new byte[] { 0xF5 });
64+
mcParams1.AddExtension(Extensions.LargeBlobKey, new byte[] { 0xF5 });
6565
mcParams1.AddOption(AuthenticatorOptions.rk, true);
6666

6767
fido2Session.AddPermissions(PinUvAuthTokenPermissions.AuthenticatorConfiguration);

0 commit comments

Comments
 (0)