Update OTP token generation and support HOTP

- Update logic: Only fields otp_secret and otp_type are required for TOTP. Only fields otp_secret, otp_type and otp_counter are required for HOTP. Other fields (i.e., otp_algorithm, otp_digits, otp_period) are optional.
- Support HOTP: (1) passwords are initially concealed; (2) "tap->next" generates a new password and commits the updated password file automatically
This commit is contained in:
Yishi Lin 2017-03-07 00:52:22 +08:00
parent 9580978434
commit 0dccd911fd
3 changed files with 135 additions and 31 deletions

View file

@ -7,6 +7,7 @@
//
import UIKit
import SVProgressHUD
struct LabelTableViewCellData {
@ -22,15 +23,27 @@ class LabelTableViewCell: UITableViewCell {
var isPasswordCell = false
var isURLCell = false
var isReveal = false
var password: Password?
var isHOTPCell = false
let passwordDots = "••••••••••••"
weak var passwordTableView : PasswordDetailTableViewController?
var cellData: LabelTableViewCellData? {
didSet {
titleLabel.text = cellData?.title
titleLabel.text = cellData?.title ?? ""
if isPasswordCell {
contentLabel.text = passwordDots
if isReveal {
contentLabel.attributedText = Utils.attributedPassword(plainPassword: cellData?.content ?? "")
} else {
contentLabel.text = passwordDots
}
contentLabel.font = UIFont(name: "Menlo", size: contentLabel.font.pointSize)
} else if isHOTPCell {
if isReveal {
contentLabel.text = cellData?.content ?? ""
} else {
contentLabel.text = passwordDots
}
} else {
contentLabel.text = cellData?.content
}
@ -62,6 +75,13 @@ class LabelTableViewCell: UITableViewCell {
if isURLCell {
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.openLink(_:))
}
if isHOTPCell {
if isReveal {
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.concealPassword(_:)) || action == #selector(LabelTableViewCell.nextPassword(_:))
} else {
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.revealPassword(_:)) || action == #selector(LabelTableViewCell.nextPassword(_:))
}
}
return action == #selector(copy(_:))
}
@ -71,7 +91,11 @@ class LabelTableViewCell: UITableViewCell {
func revealPassword(_ sender: Any?) {
if let plainPassword = cellData?.content {
contentLabel.attributedText = Utils.attributedPassword(plainPassword: plainPassword)
if isHOTPCell {
contentLabel.text = plainPassword
} else {
contentLabel.attributedText = Utils.attributedPassword(plainPassword: plainPassword)
}
} else {
contentLabel.text = ""
}
@ -83,8 +107,45 @@ class LabelTableViewCell: UITableViewCell {
isReveal = false
}
func nextPassword(_ sender: Any?) {
guard let password = passwordTableView?.password,
let passwordEntity = passwordTableView?.passwordEntity else {
print("Cannot find password/passwordEntity of a cell")
return;
}
// increase HOTP counter
password.increaseHotpCounter()
// only the HOTP password needs update
if let plainPassword = password.otpToken?.currentPassword {
cellData?.content = plainPassword
// contentLabel will be updated automatically
}
// commit
if password.changed {
DispatchQueue.global(qos: .userInitiated).async {
PasswordStore.shared.update(passwordEntity: passwordEntity, password: password, progressBlock: {_ in })
DispatchQueue.main.async {
passwordEntity.synced = false
PasswordStore.shared.saveUpdated(passwordEntity: passwordEntity)
NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated")))
// reload so that the "unsynced" symbol could be added
self.passwordTableView?.tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: UITableViewRowAnimation.automatic)
SVProgressHUD.showSuccess(withStatus: "Password Copied\nCounter Updated")
SVProgressHUD.dismiss(withDelay: 1)
}
}
}
}
func openLink(_ sender: Any?) {
Utils.copyToPasteboard(textToCopy: password?.password)
guard let password = passwordTableView?.password else {
print("Cannot find password of a cell")
return;
}
Utils.copyToPasteboard(textToCopy: password.password)
UIApplication.shared.open(URL(string: cellData!.content)!, options: [:], completionHandler: nil)
}
}