Skip to content

Commit 4964087

Browse files
committed
Cleanups
1 parent 7aa5a11 commit 4964087

File tree

9 files changed

+45
-43
lines changed

9 files changed

+45
-43
lines changed

src/Identity/Extensions.Core/src/AuthenticatorSelectionCriteria.cs renamed to src/Identity/Core/src/AuthenticatorSelectionCriteria.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
5-
64
namespace Microsoft.AspNetCore.Identity;
75

86
/// <summary>

src/Identity/Core/src/IdentityJsonSerializerContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ namespace Microsoft.AspNetCore.Identity;
1111
[JsonSerializable(typeof(PublicKeyCredentialRequestOptions))]
1212
[JsonSerializable(typeof(PublicKeyCredential<AuthenticatorAssertionResponse>))]
1313
[JsonSerializable(typeof(PublicKeyCredential<AuthenticatorAttestationResponse>))]
14-
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
14+
[JsonSourceGenerationOptions(
15+
JsonSerializerDefaults.Web,
16+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
17+
RespectNullableAnnotations = true)]
1518
internal partial class IdentityJsonSerializerContext : JsonSerializerContext;

src/Identity/Core/src/PasskeyExceptionExtensions.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Formats.Cbor;
54
using System.Text.Json;
65

76
namespace Microsoft.AspNetCore.Identity;
@@ -14,7 +13,7 @@ public static PasskeyException InvalidCredentialType(string expectedType, string
1413
=> new($"Expected credential type '{expectedType}', got '{actualType}'.");
1514

1615
public static PasskeyException InvalidClientDataType(string expectedType, string actualType)
17-
=> new($"Expected the 'type' field of client data to be '{expectedType}', but it was actually '{actualType}'.");
16+
=> new($"Expected the client data JSON 'type' field to be '{expectedType}', got '{actualType}'.");
1817

1918
public static PasskeyException InvalidChallenge()
2019
=> new("The authenticator response challenge does not match original challenge.");
@@ -91,7 +90,7 @@ public static PasskeyException SignCountLessThanStoredSignCount()
9190
public static PasskeyException InvalidAttestationObject(Exception ex)
9291
=> new($"An exception occurred while parsing the attestation object: {ex.Message}", ex);
9392

94-
public static PasskeyException InvalidAttestationObjectFormat(CborContentException ex)
93+
public static PasskeyException InvalidAttestationObjectFormat(Exception ex)
9594
=> new("The attestation object had an invalid format.", ex);
9695

9796
public static PasskeyException MissingAttestationStatementFormat()
@@ -104,13 +103,13 @@ public static PasskeyException MissingAuthenticatorData()
104103
=> new("The attestation object did not include authenticator data.");
105104

106105
public static PasskeyException InvalidAuthenticatorDataLength(int length)
107-
=> new($"The authenticator data had an invalid length of {length} bytes.");
106+
=> new($"The authenticator data had an invalid byte count of {length}.");
108107

109108
public static PasskeyException InvalidAuthenticatorDataFormat(Exception? ex = null)
110109
=> new($"The authenticator data had an invalid format.", ex);
111110

112111
public static PasskeyException InvalidAttestedCredentialDataLength(int length)
113-
=> new($"The attested credential data had an invalid length of {length} bytes.");
112+
=> new($"The attested credential data had an invalid byte count of {length}.");
114113

115114
public static PasskeyException InvalidAttestedCredentialDataFormat(Exception? ex = null)
116115
=> new($"The attested credential data had an invalid format.", ex);

src/Identity/Core/src/Passkeys/AttestationObject.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public static AttestationObject Parse(ReadOnlyMemory<byte> data)
5151
{
5252
throw PasskeyException.InvalidAttestationObjectFormat(ex);
5353
}
54+
catch (InvalidOperationException ex)
55+
{
56+
throw PasskeyException.InvalidAttestationObjectFormat(ex);
57+
}
5458
catch (Exception ex)
5559
{
5660
throw PasskeyException.InvalidAttestationObject(ex);

src/Identity/Core/src/Passkeys/CredentialPublicKey.cs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public bool Verify(ReadOnlySpan<byte> data, ReadOnlySpan<byte> signature)
6969
return _type switch
7070
{
7171
COSEKeyType.EC2 => _ecdsa!.VerifyData(data, signature, HashAlgFromCOSEAlg(_alg), DSASignatureFormat.Rfc3279DerSequence),
72-
COSEKeyType.RSA => _rsa!.VerifyData(data, signature, HashAlgFromCOSEAlg(_alg), Padding),
72+
COSEKeyType.RSA => _rsa!.VerifyData(data, signature, HashAlgFromCOSEAlg(_alg), GetRSASignaturePadding()),
7373
_ => throw new InvalidOperationException($"Missing or unknown kty {_type}"),
7474
};
7575
}
@@ -159,31 +159,29 @@ static bool IsValidKtyCrvCombination(COSEKeyType kty, COSEEllipticCurve crv)
159159
}
160160
}
161161

162-
internal RSASignaturePadding Padding
162+
private RSASignaturePadding GetRSASignaturePadding()
163163
{
164-
get
164+
if (_type != COSEKeyType.RSA)
165165
{
166-
if (_type != COSEKeyType.RSA)
167-
{
168-
throw new InvalidOperationException($"Must be a RSA key. Was {_type}");
169-
}
170-
171-
switch (_alg) // https://www.iana.org/assignments/cose/cose.xhtml#algorithms
172-
{
173-
case COSEAlgorithmIdentifier.PS256:
174-
case COSEAlgorithmIdentifier.PS384:
175-
case COSEAlgorithmIdentifier.PS512:
176-
return RSASignaturePadding.Pss;
177-
178-
case COSEAlgorithmIdentifier.RS1:
179-
case COSEAlgorithmIdentifier.RS256:
180-
case COSEAlgorithmIdentifier.RS384:
181-
case COSEAlgorithmIdentifier.RS512:
182-
return RSASignaturePadding.Pkcs1;
183-
default:
184-
throw new InvalidOperationException($"Missing or unknown alg {_alg}");
185-
}
166+
throw new InvalidOperationException($"Cannot get RSA signature padding for key type {_type}.");
186167
}
168+
169+
// https://www.iana.org/assignments/cose/cose.xhtml#algorithms
170+
return _alg switch
171+
{
172+
COSEAlgorithmIdentifier.PS256 or
173+
COSEAlgorithmIdentifier.PS384 or
174+
COSEAlgorithmIdentifier.PS512
175+
=> RSASignaturePadding.Pss,
176+
177+
COSEAlgorithmIdentifier.RS1 or
178+
COSEAlgorithmIdentifier.RS256 or
179+
COSEAlgorithmIdentifier.RS384 or
180+
COSEAlgorithmIdentifier.RS512
181+
=> RSASignaturePadding.Pkcs1,
182+
183+
_ => throw new InvalidOperationException($"Missing or unknown alg {_alg}"),
184+
};
187185
}
188186

189187
private static HashAlgorithmName HashAlgFromCOSEAlg(COSEAlgorithmIdentifier alg)

src/Identity/Core/src/Passkeys/PublicKeyCredential.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Identity;
1212
/// See <see href="https://www.w3.org/TR/webauthn-3/#typedefdef-publickeycredentialjson" />
1313
/// </remarks>
1414
internal sealed class PublicKeyCredential<TResponse>
15-
where TResponse : AuthenticatorResponse
15+
where TResponse : notnull, AuthenticatorResponse
1616
{
1717
/// <summary>
1818
/// Gets or sets the credential ID.

src/Identity/Core/src/PublicAPI.Unshipped.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
#nullable enable
2+
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria
3+
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.AuthenticatorAttachment.get -> string?
4+
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.AuthenticatorAttachment.set -> void
5+
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.AuthenticatorSelectionCriteria() -> void
6+
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.RequireResidentKey.get -> bool
7+
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.ResidentKey.get -> string?
8+
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.ResidentKey.set -> void
9+
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.UserVerification.get -> string!
10+
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.UserVerification.set -> void
211
Microsoft.AspNetCore.Identity.DefaultPasskeyHandler<TUser>
312
Microsoft.AspNetCore.Identity.DefaultPasskeyHandler<TUser>.DefaultPasskeyHandler(Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Identity.IdentityOptions!>! options) -> void
413
Microsoft.AspNetCore.Identity.DefaultPasskeyHandler<TUser>.PerformAssertionAsync(Microsoft.AspNetCore.Identity.PasskeyAssertionContext<TUser!>! context) -> System.Threading.Tasks.Task<Microsoft.AspNetCore.Identity.PasskeyAssertionResult<TUser!>!>!

src/Identity/Core/src/SignInManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ public virtual async Task<PasskeyCreationOptions> ConfigurePasskeyCreationOption
585585

586586
var props = new AuthenticationProperties();
587587
props.Items[PasskeyCreationOptionsKey] = options.AsJson();
588-
var claimsIdentity = new ClaimsIdentity(new ClaimsIdentity(IdentityConstants.TwoFactorUserIdScheme));
588+
var claimsIdentity = new ClaimsIdentity(IdentityConstants.TwoFactorUserIdScheme);
589589
claimsIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, options.UserEntity.Id));
590590
claimsIdentity.AddClaim(new Claim(ClaimTypes.Email, options.UserEntity.Name));
591591
claimsIdentity.AddClaim(new Claim(ClaimTypes.Name, options.UserEntity.DisplayName));
@@ -679,7 +679,7 @@ public virtual async Task<PasskeyRequestOptions> ConfigurePasskeyRequestOptionsA
679679

680680
var props = new AuthenticationProperties();
681681
props.Items[PasskeyRequestOptionsKey] = options.AsJson();
682-
var claimsIdentity = new ClaimsIdentity(new ClaimsIdentity(IdentityConstants.TwoFactorUserIdScheme));
682+
var claimsIdentity = new ClaimsIdentity(IdentityConstants.TwoFactorUserIdScheme);
683683

684684
if (options.UserId is { } userId)
685685
{

src/Identity/Extensions.Core/src/PublicAPI.Unshipped.txt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
#nullable enable
22
*REMOVED*Microsoft.AspNetCore.Identity.UserLoginInfo.UserLoginInfo(string! loginProvider, string! providerKey, string? displayName) -> void
3-
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria
4-
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.AuthenticatorAttachment.get -> string?
5-
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.AuthenticatorAttachment.set -> void
6-
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.AuthenticatorSelectionCriteria() -> void
7-
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.RequireResidentKey.get -> bool
8-
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.ResidentKey.get -> string?
9-
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.ResidentKey.set -> void
10-
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.UserVerification.get -> string!
11-
Microsoft.AspNetCore.Identity.AuthenticatorSelectionCriteria.UserVerification.set -> void
123
Microsoft.AspNetCore.Identity.IdentityOptions.Passkey.get -> Microsoft.AspNetCore.Identity.PasskeyOptions!
134
Microsoft.AspNetCore.Identity.IdentityOptions.Passkey.set -> void
145
Microsoft.AspNetCore.Identity.IUserPasskeyStore<TUser>

0 commit comments

Comments
 (0)