Provide basic support about one time password (totp only)
This commit is contained in:
parent
71cb9a02c0
commit
3dac155d6c
2 changed files with 95 additions and 0 deletions
|
|
@ -199,6 +199,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
||||||
self.tableData[tableDataIndex].item.append(TableCell(title: "username", content: username))
|
self.tableData[tableDataIndex].item.append(TableCell(title: "username", content: username))
|
||||||
}
|
}
|
||||||
self.tableData[tableDataIndex].item.append(TableCell(title: "password", content: password.password))
|
self.tableData[tableDataIndex].item.append(TableCell(title: "password", content: password.password))
|
||||||
|
// Show additional information
|
||||||
if password.additions.count > 0 {
|
if password.additions.count > 0 {
|
||||||
self.tableData.append(TableSection(title: "additions", item: []))
|
self.tableData.append(TableSection(title: "additions", item: []))
|
||||||
tableDataIndex += 1
|
tableDataIndex += 1
|
||||||
|
|
@ -212,6 +213,14 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Show one time password
|
||||||
|
if password.otpType == "totp", password.otpToken != nil {
|
||||||
|
self.tableData.append(TableSection(title: "One time password (TOTP)", item: []))
|
||||||
|
tableDataIndex += 1
|
||||||
|
if let crtPassword = password.otpToken?.currentPassword {
|
||||||
|
self.tableData[tableDataIndex].item.append(TableCell(title: "current", content: crtPassword))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import SwiftyUserDefaults
|
import SwiftyUserDefaults
|
||||||
|
import OneTimePassword
|
||||||
|
import Base32
|
||||||
|
|
||||||
struct AdditionField {
|
struct AdditionField {
|
||||||
var title: String
|
var title: String
|
||||||
|
|
@ -15,12 +17,16 @@ struct AdditionField {
|
||||||
}
|
}
|
||||||
|
|
||||||
class Password {
|
class Password {
|
||||||
|
static let otpKeywords = ["otp_secret", "otp_type", "otp_algorithm", "otp_period", "otp_digits", "otp_counter"]
|
||||||
|
|
||||||
var name = ""
|
var name = ""
|
||||||
var password = ""
|
var password = ""
|
||||||
var additions = [String: String]()
|
var additions = [String: String]()
|
||||||
var additionKeys = [String]()
|
var additionKeys = [String]()
|
||||||
var plainText = ""
|
var plainText = ""
|
||||||
var changed = false
|
var changed = false
|
||||||
|
var otpType: String?
|
||||||
|
var otpToken: Token?
|
||||||
|
|
||||||
init(name: String, plainText: String) {
|
init(name: String, plainText: String) {
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
@ -32,6 +38,16 @@ class Password {
|
||||||
}.map(String.init)
|
}.map(String.init)
|
||||||
self.password = plainTextSplit[0]
|
self.password = plainTextSplit[0]
|
||||||
(self.additions, self.additionKeys) = Password.getAdditionFields(from: plainTextSplit[1])
|
(self.additions, self.additionKeys) = Password.getAdditionFields(from: plainTextSplit[1])
|
||||||
|
|
||||||
|
// check whether the first line of the plainText looks like an otp entry
|
||||||
|
let (key, value) = Password.getKeyValuePair(from: plainTextSplit[0])
|
||||||
|
if key != nil && Password.otpKeywords.contains(key!) {
|
||||||
|
self.additions[key!] = value
|
||||||
|
self.additionKeys.append(key!)
|
||||||
|
}
|
||||||
|
|
||||||
|
// construct the otp token
|
||||||
|
self.updateOtpToken()
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUsername() -> String? {
|
func getUsername() -> String? {
|
||||||
|
|
@ -91,6 +107,9 @@ class Password {
|
||||||
self.password = plainTextSplit[0]
|
self.password = plainTextSplit[0]
|
||||||
(self.additions, self.additionKeys) = Password.getAdditionFields(from: plainTextSplit[1])
|
(self.additions, self.additionKeys) = Password.getAdditionFields(from: plainTextSplit[1])
|
||||||
|
|
||||||
|
// construct the otp token
|
||||||
|
self.updateOtpToken()
|
||||||
|
|
||||||
changed = true
|
changed = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -118,4 +137,71 @@ class Password {
|
||||||
return self.additions[key]
|
return self.additions[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Set otpType and otpToken, if we are able to construct a valid token.
|
||||||
|
|
||||||
|
Example of TOTP fields
|
||||||
|
otp_secret: secretsecretsecretsecretsecretsecret
|
||||||
|
otp_type: totp
|
||||||
|
otp_algorithm: sha1
|
||||||
|
otp_period: 30
|
||||||
|
otp_digits: 6
|
||||||
|
|
||||||
|
Example of HOTP fields
|
||||||
|
otp_secret: secretsecretsecretsecretsecretsecret
|
||||||
|
otp_type: hotp
|
||||||
|
otp_counter: 1
|
||||||
|
otp_digits: 6
|
||||||
|
|
||||||
|
*/
|
||||||
|
func updateOtpToken() {
|
||||||
|
// get secret data
|
||||||
|
guard let secretString = getAdditionValue(withKey: "otp_secret"),
|
||||||
|
let secretData = MF_Base32Codec.data(fromBase32String: secretString),
|
||||||
|
!secretData.isEmpty else {
|
||||||
|
// print("Missing / Invalid otp secret")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// get type
|
||||||
|
guard let type = getAdditionValue(withKey: "otp_type")?.lowercased(),
|
||||||
|
(type == "totp" || type == "hotp") else {
|
||||||
|
// print("Missing / Invalid otp type")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// get algorithm
|
||||||
|
var algorithm = Generator.Algorithm.sha1
|
||||||
|
if let algoString = getAdditionValue(withKey: "otp_algorithm") {
|
||||||
|
switch algoString.lowercased() {
|
||||||
|
case "sha1":
|
||||||
|
algorithm = Generator.Algorithm.sha1
|
||||||
|
case "sha256":
|
||||||
|
algorithm = Generator.Algorithm.sha256
|
||||||
|
case "sha512":
|
||||||
|
algorithm = Generator.Algorithm.sha512
|
||||||
|
default:
|
||||||
|
algorithm = Generator.Algorithm.sha1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// construct the token
|
||||||
|
if type == "totp" {
|
||||||
|
if let digits = Int(getAdditionValue(withKey: "otp_digits") ?? ""),
|
||||||
|
let period = Double(getAdditionValue(withKey: "otp_period") ?? "") {
|
||||||
|
guard let generator = Generator(
|
||||||
|
factor: .timer(period: period),
|
||||||
|
secret: secretData,
|
||||||
|
algorithm: algorithm,
|
||||||
|
digits: digits) else {
|
||||||
|
print("Invalid generator parameters \(self.plainText)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.otpType = "totp"
|
||||||
|
self.otpToken = Token(name: self.name, issuer: "", generator: generator)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
print("We do not support HOTP currently.")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue