Skip to content

Commit

Permalink
[WebAuthn] Remove SetApiVersion and provide better class names
Browse files Browse the repository at this point in the history
 - API version is now set automatically on the first call.
 - Remove abbreviations from class names.
  • Loading branch information
Filip Skrzeczkowski committed Sep 19, 2024
1 parent 780fe8d commit 530ecef
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@ namespace Tizen.Security.WebAuthn
/// Authenticator extension.
/// </summary>
/// <since_tizen> 12 </since_tizen>
public class AuthenticationExt
public class AuthenticationExtension
{
/// <summary>
/// Initializes a new instance of the <see cref="AuthenticationExt"/> class.
/// Initializes a new instance of the <see cref="AuthenticationExtension"/> class.
/// </summary>
/// <since_tizen> 12 </since_tizen>
/// <param name="extensionId">
/// Extension Identifier defined in the following registry:
/// https://www.iana.org/assignments/webauthn/webauthn.xhtml#webauthn-extension-ids
/// </param>
/// <param name="extensionValue">Extension value.</param>
public AuthenticationExt(byte[] extensionId, byte[] extensionValue)
public AuthenticationExtension(byte[] extensionId, byte[] extensionValue)
{
ExtensionId = extensionId;
ExtensionValue = extensionValue;
}

internal AuthenticationExt(WauthnAuthenticationExt ext)
internal AuthenticationExtension(WauthnAuthenticationExt ext)
{
ExtensionId = NullSafeMarshal.PtrToArray(ext.extensionId);
ExtensionValue = NullSafeMarshal.PtrToArray(ext.extensionValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ namespace Tizen.Security.WebAuthn
/// Authenticator selection criteria.
/// </summary>
/// <since_tizen> 12 </since_tizen>
public class AuthenticationSelCri
public class AuthenticationSelectionCriteria
{
/// <summary>
/// Initializes a new instance of the <see cref="AuthenticationSelCri"/> class.
/// Initializes a new instance of the <see cref="AuthenticationSelectionCriteria"/> class.
/// </summary>
/// <since_tizen> 12 </since_tizen>
/// <param name="attachment">Authenticator attachment modality.</param>
/// <param name="residentKey">Specifies the extent to which the Relying Party desires to create a client-side discoverable credential.</param>
/// <param name="requireResidentKey">Relying Parties SHOULD set it to true if, and only if, residentKey is set to required.</param>
/// <param name="userVerification">Specifies the Relying Party's requirements regarding user verification.</param>
public AuthenticationSelCri(
public AuthenticationSelectionCriteria(
AuthenticatorAttachment attachment,
ResidentKeyRequirement residentKey,
bool requireResidentKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace Tizen.Security.WebAuthn
/// <since_tizen> 12 </since_tizen>
public static class Authenticator
{
private const int API_VERSION_NUMBER = 0x00000001;
private static bool _apiVersionSet = false;
private static bool _busy = false;
private static object _userData = null;
Expand All @@ -40,21 +41,6 @@ public static class Authenticator
private static WauthnGaCallbacks _wauthnGaCallbacks;

#region Public API
/// <summary>
/// Sets API version that the caller uses.
/// </summary>
/// <since_tizen> 12 </since_tizen>
/// <remarks>This method must be called before other methods are called.</remarks>
/// <feature>http://tizen.org/feature/security.webauthn</feature>
/// <param name="apiVersionNumber">API version number to set. Use <see cref="ApiVersionNumber"/> as an input.</param>
/// <exception cref="NotSupportedException">The specified API version or required feature is not supported.</exception>
public static void SetApiVersion(int apiVersionNumber)
{
int ret = Libwebauthn.SetApiVersion(apiVersionNumber);
CheckErrNThrow(ret, "Set API version");
_apiVersionSet = true;
}

/// <summary>
/// Gets information on authenticator types that the client platform supports.
/// </summary>
Expand Down Expand Up @@ -97,7 +83,7 @@ public static AuthenticatorTransport SupportedAuthenticators()
/// <exception cref="ArgumentException">Input parameter is invalid.</exception>
/// <exception cref="InvalidOperationException">Operation invalid in current state.</exception>
/// <exception cref="OperationCanceledException">Canceled by a cancel request.</exception>
public static void MakeCredential(ClientData clientData, PubkeyCredCreationOptions options, McCallbacks callbacks)
public static void MakeCredential(ClientData clientData, PubkeyCredCreationOptions options, MakeCredentialCallbacks callbacks)
{
CheckPreconditions();
try
Expand Down Expand Up @@ -157,7 +143,7 @@ public static void MakeCredential(ClientData clientData, PubkeyCredCreationOptio
/// <exception cref="ArgumentException">Input parameter is invalid.</exception>
/// <exception cref="InvalidOperationException">Operation invalid in current state.</exception>
/// <exception cref="OperationCanceledException">Canceled by a cancel request.</exception>
public static void GetAssertion(ClientData clientData, PubkeyCredRequestOptions options, GaCallbacks callbacks)
public static void GetAssertion(ClientData clientData, PubkeyCredRequestOptions options, GetAssertionCallbacks callbacks)
{
CheckPreconditions();
try
Expand Down Expand Up @@ -203,7 +189,13 @@ public static void Cancel()
#endregion
#region Helper methods

private static void WrapMcCallbacks(McCallbacks callbacks)
private static void SetApiVersion(int apiVersionNumber)
{
int ret = Libwebauthn.SetApiVersion(apiVersionNumber);
CheckErrNThrow(ret, "Set API version");
_apiVersionSet = true;
}
private static void WrapMcCallbacks(MakeCredentialCallbacks callbacks)
{
_userData = callbacks.UserData;

Expand All @@ -214,7 +206,7 @@ void qrCodeWrapper(string qrContents, IntPtr _)

void onResponseWrapper(WauthnPubkeyCredentialAttestation pubkeyCred, WauthnError result, IntPtr _)
{
PubkeyCredentialAttestation pubkeyCredManaged = pubkeyCred is not null ? new(pubkeyCred) : null;
PubkeyCredAttestation pubkeyCredManaged = pubkeyCred is not null ? new(pubkeyCred) : null;
callbacks.ResponseCallback(pubkeyCredManaged, result, _userData);

if (result != WauthnError.None)
Expand All @@ -237,7 +229,7 @@ void linkedDataWrapper(IntPtr linkedData, WauthnError result, IntPtr _)
_wauthnMcCallbacks = new WauthnMcCallbacks(_qrCodeCallback, _mcResponseCallback, _linkedDataCallback);
}

private static void WrapGaCallbacks(GaCallbacks callbacks)
private static void WrapGaCallbacks(GetAssertionCallbacks callbacks)
{
_userData = callbacks.UserData;

Expand All @@ -248,7 +240,7 @@ void qrCodeWrapper(string qrContents, IntPtr _)

void onResponseWrapper(WauthnPubkeyCredentialAssertion pubkeyCred, WauthnError result, IntPtr _)
{
PubkeyCredentialAssertion pubkeyCredManaged = pubkeyCred is not null ? new(pubkeyCred) : null;
PubkeyCredAssertion pubkeyCredManaged = pubkeyCred is not null ? new(pubkeyCred) : null;
callbacks.ResponseCallback(pubkeyCredManaged, result, _userData);

if (result != WauthnError.None)
Expand All @@ -273,7 +265,7 @@ void linkedDataWrapper(IntPtr linkedData, WauthnError result, IntPtr _)
private static void CheckPreconditions()
{
if (!_apiVersionSet)
throw new InvalidOperationException("API version not set");
SetApiVersion(API_VERSION_NUMBER);
if (_busy)
throw new InvalidOperationException("Authenticator busy");

Expand All @@ -287,11 +279,5 @@ private static void Cleanup()
}

#endregion

/// <summary>
/// Current API version.
/// </summary>
/// <since_tizen> 12 </since_tizen>
public static int ApiVersionNumber { get; } = 0x00000001;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private static void CopyCredRequestOptions(PubkeyCredRequestOptions options)
_linkedDeviceUnmanaged);
}

private static void CopyRp(RpEntity rp)
private static void CopyRp(RelyingPartyEntity rp)
{
_rpNameUnmanaged = new UnmanagedMemory(rp.Name);
_rpIdUnmanaged = new UnmanagedMemory(rp.Id);
Expand Down Expand Up @@ -239,7 +239,7 @@ private static void CopyCredentials(IEnumerable<PubkeyCredDescriptor> credential
_credentialsUnmanaged = new UnmanagedMemory(new WauthnPubkeyCredDescriptors((nuint)credentialsCount, _credentialsDescriptorsUnmanaged));
}

private static void CopyAuthenticatorSelection(AuthenticationSelCri selection)
private static void CopyAuthenticatorSelection(AuthenticationSelectionCriteria selection)
{
if (selection is null)
return;
Expand Down Expand Up @@ -283,7 +283,7 @@ private static void CopyAttestationFormats(IEnumerable<byte[]> attestationFormat
_attestationFormatsUnmanaged = new UnmanagedMemory(new WauthnAttestationFormats((nuint)attestationFormatsCount, _attestationFormatsArrayUnmanaged));
}

private static void CopyExtensions(IEnumerable<AuthenticationExt> extensions)
private static void CopyExtensions(IEnumerable<AuthenticationExtension> extensions)
{
if (extensions is null || !extensions.Any())
return;
Expand All @@ -297,7 +297,7 @@ private static void CopyExtensions(IEnumerable<AuthenticationExt> extensions)

for (int i = 0; i < extensionCount; i++)
{
AuthenticationExt ext = extensions.ElementAt(i);
AuthenticationExtension ext = extensions.ElementAt(i);
_extensionIdUnmanagedDataArray[i] = UnmanagedMemory.PinArray(ext.ExtensionId);
var extensionIdUnmanagedConstBuffer = new UnmanagedMemory(new WauthnConstBuffer(_extensionIdUnmanagedDataArray[i], (nuint)ext.ExtensionId.Length));
_extensionIdUnmanagedConstBufferArray[i] = extensionIdUnmanagedConstBuffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@
*/

using System;
using static Tizen.Security.WebAuthn.ErrorFactory;

namespace Tizen.Security.WebAuthn
{
/// <summary>
/// Callback function list used to get assertion.
/// Callback function list used to get assertion with <see cref="Authenticator.GetAssertion"/>.
/// </summary>
/// <since_tizen> 12 </since_tizen>
public class GaCallbacks
public class GetAssertionCallbacks
{
/// <summary>
/// Initializes a new instance of the <see cref="GaCallbacks"/> class.
/// Initializes a new instance of the <see cref="GetAssertionCallbacks"/> class.
/// </summary>
/// <remarks>
/// Provided callbacks MUST NOT THROW.
Expand All @@ -39,7 +38,7 @@ public class GaCallbacks
/// https://fidoalliance.org/specs/fido-v2.2-rd-20230321/fido-client-to-authenticator-protocol-v2.2-rd-20230321.html#hybrid-qr-initiated.
/// The qr_contents is encoded like "FIDO:/0254318383..........7406596245".
/// The image to be displayed shall be created from qr_contents
/// with media vision API(mv_barcode_generate_image()).
/// with media vision API (<see cref="Tizen.Multimedia.Vision.BarcodeGenerator"/>).
/// If the request does not need to display a QR code
/// then this callback function won't be invoked.
/// </param>
Expand All @@ -56,9 +55,9 @@ public class GaCallbacks
/// * lack of push notifications support (e.g. missing Google Account).
/// </param>
/// <param name="linkedDataCallback">
/// Callback function for getting the updated linked device data.
/// Invoked when the response for the get assertion request needs to be returned.
/// The result of the MakeCredential request may be one of the following:
/// Callback function for getting the updated linked device data. May be called multiple times.
/// Invoked when the response for the <see cref="Authenticator.GetAssertion"/> request
/// needs to be returned. The result of this request may be one of the following:
/// * <see cref="WauthnError.None"/> if the request is completed well,
/// * <see cref="WauthnError.Canceled"/> if the request is cancelled by a Cancel() request.
/// * <see cref="WauthnError.InvalidState"/> if the server entered invalid state. Known causes:
Expand All @@ -69,9 +68,9 @@ public class GaCallbacks
/// * lack of push notifications support (e.g. missing Google Account).
/// </param>
/// <param name="userData">User data to be passed to <see cref="QrcodeCallback"/>, <see cref="ResponseCallback"/> and <see cref="LinkedDataCallback"/>.</param>
public GaCallbacks(
public GetAssertionCallbacks(
Action<string, object> qrcodeCallback,
Action<PubkeyCredentialAssertion, WauthnError, object> responseCallback,
Action<PubkeyCredAssertion, WauthnError, object> responseCallback,
Action<HybridLinkedData, WauthnError, object> linkedDataCallback,
object userData)
{
Expand All @@ -88,7 +87,7 @@ public GaCallbacks(
/// <summary>
/// Callback function for getting the final response.
/// </summary>
public Action<PubkeyCredentialAssertion, WauthnError, object> ResponseCallback { get; init; }
public Action<PubkeyCredAssertion, WauthnError, object> ResponseCallback { get; init; }
/// <summary>
/// Callback function for getting the updated linked device data.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Tizen.Security.WebAuthn
/// The linked device data is used for state assisted transaction.
/// From the successful QR initiated transaction, the linked device data
/// might be returned from an authenticator to a webauthn client
/// via <see cref="PubkeyCredentialAttestation"/> or <see cref="PubkeyCredentialAssertion"/>.
/// via <see cref="PubkeyCredAttestation"/> or <see cref="PubkeyCredAssertion"/>.
/// Then the client can store the linked device data and use it in the next call
/// for <see cref="PubkeyCredCreationOptions"/> or <see cref="PubkeyCredRequestOptions"/>.
/// Then the stated assisted transaction will start instead of QR initiated transaction.
Expand All @@ -44,6 +44,10 @@ public class HybridLinkedData
/// <summary>
/// Initializes a new instance of the <see cref="HybridLinkedData"/> class.
/// </summary>
/// <remarks>
/// More information on the CBOR format can be found in the following specification:
/// https://www.rfc-editor.org/rfc/rfc8949.html
/// </remarks>
/// <param name="contactId">CBOR:"1".</param>
/// <param name="linkId">CBOR:"2".</param>
/// <param name="linkSecret">CBOR:"3".</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@
*/

using System;
using static Tizen.Security.WebAuthn.ErrorFactory;

namespace Tizen.Security.WebAuthn
{
/// <summary>
/// Callback function list used to make credential with <see cref="Authenticator.MakeCredential"/>.
/// </summary>
/// <since_tizen> 12 </since_tizen>
public class McCallbacks
public class MakeCredentialCallbacks
{
/// <summary>
/// Initializes a new instance of the <see cref="McCallbacks"/> class.
/// Initializes a new instance of the <see cref="MakeCredentialCallbacks"/> class.
/// </summary>
/// <remarks>
/// Provided callbacks MUST NOT THROW.
Expand All @@ -44,8 +43,8 @@ public class McCallbacks
/// then this callback function won't be invoked.
/// </param>
/// <param name="responseCallback">Callback function for getting the final response.
/// Invoked when the response for the MakeCredential request needs to be returned.
/// The result of the MakeCredential request may be one of the following:
/// Invoked when the response for the <see cref="Authenticator.MakeCredential"/> request
/// needs to be returned. The result of this request may be one of the following:
/// * <see cref="WauthnError.None"/> if the request is completed well,
/// * <see cref="WauthnError.Canceled"/> if the request is cancelled by a Cancel() request.
/// * <see cref="WauthnError.InvalidState"/> if the server entered invalid state. Known causes:
Expand All @@ -56,7 +55,7 @@ public class McCallbacks
/// * lack of push notifications support (e.g. missing Google Account).
/// </param>
/// <param name="linkedDataCallback">
/// Callback function for getting the updated linked device data.
/// Callback function for getting the updated linked device data. May be called multiple times.
/// Invoked when the response for the get assertion request needs to be returned.
/// The result of the MakeCredential request may be one of the following:
/// * <see cref="WauthnError.None"/> if the request is completed well,
Expand All @@ -69,9 +68,9 @@ public class McCallbacks
/// * lack of push notifications support (e.g. missing Google Account).
/// </param>
/// <param name="userData">User data to be passed to <see cref="QrcodeCallback"/>, <see cref="ResponseCallback"/> and <see cref="LinkedDataCallback"/>.</param>
public McCallbacks(
public MakeCredentialCallbacks(
Action<string, object> qrcodeCallback,
Action<PubkeyCredentialAttestation, WauthnError, object> responseCallback,
Action<PubkeyCredAttestation, WauthnError, object> responseCallback,
Action<HybridLinkedData, WauthnError, object> linkedDataCallback,
object userData)
{
Expand All @@ -88,7 +87,7 @@ public McCallbacks(
/// <summary>
/// Callback function for getting the final response.
/// </summary>
public Action<PubkeyCredentialAttestation, WauthnError, object> ResponseCallback { get; init; }
public Action<PubkeyCredAttestation, WauthnError, object> ResponseCallback { get; init; }
/// <summary>
/// Callback function for getting the updated linked device data.
/// </summary>
Expand Down
Loading

0 comments on commit 530ecef

Please sign in to comment.