Skip to content

Commit

Permalink
Tidying (#500)
Browse files Browse the repository at this point in the history
* Use primary constructors

* Use collection expressions

* Use primary constructors (part 2)

* Move InternalsVisibleTo to csproj
  • Loading branch information
iamcarbon authored Feb 16, 2024
1 parent 274e7a8 commit 57b8652
Show file tree
Hide file tree
Showing 20 changed files with 101 additions and 183 deletions.
37 changes: 13 additions & 24 deletions Src/Fido2.Ctap2/Commands/AuthenticatorClientPinCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,51 @@

namespace Fido2NetLib.Ctap2;

public sealed class AuthenticatorClientPinCommand : CtapCommand
public sealed class AuthenticatorClientPinCommand(
uint pinProtocol,
AuthenticatorClientPinSubCommand subCommand,
CredentialPublicKey? keyAgreement = null,
byte[]? pinAuth = null,
byte[]? newPinEnc = null,
byte[]? pinHashEnc = null) : CtapCommand
{
public AuthenticatorClientPinCommand(
uint pinProtocol,
AuthenticatorClientPinSubCommand subCommand,
CredentialPublicKey? keyAgreement = null,
byte[]? pinAuth = null,
byte[]? newPinEnc = null,
byte[]? pinHashEnc = null)
{

PinProtocol = pinProtocol;
SubCommand = subCommand;
KeyAgreement = keyAgreement;
PinAuth = pinAuth;
NewPinEnc = newPinEnc;
PinHashEnc = pinHashEnc;
}

/// <summary>
/// Required PIN protocol version chosen by the client.
/// </summary>
[CborMember(0x01)]
public uint PinProtocol { get; }
public uint PinProtocol { get; } = pinProtocol;

/// <summary>
/// The authenticator Client PIN sub command currently being requested.
/// </summary>
[CborMember(0x02)]
public AuthenticatorClientPinSubCommand SubCommand { get; }
public AuthenticatorClientPinSubCommand SubCommand { get; } = subCommand;

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

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

/// <summary>
/// Encrypted new PIN using sharedSecret.
/// </summary>
[CborMember(0x05)]
public byte[]? NewPinEnc { get; }
public byte[]? NewPinEnc { get; } = newPinEnc;

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

public override CtapCommandType Type => CtapCommandType.AuthenticatorClientPin;

Expand Down
49 changes: 21 additions & 28 deletions Src/Fido2.Models/CredentialCreateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public sealed class CredentialCreateOptions : Fido2ResponseBase
/// 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.
/// </summary>
[JsonPropertyName("excludeCredentials")]
public IReadOnlyList<PublicKeyCredentialDescriptor> ExcludeCredentials { get; set; } = Array.Empty<PublicKeyCredentialDescriptor>();
public IReadOnlyList<PublicKeyCredentialDescriptor> ExcludeCredentials { get; set; } = [];

/// <summary>
/// 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.
Expand All @@ -85,8 +85,8 @@ public static CredentialCreateOptions Create(
Rp = new PublicKeyCredentialRpEntity(config.ServerDomain, config.ServerName, config.ServerIcon),
Timeout = config.Timeout,
User = user,
PubKeyCredParams = new List<PubKeyCredParam>(10)
{
PubKeyCredParams =
[
// Add additional as appropriate
PubKeyCredParam.Ed25519,
PubKeyCredParam.ES256,
Expand All @@ -98,7 +98,7 @@ public static CredentialCreateOptions Create(
PubKeyCredParam.ES512,
PubKeyCredParam.RS512,
PubKeyCredParam.PS512,
},
],
AuthenticatorSelection = authenticatorSelection,
Attestation = attestationConveyancePreference,
ExcludeCredentials = excludeCredentials,
Expand All @@ -119,29 +119,25 @@ public static CredentialCreateOptions FromJson(string json)

#nullable enable

public sealed class PubKeyCredParam
/// <summary>
/// Constructs a PubKeyCredParam instance
/// </summary>
[method: JsonConstructor]
public sealed class PubKeyCredParam(
COSE.Algorithm alg,
PublicKeyCredentialType type = PublicKeyCredentialType.PublicKey)
{
/// <summary>
/// Constructs a PubKeyCredParam instance
/// </summary>
[JsonConstructor]
public PubKeyCredParam(COSE.Algorithm alg, PublicKeyCredentialType type = PublicKeyCredentialType.PublicKey)
{
Type = type;
Alg = alg;
}

/// <summary>
/// The type member specifies the type of credential to be created.
/// </summary>
[JsonPropertyName("type")]
public PublicKeyCredentialType Type { get; }
public PublicKeyCredentialType Type { get; } = type;

/// <summary>
/// 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.
/// </summary>
[JsonPropertyName("alg")]
public COSE.Algorithm Alg { get; }
public COSE.Algorithm Alg { get; } = alg;

public static readonly PubKeyCredParam ES256 = new(COSE.Algorithm.ES256); // External authenticators support the ES256 algorithm
public static readonly PubKeyCredParam ES384 = new(COSE.Algorithm.ES384);
Expand All @@ -158,31 +154,28 @@ public PubKeyCredParam(COSE.Algorithm alg, PublicKeyCredentialType type = Public
/// <summary>
/// PublicKeyCredentialRpEntity
/// </summary>
public sealed class PublicKeyCredentialRpEntity
public sealed class PublicKeyCredentialRpEntity(
string id,
string name,
string? icon = null)
{
public PublicKeyCredentialRpEntity(string id, string name, string? icon = null)
{
Name = name;
Id = id;
Icon = icon;
}

/// <summary>
/// A unique identifier for the Relying Party entity, which sets the RP ID.
/// </summary>
[JsonPropertyName("id")]
public string Id { get; set; }
public string Id { get; set; } = id;

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

[JsonPropertyName("icon")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Icon { get; set; }
public string? Icon { get; set; } = icon;
}

#nullable disable

/// <summary>
Expand Down
11 changes: 4 additions & 7 deletions Src/Fido2.Models/Fido2Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ public IReadOnlySet<string> Origins
{
get
{
if (_origins == null)
{
_origins = new HashSet<string>(0);
}
_origins ??= new HashSet<string>(0);

return _origins;
}
Expand Down Expand Up @@ -91,14 +88,14 @@ public IReadOnlySet<string> FullyQualifiedOrigins
/// <summary>
/// List of metadata statuses for an authenticator that should cause attestations to be rejected.
/// </summary>
public AuthenticatorStatus[] UndesiredAuthenticatorMetadataStatuses { get; set; } = new AuthenticatorStatus[]
{
public AuthenticatorStatus[] UndesiredAuthenticatorMetadataStatuses { get; set; } =
[
AuthenticatorStatus.ATTESTATION_KEY_COMPROMISE,
AuthenticatorStatus.USER_VERIFICATION_BYPASS,
AuthenticatorStatus.USER_KEY_REMOTE_COMPROMISE,
AuthenticatorStatus.USER_KEY_PHYSICAL_COMPROMISE,
AuthenticatorStatus.REVOKED
};
];

/// <summary>
/// Whether or not to accept a backup eligible credential
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ namespace Fido2NetLib.Objects;

using System.Text.Json.Serialization;

public sealed class AuthenticationExtensionsDevicePublicKeyOutputs
[method: JsonConstructor]
public sealed class AuthenticationExtensionsDevicePublicKeyOutputs(
byte[] authenticatorOutput,
byte[] signature)
{
[JsonConstructor]
public AuthenticationExtensionsDevicePublicKeyOutputs(byte[] authenticatorOutput, byte[] signature)
{
AuthenticatorOutput = authenticatorOutput;
Signature = signature;
}

[JsonConverter(typeof(Base64UrlConverter))]
[JsonPropertyName("authenticatorOutput")]
public byte[] AuthenticatorOutput { get; }
public byte[] AuthenticatorOutput { get; } = authenticatorOutput;

[JsonConverter(typeof(Base64UrlConverter))]
[JsonPropertyName("signature")]
public byte[] Signature { get; }
public byte[] Signature { get; } = signature;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
/// <summary>
/// Exception thrown when a new attestation comes from an authenticator with a current reported security issue.
/// </summary>
public class UndesiredMetadataStatusFido2VerificationException : Fido2VerificationException
public class UndesiredMetadataStatusFido2VerificationException(StatusReport statusReport)
: Fido2VerificationException($"Authenticator found with undesirable status. Was {statusReport.Status}")
{
public UndesiredMetadataStatusFido2VerificationException(StatusReport statusReport) : base($"Authenticator found with undesirable status. Was {statusReport.Status}")
{
StatusReport = statusReport;
}

/// <summary>
/// Status report from the authenticator that caused the attestation to be rejected.
/// </summary>
public StatusReport StatusReport { get; }
public StatusReport StatusReport { get; } = statusReport;
}
3 changes: 0 additions & 3 deletions Src/Fido2/Assembly.cs

This file was deleted.

10 changes: 5 additions & 5 deletions Src/Fido2/AttestationFormat/Tpm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace Fido2NetLib;

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

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

foreach (Asn1Element o in deviceAttributes[0].Sequence)
{
wrappedElements.Add(Asn1Element.CreateSetOf(new List<Asn1Element>(1) {
wrappedElements.Add(Asn1Element.CreateSetOf([
Asn1Element.CreateSequence((List<Asn1Element>)o.Sequence)
}));
]));
}

deviceAttributes = wrappedElements;
Expand Down
9 changes: 2 additions & 7 deletions Src/Fido2/Cbor/CborBoolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@

namespace Fido2NetLib.Cbor;

public sealed class CborBoolean : CborObject
public sealed class CborBoolean(bool value) : CborObject
{
public static readonly CborBoolean True = new(true);
public static readonly CborBoolean False = new(false);

public CborBoolean(bool value)
{
Value = value;
}

public override CborType Type => CborType.Boolean;

public bool Value { get; }
public bool Value { get; } = value;

public override int GetHashCode()
{
Expand Down
11 changes: 2 additions & 9 deletions Src/Fido2/Cbor/CborByteString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

namespace Fido2NetLib.Cbor;

public sealed class CborByteString : CborObject
public sealed class CborByteString(byte[] value) : CborObject
{
public CborByteString(byte[] value)
{
ArgumentNullException.ThrowIfNull(value);

Value = value;
}

public override CborType Type => CborType.ByteString;

public byte[] Value { get; }
public byte[] Value { get; } = value ?? throw new ArgumentNullException(nameof(value));

public int Length => Value.Length;

Expand Down
9 changes: 2 additions & 7 deletions Src/Fido2/Cbor/CborInteger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@

namespace Fido2NetLib.Cbor;

internal sealed class CborInteger : CborObject
internal sealed class CborInteger(long value) : CborObject
{
public CborInteger(long value)
{
Value = value;
}

public override CborType Type => CborType.Integer;

public long Value { get; }
public long Value { get; } = value;

public override bool Equals(object? obj)
{
Expand Down
2 changes: 1 addition & 1 deletion Src/Fido2/Cbor/CborMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class CborMap : CborObject, IReadOnlyDictionary<CborObject, CborOb

public CborMap()
{
_items = new();
_items = [];
}

public CborMap(int capacity)
Expand Down
9 changes: 2 additions & 7 deletions Src/Fido2/Cbor/CborTextString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

namespace Fido2NetLib.Cbor;

public sealed class CborTextString : CborObject
public sealed class CborTextString(string value) : CborObject
{
public CborTextString(string value)
{
Value = value;
}

public override CborType Type => CborType.TextString;

public int Length => Value.Length;

public string Value { get; }
public string Value { get; } = value ?? throw new ArgumentNullException(nameof(value));

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

Expand Down
4 changes: 4 additions & 0 deletions Src/Fido2/Fido2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="7.2.0" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Test" />
</ItemGroup>

<ItemGroup>
<!--
The name of the file must equal to the name of the package which is currently
Expand Down
Loading

0 comments on commit 57b8652

Please sign in to comment.