Polish codes about OTP

This commit is contained in:
Yishi Lin 2017-03-24 23:14:44 +08:00
parent f5875c519c
commit 0f9b95eaa9
2 changed files with 28 additions and 30 deletions

View file

@ -163,14 +163,14 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
[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 otpType = strongSelf.password?.otpType,
otpType != .none,
let indexPath = strongSelf.oneTimePasswordIndexPath,
let cell = strongSelf.tableView.cellForRow(at: indexPath) as? LabelTableViewCell else {
return
}
switch token.generator.factor {
case .timer:
// totp
switch otpType {
case .totp:
if let (title, otp) = strongSelf.password?.getOtpStrings() {
strongSelf.tableData[indexPath.section].item[indexPath.row].title = title
strongSelf.tableData[indexPath.section].item[indexPath.row].content = otp
@ -235,26 +235,12 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
self.tableData[tableDataIndex].item.append(TableCell(title: "password", content: password.password))
// show one time password
if let token = password.otpToken {
switch token.generator.factor {
case .counter(_):
// counter-based one time password
if password.otpType != .none {
if let (title, otp) = self.password?.getOtpStrings() {
self.tableData.append(TableSection(title: "One time password", item: []))
tableDataIndex += 1
oneTimePasswordIndexPath = IndexPath(row: 0, section: tableDataIndex)
if let crtPassword = password.otpToken?.currentPassword {
self.tableData[tableDataIndex].item.append(TableCell(title: "HMAC-based", content: crtPassword))
}
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))
}
self.tableData[tableDataIndex].item.append(TableCell(title: title, content: otp))
}
}
@ -354,7 +340,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
password!.increaseHotpCounter()
// copy HOTP to pasteboard
if let plainPassword = password!.otpToken?.currentPassword {
if let plainPassword = password!.getOtp() {
Utils.copyToPasteboard(textToCopy: plainPassword)
}

View file

@ -23,10 +23,11 @@ class Password {
var password = ""
var additions = [String: String]()
var additionKeys = [String]()
var plainText = ""
var changed = false
var firstLineIsOTPField = false
var otpToken: Token?
private var plainText = ""
private var firstLineIsOTPField = false
private var otpToken: Token?
enum OtpType {
case totp, hotp, none
@ -57,7 +58,7 @@ class Password {
}
}
func initEverything(name: String, plainText: String) {
private func initEverything(name: String, plainText: String) {
self.name = name
self.plainText = plainText
@ -97,7 +98,7 @@ class Password {
// return a key-value pair from the line
// key might be nil, if there is no ":" in the line
static func getKeyValuePair(from line: String) -> (key: String?, value: String) {
static private func getKeyValuePair(from line: String) -> (key: String?, value: String) {
let items = line.characters.split(separator: ":", maxSplits: 1, omittingEmptySubsequences: true).map(String.init)
var key : String?
var value = ""
@ -110,7 +111,7 @@ class Password {
return (key, value)
}
static func getAdditionFields(from additionFieldsPlainText: String) -> ([String: String], [String]){
static private func getAdditionFields(from additionFieldsPlainText: String) -> ([String: String], [String]){
var additions = [String: String]()
var additionKeys = [String]()
var unknownIndex = 0
@ -143,7 +144,7 @@ class Password {
}
}
func getPlainText() -> String {
private func getPlainText() -> String {
return self.plainText
}
@ -176,7 +177,7 @@ class Password {
otp_digits: 6 (default: 6, optional)
*/
func updateOtpToken() {
private func updateOtpToken() {
// get otpauth, if we are able to generate a token, return
if var otpauthString = getAdditionValue(withKey: "otpauth") {
if !otpauthString.hasPrefix("otpauth:") {
@ -293,4 +294,15 @@ class Password {
let otp = self.otpToken?.currentPassword ?? "error"
return (description, otp)
}
// return the password strings
func getOtp() -> String? {
if let otp = self.otpToken?.currentPassword {
return otp
} else {
return nil
}
}
}