Skip to content

Commit

Permalink
Remove base-class
Browse files Browse the repository at this point in the history
  • Loading branch information
abergs committed Jul 15, 2024
1 parent df37692 commit 8ac3524
Show file tree
Hide file tree
Showing 13 changed files with 18 additions and 68 deletions.
5 changes: 0 additions & 5 deletions BlazorWasmDemo/Client/Shared/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ public async Task<string> LoginAsync(string? username)
return "No options received";
}

if (options.Status != "ok")
{
return options.ErrorMessage ?? string.Empty;
}

// 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)
var assertion = await _webAuthn.VerifyAsync(options);

Expand Down
27 changes: 8 additions & 19 deletions BlazorWasmDemo/Server/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public CredentialCreateOptions GetCredentialOptions(
// 6. return options to client
return options;
}
catch (Exception e)
catch (Exception)
{
return new CredentialCreateOptions { Status = "error", ErrorMessage = FormatException(e) };
throw;
}
}

Expand All @@ -152,11 +152,6 @@ public async Task<string> CreateCredentialAsync([FromRoute] string username, [Fr
// 3. Verify and make the credentials
var result = await _fido2.MakeNewCredentialAsync(attestationResponse, options, CredentialIdUniqueToUserAsync, cancellationToken: cancellationToken);

if (result.Status is "error" || result.Result is null)
{
return result.ErrorMessage ?? string.Empty;
}

// 4. Store the credentials in db
_demoStorage.AddCredentialToUser(options.User, new StoredCredential
{
Expand Down Expand Up @@ -229,9 +224,9 @@ public AssertionOptions MakeAssertionOptions([FromRoute] string? username, [From
// 5. return options to client
return options;
}
catch (Exception e)
catch (Exception)
{
return new AssertionOptions { Status = "error", ErrorMessage = FormatException(e) };
throw;
}
}

Expand Down Expand Up @@ -281,18 +276,12 @@ public async Task<string> MakeAssertionAsync([FromBody] AuthenticatorAssertionRa
cancellationToken: cancellationToken);

// 4. Store the updated counter
if (res.Status is "ok")
{
_demoStorage.UpdateCounter(res.CredentialId, res.SignCount);
if (res.DevicePublicKey is not null)
{
creds.DevicePublicKeys.Add(res.DevicePublicKey);
}
}
else
_demoStorage.UpdateCounter(res.CredentialId, res.SignCount);
if (res.DevicePublicKey is not null)
{
return $"Error: {res.ErrorMessage}";
creds.DevicePublicKeys.Add(res.DevicePublicKey);
}


// 5. return result to client
var handler = new JwtSecurityTokenHandler();
Expand Down
8 changes: 4 additions & 4 deletions Demo/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public JsonResult MakeCredentialOptions([FromForm] string username,
}
catch (Exception e)
{
return Json(new CredentialCreateOptions { Status = "error", ErrorMessage = FormatException(e) });
return Json(new { Status = "error", ErrorMessage = FormatException(e) });
}
}

Expand Down Expand Up @@ -132,7 +132,7 @@ public async Task<JsonResult> MakeCredential([FromBody] AuthenticatorAttestation
}
catch (Exception e)
{
return Json(new MakeNewCredentialResult(status: "error", errorMessage: FormatException(e), result: null));
return Json(new { status = "error", errorMessage = FormatException(e)});
}
}

Expand Down Expand Up @@ -177,7 +177,7 @@ public ActionResult AssertionOptionsPost([FromForm] string username, [FromForm]

catch (Exception e)
{
return Json(new AssertionOptions { Status = "error", ErrorMessage = FormatException(e) });
return Json(new { Status = "error", ErrorMessage = FormatException(e) });
}
}

Expand Down Expand Up @@ -218,7 +218,7 @@ public async Task<JsonResult> MakeAssertion([FromBody] AuthenticatorAssertionRaw
}
catch (Exception e)
{
return Json(new VerifyAssertionResult { Status = "error", ErrorMessage = FormatException(e) });
return Json(new { Status = "error", ErrorMessage = FormatException(e) });
}
}
}
4 changes: 1 addition & 3 deletions Src/Fido2.Models/AssertionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Fido2NetLib;
/// <summary>
/// Sent to the browser when we want to Assert credentials and authenticate a user
/// </summary>
public class AssertionOptions : Fido2ResponseBase
public class AssertionOptions
{
/// <summary>
/// This member represents a challenge that the selected authenticator signs, along with other data, when producing an authentication assertion.
Expand Down Expand Up @@ -71,8 +71,6 @@ public static AssertionOptions Create(
{
return new AssertionOptions()
{
Status = "ok",
ErrorMessage = string.Empty,
Challenge = challenge,
Timeout = config.Timeout,
RpId = config.ServerDomain,
Expand Down
4 changes: 1 addition & 3 deletions Src/Fido2.Models/CredentialCreateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Fido2NetLib;

public sealed class CredentialCreateOptions : Fido2ResponseBase
public sealed class CredentialCreateOptions
{
/// <summary>
///
Expand Down Expand Up @@ -117,8 +117,6 @@ public static CredentialCreateOptions Create(
{
return new CredentialCreateOptions
{
Status = "ok",
ErrorMessage = string.Empty,
Challenge = challenge,
Rp = new PublicKeyCredentialRpEntity(config.ServerDomain, config.ServerName, config.ServerIcon),
Timeout = config.Timeout,
Expand Down
14 changes: 0 additions & 14 deletions Src/Fido2.Models/Fido2ResponseBase.cs

This file was deleted.

11 changes: 2 additions & 9 deletions Src/Fido2.Models/Objects/MakeNewCredentialResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@ namespace Fido2NetLib.Objects;
/// <summary>
/// Result of parsing and verifying attestation. Used to transport Public Key back to RP.
/// </summary>
public sealed class MakeNewCredentialResult : Fido2ResponseBase
public sealed class MakeNewCredentialResult(RegisteredPublicKeyCredential result)
{
public MakeNewCredentialResult(string status, string errorMessage, RegisteredPublicKeyCredential? result)
{
Status = status;
ErrorMessage = errorMessage;
Result = result;
}

public RegisteredPublicKeyCredential? Result { get; }
public RegisteredPublicKeyCredential Result { get; } = result;

// todo: add debuginfo?
}
2 changes: 1 addition & 1 deletion Src/Fido2.Models/Objects/RegisteredPublicKeyCredential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Fido2NetLib.Objects;
/// <summary>
/// Holds parsed credential data
/// </summary>
public class RegisteredPublicKeyCredential : Fido2ResponseBase
public class RegisteredPublicKeyCredential
{
/// <summary>
/// The type of the public key credential source.
Expand Down
2 changes: 1 addition & 1 deletion Src/Fido2.Models/Objects/VerifyAssertionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Result of the MakeAssertion verification
/// </summary>
public class VerifyAssertionResult : Fido2ResponseBase
public class VerifyAssertionResult
{
public byte[] CredentialId { get; init; }

Expand Down
1 change: 0 additions & 1 deletion Src/Fido2/AuthenticatorAssertionResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ public async Task<VerifyAssertionResult> VerifyAsync(

return new VerifyAssertionResult
{
Status = "ok",
CredentialId = Raw.Id,
SignCount = authData.SignCount,
IsBackedUp = authData.IsBackedUp,
Expand Down
2 changes: 0 additions & 2 deletions Src/Fido2/Fido2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ public async Task<MakeNewCredentialResult> MakeNewCredentialAsync(

// todo: Set Errormessage etc.
return new MakeNewCredentialResult(
status: "ok",
errorMessage: string.Empty,
result: success
);
}
Expand Down
4 changes: 0 additions & 4 deletions Test/Attestation/AndroidKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,11 @@ public AndroidKey()
public async Task TestAndroidKey()
{
var res = await MakeAttestationResponseAsync();
Assert.Equal(string.Empty, res.ErrorMessage);
Assert.Equal("ok", res.Status);
Assert.Equal(_aaguid, res.Result.AaGuid);
Assert.Equal(_signCount, res.Result.SignCount);
Assert.Equal("android-key", res.Result.AttestationFormat);
Assert.Equal(_credentialID, res.Result.Id);
Assert.Null(res.Result.ErrorMessage);
Assert.Equal(_credentialPublicKey.GetBytes(), res.Result.PublicKey);
Assert.Null(res.Result.Status);
Assert.Equal("Test User", res.Result.User.DisplayName);
Assert.Equal("testuser"u8.ToArray(), res.Result.User.Id);
Assert.Equal("testuser", res.Result.User.Name);
Expand Down
2 changes: 0 additions & 2 deletions Test/AuthenticatorResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,11 @@ public async Task TestAuthenticatorOriginsAsync(string origin, string expectedOr
UserVerification = UserVerificationRequirement.Discouraged,
},
Challenge = challenge,
ErrorMessage = "",
PubKeyCredParams =
[
PubKeyCredParam.ES256
],
Rp = new PublicKeyCredentialRpEntity(rp, rp, ""),
Status = "ok",
User = new Fido2User
{
Name = "testuser",
Expand Down

0 comments on commit 8ac3524

Please sign in to comment.