NCNetworkingSync.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // NCNetworkingSync.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 29/10/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. #import "NCNetworkingSync.h"
  9. #import "CCUtility.h"
  10. #import "CCCertificate.h"
  11. @implementation NCNetworkingSync
  12. + (NCNetworkingSync *)sharedManager {
  13. static NCNetworkingSync *sharedManager;
  14. @synchronized(self)
  15. {
  16. if (!sharedManager) {
  17. sharedManager = [NCNetworkingSync new];
  18. }
  19. return sharedManager;
  20. }
  21. }
  22. #pragma --------------------------------------------------------------------------------------------
  23. #pragma mark ============================
  24. #pragma --------------------------------------------------------------------------------------------
  25. - (NSError *)uploadFile:(NSString *)localFilePathName remoteFilePathName:(NSString *)remoteFilePathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  26. {
  27. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  28. __block NSError *returnError = nil;
  29. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  30. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  31. [communication setUserAgent:[CCUtility getUserAgent]];
  32. [communication uploadFileSession:localFilePathName toDestiny:remoteFilePathName onCommunication:communication progress:^(NSProgress *progress) {
  33. // Progress
  34. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  35. dispatch_semaphore_signal(semaphore);
  36. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  37. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  38. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:httpResponse.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Upload file error" forKey:NSLocalizedDescriptionKey]];
  39. dispatch_semaphore_signal(semaphore);
  40. } failureBeforeRequest:^(NSError *error) {
  41. returnError = error;
  42. dispatch_semaphore_signal(semaphore);
  43. }];
  44. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  45. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  46. return returnError;
  47. }
  48. - (NSError *)checkServer:(NSString *)serverUrl user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  49. {
  50. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  51. __block NSError *returnError = nil;
  52. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  53. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  54. [communication setUserAgent:[CCUtility getUserAgent]];
  55. [communication checkServer:serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  56. dispatch_semaphore_signal(semaphore);
  57. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  58. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Check server error" forKey:NSLocalizedDescriptionKey]];
  59. dispatch_semaphore_signal(semaphore);
  60. }];
  61. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  62. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  63. return returnError;
  64. }
  65. - (NSError *)readFile:(NSString *)filePathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  66. {
  67. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  68. __block NSError *returnError = nil;
  69. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  70. [communication setCredentialsWithUser: user andUserID: userID andPassword: password];
  71. [communication setUserAgent:[CCUtility getUserAgent]];
  72. [communication readFile:filePathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  73. dispatch_semaphore_signal(semaphore);
  74. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  75. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Read file error" forKey:NSLocalizedDescriptionKey]];
  76. dispatch_semaphore_signal(semaphore);
  77. }];
  78. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  79. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  80. return returnError;
  81. }
  82. #pragma --------------------------------------------------------------------------------------------
  83. #pragma mark ===== End-to-End Encryption =====
  84. #pragma --------------------------------------------------------------------------------------------
  85. - (NSError *)markEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString **)token
  86. {
  87. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  88. __block NSError *returnError = nil;
  89. __block NSString *returnToken = nil;
  90. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  91. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  92. [communication setUserAgent:[CCUtility getUserAgent]];
  93. // LOCK
  94. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  95. returnToken = token;
  96. // MARK
  97. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  98. // UNLOCK
  99. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  100. returnToken = nil;
  101. dispatch_semaphore_signal(semaphore);
  102. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  103. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  104. dispatch_semaphore_signal(semaphore);
  105. }];
  106. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  107. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  108. dispatch_semaphore_signal(semaphore);
  109. }];
  110. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  111. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  112. dispatch_semaphore_signal(semaphore);
  113. }];
  114. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  115. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  116. *token = returnToken;
  117. return returnError;
  118. }
  119. - (NSError *)deletemarkEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString **)token
  120. {
  121. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  122. __block NSError *returnError = nil;
  123. __block NSString *returnToken = nil;
  124. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  125. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  126. [communication setUserAgent:[CCUtility getUserAgent]];
  127. // LOCK
  128. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  129. returnToken = token;
  130. // REMOVE METADATA
  131. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  132. // DELETE MARK
  133. [communication deletemarkEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  134. // UNLOCK
  135. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  136. returnToken = nil;
  137. dispatch_semaphore_signal(semaphore);
  138. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  139. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  140. dispatch_semaphore_signal(semaphore);
  141. }];
  142. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  143. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  144. dispatch_semaphore_signal(semaphore);
  145. }];
  146. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  147. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  148. dispatch_semaphore_signal(semaphore);
  149. }];
  150. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  151. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  152. dispatch_semaphore_signal(semaphore);
  153. }];
  154. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  155. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  156. *token = returnToken;
  157. return returnError;
  158. }
  159. - (NSError *)storeEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString *)metadata token:(NSString **)token
  160. {
  161. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  162. __block NSError *returnError = nil;
  163. __block NSString *returnToken = nil;
  164. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  165. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  166. [communication setUserAgent:[CCUtility getUserAgent]];
  167. // LOCK
  168. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  169. returnToken = token;
  170. // STORE METADATA
  171. [communication storeEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  172. // UNLOCK
  173. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  174. returnToken = nil;
  175. dispatch_semaphore_signal(semaphore);
  176. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  177. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  178. dispatch_semaphore_signal(semaphore);
  179. }];
  180. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  181. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  182. dispatch_semaphore_signal(semaphore);
  183. }];
  184. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  185. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  186. dispatch_semaphore_signal(semaphore);
  187. }];
  188. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  189. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  190. *token = returnToken;
  191. return returnError;
  192. }
  193. - (NSError *)updateEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString *)metadata token:(NSString **)token
  194. {
  195. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  196. __block NSError *returnError = nil;
  197. __block NSString *returnToken = nil;
  198. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  199. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  200. [communication setUserAgent:[CCUtility getUserAgent]];
  201. // LOCK
  202. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  203. returnToken = token;
  204. // UPDATA METADATA
  205. [communication updateEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  206. // UNLOCK
  207. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  208. returnToken = nil;
  209. dispatch_semaphore_signal(semaphore);
  210. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  211. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  212. dispatch_semaphore_signal(semaphore);
  213. }];
  214. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  215. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  216. dispatch_semaphore_signal(semaphore);
  217. }];
  218. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  219. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  220. dispatch_semaphore_signal(semaphore);
  221. }];
  222. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  223. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  224. *token = returnToken;
  225. return returnError;
  226. }
  227. - (NSError *)lockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString **)token
  228. {
  229. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  230. __block NSError *returnError = nil;
  231. __block NSString *returnToken = nil;
  232. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  233. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  234. [communication setUserAgent:[CCUtility getUserAgent]];
  235. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  236. returnToken = token;
  237. dispatch_semaphore_signal(semaphore);
  238. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  239. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  240. dispatch_semaphore_signal(semaphore);
  241. }];
  242. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  243. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  244. *token = returnToken;
  245. return returnError;
  246. }
  247. - (NSError *)unlockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString *)token
  248. {
  249. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  250. __block NSError *returnError= nil;
  251. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  252. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  253. [communication setUserAgent:[CCUtility getUserAgent]];
  254. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  255. dispatch_semaphore_signal(semaphore);
  256. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  257. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  258. dispatch_semaphore_signal(semaphore);
  259. }];
  260. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  261. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  262. return returnError;
  263. }
  264. @end