Added auth

This commit is contained in:
Maurice 2024-06-19 19:02:44 +02:00
parent 94a4ba1e1e
commit 4f21ecb859
3 changed files with 74 additions and 7 deletions

View File

@ -1,11 +1,51 @@
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using Android.Content;
using Android.Hardware.Biometrics;
using Android.OS;
using Java.Lang;
namespace DemoAndroid; namespace DemoAndroid;
class BiometricCallback : BiometricPrompt.AuthenticationCallback, IDialogInterfaceOnClickListener
{
private Context _context;
public BiometricCallback(Context context)
{
_context = context;
}
public override void OnAuthenticationError(BiometricErrorCode errorCode, ICharSequence? errString)
{
Toast.MakeText(_context, $"Authentication error: {errorCode}, {errString?.ToString()}", ToastLength.Long).Show();
}
public override void OnAuthenticationSucceeded(BiometricPrompt.AuthenticationResult? result)
{
Toast.MakeText(_context, "Authentication succeeded!", ToastLength.Long).Show();
}
public override void OnAuthenticationFailed()
{
Toast.MakeText(_context, "Authentication failed!", ToastLength.Long).Show();
}
public void OnClick(IDialogInterface? dialog, int which)
{
Toast.MakeText(_context, "Cancel clicked!", ToastLength.Long).Show();
}
}
[Activity(Label = "WakeOnLanActivity")] [Activity(Label = "WakeOnLanActivity")]
public class WakeOnLanActivity : Activity public class WakeOnLanActivity : Activity
{ {
private const int BiometricStrong = 0x0000000f;
private const int BiometricWeak = 0x000000ff;
private const int DeviceCredential = 0x00008000;
private bool _authenticated = false;
protected override void OnCreate(Bundle? savedInstanceState) protected override void OnCreate(Bundle? savedInstanceState)
{ {
base.OnCreate(savedInstanceState); base.OnCreate(savedInstanceState);
@ -16,25 +56,47 @@ public class WakeOnLanActivity : Activity
var button = FindViewById<Button>(Resource.Id.btnWakeOnLan); var button = FindViewById<Button>(Resource.Id.btnWakeOnLan);
button.Click += (sender, args) => button.Click += (sender, args) =>
{ {
if (!_authenticated)
{
Authenticate();
return;
}
WakePc(); WakePc();
}; };
} }
private void Authenticate()
{
var callback = new BiometricCallback(this);
var prompt = new BiometricPrompt.Builder(this)
.SetNegativeButton("Negative", MainExecutor, callback)
.SetTitle("Bewijs jezelf met je vingerafdruk")
.SetSubtitle("We willen zeker weten dat je niet Colinde bent")
.SetAllowedAuthenticators(BiometricStrong)
.Build();
var cancellation = new CancellationSignal();
prompt.Authenticate(cancellation, MainExecutor, callback);
}
private void WakePc() private void WakePc()
{ {
byte[] macAddress = { 0x2c, 0x4d, 0x54, 0x4d, 0x10, 0x0f }; byte[] macAddress = [0x2c, 0x4d, 0x54, 0x4d, 0x10, 0x0f];
byte[] magicPacket = new byte[102]; var magicPacket = new byte[102];
// Fill first 6 bytes with 0xFF // Fill first 6 bytes with 0xFF
for (int i = 0; i < 6; i++) for (var i = 0; i < 6; i++)
{ {
magicPacket[i] = 0xFF; magicPacket[i] = 0xFF;
} }
// Repeat MAC address 16 times // Repeat MAC address 16 times
for (int i = 1; i <= 16; i++) for (var i = 1; i <= 16; i++)
{ {
for (int j = 0; j < 6; j++) for (var j = 0; j < 6; j++)
{ {
magicPacket[i * 6 + j] = macAddress[j]; magicPacket[i * 6 + j] = macAddress[j];
} }
@ -44,10 +106,12 @@ public class WakeOnLanActivity : Activity
client.EnableBroadcast = true; client.EnableBroadcast = true;
var endpoint = new IPEndPoint(IPAddress.Broadcast, 9); // 255.255.255.255 port 9 var endpoint = new IPEndPoint(IPAddress.Broadcast, 9); // 255.255.255.255 port 9
for (int i = 0; i < 5; i++) for (var i = 0; i < 5; i++)
{ {
// Try 5 times // Try 5 times
client.Send(magicPacket, magicPacket.Length, endpoint); client.Send(magicPacket, magicPacket.Length, endpoint);
} }
Toast.MakeText(this, "Computer started!", ToastLength.Long).Show();
} }
} }

View File

@ -3,4 +3,6 @@
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:label="@string/app_name" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"> <application android:allowBackup="true" android:icon="@mipmap/appicon" android:label="@string/app_name" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true">
</application> </application>
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
</manifest> </manifest>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0-android</TargetFramework> <TargetFramework>net8.0-android</TargetFramework>
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion> <SupportedOSPlatformVersion>30</SupportedOSPlatformVersion>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
@ -12,6 +12,7 @@
<!-- With AOT, our app is crashing in Release mode --> <!-- With AOT, our app is crashing in Release mode -->
<RunAOTCompilation>False</RunAOTCompilation> <RunAOTCompilation>False</RunAOTCompilation>
<PublishAot>False</PublishAot> <PublishAot>False</PublishAot>
<TargetFrameworkVersion>v13.0</TargetFrameworkVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>