Skip to content

Commit

Permalink
feat: added a way to confirm if a platform is supported by the plugin (
Browse files Browse the repository at this point in the history
  • Loading branch information
FreakyAli authored Jun 21, 2024
1 parent 6adb23f commit a906545
Show file tree
Hide file tree
Showing 33 changed files with 205 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class AuthenticationRequest
public bool AllowPasswordAuth { get; set; }
public string Title { get; set; }
public string Subtitle { get; set; }
public string NegativeText { get; set; }
public string Description { get; set; }
public string NegativeText { get; set; }
public string Description { get; set; }
public AuthenticatorStrength AuthStrength { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ public class AuthenticationResponse
public BiometricResponseStatus Status { get; set; }
public AuthenticationType AuthenticationType { get; set; }
public string ErrorMsg { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum AuthenticatorStrength
{
//Authentication using a Class 3 biometric, as defined on the Android compatibility definition page.
Strong,

//Authentication using a Class 2 biometric, as defined on the Android compatibility definition page.
Weak
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Plugin.Maui.Biometric;

public class BiometricAuthenticationService
public static class BiometricAuthenticationService
{
private static readonly Lazy<IBiometric> defaultImpl =
new(() => new BiometricService(), LazyThreadSafetyMode.PublicationOnly);
Expand Down
10 changes: 5 additions & 5 deletions Plugin.Maui.Biometric/Plugin.Maui.Biometric/BiometricHwStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
/// </summary>
public enum BiometricHwStatus
{
NoHardware,
Unavailable,
Unsupported,
NotEnrolled,
LockedOut,
NoHardware,
Unavailable,
Unsupported,
NotEnrolled,
LockedOut,
Success,
Failure
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public enum BiometricResponseStatus
{
Failure,
Success
Failure,
Success
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
namespace Plugin.Maui.Biometric;
namespace Plugin.Maui.Biometric;

internal partial class BiometricService : IBiometric
{
public bool IsPlatformSupported { get; } = GetIsPlatformSupported();

public partial Task<BiometricHwStatus> GetAuthenticationStatusAsync(AuthenticatorStrength authStrength = AuthenticatorStrength.Strong);

public partial Task<AuthenticationResponse> AuthenticateAsync(AuthenticationRequest request, CancellationToken token);

public partial Task<List<BiometricType>> GetEnrolledBiometricTypesAsync();

private static partial bool GetIsPlatformSupported();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Plugin.Maui.Biometric;
namespace Plugin.Maui.Biometric;
#if NET && !ANDROID && !IOS
internal partial class BiometricService : IBiometric
{
Expand All @@ -16,5 +16,7 @@ public partial Task<List<BiometricType>> GetEnrolledBiometricTypesAsync()
{
throw new NotImplementedException();
}

private static partial bool GetIsPlatformSupported() => false;
}
#endif
4 changes: 2 additions & 2 deletions Plugin.Maui.Biometric/Plugin.Maui.Biometric/BiometricType.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Plugin.Maui.Biometric;

public enum BiometricType
{
None,
Fingerprint,
Face
}

}
6 changes: 5 additions & 1 deletion Plugin.Maui.Biometric/Plugin.Maui.Biometric/IBiometric.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
namespace Plugin.Maui.Biometric;
namespace Plugin.Maui.Biometric;

public interface IBiometric
{
Task<BiometricHwStatus> GetAuthenticationStatusAsync(AuthenticatorStrength authStrength = AuthenticatorStrength.Strong);

Task<AuthenticationResponse> AuthenticateAsync(AuthenticationRequest request, CancellationToken token);

Task<List<BiometricType>> GetEnrolledBiometricTypesAsync();

bool IsPlatformSupported { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override void OnAuthenticationSucceeded(BiometricPrompt.AuthenticationRes
ErrorMsg = string.Empty
});
}

public override void OnAuthenticationError(int errorCode, ICharSequence errString)
{
base.OnAuthenticationError(errorCode, errString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,6 @@ public partial Task<List<BiometricType>> GetEnrolledBiometricTypesAsync()
}
return Task.FromResult(availableOptions);
}

private static partial bool GetIsPlatformSupported() => true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ public partial Task<List<BiometricType>> GetEnrolledBiometricTypesAsync()
}
return Task.FromResult(availableOptions);
}

private static partial bool GetIsPlatformSupported() => true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
<SkipValidateMauiImplicitPackageReferences>true</SkipValidateMauiImplicitPackageReferences>
<AssemblyVersion>0.0.2</AssemblyVersion>
<AssemblyFileVersion>0.0.2</AssemblyFileVersion>
<Version>0.0.2-pre</Version>
<Version>0.0.2</Version>
<NeutralLanguage>en</NeutralLanguage>
<!--Version of C# to use -->
<PackageId>Plugin.Maui.Biometric</PackageId>
<LangVersion>latest</LangVersion>
<Authors>FreakyAli</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Description>A Biometric plugin for .NET MAUI</Description>
<Description>A Biometric plug-in for .NET MAUI</Description>
<PackageIcon>auth_icon.jpg</PackageIcon>
<RepositoryUrl>https://github.com/FreakyAli/Plugin.Maui.Biometric</RepositoryUrl>
<PackageProjectUrl>https://github.com/FreakyAli/Plugin.Maui.Biometric</PackageProjectUrl>
Expand All @@ -40,13 +40,13 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-ios|AnyCPU'">
<CreatePackage>false</CreatePackage>
<CreatePackage>false</CreatePackage>
</PropertyGroup>
<ItemGroup>
<None Remove="Platforms\Android\" />
<None Remove="Platforms\iOS\" />
<None Include="auth_icon.jpg" Pack="true" Visible="false" PackagePath="" />
<None Include="Readme.md" Pack="true" Visible="false" PackagePath="" />
<None Include="Readme.md" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>
<ItemGroup>
<Folder Include="Platforms\Android\" />
Expand Down
14 changes: 7 additions & 7 deletions Plugin.Maui.Biometric/Samples/App.xaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Samples"
x:Class="Samples.App">
<?xml version="1.0" encoding="UTF-8" ?>
<Application
x:Class="Samples.App"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Samples">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand All @@ -11,5 +12,4 @@
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

</Application>
13 changes: 6 additions & 7 deletions Plugin.Maui.Biometric/Samples/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

public partial class App : Application
{
public App()
{
InitializeComponent();

MainPage = new AppShell();
}
}
public App()
{
InitializeComponent();

MainPage = new AppShell();
}
}
8 changes: 3 additions & 5 deletions Plugin.Maui.Biometric/Samples/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Samples"
Shell.FlyoutBehavior="Disabled"
Title="Samples">
Title="Samples"
Shell.FlyoutBehavior="Disabled">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>

</Shell>
11 changes: 5 additions & 6 deletions Plugin.Maui.Biometric/Samples/AppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}

public AppShell()
{
InitializeComponent();
}
}
37 changes: 17 additions & 20 deletions Plugin.Maui.Biometric/Samples/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Samples.MainPage">
<ContentPage
x:Class="Samples.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

<ScrollView>
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<VerticalStackLayout Padding="30,0" Spacing="25">
<Image
Source="dotnet_bot.png"
HeightRequest="185"
Aspect="AspectFit"
SemanticProperties.Description="dot net bot in a race car number eight" />
HeightRequest="185"
SemanticProperties.Description="dot net bot in a race car number eight"
Source="dotnet_bot.png" />

<Label
Text="Hello, World!"
SemanticProperties.HeadingLevel="Level1"
Style="{StaticResource Headline}"
SemanticProperties.HeadingLevel="Level1" />
Text="Hello, World!" />

<Label
Text="Welcome to &#10;.NET Multi-platform App UI"
Style="{StaticResource SubHeadline}"
SemanticProperties.Description="Welcome to dot net Multi platform App U I"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I" />
Style="{StaticResource SubHeadline}"
Text="Welcome to &#10;.NET Multi-platform App UI" />

<Button
x:Name="CounterBtn"
Text="Click me"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Fill" />
HorizontalOptions="Fill"
SemanticProperties.Hint="Counts the number of times you click"
Text="Click me" />
</VerticalStackLayout>
</ScrollView>

</ContentPage>

</ContentPage>
4 changes: 2 additions & 2 deletions Plugin.Maui.Biometric/Samples/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ private async void OnCounterClicked(object sender, EventArgs e)
{
Title = "A good title",
Subtitle = "An equally good subtitle",
NegativeText= "Cancel",
NegativeText = "Cancel",
Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua",
AllowPasswordAuth = false,
};
// biometric authentication
var data = await biometric.AuthenticateAsync(req,
CancellationToken.None); // You can also pass a valid token and use it to cancel this tsak
CancellationToken.None); // You can also pass a valid token and use it to cancel this tsak

Console.Write(data);
}
Expand Down
44 changes: 21 additions & 23 deletions Plugin.Maui.Biometric/Samples/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
using Microsoft.Extensions.DependencyInjection;
using Plugin.Maui.Biometric;

namespace Samples;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});

builder.Services.AddSingleton<IBiometric>(BiometricAuthenticationService.Default);
return builder.Build();
}
}

using Plugin.Maui.Biometric;

namespace Samples;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});

builder.Services.AddSingleton<IBiometric>(BiometricAuthenticationService.Default);
return builder.Build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ namespace Samples;
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
}

}
Loading

0 comments on commit a906545

Please sign in to comment.