Polish codes about OTP
This commit is contained in:
parent
f5875c519c
commit
0f9b95eaa9
2 changed files with 28 additions and 30 deletions
|
|
@ -163,14 +163,14 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
||||||
[weak self] timer in
|
[weak self] timer in
|
||||||
// bail out of the timer code if the object has been freed
|
// bail out of the timer code if the object has been freed
|
||||||
guard let strongSelf = self,
|
guard let strongSelf = self,
|
||||||
let token = strongSelf.password?.otpToken,
|
let otpType = strongSelf.password?.otpType,
|
||||||
|
otpType != .none,
|
||||||
let indexPath = strongSelf.oneTimePasswordIndexPath,
|
let indexPath = strongSelf.oneTimePasswordIndexPath,
|
||||||
let cell = strongSelf.tableView.cellForRow(at: indexPath) as? LabelTableViewCell else {
|
let cell = strongSelf.tableView.cellForRow(at: indexPath) as? LabelTableViewCell else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch token.generator.factor {
|
switch otpType {
|
||||||
case .timer:
|
case .totp:
|
||||||
// totp
|
|
||||||
if let (title, otp) = strongSelf.password?.getOtpStrings() {
|
if let (title, otp) = strongSelf.password?.getOtpStrings() {
|
||||||
strongSelf.tableData[indexPath.section].item[indexPath.row].title = title
|
strongSelf.tableData[indexPath.section].item[indexPath.row].title = title
|
||||||
strongSelf.tableData[indexPath.section].item[indexPath.row].content = otp
|
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))
|
self.tableData[tableDataIndex].item.append(TableCell(title: "password", content: password.password))
|
||||||
|
|
||||||
// show one time password
|
// show one time password
|
||||||
if let token = password.otpToken {
|
if password.otpType != .none {
|
||||||
switch token.generator.factor {
|
if let (title, otp) = self.password?.getOtpStrings() {
|
||||||
case .counter(_):
|
|
||||||
// counter-based one time password
|
|
||||||
self.tableData.append(TableSection(title: "One time password", item: []))
|
self.tableData.append(TableSection(title: "One time password", item: []))
|
||||||
tableDataIndex += 1
|
tableDataIndex += 1
|
||||||
oneTimePasswordIndexPath = IndexPath(row: 0, section: tableDataIndex)
|
oneTimePasswordIndexPath = IndexPath(row: 0, section: tableDataIndex)
|
||||||
if let crtPassword = password.otpToken?.currentPassword {
|
self.tableData[tableDataIndex].item.append(TableCell(title: title, content: otp))
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -354,7 +340,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
||||||
password!.increaseHotpCounter()
|
password!.increaseHotpCounter()
|
||||||
|
|
||||||
// copy HOTP to pasteboard
|
// copy HOTP to pasteboard
|
||||||
if let plainPassword = password!.otpToken?.currentPassword {
|
if let plainPassword = password!.getOtp() {
|
||||||
Utils.copyToPasteboard(textToCopy: plainPassword)
|
Utils.copyToPasteboard(textToCopy: plainPassword)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,10 +23,11 @@ class Password {
|
||||||
var password = ""
|
var password = ""
|
||||||
var additions = [String: String]()
|
var additions = [String: String]()
|
||||||
var additionKeys = [String]()
|
var additionKeys = [String]()
|
||||||
var plainText = ""
|
|
||||||
var changed = false
|
var changed = false
|
||||||
var firstLineIsOTPField = false
|
|
||||||
var otpToken: Token?
|
private var plainText = ""
|
||||||
|
private var firstLineIsOTPField = false
|
||||||
|
private var otpToken: Token?
|
||||||
|
|
||||||
enum OtpType {
|
enum OtpType {
|
||||||
case totp, hotp, none
|
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.name = name
|
||||||
self.plainText = plainText
|
self.plainText = plainText
|
||||||
|
|
||||||
|
|
@ -97,7 +98,7 @@ class Password {
|
||||||
|
|
||||||
// return a key-value pair from the line
|
// return a key-value pair from the line
|
||||||
// key might be nil, if there is no ":" in 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)
|
let items = line.characters.split(separator: ":", maxSplits: 1, omittingEmptySubsequences: true).map(String.init)
|
||||||
var key : String?
|
var key : String?
|
||||||
var value = ""
|
var value = ""
|
||||||
|
|
@ -110,7 +111,7 @@ class Password {
|
||||||
return (key, value)
|
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 additions = [String: String]()
|
||||||
var additionKeys = [String]()
|
var additionKeys = [String]()
|
||||||
var unknownIndex = 0
|
var unknownIndex = 0
|
||||||
|
|
@ -143,7 +144,7 @@ class Password {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPlainText() -> String {
|
private func getPlainText() -> String {
|
||||||
return self.plainText
|
return self.plainText
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -176,7 +177,7 @@ class Password {
|
||||||
otp_digits: 6 (default: 6, optional)
|
otp_digits: 6 (default: 6, optional)
|
||||||
|
|
||||||
*/
|
*/
|
||||||
func updateOtpToken() {
|
private func updateOtpToken() {
|
||||||
// get otpauth, if we are able to generate a token, return
|
// get otpauth, if we are able to generate a token, return
|
||||||
if var otpauthString = getAdditionValue(withKey: "otpauth") {
|
if var otpauthString = getAdditionValue(withKey: "otpauth") {
|
||||||
if !otpauthString.hasPrefix("otpauth:") {
|
if !otpauthString.hasPrefix("otpauth:") {
|
||||||
|
|
@ -293,4 +294,15 @@ class Password {
|
||||||
let otp = self.otpToken?.currentPassword ?? "error"
|
let otp = self.otpToken?.currentPassword ?? "error"
|
||||||
return (description, otp)
|
return (description, otp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return the password strings
|
||||||
|
func getOtp() -> String? {
|
||||||
|
if let otp = self.otpToken?.currentPassword {
|
||||||
|
return otp
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue