Update time-based one time passwords automatically

- Improve how we differ between TOTP and HTOP
This commit is contained in:
Yishi Lin 2017-03-05 02:54:36 +08:00
parent e48bc5a7a4
commit 2f1721a7e7
2 changed files with 49 additions and 10 deletions

View file

@ -16,6 +16,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
var passwordCategoryText = "" var passwordCategoryText = ""
var password: Password? var password: Password?
var passwordImage: UIImage? var passwordImage: UIImage?
var oneTimePasswordIndexPath : IndexPath?
let indicatorLable: UILabel = { let indicatorLable: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 21)) let label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 21))
@ -116,6 +117,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
self.present(alert, animated: true, completion: nil) self.present(alert, animated: true, completion: nil)
} }
self.setupUdateOneTimePassword()
} }
@ -157,6 +159,35 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
} }
} }
func setupUdateOneTimePassword() {
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {
[weak self] timer in
// bail out of the timer code if the object has been freed
guard let strongSelf = self,
let token = strongSelf.password?.otpToken,
let indexPath = strongSelf.oneTimePasswordIndexPath,
let cell = strongSelf.tableView.cellForRow(at: indexPath) as? LabelTableViewCell else {
return
}
switch token.generator.factor {
case .counter:
// htop
break
case .timer(let period):
// totp
let timeSinceEpoch = Date().timeIntervalSince1970
let validTime = Int(period - timeSinceEpoch.truncatingRemainder(dividingBy: period))
strongSelf.tableData[indexPath.section].item[indexPath.row].title = "time-based (expiring in \(validTime)s)"
cell.cellData?.title = "time-based (valid within \(validTime)s)"
if validTime <= 1 || validTime >= Int(period - 1) {
let otp = token.currentPassword ?? "error"
strongSelf.tableData[indexPath.section].item[indexPath.row].content = otp
cell.cellData?.content = otp
}
}
}
}
func pressEdit(_ sender: Any?) { func pressEdit(_ sender: Any?) {
print("pressEdit") print("pressEdit")
performSegue(withIdentifier: "editPasswordSegue", sender: self) performSegue(withIdentifier: "editPasswordSegue", sender: self)
@ -200,16 +231,26 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
} }
self.tableData[tableDataIndex].item.append(TableCell(title: "password", content: password.password)) self.tableData[tableDataIndex].item.append(TableCell(title: "password", content: password.password))
// Show one time password // show one time password
if password.otpType == "totp", password.otpToken != nil { if let token = password.otpToken {
self.tableData.append(TableSection(title: "One time password (TOTP)", item: [])) switch token.generator.factor {
tableDataIndex += 1 case .counter(_):
if let crtPassword = password.otpToken?.currentPassword { // counter-based one time password
self.tableData[tableDataIndex].item.append(TableCell(title: "current", content: crtPassword)) break
case .timer(let period):
// time-based one time password
self.tableData.append(TableSection(title: "One time password", item: []))
tableDataIndex += 1
oneTimePasswordIndexPath = IndexPath(row: 0, section: tableDataIndex)
if let crtPassword = password.otpToken?.currentPassword {
let timeSinceEpoch = Date().timeIntervalSince1970
let validTime = Int(period - timeSinceEpoch.truncatingRemainder(dividingBy: period))
self.tableData[tableDataIndex].item.append(TableCell(title: "time-based (expiring in \(validTime)s)", content: crtPassword))
}
} }
} }
// Show additional information // show additional information
let filteredAdditionKeys = password.additionKeys.filter { let filteredAdditionKeys = password.additionKeys.filter {
$0.lowercased() != "username" && $0.lowercased() != "username" &&
$0.lowercased() != "password" && $0.lowercased() != "password" &&

View file

@ -26,7 +26,6 @@ class Password {
var plainText = "" var plainText = ""
var changed = false var changed = false
var firstLineIsOTPField = false var firstLineIsOTPField = false
var otpType: String?
var otpToken: Token? var otpToken: Token?
init(name: String, plainText: String) { init(name: String, plainText: String) {
@ -198,7 +197,6 @@ class Password {
print("Invalid generator parameters \(self.plainText)") print("Invalid generator parameters \(self.plainText)")
return return
} }
self.otpType = "totp"
self.otpToken = Token(name: self.name, issuer: "", generator: generator) self.otpToken = Token(name: self.name, issuer: "", generator: generator)
} }
} else { } else {