2017-02-02 18:02:43 +08:00
|
|
|
//
|
|
|
|
|
// Password.swift
|
2021-08-28 07:32:31 +02:00
|
|
|
// passKit
|
2017-02-02 18:02:43 +08:00
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 2/2/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
2017-03-03 14:45:16 +08:00
|
|
|
import Base32
|
2020-06-28 21:25:40 +02:00
|
|
|
import OneTimePassword
|
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
|
2020-04-19 15:41:30 +02:00
|
|
|
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
|
2018-12-15 21:49:18 +01:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
private var otpToken: Token? {
|
|
|
|
|
didSet {
|
2020-04-19 15:41:30 +02:00
|
|
|
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 {
|
2020-06-28 21:25:40 +02:00
|
|
|
url.deletingPathExtension().path
|
2018-11-11 18:09:52 +01:00
|
|
|
}
|
2018-07-01 19:49:38 +02:00
|
|
|
|
2019-05-17 17:30:41 +02:00
|
|
|
public var nameFromPath: String? {
|
2020-06-28 21:25:40 +02:00
|
|
|
url.deletingPathExtension().path.split(separator: "/").last.map { String($0) }
|
2019-05-17 17:30:41 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
public var password: String {
|
2020-06-28 21:25:40 +02:00
|
|
|
parser.firstLine
|
2018-11-11 18:09:52 +01:00
|
|
|
}
|
2018-07-01 19:49:38 +02:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
public var plainData: Data {
|
2020-06-28 21:25:40 +02:00
|
|
|
plainText.data(using: .utf8)!
|
2018-11-11 18:09:52 +01:00
|
|
|
}
|
2017-04-25 13:01:17 -07:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
public var additionsPlainText: String {
|
2020-06-28 21:25:40 +02:00
|
|
|
parser.additionsSection
|
2018-11-11 18:09:52 +01:00
|
|
|
}
|
2018-07-02 22:13:07 +02:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
public var username: String? {
|
2020-06-28 21:25:40 +02:00
|
|
|
getAdditionValue(withKey: Constants.USERNAME_KEYWORD)
|
2018-11-11 18:09:52 +01:00
|
|
|
}
|
2018-05-05 20:05:03 +02:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
public var login: String? {
|
2020-06-28 21:25:40 +02:00
|
|
|
getAdditionValue(withKey: Constants.LOGIN_KEYWORD)
|
2018-11-11 18:09:52 +01:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-11-11 18:09:52 +01:00
|
|
|
public var urlString: String? {
|
2020-06-28 21:25:40 +02:00
|
|
|
getAdditionValue(withKey: Constants.URL_KEYWORD)
|
2018-11-11 18:09:52 +01:00
|
|
|
}
|
2018-07-02 22:13:07 +02:00
|
|
|
|
2018-12-01 13:27:30 +01:00
|
|
|
public var currentOtp: String? {
|
2020-06-28 21:25:40 +02:00
|
|
|
otpToken?.currentPassword
|
2018-12-01 13:27:30 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-08 00:57:54 +01:00
|
|
|
public var numberOfUnknowns: Int {
|
2020-06-28 21:25:40 +02:00
|
|
|
additions.map(\.title).filter(Constants.isUnknown).count
|
2018-12-08 00:57:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public var numberOfOtpRelated: Int {
|
2020-06-28 21:25:40 +02:00
|
|
|
additions.map(\.title).filter(Constants.isOtpKeyword).count - (firstLineIsOTPField ? 1 : 0)
|
2018-12-08 00:57:54 +01:00
|
|
|
}
|
|
|
|
|
|
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-12-09 16:59:07 -08: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
|
2018-12-15 21:49:18 +01:00
|
|
|
changed |= PasswordChange.content.rawValue
|
2018-11-11 18:09:52 +01:00
|
|
|
}
|
|
|
|
|
if self.url != url {
|
|
|
|
|
self.url = url
|
2018-12-15 21:49:18 +01:00
|
|
|
changed |= PasswordChange.path.rawValue
|
2018-11-11 18:09:52 +01:00
|
|
|
}
|
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)
|
2019-01-26 16:40:53 +01:00
|
|
|
if let key = key, Constants.isOtpKeyword(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] {
|
2020-06-28 21:25:40 +02:00
|
|
|
additions.filter { field in
|
2018-12-02 23:07:41 +01:00
|
|
|
let title = field.title.lowercased()
|
|
|
|
|
return title != Constants.USERNAME_KEYWORD
|
|
|
|
|
&& title != Constants.LOGIN_KEYWORD
|
|
|
|
|
&& title != Constants.PASSWORD_KEYWORD
|
2020-01-02 00:48:00 +01:00
|
|
|
&& (!Constants.isUnknown(title) || !Defaults.isHideUnknownOn)
|
|
|
|
|
&& (!Constants.isOtpKeyword(title) || !Defaults.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() }
|
2020-07-05 23:15:29 +02:00
|
|
|
return additions.first { toLowercase($0.title) == toLowercase(key) }?.content
|
2017-02-02 18:02:43 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-12-02 16:55:35 +01:00
|
|
|
/// Set the OTP token if we are able to construct a valid one.
|
|
|
|
|
///
|
|
|
|
|
/// Example of TOTP otpauth:
|
|
|
|
|
///
|
|
|
|
|
/// otpauth://totp/totp-secret?secret=AAAAAAAAAAAAAAAA&issuer=totp-secret
|
|
|
|
|
///
|
|
|
|
|
/// See also [Key Uri Format](https://github.com/google/google-authenticator/wiki/Key-Uri-Format).
|
|
|
|
|
///
|
|
|
|
|
/// In case no otpauth is given in the password file, try to construct the token from separate fields using a
|
|
|
|
|
/// `TokenBuilder`. This means that tokens provided as otpauth have higher priority.
|
|
|
|
|
///
|
2017-03-24 23:14:44 +08:00
|
|
|
private func updateOtpToken() {
|
2018-12-02 16:55:35 +01: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
|
|
|
}
|
2018-12-01 15:53:48 +01:00
|
|
|
if let otpauthUrl = URL(string: otpauthString), let token = Token(url: otpauthUrl) {
|
|
|
|
|
otpToken = token
|
2017-03-24 22:47:40 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-02 16:55:35 +01:00
|
|
|
|
|
|
|
|
// Construct OTP token from separate fields provided in the password file.
|
2018-12-01 15:53:48 +01:00
|
|
|
otpToken = TokenBuilder()
|
|
|
|
|
.usingName(name)
|
|
|
|
|
.usingSecret(getAdditionValue(withKey: Constants.OTP_SECRET))
|
|
|
|
|
.usingType(getAdditionValue(withKey: Constants.OTP_TYPE))
|
|
|
|
|
.usingAlgorithm(getAdditionValue(withKey: Constants.OTP_ALGORITHM))
|
|
|
|
|
.usingDigits(getAdditionValue(withKey: Constants.OTP_DIGITS))
|
|
|
|
|
.usingPeriod(getAdditionValue(withKey: Constants.OTP_PERIOD))
|
|
|
|
|
.usingCounter(getAdditionValue(withKey: Constants.OTP_COUNTER))
|
|
|
|
|
.build()
|
2017-03-07 00:52:22 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-12-02 17:49:16 +01:00
|
|
|
/// Get the OTP description and the current password.
|
2017-06-13 11:42:49 +08:00
|
|
|
public func getOtpStrings() -> (description: String, otp: String)? {
|
2018-12-02 17:49:16 +01:00
|
|
|
guard otpToken != nil else {
|
2017-03-07 09:50:18 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
2018-12-02 17:49:16 +01:00
|
|
|
var description = otpType.description
|
2018-12-15 21:49:18 +01:00
|
|
|
if case let .timer(period) = otpToken!.generator.factor {
|
2017-03-07 09:50:18 +08:00
|
|
|
let timeSinceEpoch = Date().timeIntervalSince1970
|
|
|
|
|
let validTime = Int(period - timeSinceEpoch.truncatingRemainder(dividingBy: period))
|
2019-01-14 20:57:45 +01:00
|
|
|
description += " " + "ExpiresIn".localize(validTime)
|
2017-03-07 09:50:18 +08:00
|
|
|
}
|
2019-01-14 20:57:45 +01:00
|
|
|
return (description, otpToken!.currentPassword ?? "Error".localize())
|
2017-03-07 09:50:18 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -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()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-04-09 02:54:05 +08:00
|
|
|
// 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))
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
var lines: [String] = []
|
|
|
|
|
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)
|
2020-06-28 21:25:40 +02: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!)
|
|
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
updatePassword(name: name, url: url, plainText: lines.joined(separator: "\n"))
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-04-09 02:54:05 +08:00
|
|
|
// get and return the password
|
2020-06-28 21:25:40 +02:00
|
|
|
return otpToken?.currentPassword
|
2017-04-09 02:54:05 +08:00
|
|
|
}
|
2020-04-18 22:35:17 -07:00
|
|
|
|
|
|
|
|
public func getUsernameForCompletion() -> String {
|
|
|
|
|
username ?? login ?? nameFromPath ?? ""
|
|
|
|
|
}
|
2017-02-02 18:02:43 +08:00
|
|
|
}
|