Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Sources/NextcloudKit/NKCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,22 @@ public struct NKCommon: Sendable {
public func getStandardHeaders(account: String,
options: NKRequestOptions? = nil,
contentType: String? = nil,
accept: String? = nil) -> HTTPHeaders? {
accept: String? = nil,
password: String? = nil) -> HTTPHeaders? {
guard let session = nksessions.session(forAccount: account) else {
return nil
}
var headers: HTTPHeaders = []
let authPassword: String

if let password, !password.isEmpty {
authPassword = password
} else {
authPassword = session.password
}

headers.update(.authorization(username: session.user, password: authPassword))

headers.update(.authorization(username: session.user, password: session.password))
headers.update(.userAgent(session.userAgent))
if let customUserAgent = options?.customUserAgent {
headers.update(.userAgent(customUserAgent))
Expand Down
22 changes: 17 additions & 5 deletions Sources/NextcloudKit/NextcloudKit+E2EE.swift
Original file line number Diff line number Diff line change
Expand Up @@ -808,10 +808,12 @@ public extension NextcloudKit {
///
/// - Parameters:
/// - account: The Nextcloud account requesting the deletion of the certificate.
/// - password: the account password if requested
/// - options: request configuration, including the optional E2EE API version specified by `options.version` v1/v2.
/// - taskHandler: Closure to access the URLSessionTask.
/// - completion: Completion handler returning the account, raw response, and NKError.
func deleteE2EEPublicKey(account: String,
password: String?,
options: NKRequestOptions = NKRequestOptions(),
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },
completion: @escaping (_ account: String, _ responseData: AFDataResponse<Data>?, _ error: NKError) -> Void) {
Expand All @@ -822,7 +824,7 @@ public extension NextcloudKit {
let endpoint = "ocs/v2.php/apps/end_to_end_encryption/api/\(version)/public-key"
guard let nkSession = nkCommonInstance.nksessions.session(forAccount: account),
let url = nkCommonInstance.createStandardUrl(serverUrl: nkSession.urlBase, endpoint: endpoint),
let headers = nkCommonInstance.getStandardHeaders(account: account, options: options) else {
let headers = nkCommonInstance.getStandardHeaders(account: account, options: options, password: password) else {
return options.queue.async { completion(account, nil, .urlError) }
}
nkSession.sessionData.request(url, method: .delete, encoding: URLEncoding.default, headers: headers, interceptor: NKInterceptor(nkCommonInstance: nkCommonInstance)).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in
Expand All @@ -842,10 +844,12 @@ public extension NextcloudKit {
/// Asynchronously deletes the E2EE public key from the server for the given account.
/// - Parameters:
/// - account: The Nextcloud account to remove the certificate from.
/// - password: the account password if requested
/// - options: request configuration, including the optional E2EE API version specified by `options.version` v1/v2.
/// - taskHandler: Optional monitoring of the URLSessionTask.
/// - Returns: A tuple containing the account, response data, and error.
func deleteE2EEPublicKeyAsync(account: String,
password: String?,
options: NKRequestOptions = NKRequestOptions(),
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
) async -> (
Expand All @@ -855,8 +859,9 @@ public extension NextcloudKit {
) {
await withCheckedContinuation { continuation in
deleteE2EEPublicKey(account: account,
options: options,
taskHandler: taskHandler) { account, responseData, error in
password: password,
options: options,
taskHandler: taskHandler) { account, responseData, error in
continuation.resume(returning: (
account: account,
responseData: responseData,
Expand All @@ -872,10 +877,12 @@ public extension NextcloudKit {
///
/// - Parameters:
/// - account: The Nextcloud account requesting the deletion of its private key.
/// - password: the account password if requested
/// - options: request configuration, including the optional E2EE API version specified by `options.version` v1/v2.
/// - taskHandler: Closure to access the URLSessionTask.
/// - completion: Completion handler returning the account, raw response, and NKError.
func deleteE2EEPrivateKey(account: String,
password: String?,
options: NKRequestOptions = NKRequestOptions(),
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },
completion: @escaping (_ account: String, _ responseData: AFDataResponse<Data>?, _ error: NKError) -> Void) {
Expand All @@ -886,7 +893,7 @@ public extension NextcloudKit {
let endpoint = "ocs/v2.php/apps/end_to_end_encryption/api/\(version)/private-key"
guard let nkSession = nkCommonInstance.nksessions.session(forAccount: account),
let url = nkCommonInstance.createStandardUrl(serverUrl: nkSession.urlBase, endpoint: endpoint),
let headers = nkCommonInstance.getStandardHeaders(account: account, options: options) else {
let headers = nkCommonInstance.getStandardHeaders(account: account, options: options, password: password) else {
return options.queue.async { completion(account, nil, .urlError) }
}

Expand All @@ -907,10 +914,12 @@ public extension NextcloudKit {
/// Asynchronously deletes the E2EE private key from the server for the specified account.
/// - Parameters:
/// - account: The Nextcloud account for which the private key will be deleted.
/// - password: the account password if requested
/// - options: request configuration, including the optional E2EE API version specified by `options.version` v1/v2.
/// - taskHandler: Optional monitoring of the URLSessionTask.
/// - Returns: A tuple containing the account, response data, and error.
func deleteE2EEPrivateKeyAsync(account: String,
password: String?,
options: NKRequestOptions = NKRequestOptions(),
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
) async -> (
Expand All @@ -920,6 +929,7 @@ public extension NextcloudKit {
) {
await withCheckedContinuation { continuation in
deleteE2EEPrivateKey(account: account,
password: password,
options: options,
taskHandler: taskHandler) { account, responseData, error in
continuation.resume(returning: (
Expand All @@ -935,18 +945,20 @@ public extension NextcloudKit {
///
/// - Parameters:
/// - account: The Nextcloud account identifier.
/// - password: the account password if requested
/// - options: request configuration, including the optional E2EE API version specified by `options.version` v1/v2.
/// - taskHandler: Closure invoked when the underlying URLSession task is created.
/// - Returns: A tuple containing the account identifier, the Alamofire response data, and the resulting NKError.
func deleteE2EEKeysAsync(account: String,
password: String?,
options: NKRequestOptions = NKRequestOptions(),
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }) async -> (account: String, responseData: AFDataResponse<Data>?, error: NKError) {
let version = options.version ?? "v2"
let endpoint = "ocs/v2.php/apps/end_to_end_encryption/api/\(version)/encrypted-files"

guard let nkSession = nkCommonInstance.nksessions.session(forAccount: account),
let url = nkCommonInstance.createStandardUrl(serverUrl: nkSession.urlBase, endpoint: endpoint),
let headers = nkCommonInstance.getStandardHeaders(account: account, options: options) else {
let headers = nkCommonInstance.getStandardHeaders(account: account, options: options, password: password) else {
return (account: account, responseData: nil, error: .urlError)
}

Expand Down
Loading