Skip to content

Commit cb71a15

Browse files
authored
Remove base-class, Status and ErrorMessage (#529)
* Remove base-class * Removed usage of Status/ErrorMessage in tests * Rename MakeNewCredentialResult.Result to .Credential * format * Removed MakeNewCredentialResult * fixed after rebase * Passes conformance tests * undo local changes * small fix * small fix
1 parent 184f70f commit cb71a15

23 files changed

+182
-305
lines changed

BlazorWasmDemo/Client/Shared/UserService.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,6 @@ public async Task<string> LoginAsync(string? username)
134134
return "No options received";
135135
}
136136

137-
if (options.Status != "ok")
138-
{
139-
return options.ErrorMessage ?? string.Empty;
140-
}
141-
142137
// Present options to user and get response (usernameless users will be asked by their authenticator, which credential they want to use to sign the challenge)
143138
var assertion = await _webAuthn.VerifyAsync(options);
144139

BlazorWasmDemo/Server/Controllers/UserController.cs

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ public CredentialCreateOptions GetCredentialOptions(
126126
// 6. return options to client
127127
return options;
128128
}
129-
catch (Exception e)
129+
catch (Exception)
130130
{
131-
return new CredentialCreateOptions { Status = "error", ErrorMessage = FormatException(e) };
131+
throw;
132132
}
133133
}
134134

@@ -150,29 +150,25 @@ public async Task<string> CreateCredentialAsync([FromRoute] string username, [Fr
150150
// 2. Create callback so that lib can verify credential id is unique to this user
151151

152152
// 3. Verify and make the credentials
153-
var result = await _fido2.MakeNewCredentialAsync(attestationResponse, options, CredentialIdUniqueToUserAsync, cancellationToken: cancellationToken);
154-
155-
if (result.Status is "error" || result.Result is null)
156-
{
157-
return result.ErrorMessage ?? string.Empty;
158-
}
153+
var credential = await _fido2.MakeNewCredentialAsync(attestationResponse, options, CredentialIdUniqueToUserAsync, cancellationToken: cancellationToken);
159154

160155
// 4. Store the credentials in db
161156
_demoStorage.AddCredentialToUser(options.User, new StoredCredential
162157
{
163-
AttestationFormat = result.Result.AttestationFormat,
164-
Id = result.Result.Id,
165-
PublicKey = result.Result.PublicKey,
166-
UserHandle = result.Result.User.Id,
167-
SignCount = result.Result.SignCount,
158+
159+
AttestationFormat = credential.AttestationFormat,
160+
Id = credential.Id,
161+
PublicKey = credential.PublicKey,
162+
UserHandle = credential.User.Id,
163+
SignCount = credential.SignCount,
168164
RegDate = DateTimeOffset.UtcNow,
169-
AaGuid = result.Result.AaGuid,
170-
DevicePublicKeys = [result.Result.DevicePublicKey],
171-
Transports = result.Result.Transports,
172-
IsBackupEligible = result.Result.IsBackupEligible,
173-
IsBackedUp = result.Result.IsBackedUp,
174-
AttestationObject = result.Result.AttestationObject,
175-
AttestationClientDataJson = result.Result.AttestationClientDataJson,
165+
AaGuid = credential.AaGuid,
166+
DevicePublicKeys = [credential.DevicePublicKey],
167+
Transports = credential.Transports,
168+
IsBackupEligible = credential.IsBackupEligible,
169+
IsBackedUp = credential.IsBackedUp,
170+
AttestationObject = credential.AttestationObject,
171+
AttestationClientDataJson = credential.AttestationClientDataJson,
176172
});
177173

178174
// 5. Now we need to remove the options from the pending dictionary
@@ -228,9 +224,9 @@ public AssertionOptions MakeAssertionOptions([FromRoute] string? username, [From
228224
// 5. return options to client
229225
return options;
230226
}
231-
catch (Exception e)
227+
catch (Exception)
232228
{
233-
return new AssertionOptions { Status = "error", ErrorMessage = FormatException(e) };
229+
throw;
234230
}
235231
}
236232

@@ -280,19 +276,13 @@ public async Task<string> MakeAssertionAsync([FromBody] AuthenticatorAssertionRa
280276
cancellationToken: cancellationToken);
281277

282278
// 4. Store the updated counter
283-
if (res.Status is "ok")
279+
_demoStorage.UpdateCounter(res.CredentialId, res.SignCount);
280+
if (res.DevicePublicKey is not null)
284281
{
285-
_demoStorage.UpdateCounter(res.CredentialId, res.SignCount);
286-
if (res.DevicePublicKey is not null)
287-
{
288-
creds.DevicePublicKeys.Add(res.DevicePublicKey);
289-
}
290-
}
291-
else
292-
{
293-
return $"Error: {res.ErrorMessage}";
282+
creds.DevicePublicKeys.Add(res.DevicePublicKey);
294283
}
295284

285+
296286
// 5. return result to client
297287
var handler = new JwtSecurityTokenHandler();
298288
var token = handler.CreateEncodedJwt(

Demo/Controller.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public JsonResult MakeCredentialOptions([FromForm] string username,
8181
}
8282
catch (Exception e)
8383
{
84-
return Json(new CredentialCreateOptions { Status = "error", ErrorMessage = FormatException(e) });
84+
return Json(new { Status = "error", ErrorMessage = FormatException(e) });
8585
}
8686
}
8787

@@ -106,32 +106,32 @@ public async Task<JsonResult> MakeCredential([FromBody] AuthenticatorAttestation
106106
};
107107

108108
// 2. Verify and make the credentials
109-
var success = await _fido2.MakeNewCredentialAsync(attestationResponse, options, callback, cancellationToken: cancellationToken);
109+
var credential = await _fido2.MakeNewCredentialAsync(attestationResponse, options, callback, cancellationToken: cancellationToken);
110110

111111
// 3. Store the credentials in db
112112
DemoStorage.AddCredentialToUser(options.User, new StoredCredential
113113
{
114-
Id = success.Result.Id,
115-
PublicKey = success.Result.PublicKey,
116-
UserHandle = success.Result.User.Id,
117-
SignCount = success.Result.SignCount,
118-
AttestationFormat = success.Result.AttestationFormat,
114+
Id = credential.Id,
115+
PublicKey = credential.PublicKey,
116+
UserHandle = credential.User.Id,
117+
SignCount = credential.SignCount,
118+
AttestationFormat = credential.AttestationFormat,
119119
RegDate = DateTimeOffset.UtcNow,
120-
AaGuid = success.Result.AaGuid,
121-
Transports = success.Result.Transports,
122-
IsBackupEligible = success.Result.IsBackupEligible,
123-
IsBackedUp = success.Result.IsBackedUp,
124-
AttestationObject = success.Result.AttestationObject,
125-
AttestationClientDataJson = success.Result.AttestationClientDataJson,
126-
DevicePublicKeys = [success.Result.DevicePublicKey]
120+
AaGuid = credential.AaGuid,
121+
Transports = credential.Transports,
122+
IsBackupEligible = credential.IsBackupEligible,
123+
IsBackedUp = credential.IsBackedUp,
124+
AttestationObject = credential.AttestationObject,
125+
AttestationClientDataJson = credential.AttestationClientDataJson,
126+
DevicePublicKeys = [credential.DevicePublicKey]
127127
});
128128

129129
// 4. return "ok" to the client
130-
return Json(success);
130+
return Json(credential);
131131
}
132132
catch (Exception e)
133133
{
134-
return Json(new MakeNewCredentialResult(status: "error", errorMessage: FormatException(e), result: null));
134+
return Json(new { status = "error", errorMessage = FormatException(e) });
135135
}
136136
}
137137

@@ -176,7 +176,7 @@ public ActionResult AssertionOptionsPost([FromForm] string username, [FromForm]
176176

177177
catch (Exception e)
178178
{
179-
return Json(new AssertionOptions { Status = "error", ErrorMessage = FormatException(e) });
179+
return Json(new { Status = "error", ErrorMessage = FormatException(e) });
180180
}
181181
}
182182

@@ -217,7 +217,7 @@ public async Task<JsonResult> MakeAssertion([FromBody] AuthenticatorAssertionRaw
217217
}
218218
catch (Exception e)
219219
{
220-
return Json(new VerifyAssertionResult { Status = "error", ErrorMessage = FormatException(e) });
220+
return Json(new { Status = "error", ErrorMessage = FormatException(e) });
221221
}
222222
}
223223
}

Demo/TestController.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Text;
2-
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
34
using Fido2NetLib;
45
using Fido2NetLib.Development;
56
using Fido2NetLib.Objects;
@@ -34,7 +35,7 @@ public TestController(IOptions<Fido2Configuration> fido2Configuration)
3435

3536
[HttpPost]
3637
[Route("/attestation/options")]
37-
public JsonResult MakeCredentialOptionsTest([FromBody] TEST_MakeCredentialParams opts)
38+
public OkObjectResult MakeCredentialOptionsTest([FromBody] TEST_MakeCredentialParams opts)
3839
{
3940
var attType = opts.Attestation;
4041

@@ -72,12 +73,16 @@ public JsonResult MakeCredentialOptionsTest([FromBody] TEST_MakeCredentialParams
7273
HttpContext.Session.SetString("fido2.attestationOptions", options.ToJson());
7374

7475
// 5. return options to client
75-
return Json(options);
76+
77+
var jsonResponse = JsonSerializer.SerializeToNode(options);
78+
jsonResponse["status"] = "ok";
79+
jsonResponse["errorMessage"] = "";
80+
return new OkObjectResult(jsonResponse);
7681
}
7782

7883
[HttpPost]
7984
[Route("/attestation/result")]
80-
public async Task<JsonResult> MakeCredentialResultTestAsync([FromBody] AuthenticatorAttestationRawResponse attestationResponse, CancellationToken cancellationToken)
85+
public async Task<OkObjectResult> MakeCredentialResultTestAsync([FromBody] AuthenticatorAttestationRawResponse attestationResponse, CancellationToken cancellationToken)
8186
{
8287
// 1. get the options we sent the client
8388
var jsonOptions = HttpContext.Session.GetString("fido2.attestationOptions");
@@ -91,19 +96,22 @@ public async Task<JsonResult> MakeCredentialResultTestAsync([FromBody] Authentic
9196
};
9297

9398
// 2. Verify and make the credentials
94-
var success = await _fido2.MakeNewCredentialAsync(attestationResponse, options, callback, cancellationToken: cancellationToken);
99+
var credential = await _fido2.MakeNewCredentialAsync(attestationResponse, options, callback, cancellationToken: cancellationToken);
95100

96101
// 3. Store the credentials in db
97102
_demoStorage.AddCredentialToUser(options.User, new StoredCredential
98103
{
99-
Id = success.Result.Id,
100-
PublicKey = success.Result.PublicKey,
101-
UserHandle = success.Result.User.Id,
102-
SignCount = success.Result.SignCount
104+
Id = credential.Id,
105+
PublicKey = credential.PublicKey,
106+
UserHandle = credential.User.Id,
107+
SignCount = credential.SignCount
103108
});
104109

105110
// 4. return "ok" to the client
106-
return Json(success);
111+
var jsonResponse = JsonSerializer.SerializeToNode(credential);
112+
jsonResponse["status"] = "ok";
113+
jsonResponse["errorMessage"] = "";
114+
return new OkObjectResult(jsonResponse);
107115
}
108116

109117
[HttpPost]
@@ -142,7 +150,10 @@ public IActionResult AssertionOptionsTest([FromBody] TEST_AssertionClientParams
142150
HttpContext.Session.SetString("fido2.assertionOptions", options.ToJson());
143151

144152
// 5. Return options to client
145-
return Json(options);
153+
var jsonResponse = JsonSerializer.SerializeToNode(options);
154+
jsonResponse["status"] = "ok";
155+
jsonResponse["errorMessage"] = "";
156+
return new OkObjectResult(jsonResponse);
146157
}
147158

148159
[HttpPost]

Src/Fido2.Models/AssertionOptions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Fido2NetLib;
99
/// <summary>
1010
/// Sent to the browser when we want to Assert credentials and authenticate a user
1111
/// </summary>
12-
public class AssertionOptions : Fido2ResponseBase
12+
public class AssertionOptions
1313
{
1414
/// <summary>
1515
/// This member represents a challenge that the selected authenticator signs, along with other data, when producing an authentication assertion.
@@ -71,8 +71,6 @@ public static AssertionOptions Create(
7171
{
7272
return new AssertionOptions()
7373
{
74-
Status = "ok",
75-
ErrorMessage = string.Empty,
7674
Challenge = challenge,
7775
Timeout = config.Timeout,
7876
RpId = config.ServerDomain,

Src/Fido2.Models/CredentialCreateOptions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Fido2NetLib;
88

9-
public sealed class CredentialCreateOptions : Fido2ResponseBase
9+
public sealed class CredentialCreateOptions
1010
{
1111
/// <summary>
1212
///
@@ -117,8 +117,6 @@ public static CredentialCreateOptions Create(
117117
{
118118
return new CredentialCreateOptions
119119
{
120-
Status = "ok",
121-
ErrorMessage = string.Empty,
122120
Challenge = challenge,
123121
Rp = new PublicKeyCredentialRpEntity(config.ServerDomain, config.ServerName, config.ServerIcon),
124122
Timeout = config.Timeout,

Src/Fido2.Models/Fido2ResponseBase.cs

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

Src/Fido2.Models/Objects/MakeNewCredentialResult.cs

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

Src/Fido2.Models/Objects/RegisteredPublicKeyCredential.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Fido2NetLib.Objects;
55
/// <summary>
66
/// Holds parsed credential data
77
/// </summary>
8-
public class RegisteredPublicKeyCredential : Fido2ResponseBase
8+
public class RegisteredPublicKeyCredential
99
{
1010
/// <summary>
1111
/// The type of the public key credential source.

Src/Fido2.Models/Objects/VerifyAssertionResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/// <summary>
44
/// Result of the MakeAssertion verification
55
/// </summary>
6-
public class VerifyAssertionResult : Fido2ResponseBase
6+
public class VerifyAssertionResult
77
{
88
public byte[] CredentialId { get; init; }
99

Src/Fido2/AuthenticatorAssertionResponse.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ public async Task<VerifyAssertionResult> VerifyAsync(
176176

177177
return new VerifyAssertionResult
178178
{
179-
Status = "ok",
180179
CredentialId = Raw.Id,
181180
SignCount = authData.SignCount,
182181
IsBackedUp = authData.IsBackedUp,

Src/Fido2/Fido2.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,16 @@ public CredentialCreateOptions RequestNewCredential(
6767
/// <param name="isCredentialIdUniqueToUser">The delegate used to validate that the CredentialID is unique to this user.</param>
6868
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
6969
/// <returns></returns>
70-
public async Task<MakeNewCredentialResult> MakeNewCredentialAsync(
70+
public async Task<RegisteredPublicKeyCredential> MakeNewCredentialAsync(
7171
AuthenticatorAttestationRawResponse attestationResponse,
7272
CredentialCreateOptions originalOptions,
7373
IsCredentialIdUniqueToUserAsyncDelegate isCredentialIdUniqueToUser,
7474
CancellationToken cancellationToken = default)
7575
{
7676
var parsedResponse = AuthenticatorAttestationResponse.Parse(attestationResponse);
77-
var success = await parsedResponse.VerifyAsync(originalOptions, _config, isCredentialIdUniqueToUser, _metadataService, cancellationToken);
78-
79-
// todo: Set Errormessage etc.
80-
return new MakeNewCredentialResult(
81-
status: "ok",
82-
errorMessage: string.Empty,
83-
result: success
84-
);
77+
var credential = await parsedResponse.VerifyAsync(originalOptions, _config, isCredentialIdUniqueToUser, _metadataService, cancellationToken);
78+
79+
return credential;
8580
}
8681

8782
/// <summary>

Src/Fido2/IFido2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Task<VerifyAssertionResult> MakeAssertionAsync(
2222
IsUserHandleOwnerOfCredentialIdAsync isUserHandleOwnerOfCredentialIdCallback,
2323
CancellationToken cancellationToken = default);
2424

25-
Task<MakeNewCredentialResult> MakeNewCredentialAsync(
25+
Task<RegisteredPublicKeyCredential> MakeNewCredentialAsync(
2626
AuthenticatorAttestationRawResponse attestationResponse,
2727
CredentialCreateOptions originalOptions,
2828
IsCredentialIdUniqueToUserAsyncDelegate isCredentialIdUniqueToUser,

0 commit comments

Comments
 (0)