NCNetworkingSync.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. // DELETE MARK
  131. [communication deletemarkEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  132. // UNLOCK
  133. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  134. returnToken = nil;
  135. dispatch_semaphore_signal(semaphore);
  136. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  137. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  138. dispatch_semaphore_signal(semaphore);
  139. }];
  140. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  141. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  142. dispatch_semaphore_signal(semaphore);
  143. }];
  144. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  145. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  146. dispatch_semaphore_signal(semaphore);
  147. }];
  148. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  149. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  150. *token = returnToken;
  151. return returnError;
  152. }
  153. - (NSError *)storeEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString *)metadata token:(NSString **)token
  154. {
  155. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  156. __block NSError *returnError = nil;
  157. __block NSString *returnToken = nil;
  158. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  159. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  160. [communication setUserAgent:[CCUtility getUserAgent]];
  161. // LOCK
  162. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  163. returnToken = token;
  164. // STORE METADATA
  165. [communication storeEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  166. // UNLOCK
  167. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  168. returnToken = nil;
  169. dispatch_semaphore_signal(semaphore);
  170. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  171. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  172. dispatch_semaphore_signal(semaphore);
  173. }];
  174. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  175. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  176. dispatch_semaphore_signal(semaphore);
  177. }];
  178. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  179. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  180. dispatch_semaphore_signal(semaphore);
  181. }];
  182. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  183. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  184. *token = returnToken;
  185. return returnError;
  186. }
  187. - (NSError *)updateEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString *)metadata token:(NSString **)token
  188. {
  189. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  190. __block NSError *returnError = nil;
  191. __block NSString *returnToken = nil;
  192. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  193. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  194. [communication setUserAgent:[CCUtility getUserAgent]];
  195. // LOCK
  196. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  197. returnToken = token;
  198. // UPDATA METADATA
  199. [communication updateEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  200. // UNLOCK
  201. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  202. returnToken = nil;
  203. dispatch_semaphore_signal(semaphore);
  204. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  205. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  206. dispatch_semaphore_signal(semaphore);
  207. }];
  208. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  209. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  210. dispatch_semaphore_signal(semaphore);
  211. }];
  212. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  213. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  214. dispatch_semaphore_signal(semaphore);
  215. }];
  216. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  217. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  218. *token = returnToken;
  219. return returnError;
  220. }
  221. - (NSError *)lockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString **)token
  222. {
  223. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  224. __block NSError *returnError = nil;
  225. __block NSString *returnToken = nil;
  226. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  227. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  228. [communication setUserAgent:[CCUtility getUserAgent]];
  229. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  230. returnToken = token;
  231. dispatch_semaphore_signal(semaphore);
  232. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  233. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  234. dispatch_semaphore_signal(semaphore);
  235. }];
  236. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  237. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  238. *token = returnToken;
  239. return returnError;
  240. }
  241. - (NSError *)unlockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString *)token
  242. {
  243. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  244. __block NSError *returnError= nil;
  245. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  246. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  247. [communication setUserAgent:[CCUtility getUserAgent]];
  248. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  249. dispatch_semaphore_signal(semaphore);
  250. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  251. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  252. dispatch_semaphore_signal(semaphore);
  253. }];
  254. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  255. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  256. return returnError;
  257. }
  258. @end