From cf41b524b0a0c005946817c157299c50dfd8de39 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 27 Jun 2017 23:33:13 -0400 Subject: [PATCH] read yubikey otp via nfc --- src/Android/MainActivity.cs | 40 ++++++++++++++++++++++ src/Android/Properties/AndroidManifest.xml | 1 + 2 files changed, 41 insertions(+) diff --git a/src/Android/MainActivity.cs b/src/Android/MainActivity.cs index 643a7b7ef..8acccc73a 100644 --- a/src/Android/MainActivity.cs +++ b/src/Android/MainActivity.cs @@ -15,6 +15,7 @@ using Xamarin.Forms; using System.Threading.Tasks; using Bit.App.Models.Page; using Bit.App; +using Android.Nfc; namespace Bit.Android { @@ -26,6 +27,7 @@ namespace Bit.Android { private const string HockeyAppId = "d3834185b4a643479047b86c65293d42"; private DateTime? _lastAction; + private Java.Util.Regex.Pattern _otpPattern = Java.Util.Regex.Pattern.Compile("^.*?([cbdefghijklnrtuv]{32,64})$"); protected override void OnCreate(Bundle bundle) { @@ -102,6 +104,12 @@ namespace Bit.Android { LaunchApp(args); }); + + MessagingCenter.Subscribe( + Xamarin.Forms.Application.Current, "ListenYubiKeyOTP", (sender) => + { + ListenYubiKey(); + }); } private void ReturnCredentials(VaultListPageModel.Login login) @@ -228,5 +236,37 @@ namespace Bit.Android StartActivity(launchIntent); } } + + private void ListenYubiKey() + { + var intent = new Intent(this, Class); + intent.AddFlags(ActivityFlags.SingleTop); + var pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0); + + // register for all NDEF tags starting with http och https + var ndef = new IntentFilter(NfcAdapter.ActionNdefDiscovered); + ndef.AddDataScheme("http"); + ndef.AddDataScheme("https"); + + // register for foreground dispatch so we'll receive tags according to our intent filters + var adapter = NfcAdapter.GetDefaultAdapter(this); + adapter.EnableForegroundDispatch(this, pendingIntent, new IntentFilter[] { ndef }, null); + + var data = Intent.DataString; + if(data != null) + { + var otpMatch = _otpPattern.Matcher(data); + if(otpMatch.Matches()) + { + var otp = otpMatch.Group(1); + Console.WriteLine("Got OTP: " + otp); + MessagingCenter.Send(Xamarin.Forms.Application.Current, "GotYubiKeyOTP", otp); + } + else + { + Console.WriteLine("Data from ndef didn't match, it was: " + data); + } + } + } } } diff --git a/src/Android/Properties/AndroidManifest.xml b/src/Android/Properties/AndroidManifest.xml index 80aa9a2fc..12298232d 100644 --- a/src/Android/Properties/AndroidManifest.xml +++ b/src/Android/Properties/AndroidManifest.xml @@ -3,6 +3,7 @@ +