Exemplo n.º 1
0
		/// <summary>
		/// Unprotected an authenticator (if possible)
		/// </summary>
		/// <param name="item">item to unprotect</param>
		/// <param name="screen">screen to display dialog for multi-monitors</param>
		/// <returns></returns>
		private DialogResult UnprotectAuthenticator(AuthenticatorListitem item, Screen screen = null)
		{
			// keep a count so we can have multiples
			if (item.UnprotectCount > 0)
			{
				item.UnprotectCount++;
				return DialogResult.OK;
			}

			// if there is no protection return None
			WinAuthAuthenticator auth = item.Authenticator;
			if (auth.AuthenticatorData == null || auth.AuthenticatorData.RequiresPassword == false)
			{
				return DialogResult.None;
			}

			// request the password
			UnprotectPasswordForm getPassForm = new UnprotectPasswordForm();
			getPassForm.Authenticator = auth;
			if (screen != null)
			{
				// center on the current windows screen (in case of multiple monitors)
				getPassForm.StartPosition = FormStartPosition.Manual;
				int left = (screen.Bounds.Width / 2) - (getPassForm.Width / 2) + screen.Bounds.Left;
				int top = (screen.Bounds.Height / 2) - (getPassForm.Height / 2) + screen.Bounds.Top;
				getPassForm.Location = new Point(left, top);
			}
			else
			{
				getPassForm.StartPosition = FormStartPosition.CenterScreen;
			}
			DialogResult result = getPassForm.ShowDialog(this.Parent as Form);
			if (result == DialogResult.OK)
			{
				item.UnprotectCount++;
			}

			return result;
		}
Exemplo n.º 2
0
		public static void ExportAuthenticators(Form form, IList<WinAuthAuthenticator> authenticators, string file, string password, string pgpKey)
		{
			// create file in memory
			using (var ms = new MemoryStream())
			{
				using (var sw = new StreamWriter(ms))
				{
					List<WinAuthAuthenticator> unprotected = new List<WinAuthAuthenticator>();
					foreach (var auth in authenticators)
					{
						// unprotect if necessary
						if (auth.AuthenticatorData.RequiresPassword == true)
						{
							// request the password
							UnprotectPasswordForm getPassForm = new UnprotectPasswordForm();
							getPassForm.Authenticator = auth;
							DialogResult result = getPassForm.ShowDialog(form);
							if (result == DialogResult.OK)
							{
								unprotected.Add(auth);
							}
							else
							{
								continue;
							}
						}

						string line = auth.ToUrl();
						sw.WriteLine(line);
					}

					// reprotect
					foreach (var auth in unprotected)
					{
						auth.AuthenticatorData.Protect();
					}

					// reset and write stream out to disk or as zip
					sw.Flush();
					ms.Seek(0, SeekOrigin.Begin);

					// reset and write stream out to disk or as zip
					if (string.Compare(Path.GetExtension(file), ".zip", true) == 0)
					{
						using (var zip = new ZipOutputStream(new FileStream(file, FileMode.Create, FileAccess.Write)))
						{
							if (string.IsNullOrEmpty(password) == false)
							{
								zip.Password = password;
							}

							zip.IsStreamOwner = true;

							ZipEntry entry = new ZipEntry(ZipEntry.CleanName(Path.GetFileNameWithoutExtension(file) + ".txt"));
							entry.DateTime = DateTime.Now;
							zip.UseZip64 = UseZip64.Off;

							zip.PutNextEntry(entry);

							byte[] buffer = new byte[4096];
							StreamUtils.Copy(ms, zip, buffer);

							zip.CloseEntry();
						}
					}
					else if (string.IsNullOrEmpty(pgpKey) == false)
					{
						using (var sr = new StreamReader(ms))
						{
							var plain = sr.ReadToEnd();
							string encoded = PGPEncrypt(plain, pgpKey);

							File.WriteAllText(file, encoded);
						}
					}
					else
					{
						using (var fs = new FileStream(file, FileMode.Create, FileAccess.Write))
						{
							byte[] buffer = new byte[4096];
							StreamUtils.Copy(ms, fs, buffer);
						}
					}
				}
			}
		}