QR code scanning for authenticator keys

This commit is contained in:
Kyle Spearrin 2017-07-13 17:23:18 -04:00
parent 26c110291e
commit 87e71ea860
16 changed files with 319 additions and 4 deletions

View file

@ -297,6 +297,21 @@
<Reference Include="XLabs.Ioc.SimpleInjector, Version=2.0.5782.12229, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\XLabs.IoC.SimpleInjector.2.0.5782\lib\portable-net45+netcore45+wp8+MonoAndroid1+MonoTouch1\XLabs.Ioc.SimpleInjector.dll</HintPath>
</Reference>
<Reference Include="ZXing.Net.Mobile.Core, Version=2.1.47.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\ZXing.Net.Mobile.2.1.47\lib\MonoAndroid403\ZXing.Net.Mobile.Core.dll</HintPath>
</Reference>
<Reference Include="ZXing.Net.Mobile.Forms, Version=2.1.47.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\ZXing.Net.Mobile.Forms.2.1.47\lib\MonoAndroid403\ZXing.Net.Mobile.Forms.dll</HintPath>
</Reference>
<Reference Include="ZXing.Net.Mobile.Forms.Android, Version=2.1.47.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\ZXing.Net.Mobile.Forms.2.1.47\lib\MonoAndroid403\ZXing.Net.Mobile.Forms.Android.dll</HintPath>
</Reference>
<Reference Include="zxing.portable, Version=2.1.47.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\ZXing.Net.Mobile.2.1.47\lib\MonoAndroid403\zxing.portable.dll</HintPath>
</Reference>
<Reference Include="ZXingNetMobile, Version=2.1.47.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\ZXing.Net.Mobile.2.1.47\lib\MonoAndroid403\ZXingNetMobile.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AutofillActivity.cs" />

View file

@ -199,6 +199,11 @@ namespace Bit.Android
ParseYubiKey(intent.DataString);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
ZXing.Net.Mobile.Forms.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
public void RateApp()
{
try

View file

@ -188,6 +188,7 @@ namespace Bit.Android
{
UserDialogs.Init(application);
CachedImageRenderer.Init();
ZXing.Net.Mobile.Forms.Android.Platform.Init();
CrossFingerprint.SetCurrentActivityResolver(() => CrossCurrentActivity.Current.Activity);
//var container = new UnityContainer();

View file

@ -193,6 +193,12 @@ namespace Bit.Android
global::Plugin.Fingerprint.Resource.Layout.FingerprintDialog = global::Bit.Android.Resource.Layout.FingerprintDialog;
global::Splat.Resource.String.library_name = global::Bit.Android.Resource.String.library_name;
global::Xamarin.Forms.Platform.Android.Resource.Attribute.actionBarSize = global::Bit.Android.Resource.Attribute.actionBarSize;
global::ZXing.Net.Mobile.Forms.Android.Resource.Layout.zxingscanneractivitylayout = global::Bit.Android.Resource.Layout.zxingscanneractivitylayout;
global::ZXing.Net.Mobile.Forms.Android.Resource.Layout.zxingscannerfragmentlayout = global::Bit.Android.Resource.Layout.zxingscannerfragmentlayout;
global::ZXing.Net.Mobile.Forms.Android.Resource.String.library_name = global::Bit.Android.Resource.String.library_name;
global::ZXing.Mobile.Resource.Id.contentFrame = global::Bit.Android.Resource.Id.contentFrame;
global::ZXing.Mobile.Resource.Layout.zxingscanneractivitylayout = global::Bit.Android.Resource.Layout.zxingscanneractivitylayout;
global::ZXing.Mobile.Resource.Layout.zxingscannerfragmentlayout = global::Bit.Android.Resource.Layout.zxingscannerfragmentlayout;
}
public partial class Animation
@ -2928,6 +2934,9 @@ namespace Bit.Android
// aapt resource value: 0x7f0c0027
public const int collapseActionView = 2131492903;
// aapt resource value: 0x7f0c00c6
public const int contentFrame = 2131493062;
// aapt resource value: 0x7f0c0052
public const int contentPanel = 2131492946;
@ -3684,6 +3693,12 @@ namespace Bit.Android
// aapt resource value: 0x7f030042
public const int toolbar = 2130903106;
// aapt resource value: 0x7f030043
public const int zxingscanneractivitylayout = 2130903107;
// aapt resource value: 0x7f030044
public const int zxingscannerfragmentlayout = 2130903108;
static Layout()
{
global::Android.Runtime.ResourceIdManager.UpdateIdValues();

View file

@ -92,4 +92,6 @@
<package id="Xamarin.GooglePlayServices.Measurement" version="29.0.0.2" targetFramework="monoandroid60" />
<package id="XLabs.IoC" version="2.0.5782" targetFramework="monoandroid60" />
<package id="XLabs.IoC.SimpleInjector" version="2.0.5782" targetFramework="monoandroid71" />
<package id="ZXing.Net.Mobile" version="2.1.47" targetFramework="monoandroid71" />
<package id="ZXing.Net.Mobile.Forms" version="2.1.47" targetFramework="monoandroid71" />
</packages>

View file

@ -143,6 +143,7 @@
<Compile Include="Pages\LoginTwoFactorPage.cs" />
<Compile Include="Pages\PasswordHintPage.cs" />
<Compile Include="Pages\RegisterPage.cs" />
<Compile Include="Pages\ScanPage.cs" />
<Compile Include="Pages\Settings\SettingsCreditsPage.cs" />
<Compile Include="Pages\Settings\SettingsHelpPage.cs" />
<Compile Include="Pages\Settings\SettingsFeaturesPage.cs" />
@ -495,6 +496,18 @@
<HintPath>..\..\packages\XLabs.IoC.2.0.5782\lib\portable-net45+netcore45+wpa81+wp8+MonoAndroid1+MonoTouch1+Xamarin.iOS10\XLabs.Ioc.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="ZXing.Net.Mobile.Core, Version=2.1.47.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\ZXing.Net.Mobile.2.1.47\lib\portable-net45+netcore45+wpa81+wp8\ZXing.Net.Mobile.Core.dll</HintPath>
</Reference>
<Reference Include="ZXing.Net.Mobile.Forms, Version=2.1.47.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\ZXing.Net.Mobile.Forms.2.1.47\lib\portable-net45+netcore45+wpa81+wp8\ZXing.Net.Mobile.Forms.dll</HintPath>
</Reference>
<Reference Include="zxing.portable, Version=2.1.47.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\ZXing.Net.Mobile.2.1.47\lib\portable-net45+netcore45+wpa81+wp8\zxing.portable.dll</HintPath>
</Reference>
<Reference Include="ZXingNetMobile, Version=2.1.47.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\ZXing.Net.Mobile.2.1.47\lib\portable-net45+netcore45+wpa81+wp8\ZXingNetMobile.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\AppResources.resx">

148
src/App/Pages/ScanPage.cs Normal file
View file

@ -0,0 +1,148 @@
using Bit.App.Controls;
using Bit.App.Resources;
using System;
using System.Collections.Generic;
using Xamarin.Forms;
using ZXing.Net.Mobile.Forms;
namespace Bit.App.Pages
{
public class ScanPage : ExtendedContentPage
{
private readonly ZXingScannerView _zxing;
private readonly OverlayGrid _overlay;
public ScanPage(Action<string> callback)
: base(updateActivity: false)
{
_zxing = new ZXingScannerView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
AutomationId = "zxingScannerView",
Options = new ZXing.Mobile.MobileBarcodeScanningOptions
{
UseNativeScanning = true,
PossibleFormats = new List<ZXing.BarcodeFormat> { ZXing.BarcodeFormat.QR_CODE },
AutoRotate = false
}
};
_zxing.OnScanResult += (result) =>
{
// Stop analysis until we navigate away so we don't keep reading barcodes
_zxing.IsAnalyzing = false;
_zxing.IsScanning = false;
Uri uri;
if(!string.IsNullOrWhiteSpace(result.Text) && Uri.TryCreate(result.Text, UriKind.Absolute, out uri) &&
!string.IsNullOrWhiteSpace(uri.Query))
{
var queryParts = uri.Query.Substring(1).ToLowerInvariant().Split('&');
foreach(var part in queryParts)
{
if(part.StartsWith("secret="))
{
callback(part.Substring(7)?.ToUpperInvariant());
return;
}
}
}
callback(null);
};
_overlay = new OverlayGrid
{
AutomationId = "zxingDefaultOverlay"
};
_overlay.TopLabel.Text = AppResources.CameraInstructionTop;
_overlay.BottomLabel.Text = AppResources.CameraInstructionBottom;
var grid = new Grid
{
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children = { _zxing, _overlay }
};
if(Device.RuntimePlatform == Device.iOS)
{
ToolbarItems.Add(new DismissModalToolBarItem(this, AppResources.Close));
}
Title = AppResources.ScanQrTitle;
Content = grid;
}
protected override void OnAppearing()
{
base.OnAppearing();
_zxing.IsScanning = true;
}
protected override void OnDisappearing()
{
_zxing.IsScanning = false;
base.OnDisappearing();
}
public class OverlayGrid : Grid
{
public OverlayGrid()
{
VerticalOptions = LayoutOptions.FillAndExpand;
HorizontalOptions = LayoutOptions.FillAndExpand;
RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
RowDefinitions.Add(new RowDefinition { Height = new GridLength(2, GridUnitType.Star) });
RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
Children.Add(new BoxView
{
VerticalOptions = LayoutOptions.Fill,
HorizontalOptions = LayoutOptions.FillAndExpand,
BackgroundColor = Color.Black,
Opacity = 0.7,
}, 0, 0);
Children.Add(new BoxView
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.FillAndExpand,
BackgroundColor = Color.Transparent
}, 0, 1);
Children.Add(new BoxView
{
VerticalOptions = LayoutOptions.Fill,
HorizontalOptions = LayoutOptions.FillAndExpand,
BackgroundColor = Color.Black,
Opacity = 0.7,
}, 0, 2);
TopLabel = new Label
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
TextColor = Color.White,
AutomationId = "zxingDefaultOverlay_TopTextLabel",
};
Children.Add(TopLabel, 0, 0);
BottomLabel = new Label
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
TextColor = Color.White,
AutomationId = "zxingDefaultOverlay_BottomTextLabel",
};
Children.Add(BottomLabel, 0, 2);
}
public Label TopLabel { get; set; }
public Label BottomLabel { get; set; }
}
}
}

View file

@ -276,9 +276,26 @@ namespace Bit.App.Pages
PasswordCell.Button.Image = "eye" + (!PasswordCell.Entry.IsPasswordFromToggled ? "_slash" : string.Empty);
}
private void TotpButton_Clicked(object sender, EventArgs e)
private async void TotpButton_Clicked(object sender, EventArgs e)
{
// launch camera
var scanPage = new ScanPage((key) =>
{
Device.BeginInvokeOnMainThread(async () =>
{
await Navigation.PopModalAsync();
if(!string.IsNullOrWhiteSpace(key))
{
TotpCell.Entry.Text = key;
_userDialogs.Toast(AppResources.AuthenticatorKeyAdded);
}
else
{
_userDialogs.Alert(AppResources.AuthenticatorKeyReadError);
}
});
});
await Navigation.PushModalAsync(new ExtendedNavigationPage(scanPage));
}
private async void GenerateCell_Tapped(object sender, EventArgs e)

View file

@ -298,9 +298,26 @@ namespace Bit.App.Pages
PasswordCell.Button.Image = "eye" + (!PasswordCell.Entry.IsPasswordFromToggled ? "_slash" : string.Empty);
}
private void TotpButton_Clicked(object sender, EventArgs e)
private async void TotpButton_Clicked(object sender, EventArgs e)
{
// launch camera
var scanPage = new ScanPage((key) =>
{
Device.BeginInvokeOnMainThread(async () =>
{
await Navigation.PopModalAsync();
if(!string.IsNullOrWhiteSpace(key))
{
TotpCell.Entry.Text = key;
_userDialogs.Toast(AppResources.AuthenticatorKeyAdded);
}
else
{
_userDialogs.Alert(AppResources.AuthenticatorKeyReadError);
}
});
});
await Navigation.PushModalAsync(new ExtendedNavigationPage(scanPage));
}
private async void GenerateCell_Tapped(object sender, EventArgs e)

View file

@ -187,6 +187,24 @@ namespace Bit.App.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Authenticator key added..
/// </summary>
public static string AuthenticatorKeyAdded {
get {
return ResourceManager.GetString("AuthenticatorKeyAdded", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Cannot read authenticator key..
/// </summary>
public static string AuthenticatorKeyReadError {
get {
return ResourceManager.GetString("AuthenticatorKeyReadError", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Auto-fill.
/// </summary>
@ -430,6 +448,24 @@ namespace Bit.App.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Scanning will happen automatically..
/// </summary>
public static string CameraInstructionBottom {
get {
return ResourceManager.GetString("CameraInstructionBottom", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Point your camera at the QR code..
/// </summary>
public static string CameraInstructionTop {
get {
return ResourceManager.GetString("CameraInstructionTop", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Cancel.
/// </summary>
@ -1771,6 +1807,15 @@ namespace Bit.App.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Scan QR Code.
/// </summary>
public static string ScanQrTitle {
get {
return ResourceManager.GetString("ScanQrTitle", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Search.
/// </summary>

View file

@ -929,4 +929,19 @@
<value>Verification Code (TOTP)</value>
<comment>Totp code label</comment>
</data>
<data name="AuthenticatorKeyAdded" xml:space="preserve">
<value>Authenticator key added.</value>
</data>
<data name="AuthenticatorKeyReadError" xml:space="preserve">
<value>Cannot read authenticator key.</value>
</data>
<data name="CameraInstructionBottom" xml:space="preserve">
<value>Scanning will happen automatically.</value>
</data>
<data name="CameraInstructionTop" xml:space="preserve">
<value>Point your camera at the QR code.</value>
</data>
<data name="ScanQrTitle" xml:space="preserve">
<value>Scan QR Code</value>
</data>
</root>

View file

@ -24,4 +24,6 @@
<package id="Xamarin.FFImageLoading.Forms" version="2.2.9" targetFramework="portable45-net45+win8+wpa81" />
<package id="Xamarin.Forms" version="2.3.4.231" targetFramework="portable45-net45+win8+wpa81" />
<package id="XLabs.IoC" version="2.0.5782" targetFramework="portable45-net45+win8+wpa81" />
<package id="ZXing.Net.Mobile" version="2.1.47" targetFramework="portable45-net45+win8+wpa81" />
<package id="ZXing.Net.Mobile.Forms" version="2.1.47" targetFramework="portable45-net45+win8+wpa81" />
</packages>

View file

@ -116,6 +116,7 @@ namespace Bit.iOS
UIApplication.SharedApplication.SetStatusBarHidden(!show, false);
});
ZXing.Net.Mobile.Forms.iOS.Platform.Init();
return base.FinishedLaunching(app, options);
}

View file

@ -101,5 +101,7 @@
<string>en</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app does not require access to the photo library.</string>
<key>NSCameraUsageDescription</key>
<string>Scan QR codes</string>
</dict>
</plist>

View file

@ -412,6 +412,21 @@
<Reference Include="XLabs.Ioc.SimpleInjector, Version=2.0.5782.12229, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\XLabs.IoC.SimpleInjector.2.0.5782\lib\portable-net45+netcore45+wp8+MonoAndroid1+MonoTouch1\XLabs.Ioc.SimpleInjector.dll</HintPath>
</Reference>
<Reference Include="ZXing.Net.Mobile.Core, Version=2.1.47.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\ZXing.Net.Mobile.2.1.47\lib\Xamarin.iOS10\ZXing.Net.Mobile.Core.dll</HintPath>
</Reference>
<Reference Include="ZXing.Net.Mobile.Forms, Version=2.1.47.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\ZXing.Net.Mobile.Forms.2.1.47\lib\Xamarin.iOS10\ZXing.Net.Mobile.Forms.dll</HintPath>
</Reference>
<Reference Include="ZXing.Net.Mobile.Forms.iOS, Version=2.1.47.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\ZXing.Net.Mobile.Forms.2.1.47\lib\Xamarin.iOS10\ZXing.Net.Mobile.Forms.iOS.dll</HintPath>
</Reference>
<Reference Include="zxing.portable, Version=2.1.47.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\ZXing.Net.Mobile.2.1.47\lib\Xamarin.iOS10\zxing.portable.dll</HintPath>
</Reference>
<Reference Include="ZXingNetMobile, Version=2.1.47.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\ZXing.Net.Mobile.2.1.47\lib\Xamarin.iOS10\ZXingNetMobile.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\App\App.csproj">

View file

@ -78,4 +78,6 @@
<package id="Xamarin.Google.iOS.Analytics" version="3.17.0.1" targetFramework="xamarinios10" />
<package id="XLabs.IoC" version="2.0.5782" targetFramework="xamarinios10" />
<package id="XLabs.IoC.SimpleInjector" version="2.0.5782" targetFramework="xamarinios10" />
<package id="ZXing.Net.Mobile" version="2.1.47" targetFramework="xamarinios10" />
<package id="ZXing.Net.Mobile.Forms" version="2.1.47" targetFramework="xamarinios10" />
</packages>