OCNetworking.m 32 KB

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