2019-06-28 14:57:08 +02:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using Bit.iOS.Extension.Models;
|
|
|
|
using Foundation;
|
|
|
|
using UIKit;
|
|
|
|
using Bit.iOS.Core.Utilities;
|
|
|
|
using Bit.iOS.Core;
|
|
|
|
using MobileCoreServices;
|
|
|
|
using Bit.iOS.Core.Controllers;
|
|
|
|
using Bit.App.Resources;
|
|
|
|
using Bit.iOS.Core.Views;
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
using Bit.Core.Abstractions;
|
|
|
|
|
|
|
|
namespace Bit.iOS.Extension
|
|
|
|
{
|
|
|
|
public partial class LoginListViewController : ExtendedUITableViewController
|
|
|
|
{
|
|
|
|
public LoginListViewController(IntPtr handle)
|
|
|
|
: base(handle)
|
2020-10-15 20:34:31 +02:00
|
|
|
{
|
|
|
|
DismissModalAction = Cancel;
|
|
|
|
}
|
2019-06-28 14:57:08 +02:00
|
|
|
|
|
|
|
public Context Context { get; set; }
|
|
|
|
public LoadingViewController LoadingController { get; set; }
|
|
|
|
|
|
|
|
public async override void ViewDidLoad()
|
|
|
|
{
|
|
|
|
base.ViewDidLoad();
|
2019-07-01 21:12:40 +02:00
|
|
|
AddBarButton.TintColor = ThemeHelpers.NavBarTextColor;
|
|
|
|
CancelBarButton.TintColor = ThemeHelpers.NavBarTextColor;
|
2019-06-28 14:57:08 +02:00
|
|
|
NavItem.Title = AppResources.Items;
|
2020-03-28 14:16:28 +01:00
|
|
|
if (!CanAutoFill())
|
2019-06-28 14:57:08 +02:00
|
|
|
{
|
|
|
|
CancelBarButton.Title = AppResources.Close;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CancelBarButton.Title = AppResources.Cancel;
|
|
|
|
}
|
|
|
|
TableView.RowHeight = UITableView.AutomaticDimension;
|
|
|
|
TableView.EstimatedRowHeight = 44;
|
|
|
|
TableView.Source = new TableSource(this);
|
|
|
|
await ((TableSource)TableView.Source).LoadItemsAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CanAutoFill()
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
if (Context.ProviderType != Constants.UTTypeAppExtensionFillBrowserAction
|
2019-06-28 14:57:08 +02:00
|
|
|
&& Context.ProviderType != Constants.UTTypeAppExtensionFillWebViewAction
|
2021-02-11 23:14:37 +01:00
|
|
|
&& Context.ProviderType != UTType.PropertyList
|
|
|
|
&& Context.ProviderType != Constants.UTTypeAppExtensionUrl)
|
2019-06-28 14:57:08 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return Context.Details?.HasPasswordField ?? false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
partial void CancelBarButton_Activated(UIBarButtonItem sender)
|
2020-10-15 20:34:31 +02:00
|
|
|
{
|
|
|
|
Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Cancel()
|
2019-06-28 14:57:08 +02:00
|
|
|
{
|
2019-07-22 21:50:59 +02:00
|
|
|
LoadingController.CompleteRequest(null, null);
|
2019-06-28 14:57:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
partial void AddBarButton_Activated(UIBarButtonItem sender)
|
|
|
|
{
|
|
|
|
PerformSegue("loginAddSegue", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
if (segue.DestinationViewController is UINavigationController navController)
|
2019-06-28 14:57:08 +02:00
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
if (navController.TopViewController is LoginAddViewController addLoginController)
|
2019-06-28 14:57:08 +02:00
|
|
|
{
|
|
|
|
addLoginController.Context = Context;
|
|
|
|
addLoginController.LoginListController = this;
|
2020-10-15 20:34:31 +02:00
|
|
|
segue.DestinationViewController.PresentationController.Delegate =
|
|
|
|
new CustomPresentationControllerDelegate(addLoginController.DismissModalAction);
|
2019-06-28 14:57:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DismissModal()
|
|
|
|
{
|
|
|
|
DismissViewController(true, async () =>
|
|
|
|
{
|
|
|
|
await ((TableSource)TableView.Source).LoadItemsAsync();
|
|
|
|
TableView.ReloadData();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public class TableSource : ExtensionTableSource
|
|
|
|
{
|
|
|
|
private LoginListViewController _controller;
|
|
|
|
|
|
|
|
public TableSource(LoginListViewController controller)
|
|
|
|
: base(controller.Context, controller)
|
|
|
|
{
|
|
|
|
_controller = controller;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
|
|
|
|
{
|
|
|
|
tableView.DeselectRow(indexPath, true);
|
|
|
|
tableView.EndEditing(true);
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
if (Items == null || Items.Count() == 0)
|
2019-06-28 14:57:08 +02:00
|
|
|
{
|
|
|
|
_controller.PerformSegue("loginAddSegue", this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var item = Items.ElementAt(indexPath.Row);
|
2020-03-28 14:16:28 +01:00
|
|
|
if (item == null)
|
2019-06-28 14:57:08 +02:00
|
|
|
{
|
2019-07-22 21:50:59 +02:00
|
|
|
_controller.LoadingController.CompleteRequest(null, null);
|
2019-06-28 14:57:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
if (_controller.CanAutoFill() && !string.IsNullOrWhiteSpace(item.Password))
|
2019-06-28 14:57:08 +02:00
|
|
|
{
|
|
|
|
string totp = null;
|
|
|
|
var storageService = ServiceContainer.Resolve<IStorageService>("storageService");
|
|
|
|
var disableTotpCopy = storageService.GetAsync<bool?>(
|
|
|
|
Bit.Core.Constants.DisableAutoTotpCopyKey).GetAwaiter().GetResult();
|
2020-03-28 14:16:28 +01:00
|
|
|
if (!disableTotpCopy.GetValueOrDefault(false))
|
2019-06-28 14:57:08 +02:00
|
|
|
{
|
|
|
|
totp = GetTotpAsync(item).GetAwaiter().GetResult();
|
|
|
|
}
|
|
|
|
_controller.LoadingController.CompleteUsernamePasswordRequest(
|
2019-07-22 21:50:59 +02:00
|
|
|
item.Id, item.Username, item.Password, item.Fields, totp);
|
2019-06-28 14:57:08 +02:00
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
else if (!string.IsNullOrWhiteSpace(item.Username) || !string.IsNullOrWhiteSpace(item.Password) ||
|
2019-06-28 14:57:08 +02:00
|
|
|
!string.IsNullOrWhiteSpace(item.Totp))
|
|
|
|
{
|
|
|
|
var sheet = Dialogs.CreateActionSheet(item.Name, _controller);
|
2020-03-28 14:16:28 +01:00
|
|
|
if (!string.IsNullOrWhiteSpace(item.Username))
|
2019-06-28 14:57:08 +02:00
|
|
|
{
|
|
|
|
sheet.AddAction(UIAlertAction.Create(AppResources.CopyUsername, UIAlertActionStyle.Default, a =>
|
|
|
|
{
|
|
|
|
UIPasteboard clipboard = UIPasteboard.General;
|
|
|
|
clipboard.String = item.Username;
|
|
|
|
var alert = Dialogs.CreateMessageAlert(AppResources.CopyUsername);
|
|
|
|
_controller.PresentViewController(alert, true, () =>
|
|
|
|
{
|
|
|
|
_controller.DismissViewController(true, null);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
if (!string.IsNullOrWhiteSpace(item.Password))
|
2019-06-28 14:57:08 +02:00
|
|
|
{
|
|
|
|
sheet.AddAction(UIAlertAction.Create(AppResources.CopyPassword, UIAlertActionStyle.Default, a =>
|
|
|
|
{
|
|
|
|
UIPasteboard clipboard = UIPasteboard.General;
|
|
|
|
clipboard.String = item.Password;
|
|
|
|
var alert = Dialogs.CreateMessageAlert(
|
|
|
|
string.Format(AppResources.ValueHasBeenCopied, AppResources.Password));
|
|
|
|
_controller.PresentViewController(alert, true, () =>
|
|
|
|
{
|
|
|
|
_controller.DismissViewController(true, null);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
if (!string.IsNullOrWhiteSpace(item.Totp))
|
2019-06-28 14:57:08 +02:00
|
|
|
{
|
|
|
|
sheet.AddAction(UIAlertAction.Create(AppResources.CopyTotp, UIAlertActionStyle.Default,
|
|
|
|
async a =>
|
|
|
|
{
|
|
|
|
var totp = await GetTotpAsync(item);
|
2020-03-28 14:16:28 +01:00
|
|
|
if (string.IsNullOrWhiteSpace(totp))
|
2019-06-28 14:57:08 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UIPasteboard clipboard = UIPasteboard.General;
|
|
|
|
clipboard.String = totp;
|
|
|
|
var alert = Dialogs.CreateMessageAlert(
|
|
|
|
string.Format(AppResources.ValueHasBeenCopied, AppResources.VerificationCodeTotp));
|
|
|
|
_controller.PresentViewController(alert, true, () =>
|
|
|
|
{
|
|
|
|
_controller.DismissViewController(true, null);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
sheet.AddAction(UIAlertAction.Create(AppResources.Cancel, UIAlertActionStyle.Cancel, null));
|
|
|
|
_controller.PresentViewController(sheet, true, null);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var alert = Dialogs.CreateAlert(null, AppResources.NoUsernamePasswordConfigured, AppResources.Ok);
|
|
|
|
_controller.PresentViewController(alert, true, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|