2022-12-30 18:32:54 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import FileProvider
|
|
|
|
|
2023-01-04 23:40:30 +03:00
|
|
|
class NextcloudAccount: NSObject {
|
2023-01-10 23:21:28 +03:00
|
|
|
let webDavFilesUrlSuffix: String = "/remote.php/dav/files/"
|
2023-01-13 03:34:57 +03:00
|
|
|
let username, password, ncKitAccount: String?
|
2023-01-10 23:21:28 +03:00
|
|
|
let serverUrl, davFilesUrl: URL?
|
2022-12-30 18:32:54 +03:00
|
|
|
|
|
|
|
var isNull: Bool {
|
|
|
|
return username?.isEmpty ?? false || serverUrl?.absoluteString.isEmpty ?? false
|
|
|
|
}
|
|
|
|
|
2023-01-04 23:40:30 +03:00
|
|
|
init?(withKeychainAccount account:String) {
|
|
|
|
// The client sets the account field in the keychain entry as a colon-separated string consisting of
|
|
|
|
// an account's username, its homeserver url, and the id of the account
|
|
|
|
guard let passwordData = NextcloudAccount.getUserPasswordFromKeychain(accountString: account),
|
|
|
|
let passwordString = String(data: passwordData, encoding: .utf8) else {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let keychainAccountSplit = account.split(separator: ":")
|
|
|
|
let usernameSubstring = keychainAccountSplit[0]
|
|
|
|
let serverUrlSubstring = keychainAccountSplit[1]
|
|
|
|
let clientAccountIdSubstring = keychainAccountSplit[2]
|
|
|
|
|
|
|
|
let usernameString = String(usernameSubstring)
|
|
|
|
let serverUrlString = String(serverUrlSubstring)
|
|
|
|
let clientAccountIdString = String(clientAccountIdSubstring)
|
|
|
|
|
|
|
|
guard let serverUrlUrl = URL(string: String(serverUrlString)) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-10 23:21:28 +03:00
|
|
|
let davFilesUrlUrl = serverUrlUrl.appendingPathComponent(webDavFilesUrlSuffix + usernameString)
|
2023-01-04 23:40:30 +03:00
|
|
|
|
|
|
|
username = usernameString
|
|
|
|
password = passwordString
|
2023-01-13 03:34:57 +03:00
|
|
|
ncKitAccount = usernameString + " " + serverUrlString
|
2023-01-04 23:40:30 +03:00
|
|
|
serverUrl = serverUrlUrl
|
2023-01-10 23:21:28 +03:00
|
|
|
davFilesUrl = davFilesUrlUrl
|
2022-12-30 18:32:54 +03:00
|
|
|
|
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
2023-01-04 23:40:30 +03:00
|
|
|
private static func getUserPasswordFromKeychain(accountString:String) -> Data? {
|
2022-12-30 18:32:54 +03:00
|
|
|
let query = [
|
|
|
|
kSecClass as String : kSecClassGenericPassword,
|
|
|
|
kSecAttrAccount as String : accountString,
|
|
|
|
kSecReturnData as String : kCFBooleanTrue!,
|
|
|
|
kSecMatchLimit as String : kSecMatchLimitOne
|
|
|
|
] as [String : Any]
|
|
|
|
|
|
|
|
var dataTypeRef: AnyObject? = nil
|
|
|
|
|
|
|
|
let status: OSStatus = SecItemCopyMatching(query as CFDictionary, &dataTypeRef)
|
|
|
|
|
|
|
|
if status == noErr {
|
|
|
|
return dataTypeRef as! Data?
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|