mirror of
https://github.com/bitwarden/mobile
synced 2026-01-06 02:23:57 +00:00
QR code scanning for authenticator keys
This commit is contained in:
148
src/App/Pages/ScanPage.cs
Normal file
148
src/App/Pages/ScanPage.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user