2016-05-27 05:22:38 +03:00
|
|
|
using System;
|
2016-05-28 23:06:53 +03:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
using Bit.App.Repositories;
|
|
|
|
using Bit.App.Services;
|
|
|
|
using Bit.iOS.Core.Services;
|
2016-05-27 05:22:38 +03:00
|
|
|
using CoreGraphics;
|
2016-05-07 08:54:44 +03:00
|
|
|
using Foundation;
|
2016-05-28 23:06:53 +03:00
|
|
|
using Microsoft.Practices.Unity;
|
2016-05-27 05:22:38 +03:00
|
|
|
using MobileCoreServices;
|
2016-05-07 08:54:44 +03:00
|
|
|
using UIKit;
|
2016-05-28 23:06:53 +03:00
|
|
|
using XLabs.Ioc;
|
|
|
|
using XLabs.Ioc.Unity;
|
2016-05-07 08:54:44 +03:00
|
|
|
|
|
|
|
namespace Bit.iOS.Extension
|
|
|
|
{
|
|
|
|
public partial class ActionViewController : UIViewController
|
|
|
|
{
|
2016-05-30 10:08:12 +03:00
|
|
|
private const string AppExtensionVersionNumberKey = "version_number";
|
2016-05-31 05:51:53 +03:00
|
|
|
|
2016-05-30 10:08:12 +03:00
|
|
|
private const string AppExtensionUrlStringKey = "url_string";
|
2016-05-31 05:51:53 +03:00
|
|
|
private const string AppExtensionUsernameKey = "username";
|
|
|
|
private const string AppExtensionPasswordKey = "password";
|
|
|
|
private const string AppExtensionTotpKey = "totp";
|
|
|
|
private const string AppExtensionTitleKey = "login_title";
|
|
|
|
private const string AppExtensionNotesKey = "notes";
|
|
|
|
private const string AppExtensionSectionTitleKey = "section_title";
|
|
|
|
private const string AppExtensionFieldsKey = "fields";
|
|
|
|
private const string AppExtensionReturnedFieldsKey = "returned_fields";
|
|
|
|
private const string AppExtensionOldPasswordKey = "old_password";
|
|
|
|
private const string AppExtensionPasswordGeneratorOptionsKey = "password_generator_options";
|
|
|
|
|
|
|
|
private const string AppExtensionGeneratedPasswordMinLengthKey = "password_min_length";
|
|
|
|
private const string AppExtensionGeneratedPasswordMaxLengthKey = "password_max_length";
|
|
|
|
private const string AppExtensionGeneratedPasswordRequireDigitsKey = "password_require_digits";
|
|
|
|
private const string AppExtensionGeneratedPasswordRequireSymbolsKey = "password_require_symbols";
|
|
|
|
private const string AppExtensionGeneratedPasswordForbiddenCharactersKey = "password_forbidden_characters";
|
|
|
|
|
2016-05-30 10:08:12 +03:00
|
|
|
private const string UTTypeAppExtensionFindLoginAction = "org.appextension.find-login-action";
|
|
|
|
private const string UTTypeAppExtensionSaveLoginAction = "org.appextension.save-login-action";
|
|
|
|
private const string UTTypeAppExtensionChangePasswordAction = "org.appextension.change-password-action";
|
|
|
|
private const string UTTypeAppExtensionFillWebViewAction = "org.appextension.fill-webview-action";
|
|
|
|
private const string UTTypeAppExtensionFillBrowserAction = "org.appextension.fill-browser-action";
|
|
|
|
|
2016-05-27 05:22:38 +03:00
|
|
|
public ActionViewController() : base("ActionViewController", null)
|
2016-05-07 08:54:44 +03:00
|
|
|
{
|
2016-05-28 23:06:53 +03:00
|
|
|
if(!Resolver.IsSet)
|
|
|
|
{
|
|
|
|
SetIoc();
|
|
|
|
}
|
2016-05-07 08:54:44 +03:00
|
|
|
}
|
|
|
|
|
2016-05-31 05:51:53 +03:00
|
|
|
public string ProviderType { get; set; }
|
2016-05-27 05:22:38 +03:00
|
|
|
public Uri Url { get; set; }
|
2016-05-31 05:51:53 +03:00
|
|
|
public string SiteTitle { get; set; }
|
|
|
|
public string Username { get; set; }
|
|
|
|
public string Password { get; set; }
|
|
|
|
public string OldPassword { get; set; }
|
|
|
|
public string Notes { get; set; }
|
|
|
|
public PasswordGenerationOptions PasswordOptions { get; set; }
|
2016-05-07 08:54:44 +03:00
|
|
|
|
2016-05-28 23:06:53 +03:00
|
|
|
private void SetIoc()
|
|
|
|
{
|
|
|
|
var container = new UnityContainer();
|
|
|
|
|
|
|
|
container
|
|
|
|
// Services
|
|
|
|
.RegisterType<IDatabaseService, DatabaseService>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<ISqlService, SqlService>(new ContainerControlledLifetimeManager())
|
|
|
|
//.RegisterType<ISecureStorageService, KeyChainStorageService>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<ICryptoService, CryptoService>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<IAuthService, AuthService>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<IFolderService, FolderService>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<ISiteService, SiteService>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<ISyncService, SyncService>(new ContainerControlledLifetimeManager())
|
|
|
|
//.RegisterType<IClipboardService, ClipboardService>(new ContainerControlledLifetimeManager())
|
|
|
|
// Repositories
|
|
|
|
.RegisterType<IFolderRepository, FolderRepository>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<IFolderApiRepository, FolderApiRepository>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<ISiteRepository, SiteRepository>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<ISiteApiRepository, SiteApiRepository>(new ContainerControlledLifetimeManager())
|
|
|
|
.RegisterType<IAuthApiRepository, AuthApiRepository>(new ContainerControlledLifetimeManager());
|
2016-05-30 10:08:12 +03:00
|
|
|
// Other
|
|
|
|
//.RegisterInstance(CrossSettings.Current, new ContainerControlledLifetimeManager())
|
|
|
|
//.RegisterInstance(CrossConnectivity.Current, new ContainerControlledLifetimeManager())
|
|
|
|
//.RegisterInstance(UserDialogs.Instance, new ContainerControlledLifetimeManager())
|
|
|
|
//.RegisterInstance(CrossFingerprint.Current, new ContainerControlledLifetimeManager());
|
2016-05-28 23:06:53 +03:00
|
|
|
|
|
|
|
Resolver.SetResolver(new UnityResolver(container));
|
|
|
|
}
|
|
|
|
|
2016-05-07 08:54:44 +03:00
|
|
|
public override void DidReceiveMemoryWarning()
|
|
|
|
{
|
|
|
|
base.DidReceiveMemoryWarning();
|
|
|
|
}
|
|
|
|
|
2016-05-31 05:51:53 +03:00
|
|
|
public override void LoadView()
|
2016-05-27 05:22:38 +03:00
|
|
|
{
|
2016-05-31 05:51:53 +03:00
|
|
|
foreach(var item in ExtensionContext.InputItems)
|
|
|
|
{
|
|
|
|
foreach(var itemProvider in item.Attachments)
|
|
|
|
{
|
|
|
|
if(ProcessWebUrlProvider(itemProvider))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(ProcessFindLoginProvider(itemProvider))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(ProcessSaveLoginProvider(itemProvider))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(ProcessChangePasswordProvider(itemProvider))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 05:22:38 +03:00
|
|
|
View = new UIView(new CGRect(x: 0.0, y: 0, width: 320.0, height: 200.0));
|
|
|
|
var button = new UIButton(new CGRect(x: 10.0, y: 50.0, width: 200.0, height: 30.0));
|
|
|
|
button.SetTitle("Done", UIControlState.Normal);
|
|
|
|
button.TouchUpInside += Button_TouchUpInside;
|
|
|
|
View.AddSubview(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Button_TouchUpInside(object sender, EventArgs e)
|
|
|
|
{
|
2016-05-31 05:51:53 +03:00
|
|
|
NSDictionary itemData = null;
|
|
|
|
if(ProviderType == UTType.PropertyList)
|
|
|
|
{
|
|
|
|
itemData = new NSDictionary(
|
|
|
|
"username", "me@example.com",
|
|
|
|
"password", "mypassword",
|
|
|
|
"autoSubmit", true);
|
|
|
|
}
|
|
|
|
else if(ProviderType == UTTypeAppExtensionFindLoginAction)
|
|
|
|
{
|
|
|
|
itemData = new NSDictionary(
|
|
|
|
AppExtensionUsernameKey, "me@example.com",
|
|
|
|
AppExtensionPasswordKey, "mypassword");
|
|
|
|
}
|
|
|
|
else if(ProviderType == UTTypeAppExtensionSaveLoginAction)
|
|
|
|
{
|
|
|
|
itemData = new NSDictionary(
|
|
|
|
AppExtensionUsernameKey, "me@example.com",
|
|
|
|
AppExtensionPasswordKey, "mypassword");
|
|
|
|
}
|
|
|
|
else if(ProviderType == UTTypeAppExtensionChangePasswordAction)
|
|
|
|
{
|
|
|
|
itemData = new NSDictionary(
|
|
|
|
AppExtensionPasswordKey, "mynewpassword",
|
|
|
|
AppExtensionOldPasswordKey, "myoldpassword");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-05-27 05:22:38 +03:00
|
|
|
|
2016-05-31 05:51:53 +03:00
|
|
|
var resultsProvider = new NSItemProvider(itemData, UTType.PropertyList);
|
2016-05-28 07:59:36 +03:00
|
|
|
var resultsItem = new NSExtensionItem { Attachments = new NSItemProvider[] { resultsProvider } };
|
2016-05-27 05:22:38 +03:00
|
|
|
var returningItems = new NSExtensionItem[] { resultsItem };
|
2016-05-28 07:59:36 +03:00
|
|
|
|
2016-05-27 05:22:38 +03:00
|
|
|
ExtensionContext.CompleteRequest(returningItems, null);
|
|
|
|
}
|
|
|
|
|
2016-05-07 08:54:44 +03:00
|
|
|
public override void ViewDidLoad()
|
|
|
|
{
|
|
|
|
base.ViewDidLoad();
|
|
|
|
}
|
|
|
|
|
2016-05-31 05:51:53 +03:00
|
|
|
private bool ProcessItemProvider(NSItemProvider itemProvider, string type, Action<NSDictionary> action)
|
2016-05-30 10:08:12 +03:00
|
|
|
{
|
2016-05-31 05:51:53 +03:00
|
|
|
if(!itemProvider.HasItemConformingTo(type))
|
2016-05-30 10:08:12 +03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-31 05:51:53 +03:00
|
|
|
itemProvider.LoadItem(type, null, (NSObject list, NSError error) =>
|
2016-05-30 10:08:12 +03:00
|
|
|
{
|
|
|
|
if(list == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-31 05:51:53 +03:00
|
|
|
ProviderType = type;
|
2016-05-30 10:08:12 +03:00
|
|
|
var dict = list as NSDictionary;
|
2016-05-31 05:51:53 +03:00
|
|
|
action(dict);
|
|
|
|
|
|
|
|
Debug.WriteLine("BW LOG, ProviderType: " + ProviderType);
|
|
|
|
Debug.WriteLine("BW LOG, Url: " + Url);
|
|
|
|
Debug.WriteLine("BW LOG, Title: " + SiteTitle);
|
|
|
|
Debug.WriteLine("BW LOG, Username: " + Username);
|
|
|
|
Debug.WriteLine("BW LOG, Password: " + Password);
|
|
|
|
Debug.WriteLine("BW LOG, Old Password: " + OldPassword);
|
|
|
|
Debug.WriteLine("BW LOG, Notes: " + Notes);
|
|
|
|
|
|
|
|
if(PasswordOptions != null)
|
|
|
|
{
|
|
|
|
Debug.WriteLine("BW LOG, PasswordOptions Min Length: " + PasswordOptions.MinLength);
|
|
|
|
Debug.WriteLine("BW LOG, PasswordOptions Max Length: " + PasswordOptions.MaxLength);
|
|
|
|
Debug.WriteLine("BW LOG, PasswordOptions Require Digits: " + PasswordOptions.RequireDigits);
|
|
|
|
Debug.WriteLine("BW LOG, PasswordOptions Require Symbols: " + PasswordOptions.RequireSymbols);
|
|
|
|
Debug.WriteLine("BW LOG, PasswordOptions Forbidden Chars: " + PasswordOptions.ForbiddenCharacters);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool ProcessWebUrlProvider(NSItemProvider itemProvider)
|
|
|
|
{
|
|
|
|
return ProcessItemProvider(itemProvider, UTType.PropertyList, (dict) =>
|
|
|
|
{
|
2016-05-30 10:08:12 +03:00
|
|
|
var result = dict[NSJavaScriptExtension.PreprocessingResultsKey];
|
|
|
|
if(result == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Url = new Uri(result.ValueForKey(new NSString("url")) as NSString);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool ProcessFindLoginProvider(NSItemProvider itemProvider)
|
|
|
|
{
|
2016-05-31 05:51:53 +03:00
|
|
|
return ProcessItemProvider(itemProvider, UTTypeAppExtensionFindLoginAction, (dict) =>
|
2016-05-30 10:08:12 +03:00
|
|
|
{
|
2016-05-31 05:51:53 +03:00
|
|
|
var version = dict[AppExtensionVersionNumberKey] as NSNumber;
|
|
|
|
var url = dict[AppExtensionUrlStringKey] as NSString;
|
2016-05-30 10:08:12 +03:00
|
|
|
|
2016-05-31 05:51:53 +03:00
|
|
|
if(url != null)
|
2016-05-30 10:08:12 +03:00
|
|
|
{
|
2016-05-31 05:51:53 +03:00
|
|
|
Url = new Uri(url);
|
2016-05-30 10:08:12 +03:00
|
|
|
}
|
2016-05-31 05:51:53 +03:00
|
|
|
});
|
|
|
|
}
|
2016-05-30 10:08:12 +03:00
|
|
|
|
2016-05-31 05:51:53 +03:00
|
|
|
private bool ProcessSaveLoginProvider(NSItemProvider itemProvider)
|
|
|
|
{
|
|
|
|
return ProcessItemProvider(itemProvider, UTTypeAppExtensionSaveLoginAction, (dict) =>
|
|
|
|
{
|
2016-05-30 10:08:12 +03:00
|
|
|
var version = dict[AppExtensionVersionNumberKey] as NSNumber;
|
|
|
|
var url = dict[AppExtensionUrlStringKey] as NSString;
|
2016-05-31 05:51:53 +03:00
|
|
|
var title = dict[AppExtensionTitleKey] as NSString;
|
|
|
|
var sectionTitle = dict[AppExtensionSectionTitleKey] as NSString;
|
|
|
|
var username = dict[AppExtensionUsernameKey] as NSString;
|
|
|
|
var password = dict[AppExtensionPasswordKey] as NSString;
|
|
|
|
var notes = dict[AppExtensionNotesKey] as NSString;
|
|
|
|
var fields = dict[AppExtensionFieldsKey] as NSDictionary;
|
|
|
|
var passwordGenerationOptions = dict[AppExtensionPasswordGeneratorOptionsKey] as NSDictionary;
|
|
|
|
|
|
|
|
if(url != null)
|
2016-05-30 10:08:12 +03:00
|
|
|
{
|
2016-05-31 05:51:53 +03:00
|
|
|
Url = new Uri(url);
|
2016-05-30 10:08:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Url = new Uri(url);
|
2016-05-31 05:51:53 +03:00
|
|
|
SiteTitle = title;
|
|
|
|
Username = username;
|
|
|
|
Password = password;
|
|
|
|
Notes = notes;
|
|
|
|
PasswordOptions = new PasswordGenerationOptions(passwordGenerationOptions);
|
2016-05-30 10:08:12 +03:00
|
|
|
});
|
2016-05-31 05:51:53 +03:00
|
|
|
}
|
2016-05-30 10:08:12 +03:00
|
|
|
|
2016-05-31 05:51:53 +03:00
|
|
|
private bool ProcessChangePasswordProvider(NSItemProvider itemProvider)
|
|
|
|
{
|
|
|
|
return ProcessItemProvider(itemProvider, UTTypeAppExtensionChangePasswordAction, (dict) =>
|
|
|
|
{
|
|
|
|
var version = dict[AppExtensionVersionNumberKey] as NSNumber;
|
|
|
|
var url = dict[AppExtensionUrlStringKey] as NSString;
|
|
|
|
var title = dict[AppExtensionTitleKey] as NSString;
|
|
|
|
var sectionTitle = dict[AppExtensionSectionTitleKey] as NSString;
|
|
|
|
var username = dict[AppExtensionUsernameKey] as NSString;
|
|
|
|
var password = dict[AppExtensionPasswordKey] as NSString;
|
|
|
|
var oldPassword = dict[AppExtensionOldPasswordKey] as NSString;
|
|
|
|
var notes = dict[AppExtensionNotesKey] as NSString;
|
|
|
|
var fields = dict[AppExtensionFieldsKey] as NSDictionary;
|
|
|
|
var passwordGenerationOptions = dict[AppExtensionPasswordGeneratorOptionsKey] as NSDictionary;
|
|
|
|
|
|
|
|
if(url != null)
|
|
|
|
{
|
|
|
|
Url = new Uri(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
SiteTitle = title;
|
|
|
|
Username = username;
|
|
|
|
Password = password;
|
|
|
|
OldPassword = oldPassword;
|
|
|
|
Notes = notes;
|
|
|
|
PasswordOptions = new PasswordGenerationOptions(passwordGenerationOptions);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public class PasswordGenerationOptions
|
|
|
|
{
|
|
|
|
public PasswordGenerationOptions(NSDictionary dict)
|
|
|
|
{
|
|
|
|
if(dict == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(dict));
|
|
|
|
}
|
|
|
|
|
|
|
|
MinLength = (dict[AppExtensionGeneratedPasswordMinLengthKey] as NSNumber)?.Int32Value ?? 0;
|
|
|
|
MaxLength = (dict[AppExtensionGeneratedPasswordMaxLengthKey] as NSNumber)?.Int32Value ?? 0;
|
|
|
|
RequireDigits = (dict[AppExtensionGeneratedPasswordRequireDigitsKey] as NSNumber)?.BoolValue ?? false;
|
|
|
|
RequireSymbols = (dict[AppExtensionGeneratedPasswordRequireSymbolsKey] as NSNumber)?.BoolValue ?? false;
|
|
|
|
ForbiddenCharacters = (dict[AppExtensionGeneratedPasswordForbiddenCharactersKey] as NSString)?.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int MinLength { get; set; }
|
|
|
|
public int MaxLength { get; set; }
|
|
|
|
public bool RequireDigits { get; set; }
|
|
|
|
public bool RequireSymbols { get; set; }
|
|
|
|
public string ForbiddenCharacters { get; set; }
|
2016-05-30 10:08:12 +03:00
|
|
|
}
|
2016-05-07 08:54:44 +03:00
|
|
|
}
|
2016-05-27 05:22:38 +03:00
|
|
|
}
|