2017-02-02 18:02:43 +08:00
|
|
|
//
|
|
|
|
|
// Password.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 2/2/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
2017-03-03 14:45:16 +08:00
|
|
|
import OneTimePassword
|
|
|
|
|
import Base32
|
2017-02-02 18:02:43 +08:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
public class Password {
|
|
|
|
|
|
|
|
|
|
public var name: String
|
|
|
|
|
public var url: URL
|
|
|
|
|
public var plainText: String
|
2017-02-06 22:14:42 +08:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
public var changed: Int = 0
|
|
|
|
|
public var otpType: OtpType = .none
|
2018-07-02 22:13:07 +02:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
private var parser = Parser(plainText: "")
|
|
|
|
|
private var additions = [AdditionField]()
|
|
|
|
|
private var firstLineIsOTPField = false
|
|
|
|
|
private var otpToken: Token? {
|
|
|
|
|
didSet {
|
|
|
|
|
otpType = OtpType(token: otpToken)
|
2018-07-02 22:13:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
2017-04-25 13:01:17 -07:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
public var namePath: String {
|
|
|
|
|
return url.deletingPathExtension().path
|
|
|
|
|
}
|
2018-07-01 19:49:38 +02:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
public var password: String {
|
|
|
|
|
return parser.firstLine
|
|
|
|
|
}
|
2018-07-01 19:49:38 +02:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
public var plainData: Data {
|
|
|
|
|
return plainText.data(using: .utf8)!
|
|
|
|
|
}
|
2017-04-25 13:01:17 -07:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
public var additionsPlainText: String {
|
|
|
|
|
return parser.additionsSection
|
|
|
|
|
}
|
2018-07-02 22:13:07 +02:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
public var username: String? {
|
|
|
|
|
return getAdditionValue(withKey: Constants.USERNAME_KEYWORD)
|
|
|
|
|
}
|
2018-05-05 20:05:03 +02:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
public var login: String? {
|
|
|
|
|
return getAdditionValue(withKey: Constants.LOGIN_KEYWORD)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public var urlString: String? {
|
|
|
|
|
return getAdditionValue(withKey: Constants.URL_KEYWORD)
|
|
|
|
|
}
|
2018-07-02 22:13:07 +02:00
|
|
|
|
2018-12-01 13:27:30 +01:00
|
|
|
public var currentOtp: String? {
|
|
|
|
|
return otpToken?.currentPassword
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-10 22:38:12 -08:00
|
|
|
public init(name: String, url: URL, plainText: String) {
|
|
|
|
|
self.name = name
|
|
|
|
|
self.url = url
|
2018-11-11 18:09:52 +01:00
|
|
|
self.plainText = plainText
|
|
|
|
|
initEverything()
|
2017-03-03 17:12:25 +08:00
|
|
|
}
|
|
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
|
2018-11-10 22:38:12 -08:00
|
|
|
public func updatePassword(name: String, url: URL, plainText: String) {
|
2018-11-11 18:09:52 +01:00
|
|
|
guard self.plainText != plainText || self.url != url else {
|
|
|
|
|
return
|
2017-03-03 17:12:25 +08:00
|
|
|
}
|
2018-07-02 22:13:07 +02:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
if self.plainText != plainText {
|
|
|
|
|
self.plainText = plainText
|
|
|
|
|
changed = changed|PasswordChange.content.rawValue
|
|
|
|
|
}
|
|
|
|
|
if self.url != url {
|
|
|
|
|
self.url = url
|
|
|
|
|
changed = changed|PasswordChange.path.rawValue
|
|
|
|
|
}
|
2018-07-02 22:13:07 +02:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
self.name = name
|
|
|
|
|
initEverything()
|
|
|
|
|
}
|
2018-02-26 16:53:39 +01:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
private func initEverything() {
|
2018-12-01 13:27:30 +01:00
|
|
|
parser = Parser(plainText: plainText)
|
2018-11-11 18:09:52 +01:00
|
|
|
additions = parser.additionFields
|
2018-07-01 19:49:38 +02:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
// Check whether the first line looks like an otp entry.
|
2018-07-01 19:49:38 +02:00
|
|
|
checkPasswordForOtpToken()
|
|
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
// Construct the otp token.
|
2018-07-02 22:13:07 +02:00
|
|
|
updateOtpToken()
|
2017-02-11 16:07:59 +08:00
|
|
|
}
|
2018-07-01 19:49:38 +02:00
|
|
|
|
|
|
|
|
private func checkPasswordForOtpToken() {
|
2018-11-11 18:09:52 +01:00
|
|
|
let (key, value) = Parser.getKeyValuePair(from: password)
|
2018-12-01 13:27:30 +01:00
|
|
|
if let key = key, Constants.OTP_KEYWORDS.contains(key) {
|
2018-07-01 19:49:38 +02:00
|
|
|
firstLineIsOTPField = true
|
2018-12-01 13:27:30 +01:00
|
|
|
additions.append(key => value)
|
2018-07-01 19:49:38 +02:00
|
|
|
} else {
|
|
|
|
|
firstLineIsOTPField = false
|
2018-02-26 16:53:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-05-05 20:05:03 +02:00
|
|
|
|
|
|
|
|
public func getFilteredAdditions() -> [AdditionField] {
|
2018-07-02 22:13:07 +02:00
|
|
|
return additions.filter { field in
|
2018-11-11 18:09:52 +01:00
|
|
|
field.title.lowercased() != Constants.USERNAME_KEYWORD
|
|
|
|
|
&& field.title.lowercased() != Constants.LOGIN_KEYWORD
|
|
|
|
|
&& field.title.lowercased() != Constants.PASSWORD_KEYWORD
|
|
|
|
|
&& (!field.title.hasPrefix(Constants.UNKNOWN) || !SharedDefaults[.isHideUnknownOn])
|
|
|
|
|
&& (!Constants.OTP_KEYWORDS.contains(field.title) || !SharedDefaults[.isHideOTPOn])
|
2017-06-28 00:18:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-11-11 18:09:52 +01:00
|
|
|
|
|
|
|
|
private func getAdditionValue(withKey key: String, caseSensitive: Bool = false) -> String? {
|
2018-12-01 13:27:30 +01:00
|
|
|
let toLowercase = { (string: String) -> String in caseSensitive ? string : string.lowercased() }
|
2018-11-11 18:09:52 +01:00
|
|
|
return additions.first(where: { toLowercase($0.title) == toLowercase(key) })?.content
|
2017-02-02 18:02:43 +08:00
|
|
|
}
|
2017-02-11 22:00:04 +08:00
|
|
|
|
2017-03-03 14:45:16 +08:00
|
|
|
/*
|
|
|
|
|
Set otpType and otpToken, if we are able to construct a valid token.
|
|
|
|
|
|
2017-03-24 22:47:40 +08:00
|
|
|
Example of TOTP otpauth
|
|
|
|
|
(Key Uri Format: https://github.com/google/google-authenticator/wiki/Key-Uri-Format)
|
|
|
|
|
otpauth://totp/totp-secret?secret=AAAAAAAAAAAAAAAA&issuer=totp-secret
|
|
|
|
|
|
|
|
|
|
Example of TOTP fields [Legacy, lower priority]
|
2017-03-03 14:45:16 +08:00
|
|
|
otp_secret: secretsecretsecretsecretsecretsecret
|
|
|
|
|
otp_type: totp
|
2017-03-07 00:52:22 +08:00
|
|
|
otp_algorithm: sha1 (default: sha1, optional)
|
|
|
|
|
otp_period: 30 (default: 30, optional)
|
|
|
|
|
otp_digits: 6 (default: 6, optional)
|
2017-03-03 14:45:16 +08:00
|
|
|
|
2017-03-24 22:47:40 +08:00
|
|
|
Example of HOTP fields [Legacy, lower priority]
|
2017-03-03 14:45:16 +08:00
|
|
|
otp_secret: secretsecretsecretsecretsecretsecret
|
|
|
|
|
otp_type: hotp
|
|
|
|
|
otp_counter: 1
|
2017-03-07 00:52:22 +08:00
|
|
|
otp_digits: 6 (default: 6, optional)
|
2017-03-03 14:45:16 +08:00
|
|
|
|
|
|
|
|
*/
|
2017-03-24 23:14:44 +08:00
|
|
|
private func updateOtpToken() {
|
2017-03-27 23:06:32 +08:00
|
|
|
self.otpToken = nil
|
|
|
|
|
|
2017-03-24 22:47:40 +08:00
|
|
|
// get otpauth, if we are able to generate a token, return
|
2018-11-11 18:09:52 +01:00
|
|
|
if var otpauthString = getAdditionValue(withKey: Constants.OTPAUTH, caseSensitive: true) {
|
|
|
|
|
if !otpauthString.hasPrefix("\(Constants.OTPAUTH):") {
|
|
|
|
|
otpauthString = "\(Constants.OTPAUTH):\(otpauthString)"
|
2017-03-24 22:47:40 +08:00
|
|
|
}
|
|
|
|
|
if let otpauthUrl = URL(string: otpauthString),
|
|
|
|
|
let token = Token(url: otpauthUrl) {
|
|
|
|
|
self.otpToken = token
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-03 14:45:16 +08:00
|
|
|
// get secret data
|
2018-11-14 21:29:25 +01:00
|
|
|
guard let secretString = getAdditionValue(withKey: Constants.OTP_SECRET),
|
2017-03-03 14:45:16 +08:00
|
|
|
let secretData = MF_Base32Codec.data(fromBase32String: secretString),
|
|
|
|
|
!secretData.isEmpty else {
|
2018-11-16 23:08:35 -08:00
|
|
|
// Missing / Invalid otp secret
|
2017-03-03 14:45:16 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get type
|
2018-11-14 21:29:25 +01:00
|
|
|
guard let type = getAdditionValue(withKey: Constants.OTP_TYPE)?.lowercased(),
|
|
|
|
|
(type == Constants.TOTP || type == Constants.HOTP) else {
|
|
|
|
|
// Missing/Invalid OTP type
|
2017-03-07 00:52:22 +08:00
|
|
|
return
|
2017-03-03 14:45:16 +08:00
|
|
|
}
|
|
|
|
|
|
2017-03-07 00:52:22 +08:00
|
|
|
// get algorithm (optional)
|
2017-03-03 14:45:16 +08:00
|
|
|
var algorithm = Generator.Algorithm.sha1
|
2018-11-14 21:29:25 +01:00
|
|
|
if let algoString = getAdditionValue(withKey: Constants.OTP_ALGORITHM) {
|
2017-03-03 14:45:16 +08:00
|
|
|
switch algoString.lowercased() {
|
2018-11-14 21:29:25 +01:00
|
|
|
case Constants.SHA256:
|
2017-03-24 22:47:40 +08:00
|
|
|
algorithm = .sha256
|
2018-11-14 21:29:25 +01:00
|
|
|
case Constants.SHA512:
|
2017-03-24 22:47:40 +08:00
|
|
|
algorithm = .sha512
|
2017-03-03 14:45:16 +08:00
|
|
|
default:
|
2017-03-24 22:47:40 +08:00
|
|
|
algorithm = .sha1
|
2017-03-03 14:45:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// construct the token
|
2018-11-14 21:29:25 +01:00
|
|
|
if type == Constants.TOTP {
|
2017-03-07 00:52:22 +08:00
|
|
|
// HOTP
|
|
|
|
|
// default: 6 digits, 30 seconds
|
2018-11-14 21:29:25 +01:00
|
|
|
guard let digits = Int(getAdditionValue(withKey: Constants.OTP_DIGITS) ?? Constants.DEFAULT_DIGITS),
|
|
|
|
|
let period = Double(getAdditionValue(withKey: Constants.OTP_PERIOD) ?? Constants.DEFAULT_PERIOD) else {
|
|
|
|
|
// Invalid OTP digits or OTP period.
|
2017-03-07 00:52:22 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
guard let generator = Generator(
|
|
|
|
|
factor: .timer(period: period),
|
|
|
|
|
secret: secretData,
|
|
|
|
|
algorithm: algorithm,
|
|
|
|
|
digits: digits) else {
|
2018-11-16 23:08:35 -08:00
|
|
|
// Invalid OTP generator parameters.
|
2017-03-07 00:52:22 +08:00
|
|
|
return
|
2017-03-03 14:45:16 +08:00
|
|
|
}
|
2017-03-07 00:52:22 +08:00
|
|
|
self.otpToken = Token(name: self.name, issuer: "", generator: generator)
|
2017-03-03 14:45:16 +08:00
|
|
|
} else {
|
2017-03-07 00:52:22 +08:00
|
|
|
// HOTP
|
|
|
|
|
// default: 6 digits
|
2018-11-14 21:29:25 +01:00
|
|
|
guard let digits = Int(getAdditionValue(withKey: Constants.OTP_DIGITS) ?? Constants.DEFAULT_DIGITS),
|
|
|
|
|
let counter = UInt64(getAdditionValue(withKey: Constants.OTP_COUNTER) ?? Constants.DEFAULT_COUNTER) else {
|
|
|
|
|
// Invalid OTP digits or OTP counter.
|
2017-03-07 00:52:22 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
guard let generator = Generator(
|
|
|
|
|
factor: .counter(counter),
|
|
|
|
|
secret: secretData,
|
|
|
|
|
algorithm: algorithm,
|
|
|
|
|
digits: digits) else {
|
2018-11-16 23:08:35 -08:00
|
|
|
// Invalid OTP generator parameters.
|
2017-03-07 00:52:22 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
self.otpToken = Token(name: self.name, issuer: "", generator: generator)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 09:50:18 +08:00
|
|
|
// return the description and the password strings
|
2017-06-13 11:42:49 +08:00
|
|
|
public func getOtpStrings() -> (description: String, otp: String)? {
|
2017-03-07 09:50:18 +08:00
|
|
|
guard let token = self.otpToken else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
var description : String
|
|
|
|
|
switch token.generator.factor {
|
|
|
|
|
case .counter:
|
|
|
|
|
// htop
|
|
|
|
|
description = "HMAC-based"
|
|
|
|
|
case .timer(let period):
|
|
|
|
|
// totp
|
|
|
|
|
let timeSinceEpoch = Date().timeIntervalSince1970
|
|
|
|
|
let validTime = Int(period - timeSinceEpoch.truncatingRemainder(dividingBy: period))
|
|
|
|
|
description = "time-based (expiring in \(validTime)s)"
|
|
|
|
|
}
|
|
|
|
|
let otp = self.otpToken?.currentPassword ?? "error"
|
|
|
|
|
return (description, otp)
|
|
|
|
|
}
|
2017-03-24 23:14:44 +08:00
|
|
|
|
2017-04-09 02:54:05 +08:00
|
|
|
// return the password strings
|
|
|
|
|
// it is guaranteed that it is a HOTP password when we call this
|
2017-06-13 11:42:49 +08:00
|
|
|
public func getNextHotp() -> String? {
|
2017-04-09 02:54:05 +08:00
|
|
|
// increase the counter
|
|
|
|
|
otpToken = otpToken?.updatedToken()
|
|
|
|
|
|
|
|
|
|
// replace old HOTP settings with the new otpauth
|
|
|
|
|
var newOtpauth = try! otpToken?.toURL().absoluteString
|
|
|
|
|
newOtpauth?.append("&secret=")
|
|
|
|
|
newOtpauth?.append(MF_Base32Codec.base32String(from: otpToken?.generator.secret))
|
|
|
|
|
|
|
|
|
|
var lines : [String] = []
|
|
|
|
|
self.plainText.enumerateLines() { line, _ in
|
2018-11-11 18:09:52 +01:00
|
|
|
let (key, _) = Parser.getKeyValuePair(from: line)
|
|
|
|
|
if !Constants.OTP_KEYWORDS.contains(key ?? "") {
|
2017-04-09 02:54:05 +08:00
|
|
|
lines.append(line)
|
2018-11-11 18:09:52 +01:00
|
|
|
} else if key == Constants.OTPAUTH && newOtpauth != nil {
|
2017-04-09 02:54:05 +08:00
|
|
|
lines.append(newOtpauth!)
|
|
|
|
|
// set to nil to prevent duplication
|
|
|
|
|
newOtpauth = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if newOtpauth != nil {
|
|
|
|
|
lines.append(newOtpauth!)
|
|
|
|
|
}
|
2017-04-23 10:03:09 -07:00
|
|
|
self.updatePassword(name: self.name, url: self.url, plainText: lines.joined(separator: "\n"))
|
2017-04-09 02:54:05 +08:00
|
|
|
|
|
|
|
|
// get and return the password
|
|
|
|
|
return self.otpToken?.currentPassword
|
|
|
|
|
}
|
2017-02-02 18:02:43 +08:00
|
|
|
}
|