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.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import SwiftyUserDefaults
|
2017-03-03 14:45:16 +08:00
|
|
|
import OneTimePassword
|
|
|
|
|
import Base32
|
2017-02-02 18:02:43 +08:00
|
|
|
|
2017-02-06 22:14:42 +08:00
|
|
|
struct AdditionField {
|
|
|
|
|
var title: String
|
|
|
|
|
var content: String
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 18:02:43 +08:00
|
|
|
class Password {
|
2017-03-03 14:45:16 +08:00
|
|
|
static let otpKeywords = ["otp_secret", "otp_type", "otp_algorithm", "otp_period", "otp_digits", "otp_counter"]
|
|
|
|
|
|
2017-02-11 14:30:35 +08:00
|
|
|
var name = ""
|
|
|
|
|
var password = ""
|
2017-02-11 16:07:59 +08:00
|
|
|
var additions = [String: String]()
|
|
|
|
|
var additionKeys = [String]()
|
2017-02-13 01:15:42 +08:00
|
|
|
var plainText = ""
|
|
|
|
|
var changed = false
|
2017-03-03 17:12:25 +08:00
|
|
|
var firstLineIsOTPField = false
|
2017-03-03 14:45:16 +08:00
|
|
|
var otpType: String?
|
|
|
|
|
var otpToken: Token?
|
2017-02-02 18:02:43 +08:00
|
|
|
|
2017-02-13 01:15:42 +08:00
|
|
|
init(name: String, plainText: String) {
|
2017-03-03 17:12:25 +08:00
|
|
|
self.initEverything(name: name, plainText: plainText)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func updatePassword(name: String, plainText: String) {
|
|
|
|
|
if self.plainText != plainText {
|
|
|
|
|
self.initEverything(name: name, plainText: plainText)
|
|
|
|
|
changed = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func initEverything(name: String, plainText: String) {
|
2017-02-09 13:17:11 +08:00
|
|
|
self.name = name
|
2017-02-13 01:15:42 +08:00
|
|
|
self.plainText = plainText
|
2017-03-03 10:49:32 +08:00
|
|
|
|
|
|
|
|
// get password and additional fields
|
|
|
|
|
let plainTextSplit = plainText.characters.split(maxSplits: 1, omittingEmptySubsequences: false) {
|
|
|
|
|
$0 == "\n" || $0 == "\r\n"
|
|
|
|
|
}.map(String.init)
|
|
|
|
|
self.password = plainTextSplit[0]
|
|
|
|
|
(self.additions, self.additionKeys) = Password.getAdditionFields(from: plainTextSplit[1])
|
2017-03-03 17:12:25 +08:00
|
|
|
|
2017-03-03 14:45:16 +08:00
|
|
|
// 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!) {
|
2017-03-03 17:12:25 +08:00
|
|
|
firstLineIsOTPField = true
|
2017-03-03 14:45:16 +08:00
|
|
|
self.additions[key!] = value
|
2017-03-03 17:12:25 +08:00
|
|
|
self.additionKeys.insert(key!, at: 0)
|
|
|
|
|
} else {
|
|
|
|
|
firstLineIsOTPField = false
|
2017-03-03 14:45:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// construct the otp token
|
|
|
|
|
self.updateOtpToken()
|
2017-02-11 16:07:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getUsername() -> String? {
|
|
|
|
|
return getAdditionValue(withKey: "Username") ?? getAdditionValue(withKey: "username")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getURL() -> String? {
|
|
|
|
|
return getAdditionValue(withKey: "URL") ?? getAdditionValue(withKey: "url") ?? getAdditionValue(withKey: "Url")
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-03 10:49:32 +08:00
|
|
|
// return a key-value pair from the line
|
|
|
|
|
// key might be nil, if there is no ":" in the line
|
|
|
|
|
static func getKeyValuePair(from line: String) -> (key: String?, value: String) {
|
|
|
|
|
let items = line.characters.split(separator: ":", maxSplits: 1, omittingEmptySubsequences: true).map(String.init)
|
|
|
|
|
var key : String?
|
|
|
|
|
var value = ""
|
|
|
|
|
if items.count == 1 {
|
|
|
|
|
value = items[0]
|
|
|
|
|
} else if items.count == 2 {
|
|
|
|
|
key = items[0]
|
|
|
|
|
value = items[1].trimmingCharacters(in: .whitespaces)
|
|
|
|
|
}
|
|
|
|
|
return (key, value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func getAdditionFields(from additionFieldsPlainText: String) -> ([String: String], [String]){
|
|
|
|
|
var additions = [String: String]()
|
|
|
|
|
var additionKeys = [String]()
|
2017-02-14 11:16:30 +08:00
|
|
|
var unknownIndex = 0
|
2017-02-13 01:15:42 +08:00
|
|
|
|
|
|
|
|
additionFieldsPlainText.enumerateLines() { line, _ in
|
2017-02-20 17:43:04 +08:00
|
|
|
if line == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-03-03 10:49:32 +08:00
|
|
|
var (key, value) = getKeyValuePair(from: line)
|
|
|
|
|
if key == nil {
|
2017-02-14 11:16:30 +08:00
|
|
|
unknownIndex += 1
|
|
|
|
|
key = "unknown \(unknownIndex)"
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
2017-03-03 10:49:32 +08:00
|
|
|
additions[key!] = value
|
|
|
|
|
additionKeys.append(key!)
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
2017-03-03 10:49:32 +08:00
|
|
|
|
|
|
|
|
return (additions, additionKeys)
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getAdditionsPlainText() -> String {
|
2017-03-03 17:12:25 +08:00
|
|
|
// lines starting from the second
|
|
|
|
|
let plainTextSplit = plainText.characters.split(maxSplits: 1, omittingEmptySubsequences: false) {
|
|
|
|
|
$0 == "\n" || $0 == "\r\n"
|
|
|
|
|
}.map(String.init)
|
|
|
|
|
if plainTextSplit.count == 1 {
|
|
|
|
|
return ""
|
|
|
|
|
} else {
|
|
|
|
|
return plainTextSplit[1]
|
|
|
|
|
}
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getPlainText() -> String {
|
2017-03-03 17:12:25 +08:00
|
|
|
return self.plainText
|
2017-02-11 16:07:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getPlainData() -> Data {
|
2017-03-01 14:54:52 +08:00
|
|
|
return getPlainText().data(using: .utf8)!
|
2017-02-11 16:07:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func getAdditionValue(withKey key: String) -> String? {
|
|
|
|
|
return self.additions[key]
|
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.
|
|
|
|
|
|
|
|
|
|
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.")
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-02 18:02:43 +08:00
|
|
|
}
|