passforios/passKit/Models/Password.swift

220 lines
7.2 KiB
Swift
Raw Normal View History

//
// Password.swift
2021-08-28 07:32:31 +02:00
// passKit
//
// Created by Mingshen Sun on 2/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import Base32
import OneTimePassword
public class Password {
public var name: String
public var url: URL
public var plainText: String
2017-02-06 22:14:42 +08: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
private var parser = Parser(plainText: "")
private var additions = [AdditionField]()
private var firstLineIsOTPField = false
2018-12-15 21:49:18 +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
}
}
public var namePath: String {
url.deletingPathExtension().path
}
2019-05-17 17:30:41 +02:00
public var nameFromPath: String? {
url.deletingPathExtension().path.split(separator: "/").last.map { String($0) }
2019-05-17 17:30:41 +02:00
}
public var password: String {
parser.firstLine
}
public var plainData: Data {
plainText.data(using: .utf8)!
}
public var additionsPlainText: String {
parser.additionsSection
}
2018-07-02 22:13:07 +02:00
public var username: String? {
getAdditionValue(withKey: Constants.USERNAME_KEYWORD)
}
public var login: String? {
getAdditionValue(withKey: Constants.LOGIN_KEYWORD)
}
2018-12-09 16:59:07 -08:00
public var urlString: String? {
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? {
otpToken?.currentPassword
2018-12-01 13:27:30 +01:00
}
public var numberOfUnknowns: Int {
additions.map(\.title).filter(Constants.isUnknown).count
}
public var numberOfOtpRelated: Int {
additions.map(\.title).filter(Constants.isOtpKeyword).count - (firstLineIsOTPField ? 1 : 0)
}
public init(name: String, url: URL, plainText: String) {
self.name = name
self.url = url
self.plainText = plainText
initEverything()
}
2018-12-09 16:59:07 -08:00
public func updatePassword(name: String, url: URL, plainText: String) {
guard self.plainText != plainText || self.url != url else {
return
}
2018-07-02 22:13:07 +02:00
if self.plainText != plainText {
self.plainText = plainText
2018-12-15 21:49:18 +01:00
changed |= PasswordChange.content.rawValue
}
if self.url != url {
self.url = url
2018-12-15 21:49:18 +01:00
changed |= PasswordChange.path.rawValue
}
2018-07-02 22:13:07 +02:00
self.name = name
initEverything()
}
private func initEverything() {
2018-12-01 13:27:30 +01:00
parser = Parser(plainText: plainText)
additions = parser.additionFields
// Check whether the first line looks like an otp entry.
checkPasswordForOtpToken()
// Construct the otp token.
2018-07-02 22:13:07 +02:00
updateOtpToken()
2017-02-11 16:07:59 +08:00
}
private func checkPasswordForOtpToken() {
let (key, value) = Parser.getKeyValuePair(from: password)
2019-01-26 16:40:53 +01:00
if let key = key, Constants.isOtpKeyword(key) {
firstLineIsOTPField = true
2018-12-01 13:27:30 +01:00
additions.append(key => value)
} else {
firstLineIsOTPField = false
}
}
public func getFilteredAdditions() -> [AdditionField] {
additions.filter { field in
let title = field.title.lowercased()
return title != Constants.USERNAME_KEYWORD
&& title != Constants.LOGIN_KEYWORD
&& title != Constants.PASSWORD_KEYWORD
&& (!Constants.isUnknown(title) || !Defaults.isHideUnknownOn)
&& (!Constants.isOtpKeyword(title) || !Defaults.isHideOTPOn)
}
}
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() }
return additions.first { toLowercase($0.title) == toLowercase(key) }?.content
}
2018-12-09 16:59:07 -08: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() {
// Get otpauth. If we are able to generate a token, return.
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) {
otpToken = token
2017-03-24 22:47:40 +08:00
return
}
}
// Construct OTP token from separate fields provided in the password file.
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()
}
2018-12-09 16:59:07 -08:00
2018-12-02 17:49:16 +01:00
/// Get the OTP description and the current password.
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
// return the password strings
// it is guaranteed that it is a HOTP password when we call this
public func getNextHotp() -> String? {
// increase the counter
otpToken = otpToken?.updatedToken()
2018-12-09 16:59:07 -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
var lines: [String] = []
plainText.enumerateLines { line, _ in
let (key, _) = Parser.getKeyValuePair(from: line)
if !Constants.OTP_KEYWORDS.contains(key ?? "") {
lines.append(line)
} else if key == Constants.OTPAUTH, newOtpauth != nil {
lines.append(newOtpauth!)
// set to nil to prevent duplication
newOtpauth = nil
}
}
if newOtpauth != nil {
lines.append(newOtpauth!)
}
updatePassword(name: name, url: url, plainText: lines.joined(separator: "\n"))
2018-12-09 16:59:07 -08:00
// get and return the password
return otpToken?.currentPassword
}
public func getUsernameForCompletion() -> String {
username ?? login ?? nameFromPath ?? ""
}
}