Skip to content

Commit 57b8652

Browse files
authored
Tidying (#500)
* Use primary constructors * Use collection expressions * Use primary constructors (part 2) * Move InternalsVisibleTo to csproj
1 parent 274e7a8 commit 57b8652

20 files changed

+101
-183
lines changed

Src/Fido2.Ctap2/Commands/AuthenticatorClientPinCommand.cs

+13-24
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,51 @@
33

44
namespace Fido2NetLib.Ctap2;
55

6-
public sealed class AuthenticatorClientPinCommand : CtapCommand
6+
public sealed class AuthenticatorClientPinCommand(
7+
uint pinProtocol,
8+
AuthenticatorClientPinSubCommand subCommand,
9+
CredentialPublicKey? keyAgreement = null,
10+
byte[]? pinAuth = null,
11+
byte[]? newPinEnc = null,
12+
byte[]? pinHashEnc = null) : CtapCommand
713
{
8-
public AuthenticatorClientPinCommand(
9-
uint pinProtocol,
10-
AuthenticatorClientPinSubCommand subCommand,
11-
CredentialPublicKey? keyAgreement = null,
12-
byte[]? pinAuth = null,
13-
byte[]? newPinEnc = null,
14-
byte[]? pinHashEnc = null)
15-
{
16-
17-
PinProtocol = pinProtocol;
18-
SubCommand = subCommand;
19-
KeyAgreement = keyAgreement;
20-
PinAuth = pinAuth;
21-
NewPinEnc = newPinEnc;
22-
PinHashEnc = pinHashEnc;
23-
}
24-
2514
/// <summary>
2615
/// Required PIN protocol version chosen by the client.
2716
/// </summary>
2817
[CborMember(0x01)]
29-
public uint PinProtocol { get; }
18+
public uint PinProtocol { get; } = pinProtocol;
3019

3120
/// <summary>
3221
/// The authenticator Client PIN sub command currently being requested.
3322
/// </summary>
3423
[CborMember(0x02)]
35-
public AuthenticatorClientPinSubCommand SubCommand { get; }
24+
public AuthenticatorClientPinSubCommand SubCommand { get; } = subCommand;
3625

3726
/// <summary>
3827
/// Public key of platformKeyAgreementKey.
3928
/// The COSE_Key-encoded public key MUST contain the optional "alg" parameter and MUST NOT contain any other optional parameters.
4029
/// The "alg" parameter MUST contain a COSEAlgorithmIdentifier value.
4130
/// </summary>
4231
[CborMember(0x03)]
43-
public CredentialPublicKey? KeyAgreement { get; }
32+
public CredentialPublicKey? KeyAgreement { get; } = keyAgreement;
4433

4534
/// <summary>
4635
/// First 16 bytes of HMAC-SHA-256 of encrypted contents using sharedSecret.
4736
/// </summary>
4837
[CborMember(0x04)]
49-
public byte[]? PinAuth { get; }
38+
public byte[]? PinAuth { get; } = pinAuth;
5039

5140
/// <summary>
5241
/// Encrypted new PIN using sharedSecret.
5342
/// </summary>
5443
[CborMember(0x05)]
55-
public byte[]? NewPinEnc { get; }
44+
public byte[]? NewPinEnc { get; } = newPinEnc;
5645

5746
/// <summary>
5847
/// Encrypted first 16 bytes of SHA-256 of PIN using sharedSecret.
5948
/// </summary>
6049
[CborMember(0x06)]
61-
public byte[]? PinHashEnc { get; }
50+
public byte[]? PinHashEnc { get; } = pinHashEnc;
6251

6352
public override CtapCommandType Type => CtapCommandType.AuthenticatorClientPin;
6453

Src/Fido2.Models/CredentialCreateOptions.cs

+21-28
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public sealed class CredentialCreateOptions : Fido2ResponseBase
5959
/// This member is intended for use by Relying Parties that wish to limit the creation of multiple credentials for the same account on a single authenticator.The client is requested to return an error if the new credential would be created on an authenticator that also contains one of the credentials enumerated in this parameter.
6060
/// </summary>
6161
[JsonPropertyName("excludeCredentials")]
62-
public IReadOnlyList<PublicKeyCredentialDescriptor> ExcludeCredentials { get; set; } = Array.Empty<PublicKeyCredentialDescriptor>();
62+
public IReadOnlyList<PublicKeyCredentialDescriptor> ExcludeCredentials { get; set; } = [];
6363

6464
/// <summary>
6565
/// This OPTIONAL member contains additional parameters requesting additional processing by the client and authenticator. For example, if transaction confirmation is sought from the user, then the prompt string might be included as an extension.
@@ -85,8 +85,8 @@ public static CredentialCreateOptions Create(
8585
Rp = new PublicKeyCredentialRpEntity(config.ServerDomain, config.ServerName, config.ServerIcon),
8686
Timeout = config.Timeout,
8787
User = user,
88-
PubKeyCredParams = new List<PubKeyCredParam>(10)
89-
{
88+
PubKeyCredParams =
89+
[
9090
// Add additional as appropriate
9191
PubKeyCredParam.Ed25519,
9292
PubKeyCredParam.ES256,
@@ -98,7 +98,7 @@ public static CredentialCreateOptions Create(
9898
PubKeyCredParam.ES512,
9999
PubKeyCredParam.RS512,
100100
PubKeyCredParam.PS512,
101-
},
101+
],
102102
AuthenticatorSelection = authenticatorSelection,
103103
Attestation = attestationConveyancePreference,
104104
ExcludeCredentials = excludeCredentials,
@@ -119,29 +119,25 @@ public static CredentialCreateOptions FromJson(string json)
119119

120120
#nullable enable
121121

122-
public sealed class PubKeyCredParam
122+
/// <summary>
123+
/// Constructs a PubKeyCredParam instance
124+
/// </summary>
125+
[method: JsonConstructor]
126+
public sealed class PubKeyCredParam(
127+
COSE.Algorithm alg,
128+
PublicKeyCredentialType type = PublicKeyCredentialType.PublicKey)
123129
{
124-
/// <summary>
125-
/// Constructs a PubKeyCredParam instance
126-
/// </summary>
127-
[JsonConstructor]
128-
public PubKeyCredParam(COSE.Algorithm alg, PublicKeyCredentialType type = PublicKeyCredentialType.PublicKey)
129-
{
130-
Type = type;
131-
Alg = alg;
132-
}
133-
134130
/// <summary>
135131
/// The type member specifies the type of credential to be created.
136132
/// </summary>
137133
[JsonPropertyName("type")]
138-
public PublicKeyCredentialType Type { get; }
134+
public PublicKeyCredentialType Type { get; } = type;
139135

140136
/// <summary>
141137
/// The alg member specifies the cryptographic signature algorithm with which the newly generated credential will be used, and thus also the type of asymmetric key pair to be generated, e.g., RSA or Elliptic Curve.
142138
/// </summary>
143139
[JsonPropertyName("alg")]
144-
public COSE.Algorithm Alg { get; }
140+
public COSE.Algorithm Alg { get; } = alg;
145141

146142
public static readonly PubKeyCredParam ES256 = new(COSE.Algorithm.ES256); // External authenticators support the ES256 algorithm
147143
public static readonly PubKeyCredParam ES384 = new(COSE.Algorithm.ES384);
@@ -158,31 +154,28 @@ public PubKeyCredParam(COSE.Algorithm alg, PublicKeyCredentialType type = Public
158154
/// <summary>
159155
/// PublicKeyCredentialRpEntity
160156
/// </summary>
161-
public sealed class PublicKeyCredentialRpEntity
157+
public sealed class PublicKeyCredentialRpEntity(
158+
string id,
159+
string name,
160+
string? icon = null)
162161
{
163-
public PublicKeyCredentialRpEntity(string id, string name, string? icon = null)
164-
{
165-
Name = name;
166-
Id = id;
167-
Icon = icon;
168-
}
169-
170162
/// <summary>
171163
/// A unique identifier for the Relying Party entity, which sets the RP ID.
172164
/// </summary>
173165
[JsonPropertyName("id")]
174-
public string Id { get; set; }
166+
public string Id { get; set; } = id;
175167

176168
/// <summary>
177169
/// A human-readable name for the entity. Its function depends on what the PublicKeyCredentialEntity represents:
178170
/// </summary>
179171
[JsonPropertyName("name")]
180-
public string Name { get; set; }
172+
public string Name { get; set; } = name;
181173

182174
[JsonPropertyName("icon")]
183175
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
184-
public string? Icon { get; set; }
176+
public string? Icon { get; set; } = icon;
185177
}
178+
186179
#nullable disable
187180

188181
/// <summary>

Src/Fido2.Models/Fido2Configuration.cs

+4-7
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ public IReadOnlySet<string> Origins
5252
{
5353
get
5454
{
55-
if (_origins == null)
56-
{
57-
_origins = new HashSet<string>(0);
58-
}
55+
_origins ??= new HashSet<string>(0);
5956

6057
return _origins;
6158
}
@@ -91,14 +88,14 @@ public IReadOnlySet<string> FullyQualifiedOrigins
9188
/// <summary>
9289
/// List of metadata statuses for an authenticator that should cause attestations to be rejected.
9390
/// </summary>
94-
public AuthenticatorStatus[] UndesiredAuthenticatorMetadataStatuses { get; set; } = new AuthenticatorStatus[]
95-
{
91+
public AuthenticatorStatus[] UndesiredAuthenticatorMetadataStatuses { get; set; } =
92+
[
9693
AuthenticatorStatus.ATTESTATION_KEY_COMPROMISE,
9794
AuthenticatorStatus.USER_VERIFICATION_BYPASS,
9895
AuthenticatorStatus.USER_KEY_REMOTE_COMPROMISE,
9996
AuthenticatorStatus.USER_KEY_PHYSICAL_COMPROMISE,
10097
AuthenticatorStatus.REVOKED
101-
};
98+
];
10299

103100
/// <summary>
104101
/// Whether or not to accept a backup eligible credential

Src/Fido2.Models/Objects/AuthenticationExtensionsDevicePublicKeyOutputs.cs

+6-10
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@ namespace Fido2NetLib.Objects;
44

55
using System.Text.Json.Serialization;
66

7-
public sealed class AuthenticationExtensionsDevicePublicKeyOutputs
7+
[method: JsonConstructor]
8+
public sealed class AuthenticationExtensionsDevicePublicKeyOutputs(
9+
byte[] authenticatorOutput,
10+
byte[] signature)
811
{
9-
[JsonConstructor]
10-
public AuthenticationExtensionsDevicePublicKeyOutputs(byte[] authenticatorOutput, byte[] signature)
11-
{
12-
AuthenticatorOutput = authenticatorOutput;
13-
Signature = signature;
14-
}
15-
1612
[JsonConverter(typeof(Base64UrlConverter))]
1713
[JsonPropertyName("authenticatorOutput")]
18-
public byte[] AuthenticatorOutput { get; }
14+
public byte[] AuthenticatorOutput { get; } = authenticatorOutput;
1915

2016
[JsonConverter(typeof(Base64UrlConverter))]
2117
[JsonPropertyName("signature")]
22-
public byte[] Signature { get; }
18+
public byte[] Signature { get; } = signature;
2319
}

Src/Fido2.Models/UndesiredMetadataStatusFido2VerificationException.cs

+3-7
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@
33
/// <summary>
44
/// Exception thrown when a new attestation comes from an authenticator with a current reported security issue.
55
/// </summary>
6-
public class UndesiredMetadataStatusFido2VerificationException : Fido2VerificationException
6+
public class UndesiredMetadataStatusFido2VerificationException(StatusReport statusReport)
7+
: Fido2VerificationException($"Authenticator found with undesirable status. Was {statusReport.Status}")
78
{
8-
public UndesiredMetadataStatusFido2VerificationException(StatusReport statusReport) : base($"Authenticator found with undesirable status. Was {statusReport.Status}")
9-
{
10-
StatusReport = statusReport;
11-
}
12-
139
/// <summary>
1410
/// Status report from the authenticator that caused the attestation to be rejected.
1511
/// </summary>
16-
public StatusReport StatusReport { get; }
12+
public StatusReport StatusReport { get; } = statusReport;
1713
}

Src/Fido2/Assembly.cs

-3
This file was deleted.

Src/Fido2/AttestationFormat/Tpm.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ namespace Fido2NetLib;
1515

1616
internal sealed class Tpm : AttestationVerifier
1717
{
18-
public static readonly HashSet<string> TPMManufacturers = new()
19-
{
18+
public static readonly HashSet<string> TPMManufacturers =
19+
[
2020
"id:FFFFF1D0", // FIDO testing TPM
2121
// From https://trustedcomputinggroup.org/wp-content/uploads/TCG-TPM-Vendor-ID-Registry-Version-1.02-Revision-1.00.pdf
2222
"id:414D4400", // 'AMD' AMD
@@ -42,7 +42,7 @@ internal sealed class Tpm : AttestationVerifier
4242
"id:57454300", // 'WEC' Winbond
4343
"id:524F4343", // 'ROCC' Fuzhou Rockchip
4444
"id:474F4F47", // 'GOOG' Google
45-
};
45+
];
4646

4747
public override ValueTask<VerifyAttestationResult> VerifyAsync(VerifyAttestationRequest request)
4848
{
@@ -309,9 +309,9 @@ This detects this condition and repacks each devices attributes SEQUENCE into it
309309

310310
foreach (Asn1Element o in deviceAttributes[0].Sequence)
311311
{
312-
wrappedElements.Add(Asn1Element.CreateSetOf(new List<Asn1Element>(1) {
312+
wrappedElements.Add(Asn1Element.CreateSetOf([
313313
Asn1Element.CreateSequence((List<Asn1Element>)o.Sequence)
314-
}));
314+
]));
315315
}
316316

317317
deviceAttributes = wrappedElements;

Src/Fido2/Cbor/CborBoolean.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@
22

33
namespace Fido2NetLib.Cbor;
44

5-
public sealed class CborBoolean : CborObject
5+
public sealed class CborBoolean(bool value) : CborObject
66
{
77
public static readonly CborBoolean True = new(true);
88
public static readonly CborBoolean False = new(false);
99

10-
public CborBoolean(bool value)
11-
{
12-
Value = value;
13-
}
14-
1510
public override CborType Type => CborType.Boolean;
1611

17-
public bool Value { get; }
12+
public bool Value { get; } = value;
1813

1914
public override int GetHashCode()
2015
{

Src/Fido2/Cbor/CborByteString.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,11 @@
22

33
namespace Fido2NetLib.Cbor;
44

5-
public sealed class CborByteString : CborObject
5+
public sealed class CborByteString(byte[] value) : CborObject
66
{
7-
public CborByteString(byte[] value)
8-
{
9-
ArgumentNullException.ThrowIfNull(value);
10-
11-
Value = value;
12-
}
13-
147
public override CborType Type => CborType.ByteString;
158

16-
public byte[] Value { get; }
9+
public byte[] Value { get; } = value ?? throw new ArgumentNullException(nameof(value));
1710

1811
public int Length => Value.Length;
1912

Src/Fido2/Cbor/CborInteger.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22

33
namespace Fido2NetLib.Cbor;
44

5-
internal sealed class CborInteger : CborObject
5+
internal sealed class CborInteger(long value) : CborObject
66
{
7-
public CborInteger(long value)
8-
{
9-
Value = value;
10-
}
11-
127
public override CborType Type => CborType.Integer;
138

14-
public long Value { get; }
9+
public long Value { get; } = value;
1510

1611
public override bool Equals(object? obj)
1712
{

Src/Fido2/Cbor/CborMap.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public sealed class CborMap : CborObject, IReadOnlyDictionary<CborObject, CborOb
1313

1414
public CborMap()
1515
{
16-
_items = new();
16+
_items = [];
1717
}
1818

1919
public CborMap(int capacity)

Src/Fido2/Cbor/CborTextString.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@
22

33
namespace Fido2NetLib.Cbor;
44

5-
public sealed class CborTextString : CborObject
5+
public sealed class CborTextString(string value) : CborObject
66
{
7-
public CborTextString(string value)
8-
{
9-
Value = value;
10-
}
11-
127
public override CborType Type => CborType.TextString;
138

149
public int Length => Value.Length;
1510

16-
public string Value { get; }
11+
public string Value { get; } = value ?? throw new ArgumentNullException(nameof(value));
1712

1813
public static implicit operator string(CborTextString value) => value.Value;
1914

Src/Fido2/Fido2.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="7.2.0" />
1919
</ItemGroup>
2020

21+
<ItemGroup>
22+
<InternalsVisibleTo Include="Test" />
23+
</ItemGroup>
24+
2125
<ItemGroup>
2226
<!--
2327
The name of the file must equal to the name of the package which is currently

0 commit comments

Comments
 (0)