Skip to content

Commit bf65a9a

Browse files
committed
Create wrapper objects for GetAssertionOptinos
1 parent dc04ec3 commit bf65a9a

File tree

6 files changed

+62
-21
lines changed

6 files changed

+62
-21
lines changed

BlazorWasmDemo/Server/Controllers/UserController.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,12 @@ public AssertionOptions MakeAssertionOptions([FromRoute] string? username, [From
219219
};
220220

221221
// 2. Create options (usernameless users will be prompted by their device to select a credential from their own list)
222-
var options = _fido2.GetAssertionOptions(
223-
existingKeys,
224-
userVerification ?? UserVerificationRequirement.Discouraged,
225-
exts);
222+
var options = _fido2.GetAssertionOptions(new GetAssertionOptionsParams
223+
{
224+
AllowedCredentials = existingKeys,
225+
UserVerification = userVerification ?? UserVerificationRequirement.Discouraged,
226+
Extensions = exts
227+
});
226228

227229
// 4. Temporarily store options, session/in-memory cache/redis/db
228230
_pendingAssertions[new string(options.Challenge.Select(b => (char)b).ToArray())] = options;

Demo/Controller.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,12 @@ public ActionResult AssertionOptionsPost([FromForm] string username, [FromForm]
166166

167167
// 3. Create options
168168
var uv = string.IsNullOrEmpty(userVerification) ? UserVerificationRequirement.Discouraged : userVerification.ToEnum<UserVerificationRequirement>();
169-
var options = _fido2.GetAssertionOptions(
170-
existingCredentials,
171-
uv,
172-
exts
173-
);
169+
var options = _fido2.GetAssertionOptions(new GetAssertionOptionsParams()
170+
{
171+
AllowedCredentials = existingCredentials,
172+
UserVerification = uv,
173+
Extensions = exts
174+
});
174175

175176
// 4. Temporarily store options, session/in-memory cache/redis/db
176177
HttpContext.Session.SetString("fido2.assertionOptions", options.ToJson());

Demo/TestController.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,12 @@ public IActionResult AssertionOptionsTest([FromBody] TEST_AssertionClientParams
151151
exts.Example = assertionClientParams.Extensions.Example;
152152

153153
// 3. Create options
154-
var options = _fido2.GetAssertionOptions(
155-
existingCredentials,
156-
uv,
157-
exts
158-
);
154+
var options = _fido2.GetAssertionOptions(new GetAssertionOptionsParams
155+
{
156+
AllowedCredentials = existingCredentials,
157+
UserVerification = uv,
158+
Extensions = exts
159+
});
159160

160161
// 4. Temporarily store options, session/in-memory cache/redis/db
161162
HttpContext.Session.SetString("fido2.assertionOptions", options.ToJson());

Src/Fido2/Fido2.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,15 @@ public async Task<RegisteredPublicKeyCredential> MakeNewCredentialAsync(MakeNewC
5353
/// <summary>
5454
/// Returns AssertionOptions including a challenge to the browser/authenticator to assert existing credentials and authenticate a user.
5555
/// </summary>
56-
/// <param name="allowedCredentials"></param>
57-
/// <param name="userVerification"></param>
58-
/// <param name="extensions"></param>
56+
/// <param name="getAssertionOptionsParams">The input arguments for generating AssertionOptions</param>
5957
/// <returns></returns>
58+
public AssertionOptions GetAssertionOptions(GetAssertionOptionsParams getAssertionOptionsParams)
59+
{
60+
byte[] challenge = RandomNumberGenerator.GetBytes(_config.ChallengeSize);
61+
62+
return AssertionOptions.Create(_config, challenge, getAssertionOptionsParams.AllowedCredentials, getAssertionOptionsParams.UserVerification, getAssertionOptionsParams.Extensions);
63+
}
64+
6065
public AssertionOptions GetAssertionOptions(
6166
IReadOnlyList<PublicKeyCredentialDescriptor> allowedCredentials,
6267
UserVerificationRequirement? userVerification,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Fido2NetLib.Objects;
4+
5+
namespace Fido2NetLib;
6+
7+
/// <summary>
8+
/// The input arguments for generating AssertionOptions
9+
/// </summary>
10+
public sealed class GetAssertionOptionsParams
11+
{
12+
/// <summary>
13+
/// This OPTIONAL member is used by the client to find authenticators eligible for this authentication ceremony. It can be used in two ways:
14+
///
15+
/// * If the user account to authenticate is already identified (e.g., if the user has entered a username), then the Relying Party SHOULD use this member to list credential descriptors for credential records in the user account. This SHOULD usually include all credential records in the user account.
16+
/// The items SHOULD specify transports whenever possible. This helps the client optimize the user experience for any given situation. Also note that the Relying Party does not need to filter the list when requesting user verification — the client will automatically ignore non-eligible credentials if userVerification is set to required.
17+
/// See also the § 14.6.3 Privacy leak via credential IDs privacy consideration.
18+
/// * If the user account to authenticate is not already identified, then the Relying Party MAY leave this member empty or unspecified. In this case, only discoverable credentials will be utilized in this authentication ceremony, and the user account MAY be identified by the userHandle of the resulting AuthenticatorAssertionResponse. If the available authenticators contain more than one discoverable credential scoped to the Relying Party, the credentials are displayed by the client platform or authenticator for the user to select from (see step 7 of § 6.3.3 The authenticatorGetAssertion Operation).
19+
///
20+
/// If not empty, the client MUST return an error if none of the listed credentials can be used.
21+
///
22+
/// The list is ordered in descending order of preference: the first item in the list is the most preferred credential, and the last is the least preferred.
23+
/// </summary>
24+
public IReadOnlyList<PublicKeyCredentialDescriptor> AllowedCredentials { get; init; } = Array.Empty<PublicKeyCredentialDescriptor>();
25+
26+
/// <summary>
27+
/// This OPTIONAL member specifies the Relying Party's requirements regarding user verification for the get() operation. The value SHOULD be a member of UserVerificationRequirement but client platforms MUST ignore unknown values, treating an unknown value as if the member does not exist. Eligible authenticators are filtered to only those capable of satisfying this requirement.
28+
/// </summary>
29+
public UserVerificationRequirement? UserVerification { get; init; }
30+
31+
/// <summary>
32+
/// The Relying Party MAY use this OPTIONAL member to provide client extension inputs requesting additional processing by the client and authenticator.
33+
/// </summary>
34+
public AuthenticationExtensionsClientInputs? Extensions { get; init; }
35+
}

Src/Fido2/IFido2.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ namespace Fido2NetLib;
88

99
public interface IFido2
1010
{
11-
AssertionOptions GetAssertionOptions(
12-
IReadOnlyList<PublicKeyCredentialDescriptor> allowedCredentials,
13-
UserVerificationRequirement? userVerification,
14-
AuthenticationExtensionsClientInputs? extensions = null);
11+
AssertionOptions GetAssertionOptions(GetAssertionOptionsParams getAssertionOptionsParams);
1512

1613
Task<VerifyAssertionResult> MakeAssertionAsync(MakeAssertionParams makeAssertionParams,
1714
CancellationToken cancellationToken = default);

0 commit comments

Comments
 (0)