OCNetworking.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. //
  2. // OCnetworking.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 10/05/15.
  6. // Copyright (c) 2015 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 "OCNetworking.h"
  24. #import "OCFrameworkConstants.h"
  25. #import "OCCommunication.h"
  26. #import "OCErrorMsg.h"
  27. #import "CCUtility.h"
  28. #import "CCGraphics.h"
  29. #import "NSString+Encode.h"
  30. #import "NCBridgeSwift.h"
  31. #import "NCXMLGetAppPasswordParser.h"
  32. @implementation OCNetworking
  33. + (OCNetworking *)sharedManager {
  34. static OCNetworking *sharedManager;
  35. @synchronized(self)
  36. {
  37. if (!sharedManager) {
  38. sharedManager = [OCNetworking new];
  39. sharedManager.checkRemoteUserInProgress = false;
  40. }
  41. return sharedManager;
  42. }
  43. }
  44. - (id)init
  45. {
  46. self = [super init];
  47. [self sharedOCCommunication];
  48. return self;
  49. }
  50. #pragma --------------------------------------------------------------------------------------------
  51. #pragma mark ===== OCCommunication =====
  52. #pragma --------------------------------------------------------------------------------------------
  53. - (OCCommunication *)sharedOCCommunication
  54. {
  55. static OCCommunication* sharedOCCommunication = nil;
  56. if (sharedOCCommunication == nil)
  57. {
  58. // Network
  59. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  60. configuration.allowsCellularAccess = YES;
  61. configuration.discretionary = NO;
  62. configuration.HTTPMaximumConnectionsPerHost = k_maxConcurrentOperation;
  63. configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  64. OCURLSessionManager *networkSessionManager = [[OCURLSessionManager alloc] initWithSessionConfiguration:configuration];
  65. [networkSessionManager.operationQueue setMaxConcurrentOperationCount: k_maxConcurrentOperation];
  66. networkSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  67. // Download
  68. NSURLSessionConfiguration *configurationDownload = [NSURLSessionConfiguration defaultSessionConfiguration];
  69. configurationDownload.allowsCellularAccess = YES;
  70. configurationDownload.discretionary = NO;
  71. configurationDownload.HTTPMaximumConnectionsPerHost = k_maxHTTPConnectionsPerHost;
  72. configurationDownload.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  73. configurationDownload.timeoutIntervalForRequest = k_timeout_upload;
  74. OCURLSessionManager *downloadSessionManager = [[OCURLSessionManager alloc] initWithSessionConfiguration:configurationDownload];
  75. [downloadSessionManager.operationQueue setMaxConcurrentOperationCount:k_maxHTTPConnectionsPerHost];
  76. [downloadSessionManager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition (NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential) {
  77. return NSURLSessionAuthChallengePerformDefaultHandling;
  78. }];
  79. // Upload
  80. NSURLSessionConfiguration *configurationUpload = [NSURLSessionConfiguration defaultSessionConfiguration];
  81. configurationUpload.allowsCellularAccess = YES;
  82. configurationUpload.discretionary = NO;
  83. configurationUpload.HTTPMaximumConnectionsPerHost = k_maxHTTPConnectionsPerHost;
  84. configurationUpload.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  85. configurationUpload.timeoutIntervalForRequest = k_timeout_upload;
  86. OCURLSessionManager *uploadSessionManager = [[OCURLSessionManager alloc] initWithSessionConfiguration:configurationUpload];
  87. [uploadSessionManager.operationQueue setMaxConcurrentOperationCount:k_maxHTTPConnectionsPerHost];
  88. [uploadSessionManager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition (NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential) {
  89. return NSURLSessionAuthChallengePerformDefaultHandling;
  90. }];
  91. sharedOCCommunication = [[OCCommunication alloc] initWithUploadSessionManager:uploadSessionManager andDownloadSessionManager:downloadSessionManager andNetworkSessionManager:networkSessionManager];
  92. }
  93. return sharedOCCommunication;
  94. }
  95. #pragma --------------------------------------------------------------------------------------------
  96. #pragma mark ===== Share =====
  97. #pragma --------------------------------------------------------------------------------------------
  98. - (void)readShareWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  99. {
  100. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  101. if (tableAccount == nil) {
  102. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  103. } else if ([CCUtility getPassword:account].length == 0) {
  104. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  105. } else if ([CCUtility getCertificateError:account]) {
  106. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  107. }
  108. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  109. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  110. [communication setUserAgent:[CCUtility getUserAgent]];
  111. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  112. completion(account, items, nil, 0);
  113. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  114. NSString *message;
  115. NSInteger errorCode = response.statusCode;
  116. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  117. errorCode = error.code;
  118. // Error
  119. if (errorCode == 503) {
  120. message = NSLocalizedString(@"_server_error_retry_", nil);
  121. } else {
  122. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  123. }
  124. completion(account, nil, message, errorCode);
  125. }];
  126. }
  127. - (void)readShareWithAccount:(NSString *)account path:(NSString *)path completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  128. {
  129. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  130. if (tableAccount == nil) {
  131. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  132. } else if ([CCUtility getPassword:account].length == 0) {
  133. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  134. } else if ([CCUtility getCertificateError:account]) {
  135. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  136. }
  137. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  138. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  139. [communication setUserAgent:[CCUtility getUserAgent]];
  140. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] andPath:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer) {
  141. completion(account, listOfShared, nil, 0);
  142. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  143. NSString *message;
  144. NSInteger errorCode = response.statusCode;
  145. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  146. errorCode = error.code;
  147. // Error
  148. if (errorCode == 503) {
  149. message = NSLocalizedString(@"_server_error_retry_", nil);
  150. } else {
  151. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  152. }
  153. completion(account, nil, message, errorCode);
  154. }];
  155. }
  156. - (void)shareWithAccount:(NSString *)account fileName:(NSString *)fileName password:(NSString *)password permission:(NSInteger)permission hideDownload:(BOOL)hideDownload completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  157. {
  158. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  159. if (tableAccount == nil) {
  160. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  161. } else if ([CCUtility getPassword:account].length == 0) {
  162. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  163. } else if ([CCUtility getCertificateError:account]) {
  164. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  165. }
  166. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  167. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  168. [communication setUserAgent:[CCUtility getUserAgent]];
  169. [communication shareFileOrFolderByServer:[tableAccount.url stringByAppendingString:@"/"] andFileOrFolderPath:[fileName encodeString:NSUTF8StringEncoding] andPassword:[password encodeString:NSUTF8StringEncoding] andPermission:permission andHideDownload:hideDownload onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  170. completion(account, nil, 0);
  171. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  172. NSString *message;
  173. NSInteger errorCode = response.statusCode;
  174. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  175. errorCode = error.code;
  176. // Error
  177. if (errorCode == 503) {
  178. message = NSLocalizedString(@"_server_error_retry_", nil);
  179. } else {
  180. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  181. }
  182. completion(account, message, errorCode);
  183. }];
  184. }
  185. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  186. - (void)shareUserGroupWithAccount:(NSString *)account userOrGroup:(NSString *)userOrGroup fileName:(NSString *)fileName permission:(NSInteger)permission shareeType:(NSInteger)shareeType completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  187. {
  188. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  189. if (tableAccount == nil) {
  190. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  191. } else if ([CCUtility getPassword:account].length == 0) {
  192. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  193. } else if ([CCUtility getCertificateError:account]) {
  194. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  195. }
  196. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  197. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  198. [communication setUserAgent:[CCUtility getUserAgent]];
  199. [communication shareWith:userOrGroup shareeType:shareeType inServer:[tableAccount.url stringByAppendingString:@"/"] andFileOrFolderPath:[fileName encodeString:NSUTF8StringEncoding] andPermissions:permission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  200. completion(account, nil, 0);
  201. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  202. NSString *message;
  203. NSInteger errorCode = response.statusCode;
  204. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  205. errorCode = error.code;
  206. // Error
  207. if (errorCode == 503) {
  208. message = NSLocalizedString(@"_server_error_retry_", nil);
  209. } else {
  210. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  211. }
  212. completion(account, message, errorCode);
  213. }];
  214. }
  215. - (void)shareUpdateAccount:(NSString *)account shareID:(NSInteger)shareID password:(NSString *)password note:(NSString *)note permission:(NSInteger)permission expirationTime:(NSString *)expirationTime hideDownload:(BOOL)hideDownload completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  216. {
  217. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  218. if (tableAccount == nil) {
  219. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  220. } else if ([CCUtility getPassword:account].length == 0) {
  221. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  222. } else if ([CCUtility getCertificateError:account]) {
  223. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  224. }
  225. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  226. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  227. [communication setUserAgent:[CCUtility getUserAgent]];
  228. [communication updateShare:shareID ofServerPath:[tableAccount.url stringByAppendingString:@"/"] withPasswordProtect:[password encodeString:NSUTF8StringEncoding] andNote:note andExpirationTime:expirationTime andPermissions:permission andHideDownload:hideDownload onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  229. completion(account, nil, 0);
  230. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  231. NSString *message;
  232. NSInteger errorCode = response.statusCode;
  233. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  234. errorCode = error.code;
  235. // Error
  236. if (errorCode == 503) {
  237. message = NSLocalizedString(@"_server_error_retry_", nil);
  238. } else {
  239. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  240. }
  241. completion(account, message, errorCode);
  242. }];
  243. }
  244. - (void)unshareAccount:(NSString *)account shareID:(NSInteger)shareID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  245. {
  246. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  247. if (tableAccount == nil) {
  248. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  249. } else if ([CCUtility getPassword:account].length == 0) {
  250. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  251. } else if ([CCUtility getCertificateError:account]) {
  252. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  253. }
  254. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  255. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  256. [communication setUserAgent:[CCUtility getUserAgent]];
  257. [communication unShareFileOrFolderByServer:[tableAccount.url stringByAppendingString:@"/"] andIdRemoteShared:shareID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  258. completion(account, nil, 0);
  259. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  260. NSString *message;
  261. NSInteger errorCode = response.statusCode;
  262. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  263. errorCode = error.code;
  264. // Error
  265. if (errorCode == 503) {
  266. message = NSLocalizedString(@"_server_error_retry_", nil);
  267. } else {
  268. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  269. }
  270. completion(account, message, errorCode);
  271. }];
  272. }
  273. - (void)getUserGroupWithAccount:(NSString *)account searchString:(NSString *)searchString completion:(void (^)(NSString *account, NSArray *item, NSString *message, NSInteger errorCode))completion
  274. {
  275. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  276. if (tableAccount == nil) {
  277. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  278. } else if ([CCUtility getPassword:account].length == 0) {
  279. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  280. } else if ([CCUtility getCertificateError:account]) {
  281. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  282. }
  283. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  284. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  285. [communication setUserAgent:[CCUtility getUserAgent]];
  286. [communication searchUsersAndGroupsWith:searchString forPage:1 with:50 ofServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  287. completion(account, itemList, nil, 0);
  288. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  289. NSString *message;
  290. NSInteger errorCode = response.statusCode;
  291. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  292. errorCode = error.code;
  293. // Error
  294. if (errorCode == 503) {
  295. message = NSLocalizedString(@"_server_error_retry_", nil);
  296. } else {
  297. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  298. }
  299. completion(account, nil, message, errorCode);
  300. }];
  301. }
  302. #pragma --------------------------------------------------------------------------------------------
  303. #pragma mark ===== Third Parts =====
  304. #pragma --------------------------------------------------------------------------------------------
  305. - (void)getHCUserProfileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
  306. {
  307. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  308. if (tableAccount == nil) {
  309. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  310. } else if ([CCUtility getPassword:account].length == 0) {
  311. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  312. } else if ([CCUtility getCertificateError:account]) {
  313. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  314. }
  315. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  316. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  317. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  318. [communication setUserAgent:[CCUtility getUserAgent]];
  319. [communication getHCUserProfile:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  320. completion(account, userProfile, nil, 0);
  321. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  322. NSString *message;
  323. NSInteger errorCode = response.statusCode;
  324. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  325. errorCode = error.code;
  326. // Server Unauthorized
  327. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  328. #ifndef EXTENSION
  329. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  330. #endif
  331. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  332. [CCUtility setCertificateError:account error:YES];
  333. }
  334. // Error
  335. if (errorCode == 503)
  336. message = NSLocalizedString(@"_server_error_retry_", nil);
  337. else
  338. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  339. completion(account, nil,message, errorCode);
  340. }];
  341. }
  342. - (void)putHCUserProfileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl address:(NSString *)address businesssize:(NSString *)businesssize businesstype:(NSString *)businesstype city:(NSString *)city company:(NSString *)company country:(NSString *)country displayname:(NSString *)displayname email:(NSString *)email phone:(NSString *)phone role_:(NSString *)role_ twitter:(NSString *)twitter website:(NSString *)website zip:(NSString *)zip completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  343. {
  344. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  345. if (tableAccount == nil) {
  346. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  347. } else if ([CCUtility getPassword:account].length == 0) {
  348. completion(account, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  349. } else if ([CCUtility getCertificateError:account]) {
  350. completion(account, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  351. }
  352. // Create JSON
  353. NSMutableDictionary *dataDic = [NSMutableDictionary new];
  354. if (address) [dataDic setValue:address forKey:@"address"];
  355. if (businesssize) {
  356. if ([businesssize isEqualToString:@"1-4"]) { [dataDic setValue:[NSNumber numberWithInt:1] forKey:@"businesssize"]; }
  357. else if ([businesssize isEqualToString:@"5-9"]) { [dataDic setValue:[NSNumber numberWithInt:5] forKey:@"businesssize"]; }
  358. else if ([businesssize isEqualToString:@"10-19"]) { [dataDic setValue:[NSNumber numberWithInt:10] forKey:@"businesssize"]; }
  359. else if ([businesssize isEqualToString:@"20-49"]) { [dataDic setValue:[NSNumber numberWithInt:20] forKey:@"businesssize"]; }
  360. else if ([businesssize isEqualToString:@"50-99"]) { [dataDic setValue:[NSNumber numberWithInt:50] forKey:@"businesssize"]; }
  361. else if ([businesssize isEqualToString:@"100-249"]) { [dataDic setValue:[NSNumber numberWithInt:100] forKey:@"businesssize"]; }
  362. else if ([businesssize isEqualToString:@"250-499"]) { [dataDic setValue:[NSNumber numberWithInt:250] forKey:@"businesssize"]; }
  363. else if ([businesssize isEqualToString:@"500-999"]) { [dataDic setValue:[NSNumber numberWithInt:500] forKey:@"businesssize"]; }
  364. else if ([businesssize isEqualToString:@"1000+"]) { [dataDic setValue:[NSNumber numberWithInt:1000] forKey:@"businesssize"]; }
  365. }
  366. if (businesstype) [dataDic setValue:businesstype forKey:@"businesstype"];
  367. if (city) [dataDic setValue:city forKey:@"city"];
  368. if (company) [dataDic setValue:company forKey:@"company"];
  369. if (country) [dataDic setValue:country forKey:@"country"];
  370. if (displayname) [dataDic setValue:displayname forKey:@"displayname"];
  371. if (email) [dataDic setValue:email forKey:@"email"];
  372. if (phone) [dataDic setValue:phone forKey:@"phone"];
  373. if (role_) [dataDic setValue:role_ forKey:@"role"];
  374. if (twitter) [dataDic setValue:twitter forKey:@"twitter"];
  375. if (website) [dataDic setValue:website forKey:@"website"];
  376. if (zip) [dataDic setValue:zip forKey:@"zip"];
  377. NSString *data = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil] encoding:NSUTF8StringEncoding];
  378. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/settings/%@", serverUrl, tableAccount.userID];
  379. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  380. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  381. [communication setUserAgent:[CCUtility getUserAgent]];
  382. [communication putHCUserProfile:serverPath data:data onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  383. completion(account, nil, 0);
  384. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  385. NSString *message;
  386. NSInteger errorCode = response.statusCode;
  387. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  388. errorCode = error.code;
  389. // Server Unauthorized
  390. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  391. #ifndef EXTENSION
  392. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  393. #endif
  394. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  395. [CCUtility setCertificateError:account error:YES];
  396. }
  397. // Error
  398. if (errorCode == 503)
  399. message = NSLocalizedString(@"_server_error_retry_", nil);
  400. else
  401. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  402. completion(account, message, errorCode);
  403. }];
  404. }
  405. - (void)getHCFeaturesWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, HCFeatures *features, NSString *message, NSInteger errorCode))completion
  406. {
  407. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  408. if (tableAccount == nil) {
  409. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  410. } else if ([CCUtility getPassword:account].length == 0) {
  411. completion(account, nil, NSLocalizedString(@"_bad_username_password_", nil), kOCErrorServerUnauthorized);
  412. } else if ([CCUtility getCertificateError:account]) {
  413. completion(account, nil, NSLocalizedString(@"_ssl_certificate_untrusted_", nil), NSURLErrorServerCertificateUntrusted);
  414. }
  415. NSString *serverPath = [NSString stringWithFormat:@"%@/ocs/v2.php/apps/handwerkcloud/api/v1/features/%@", serverUrl, tableAccount.userID];
  416. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  417. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  418. [communication setUserAgent:[CCUtility getUserAgent]];
  419. [communication getHCFeatures:serverPath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, HCFeatures *features, NSString *redirectedServer) {
  420. completion(account, features, nil, 0);
  421. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  422. NSString *message;
  423. NSInteger errorCode = response.statusCode;
  424. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  425. errorCode = error.code;
  426. // Server Unauthorized
  427. if (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden) {
  428. #ifndef EXTENSION
  429. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:account];
  430. #endif
  431. } else if (errorCode == NSURLErrorServerCertificateUntrusted) {
  432. [CCUtility setCertificateError:account error:YES];
  433. }
  434. // Error
  435. if (errorCode == 503)
  436. message = NSLocalizedString(@"_server_error_retry_", nil);
  437. else
  438. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  439. completion(account, nil,message, errorCode);
  440. }];
  441. }
  442. #pragma --------------------------------------------------------------------------------------------
  443. #pragma mark ===== didReceiveChallenge =====
  444. #pragma --------------------------------------------------------------------------------------------
  445. -(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
  446. {
  447. // The pinnning check
  448. if ([[NCNetworking shared] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  449. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  450. } else {
  451. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  452. }
  453. }
  454. @end
  455. #pragma --------------------------------------------------------------------------------------------
  456. #pragma mark ===== OCURLSessionManager =====
  457. #pragma --------------------------------------------------------------------------------------------
  458. @implementation OCURLSessionManager
  459. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  460. {
  461. // The pinnning check
  462. if ([[NCNetworking shared] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  463. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  464. } else {
  465. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  466. }
  467. }
  468. @end