Fix the "clearing passwords in 45s" function

- Copied passwords in the extension are not cleaned.
This commit is contained in:
Yishi Lin 2017-07-27 23:56:24 +08:00
parent f86a5eee65
commit a4efe57db9
7 changed files with 66 additions and 13 deletions

View file

@ -391,7 +391,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
// copy HOTP to pasteboard (will update counter)
if let plainPassword = password!.getNextHotp() {
Utils.copyToPasteboard(textToCopy: plainPassword)
SecurePasteboard.shared.copy(textToCopy: plainPassword)
}
// commit the change of HOTP counter
@ -413,7 +413,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
}
return;
}
Utils.copyToPasteboard(textToCopy: password?.password)
SecurePasteboard.shared.copy(textToCopy: password?.password)
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
@ -480,7 +480,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
override func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
if action == #selector(copy(_:)) {
Utils.copyToPasteboard(textToCopy: tableData[indexPath.section].item[indexPath.row].content)
SecurePasteboard.shared.copy(textToCopy: tableData[indexPath.section].item[indexPath.row].content)
}
}

View file

@ -189,7 +189,7 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl
}
let length = passwordLengthCell?.roundedValue ?? 0
let plainPassword = Utils.generatePassword(length: length)
Utils.copyToPasteboard(textToCopy: plainPassword)
SecurePasteboard.shared.copy(textToCopy: plainPassword)
// update tableData so to make sure reloadData() works correctly
tableData[passwordSection][0][PasswordEditorCellKey.content] = plainPassword

View file

@ -370,7 +370,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
do {
decryptedPassword = try self.passwordStore.decrypt(passwordEntity: passwordEntity, requestPGPKeyPassphrase: self.requestPGPKeyPassphrase)
DispatchQueue.main.async {
Utils.copyToPasteboard(textToCopy: decryptedPassword?.password)
SecurePasteboard.shared.copy(textToCopy: decryptedPassword?.password)
SVProgressHUD.showSuccess(withStatus: "Password copied, and will be cleared in 45 seconds.")
SVProgressHUD.dismiss(withDelay: 0.6)
}

View file

@ -0,0 +1,55 @@
//
// SecurePasteboard.swift
// pass
//
// Created by Yishi Lin on 2017/7/27.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import Foundation
import UIKit
class SecurePasteboard {
public static let shared = SecurePasteboard()
private var backgroundTaskID: UIBackgroundTaskIdentifier? = nil
func copy(textToCopy: String?, expirationTime: Double = 45) {
// copy to the pasteboard
UIPasteboard.general.string = textToCopy ?? ""
// clean the pasteboard after expirationTime
guard expirationTime > 0 else {
return
}
// exit the existing background task, if any
if let backgroundTaskID = backgroundTaskID {
UIApplication.shared.endBackgroundTask(backgroundTaskID)
self.backgroundTaskID = nil
}
backgroundTaskID = UIApplication.shared.beginBackgroundTask(expirationHandler: { [weak self] in
guard let taskID = self?.backgroundTaskID else {
return
}
if textToCopy == UIPasteboard.general.string {
UIPasteboard.general.string = ""
}
UIApplication.shared.endBackgroundTask(taskID)
})
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + expirationTime) { [weak self] in
guard let strongSelf = self else {
return
}
if textToCopy == UIPasteboard.general.string {
UIPasteboard.general.string = ""
}
if let backgroundTaskID = strongSelf.backgroundTaskID {
UIApplication.shared.endBackgroundTask(backgroundTaskID)
strongSelf.backgroundTaskID = nil
}
}
}
}

View file

@ -101,7 +101,7 @@ class LabelTableViewCell: UITableViewCell {
}
override func copy(_ sender: Any?) {
Utils.copyToPasteboard(textToCopy: cellData?.content)
SecurePasteboard.shared.copy(textToCopy: cellData?.content)
}
func revealPassword(_ sender: Any?) {