NCNetworkingEndToEnd.m 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. //
  2. // NCNetworkingEndToEnd.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 29/10/17.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "NCNetworkingEndToEnd.h"
  24. #import "OCNetworking.h"
  25. #import "CCUtility.h"
  26. #import "NCBridgeSwift.h"
  27. /*********************************************************************************
  28. Netwok call synchronous mode, use this only from :
  29. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  30. });
  31. *********************************************************************************/
  32. @implementation NCNetworkingEndToEnd
  33. + (NCNetworkingEndToEnd *)sharedManager {
  34. static NCNetworkingEndToEnd *sharedManager;
  35. @synchronized(self)
  36. {
  37. if (!sharedManager) {
  38. sharedManager = [NCNetworkingEndToEnd new];
  39. }
  40. return sharedManager;
  41. }
  42. }
  43. #pragma --------------------------------------------------------------------------------------------
  44. #pragma mark ===== End-to-End Encryption NETWORKING =====
  45. #pragma --------------------------------------------------------------------------------------------
  46. - (void)getEndToEndPublicKeyWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *publicKey, NSString *message, NSInteger errorCode))completion
  47. {
  48. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  49. if (tableAccount == nil) {
  50. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  51. }
  52. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  53. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  54. [communication setUserAgent:[CCUtility getUserAgent]];
  55. [communication getEndToEndPublicKeys:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  56. completion(account, publicKey, nil, 0);
  57. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  58. NSString *message = @"";
  59. NSInteger errorCode = response.statusCode;
  60. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  61. errorCode = error.code;
  62. // Error
  63. if (errorCode == 503) {
  64. message = NSLocalizedString(@"_server_error_retry_", nil);
  65. } else {
  66. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  67. }
  68. completion(account, nil, message, errorCode);
  69. }];
  70. }
  71. - (void)getEndToEndPrivateKeyCipherWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *privateKeyChiper, NSString *message, NSInteger errorCode))completion
  72. {
  73. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  74. if (tableAccount == nil) {
  75. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  76. }
  77. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  78. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  79. [communication setUserAgent:[CCUtility getUserAgent]];
  80. [communication getEndToEndPrivateKeyCipher:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKeyChiper, NSString *redirectedServer) {
  81. completion(account, privateKeyChiper, nil, 0);
  82. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  83. NSString *message = @"";
  84. NSInteger errorCode = response.statusCode;
  85. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  86. errorCode = error.code;
  87. // Error
  88. if (errorCode == 503) {
  89. message = NSLocalizedString(@"_server_error_retry_", nil);
  90. } else {
  91. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  92. }
  93. completion(account, nil, message, errorCode);
  94. }];
  95. }
  96. - (void)signEndToEndPublicKeyWithAccount:(NSString *)account publicKey:(NSString *)publicKey completion:(void (^)(NSString *account, NSString *publicKey, NSString *message, NSInteger errorCode))completion
  97. {
  98. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  99. if (tableAccount == nil) {
  100. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  101. }
  102. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  103. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  104. [communication setUserAgent:[CCUtility getUserAgent]];
  105. [communication signEndToEndPublicKey:[tableAccount.url stringByAppendingString:@"/"] publicKey:[CCUtility URLEncodeStringFromString:publicKey] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  106. completion(account, publicKey, nil, 0);
  107. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  108. NSString *message = @"";
  109. NSInteger errorCode = response.statusCode;
  110. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  111. errorCode = error.code;
  112. // Error
  113. if (errorCode == 503) {
  114. message = NSLocalizedString(@"_server_error_retry_", nil);
  115. } else {
  116. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  117. }
  118. completion(account, nil, message, errorCode);
  119. }];
  120. }
  121. - (void)storeEndToEndPrivateKeyCipherWithAccount:(NSString *)account privateKeyString:(NSString *)privateKeyString privateKeyChiper:(NSString *)privateKeyChiper completion:(void (^)(NSString *account, NSString *privateKeyString, NSString *privateKey, NSString *message, NSInteger errorCode))completion
  122. {
  123. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  124. if (tableAccount == nil) {
  125. completion(account, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  126. }
  127. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  128. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  129. [communication setUserAgent:[CCUtility getUserAgent]];
  130. [communication storeEndToEndPrivateKeyCipher:[tableAccount.url stringByAppendingString:@"/"] privateKeyChiper:[CCUtility URLEncodeStringFromString:privateKeyChiper] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKey, NSString *redirectedServer) {
  131. completion(account, privateKeyString, privateKeyChiper, nil, 0);
  132. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  133. NSString *message = @"";
  134. NSInteger errorCode = response.statusCode;
  135. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  136. errorCode = error.code;
  137. // Error
  138. if (errorCode == 503) {
  139. message = NSLocalizedString(@"_server_error_retry_", nil);
  140. } else {
  141. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  142. }
  143. completion(account, nil, nil, message, errorCode);
  144. }];
  145. }
  146. - (void)deleteEndToEndPublicKeyWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  147. {
  148. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  149. if (tableAccount == nil) {
  150. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  151. }
  152. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  153. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  154. [communication setUserAgent:[CCUtility getUserAgent]];
  155. [communication deleteEndToEndPublicKey:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  156. completion(account, nil ,0);
  157. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  158. NSString *message = @"";
  159. NSInteger errorCode = response.statusCode;
  160. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  161. errorCode = error.code;
  162. // Error
  163. if (errorCode == 503) {
  164. message = NSLocalizedString(@"_server_error_retry_", nil);
  165. } else {
  166. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  167. }
  168. completion(account, message, errorCode);
  169. }];
  170. }
  171. - (void)deleteEndToEndPrivateKeyWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  172. {
  173. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  174. if (tableAccount == nil) {
  175. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  176. }
  177. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  178. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  179. [communication setUserAgent:[CCUtility getUserAgent]];
  180. [communication deleteEndToEndPrivateKey:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  181. completion(account, nil, 0);
  182. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  183. NSString *message = @"";
  184. NSInteger errorCode = response.statusCode;
  185. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  186. errorCode = error.code;
  187. // Error
  188. if (errorCode == 503) {
  189. message = NSLocalizedString(@"_server_error_retry_", nil);
  190. } else {
  191. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  192. }
  193. completion(account, message, errorCode);
  194. }];
  195. }
  196. - (void)getEndToEndServerPublicKeyWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *publicKey, NSString *message, NSInteger errorCode))completion
  197. {
  198. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  199. if (tableAccount == nil) {
  200. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  201. }
  202. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  203. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  204. [communication setUserAgent:[CCUtility getUserAgent]];
  205. [communication getEndToEndServerPublicKey:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  206. completion(account, publicKey, nil, 0);
  207. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  208. NSString *message = @"";
  209. NSInteger errorCode = response.statusCode;
  210. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  211. errorCode = error.code;
  212. // Error
  213. if (errorCode == 503) {
  214. message = NSLocalizedString(@"_server_error_retry_", nil);
  215. } else {
  216. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  217. }
  218. completion(account, nil, message, errorCode);
  219. }];
  220. }
  221. - (void)createEndToEndFolder:(NSString *)folderPathName account:(NSString *)account user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url encrypted:(BOOL)encrypted ocId:(NSString **)ocId fileId:(NSString **)fileId error:(NSError **)error
  222. {
  223. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  224. __block NSError *returnError = nil;
  225. __block NSString *returnFileId = nil;
  226. __block NSString *returnOcId = nil;
  227. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  228. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  229. [communication setUserAgent:[CCUtility getUserAgent]];
  230. [communication readFile:folderPathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  231. dispatch_semaphore_signal(semaphore);
  232. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  233. [communication createFolder:folderPathName onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  234. NSDictionary *fields = [response allHeaderFields];
  235. returnOcId = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  236. returnFileId = [CCUtility convertOcIdToFileId:returnOcId];
  237. if (encrypted) {
  238. // MARK
  239. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:returnFileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  240. [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:[CCUtility deletingLastPathComponentFromServerUrl:folderPathName] account:account];
  241. dispatch_semaphore_signal(semaphore);
  242. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  243. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_mark_folder_"];
  244. dispatch_semaphore_signal(semaphore);
  245. }];
  246. } else {
  247. [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:[CCUtility deletingLastPathComponentFromServerUrl:folderPathName] account:account];
  248. dispatch_semaphore_signal(semaphore);
  249. }
  250. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  251. returnError = [self getError:response error:error descriptionDefault:@"_error_"];
  252. dispatch_semaphore_signal(semaphore);
  253. } errorBeforeRequest:^(NSError *error) {
  254. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:response.description forKey:NSLocalizedDescriptionKey]];
  255. dispatch_semaphore_signal(semaphore);
  256. }];
  257. }];
  258. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  259. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  260. *ocId = returnOcId;
  261. *fileId = returnFileId;
  262. *error = returnError;
  263. }
  264. #pragma --------------------------------------------------------------------------------------------
  265. #pragma mark ===== E2EE End-to-End Encryption =====
  266. #pragma --------------------------------------------------------------------------------------------
  267. // E2EE
  268. - (NSError *)markEndToEndFolderEncryptedOnServerUrl:(NSString *)serverUrl fileId:(NSString *)fileId e2eToken:(NSString *)e2eToken user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  269. {
  270. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  271. __block NSError *returnError = nil;
  272. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  273. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  274. [communication setUserAgent:[CCUtility getUserAgent]];
  275. // MARK
  276. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  277. if (e2eToken) {
  278. // UNLOCK
  279. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  280. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  281. dispatch_semaphore_signal(semaphore);
  282. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  283. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  284. dispatch_semaphore_signal(semaphore);
  285. }];
  286. } else {
  287. dispatch_semaphore_signal(semaphore);
  288. }
  289. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  290. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_mark_folder_"];
  291. if (e2eToken) {
  292. // UNLOCK
  293. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  294. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  295. dispatch_semaphore_signal(semaphore);
  296. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  297. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  298. dispatch_semaphore_signal(semaphore);
  299. }];
  300. } else {
  301. dispatch_semaphore_signal(semaphore);
  302. }
  303. }];
  304. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  305. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  306. return returnError;
  307. }
  308. - (NSError *)deletemarkEndToEndFolderEncryptedOnServerUrl:(NSString *)serverUrl fileId:(NSString *)fileId user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  309. {
  310. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  311. __block NSError *returnError = nil;
  312. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  313. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  314. [communication setUserAgent:[CCUtility getUserAgent]];
  315. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  316. // LOCK
  317. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:tableLock.e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *e2eToken, NSString *redirectedServer) {
  318. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId e2eToken:e2eToken];
  319. // DELETE METADATA
  320. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  321. NSLog(@"[LOG] Found metadata and delete");
  322. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  323. NSLog(@"[LOG] %@", [NSString stringWithFormat:@"Remove metadata error %d", (int)response.statusCode]);
  324. }];
  325. // DELETE MARK
  326. [communication deletemarkEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  327. // UNLOCK
  328. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  329. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  330. dispatch_semaphore_signal(semaphore);
  331. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  332. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  333. dispatch_semaphore_signal(semaphore);
  334. }];
  335. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  336. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_delete_mark_folder_"];
  337. // UNLOCK
  338. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  339. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  340. dispatch_semaphore_signal(semaphore);
  341. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  342. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  343. dispatch_semaphore_signal(semaphore);
  344. }];
  345. }];
  346. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  347. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  348. dispatch_semaphore_signal(semaphore);
  349. }];
  350. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  351. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  352. return returnError;
  353. }
  354. - (NSError *)getEndToEndMetadata:(NSString **)metadata fileId:(NSString *)fileId user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  355. {
  356. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  357. __block NSError *returnError = nil;
  358. __block NSString *returnMetadata = nil;
  359. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  360. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  361. [communication setUserAgent:[CCUtility getUserAgent]];
  362. [communication getEndToEndMetadata:[url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  363. returnMetadata = encryptedMetadata;
  364. dispatch_semaphore_signal(semaphore);
  365. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  366. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_get_metadata_"];
  367. dispatch_semaphore_signal(semaphore);
  368. }];
  369. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  370. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  371. *metadata = returnMetadata;
  372. return returnError;
  373. }
  374. - (NSError *)deleteEndToEndMetadataOnServerUrl:(NSString *)serverUrl fileId:(NSString *)fileId unlock:(BOOL)unlock user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  375. {
  376. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  377. __block NSError *returnError = nil;
  378. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  379. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  380. [communication setUserAgent:[CCUtility getUserAgent]];
  381. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  382. // LOCK
  383. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:tableLock.e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *e2eToken, NSString *redirectedServer) {
  384. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId e2eToken:e2eToken];
  385. // DELETE METADATA
  386. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  387. // UNLOCK
  388. if (unlock) {
  389. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  390. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  391. dispatch_semaphore_signal(semaphore);
  392. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  393. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  394. dispatch_semaphore_signal(semaphore);
  395. }];
  396. } else {
  397. dispatch_semaphore_signal(semaphore);
  398. }
  399. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  400. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_delete_metadata_"];
  401. // UNLOCK
  402. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  403. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  404. dispatch_semaphore_signal(semaphore);
  405. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  406. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  407. dispatch_semaphore_signal(semaphore);
  408. }];
  409. }];
  410. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  411. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  412. dispatch_semaphore_signal(semaphore);
  413. }];
  414. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  415. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  416. return returnError;
  417. }
  418. - (NSError *)storeEndToEndMetadata:(NSString *)metadata serverUrl:(NSString *)serverUrl fileId:(NSString *)fileId unlock:(BOOL)unlock user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  419. {
  420. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  421. __block NSError *returnError = nil;
  422. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  423. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  424. [communication setUserAgent:[CCUtility getUserAgent]];
  425. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  426. // LOCK
  427. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:tableLock.e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *e2eToken, NSString *redirectedServer) {
  428. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId e2eToken:e2eToken];
  429. // STORE METADATA
  430. [communication storeEndToEndMetadata:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  431. // UNLOCK
  432. if (unlock) {
  433. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  434. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  435. dispatch_semaphore_signal(semaphore);
  436. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  437. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  438. dispatch_semaphore_signal(semaphore);
  439. }];
  440. } else {
  441. dispatch_semaphore_signal(semaphore);
  442. }
  443. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  444. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_store_metadata_"];
  445. // UNLOCK
  446. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  447. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  448. dispatch_semaphore_signal(semaphore);
  449. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  450. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  451. dispatch_semaphore_signal(semaphore);
  452. }];
  453. }];
  454. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  455. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  456. dispatch_semaphore_signal(semaphore);
  457. }];
  458. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  459. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  460. return returnError;
  461. }
  462. - (NSError *)updateEndToEndMetadata:(NSString *)metadata serverUrl:(NSString *)serverUrl fileId:(NSString *)fileId unlock:(BOOL)unlock user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  463. {
  464. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  465. __block NSError *returnError = nil;
  466. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  467. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  468. [communication setUserAgent:[CCUtility getUserAgent]];
  469. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  470. // LOCK
  471. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:tableLock.e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *e2eToken, NSString *redirectedServer) {
  472. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId e2eToken:e2eToken];
  473. // UPDATA METADATA
  474. [communication updateEndToEndMetadata:[url stringByAppendingString:@"/"] fileId:fileId encryptedMetadata:metadata e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  475. // UNLOCK
  476. if (unlock) {
  477. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  478. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  479. dispatch_semaphore_signal(semaphore);
  480. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  481. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  482. dispatch_semaphore_signal(semaphore);
  483. }];
  484. } else {
  485. dispatch_semaphore_signal(semaphore);
  486. }
  487. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  488. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_update_metadata_"];
  489. // UNLOCK
  490. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  491. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  492. dispatch_semaphore_signal(semaphore);
  493. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  494. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  495. dispatch_semaphore_signal(semaphore);
  496. }];
  497. }];
  498. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  499. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  500. dispatch_semaphore_signal(semaphore);
  501. }];
  502. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  503. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  504. return returnError;
  505. }
  506. - (NSError *)lockEndToEndFolderEncryptedOnServerUrl:(NSString *)serverUrl fileId:(NSString *)fileId user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  507. {
  508. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  509. __block NSError *returnError = nil;
  510. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  511. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  512. [communication setUserAgent:[CCUtility getUserAgent]];
  513. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  514. // LOCK
  515. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:tableLock.e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *e2eToken, NSString *redirectedServer) {
  516. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId e2eToken:e2eToken];
  517. dispatch_semaphore_signal(semaphore);
  518. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  519. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  520. dispatch_semaphore_signal(semaphore);
  521. }];
  522. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  523. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  524. return returnError;
  525. }
  526. - (NSError *)unlockEndToEndFolderEncryptedOnServerUrl:(NSString *)serverUrl fileId:(NSString *)fileId e2eToken:(NSString *)e2eToken user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  527. {
  528. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  529. __block NSError *returnError = nil;
  530. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  531. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  532. [communication setUserAgent:[CCUtility getUserAgent]];
  533. // UNLOCK
  534. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  535. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  536. dispatch_semaphore_signal(semaphore);
  537. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  538. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  539. dispatch_semaphore_signal(semaphore);
  540. }];
  541. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  542. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  543. return returnError;
  544. }
  545. - (NSError *)sendEndToEndMetadataOnServerUrl:(NSString *)serverUrl fileNameRename:(NSString *)fileName fileNameNewRename:(NSString *)fileNameNew unlock:(BOOL)unlock account:(NSString *)account user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  546. {
  547. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  548. NSString *metadata;
  549. NSError *error;
  550. // Enabled E2E
  551. if ([CCUtility isEndToEndEnabled:account] == NO)
  552. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_not_enabled_", nil) forKey:NSLocalizedDescriptionKey]];
  553. // get Metadata for select updateEndToEndMetadata or storeEndToEndMetadata
  554. error = [self getEndToEndMetadata:&metadata fileId:directory.fileId user:user userID:userID password:password url:url];
  555. if (error.code != kOCErrorServerPathNotFound && error != nil) {
  556. return error;
  557. }
  558. // Rename
  559. if (fileName && fileNameNew)
  560. [[NCManageDatabase sharedInstance] renameFileE2eEncryptionWithServerUrl:serverUrl fileNameIdentifier:fileName newFileName:fileNameNew newFileNamePath:[CCUtility returnFileNamePathFromFileName:fileNameNew serverUrl:serverUrl activeUrl:url]];
  561. NSArray *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionsWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  562. if (!tableE2eEncryption)
  563. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_record_not_found_", nil) forKey:NSLocalizedDescriptionKey]];
  564. NSString *e2eMetadataJSON = [[NCEndToEndMetadata sharedInstance] encoderMetadata:tableE2eEncryption privateKey:[CCUtility getEndToEndPrivateKey:account] serverUrl:serverUrl];
  565. if (!e2eMetadataJSON)
  566. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_encode_metadata_", nil) forKey:NSLocalizedDescriptionKey]];
  567. // send Metadata
  568. if (error == nil)
  569. error = [self updateEndToEndMetadata:e2eMetadataJSON serverUrl:serverUrl fileId:directory.fileId unlock:unlock user:user userID:userID password:password url:url];
  570. else if (error.code == kOCErrorServerPathNotFound)
  571. error = [self storeEndToEndMetadata:e2eMetadataJSON serverUrl:serverUrl fileId:directory.fileId unlock:unlock user:user userID:userID password:password url:url];
  572. return error;
  573. }
  574. - (NSError *)rebuildAndSendEndToEndMetadataOnServerUrl:(NSString *)serverUrl account:(NSString *)account user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  575. {
  576. NSError *error;
  577. NSString *e2eMetadataJSON;
  578. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  579. if (directory.e2eEncrypted == NO)
  580. return nil;
  581. NSArray *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionsWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  582. if (tableE2eEncryption) {
  583. e2eMetadataJSON = [[NCEndToEndMetadata sharedInstance] encoderMetadata:tableE2eEncryption privateKey:[CCUtility getEndToEndPrivateKey:account] serverUrl:serverUrl];
  584. if (!e2eMetadataJSON)
  585. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_encode_metadata_", nil) forKey:NSLocalizedDescriptionKey]];
  586. error = [self updateEndToEndMetadata:e2eMetadataJSON serverUrl:serverUrl fileId:directory.fileId unlock:YES user:user userID:userID password:password url:url];
  587. } else {
  588. [self deleteEndToEndMetadataOnServerUrl:serverUrl fileId:directory.fileId unlock:YES user:user userID:userID password:password url:url];
  589. }
  590. return error;
  591. }
  592. - (NSError *)getError:(NSHTTPURLResponse *)response error:(NSError *)error descriptionDefault:(NSString *)descriptionDefault
  593. {
  594. NSInteger errorCode = response.statusCode;
  595. NSString *errorDescription = response.description;
  596. if (errorDescription == nil || errorCode == 0) {
  597. errorCode = error.code;
  598. errorDescription = error.description;
  599. if (errorDescription == nil) errorDescription = NSLocalizedString(descriptionDefault, @"");
  600. }
  601. errorDescription = [NSString stringWithFormat:@"%@ [%ld] - %@", NSLocalizedString(descriptionDefault, @""), (long)errorCode, errorDescription];
  602. if (errorDescription.length >= 250) {
  603. errorDescription = [errorDescription substringToIndex:250];
  604. errorDescription = [errorDescription stringByAppendingString:@" ..."];
  605. }
  606. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:errorCode userInfo:[NSDictionary dictionaryWithObject:errorDescription forKey:NSLocalizedDescriptionKey]];
  607. }
  608. @end