From: Claudio Cambra Date: Fri, 30 Dec 2022 15:32:54 +0000 (+0100) Subject: Add FileProviderDomainNextcloudAccountData that grabs account info from keychain X-Git-Tag: archive/raspbian/3.16.7-1_deb13u1+rpi1~1^2~96^2^2~55^2~306 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=f795666d4b6d929cdfb311f96b0c224b56e48cc7;p=nextcloud-desktop.git Add FileProviderDomainNextcloudAccountData that grabs account info from keychain Signed-off-by: Claudio Cambra --- diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderDomainNextcloudAccountData.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderDomainNextcloudAccountData.swift new file mode 100644 index 000000000..c1eab448d --- /dev/null +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderDomainNextcloudAccountData.swift @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2022 by Claudio Cambra + * + * 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 + +class FileProviderDomainNextcloudAccountData: NSObject { + let webDavUrlSuffix: String = "/remote.php/dav" + + private(set) var username: String? + private(set) var password: String? + private(set) var serverUrl: URL? + private(set) var davUrl: URL? + + var isNull: Bool { + return username?.isEmpty ?? false || serverUrl?.absoluteString.isEmpty ?? false + } + + override init() { + super.init() + } + + init(withKeychainAccount account:String) { + super.init() + getAccountDetails(keychainAccount: account) + } + + static func getUserPasswordFromKeychain(accountString:String) -> Data? { + 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 + } + } + + private func getAccountDetails(keychainAccount: 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 = FileProviderDomainNextcloudAccountData.getUserPasswordFromKeychain(accountString: keychainAccount), + let passwordString = String(data: passwordData, encoding: .utf8) + else { return } + + let keychainAccountSplit = keychainAccount.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 } + let davUrlUrl = serverUrlUrl.appendingPathComponent(webDavUrlSuffix) + + username = usernameString + password = passwordString + serverUrl = serverUrlUrl + davUrl = davUrlUrl + } +} + diff --git a/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj b/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj index bb8d5b755..f31254e82 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj +++ b/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 536EFBF7295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 536EFBF6295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift */; }; + 536EFC36295E3C1100F4CB13 /* FileProviderDomainNextcloudAccountData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 536EFC35295E3C1100F4CB13 /* FileProviderDomainNextcloudAccountData.swift */; }; 538E396A27F4765000FA63D5 /* UniformTypeIdentifiers.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 538E396927F4765000FA63D5 /* UniformTypeIdentifiers.framework */; }; 538E396D27F4765000FA63D5 /* FileProviderExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 538E396C27F4765000FA63D5 /* FileProviderExtension.swift */; }; 538E396F27F4765000FA63D5 /* FileProviderItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 538E396E27F4765000FA63D5 /* FileProviderItem.swift */; }; @@ -123,6 +124,7 @@ /* Begin PBXFileReference section */ 536EFBF6295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileProviderSocketLineProcessor.swift; sourceTree = ""; }; + 536EFC35295E3C1100F4CB13 /* FileProviderDomainNextcloudAccountData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileProviderDomainNextcloudAccountData.swift; sourceTree = ""; }; 538E396727F4765000FA63D5 /* FileProviderExt.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = FileProviderExt.appex; sourceTree = BUILT_PRODUCTS_DIR; }; 538E396927F4765000FA63D5 /* UniformTypeIdentifiers.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UniformTypeIdentifiers.framework; path = System/Library/Frameworks/UniformTypeIdentifiers.framework; sourceTree = SDKROOT; }; 538E396C27F4765000FA63D5 /* FileProviderExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileProviderExtension.swift; sourceTree = ""; }; @@ -200,12 +202,13 @@ 538E396B27F4765000FA63D5 /* FileProviderExt */ = { isa = PBXGroup; children = ( + 536EFC35295E3C1100F4CB13 /* FileProviderDomainNextcloudAccountData.swift */, + 538E397027F4765000FA63D5 /* FileProviderEnumerator.swift */, 538E396C27F4765000FA63D5 /* FileProviderExtension.swift */, 538E396E27F4765000FA63D5 /* FileProviderItem.swift */, - 538E397027F4765000FA63D5 /* FileProviderEnumerator.swift */, 536EFBF6295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift */, - 538E397227F4765000FA63D5 /* Info.plist */, 538E397327F4765000FA63D5 /* FileProviderExt.entitlements */, + 538E397227F4765000FA63D5 /* Info.plist */, ); path = FileProviderExt; sourceTree = ""; @@ -361,6 +364,8 @@ 53903D202956164F00D0B308 /* PBXTargetDependency */, ); name = desktopclient; + packageProductDependencies = ( + ); productName = desktopclient; productReference = C2B573B11B1CD91E00303B36 /* desktopclient.app */; productType = "com.apple.product-type.application"; @@ -503,6 +508,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 536EFC36295E3C1100F4CB13 /* FileProviderDomainNextcloudAccountData.swift in Sources */, 538E396D27F4765000FA63D5 /* FileProviderExtension.swift in Sources */, 536EFBF7295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift in Sources */, 538E396F27F4765000FA63D5 /* FileProviderItem.swift in Sources */,