NCNetworkingSync.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 *)checkServer:(NSString *)serverUrl 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 checkServer:serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  33. dispatch_semaphore_signal(semaphore);
  34. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  35. returnError = error;
  36. dispatch_semaphore_signal(semaphore);
  37. }];
  38. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  39. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  40. return returnError;
  41. }
  42. - (NSError *)readFile:(NSString *)filePathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  43. {
  44. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  45. __block NSError *returnError = nil;
  46. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  47. [communication setCredentialsWithUser: user andUserID: userID andPassword: password];
  48. [communication setUserAgent:[CCUtility getUserAgent]];
  49. [communication readFile:filePathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  50. dispatch_semaphore_signal(semaphore);
  51. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  52. returnError = error;
  53. dispatch_semaphore_signal(semaphore);
  54. }];
  55. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  56. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  57. return returnError;
  58. }
  59. #pragma --------------------------------------------------------------------------------------------
  60. #pragma mark ===== End-to-End Encryption =====
  61. #pragma --------------------------------------------------------------------------------------------
  62. - (NSError *)lockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString **)token
  63. {
  64. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  65. __block NSError *returnError = nil;
  66. __block NSString *returnToken = nil;
  67. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  68. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  69. [communication setUserAgent:[CCUtility getUserAgent]];
  70. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  71. returnToken = token;
  72. dispatch_semaphore_signal(semaphore);
  73. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  74. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  75. dispatch_semaphore_signal(semaphore);
  76. }];
  77. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  78. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  79. *token = returnToken;
  80. return returnError;
  81. }
  82. - (NSError *)unlockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString *)token
  83. {
  84. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  85. __block NSError *returnError= nil;
  86. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  87. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  88. [communication setUserAgent:[CCUtility getUserAgent]];
  89. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  90. dispatch_semaphore_signal(semaphore);
  91. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  92. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  93. dispatch_semaphore_signal(semaphore);
  94. }];
  95. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  96. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  97. return returnError;
  98. }
  99. - (NSError *)markEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID
  100. {
  101. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  102. __block NSError *returnError= nil;
  103. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  104. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  105. [communication setUserAgent:[CCUtility getUserAgent]];
  106. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  107. dispatch_semaphore_signal(semaphore);
  108. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  109. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  110. dispatch_semaphore_signal(semaphore);
  111. }];
  112. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  113. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  114. return returnError;
  115. }
  116. - (NSError *)deletemarkEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID
  117. {
  118. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  119. __block NSError *returnError= nil;
  120. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  121. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  122. [communication setUserAgent:[CCUtility getUserAgent]];
  123. [communication deletemarkEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  124. dispatch_semaphore_signal(semaphore);
  125. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  126. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Remove folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  127. dispatch_semaphore_signal(semaphore);
  128. }];
  129. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  130. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  131. return returnError;
  132. }
  133. @end