1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-31 07:33:46 +00:00

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

@@ -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>