NCSettingsController.m 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /**
  2. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #import "NCSettingsController.h"
  6. @import NextcloudKit;
  7. #import <openssl/rsa.h>
  8. #import <openssl/pem.h>
  9. #import <openssl/bio.h>
  10. #import <openssl/bn.h>
  11. #import <openssl/sha.h>
  12. #import <openssl/err.h>
  13. #import "JDStatusBarNotification.h"
  14. #import "NCAPIController.h"
  15. #import "NCAppBranding.h"
  16. #import "NCConnectionController.h"
  17. #import "NCDatabaseManager.h"
  18. #import "NCExternalSignalingController.h"
  19. #import "NCKeyChainController.h"
  20. #import "NCRoomsManager.h"
  21. #import "NCUserInterfaceController.h"
  22. #import "NCUserDefaults.h"
  23. #import "NCChatFileController.h"
  24. #import "NotificationCenterNotifications.h"
  25. #import "NextcloudTalk-Swift.h"
  26. NSString * const NCSettingsControllerDidChangeActiveAccountNotification = @"NCSettingsControllerDidChangeActiveAccountNotification";
  27. @interface NCPushNotificationKeyPair : NSObject
  28. @property (nonatomic, copy) NSData *publicKey;
  29. @property (nonatomic, copy) NSData *privateKey;
  30. @end
  31. @implementation NCPushNotificationKeyPair
  32. @end
  33. @implementation NCSettingsController
  34. NSString * const kUserProfileUserId = @"id";
  35. NSString * const kUserProfileDisplayName = @"displayname";
  36. NSString * const kUserProfileDisplayNameScope = @"displaynameScope";
  37. NSString * const kUserProfileEmail = @"email";
  38. NSString * const kUserProfileEmailScope = @"emailScope";
  39. NSString * const kUserProfilePhone = @"phone";
  40. NSString * const kUserProfilePhoneScope = @"phoneScope";
  41. NSString * const kUserProfileAddress = @"address";
  42. NSString * const kUserProfileAddressScope = @"addressScope";
  43. NSString * const kUserProfileWebsite = @"website";
  44. NSString * const kUserProfileWebsiteScope = @"websiteScope";
  45. NSString * const kUserProfileTwitter = @"twitter";
  46. NSString * const kUserProfileTwitterScope = @"twitterScope";
  47. NSString * const kUserProfileAvatarScope = @"avatarScope";
  48. NSString * const kUserProfileScopePrivate = @"v2-private";
  49. NSString * const kUserProfileScopeLocal = @"v2-local";
  50. NSString * const kUserProfileScopeFederated = @"v2-federated";
  51. NSString * const kUserProfileScopePublished = @"v2-published";
  52. NSString * const kPreferredFileSorting = @"preferredFileSorting";
  53. NSString * const kContactSyncEnabled = @"contactSyncEnabled";
  54. NSString * const kDidReceiveCallsFromOldAccount = @"receivedCallsFromOldAccount";
  55. + (NCSettingsController *)sharedInstance
  56. {
  57. static dispatch_once_t once;
  58. static NCSettingsController *sharedInstance;
  59. dispatch_once(&once, ^{
  60. sharedInstance = [[self alloc] init];
  61. });
  62. return sharedInstance;
  63. }
  64. - (id)init
  65. {
  66. self = [super init];
  67. if (self) {
  68. _videoSettingsModel = [[ARDSettingsModel alloc] init];
  69. _signalingConfigurations = [NSMutableDictionary new];
  70. _externalSignalingControllers = [NSMutableDictionary new];
  71. [self configureDatabase];
  72. [self checkStoredDataInKechain];
  73. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRevokedResponseReceived:) name:NCTokenRevokedResponseReceivedNotification object:nil];
  74. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(upgradeRequiredResponseReceived:) name:NCUpgradeRequiredResponseReceivedNotification object:nil];
  75. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(talkConfigurationHasChanged:) name:NCTalkConfigurationHashChangedNotification object:nil];
  76. // No need to create the file immediately -> dispatch to background
  77. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^(void){
  78. @autoreleasepool {
  79. [self createAccountsFile];
  80. }
  81. });
  82. }
  83. return self;
  84. }
  85. #pragma mark - Database
  86. - (void)configureDatabase
  87. {
  88. // Init database
  89. [NCDatabaseManager sharedInstance];
  90. }
  91. - (void)checkStoredDataInKechain
  92. {
  93. // Removed data stored in the Keychain if there are no accounts configured
  94. // This step should be always done before the possible account migration
  95. if ([[NCDatabaseManager sharedInstance] numberOfAccounts] == 0) {
  96. NSLog(@"Removing all data stored in Keychain");
  97. [[NCKeyChainController sharedInstance] removeAllItems];
  98. }
  99. }
  100. #pragma mark - User accounts
  101. - (void)addNewAccountForUser:(NSString *)user withToken:(NSString *)token inServer:(NSString *)server
  102. {
  103. NSString *accountId = [[NCDatabaseManager sharedInstance] accountIdForUser:user inServer:server];
  104. TalkAccount *account = [[NCDatabaseManager sharedInstance] talkAccountForAccountId:accountId];
  105. if (!account) {
  106. [[NCDatabaseManager sharedInstance] createAccountForUser:user inServer:server];
  107. [[NCDatabaseManager sharedInstance] setActiveAccountWithAccountId:accountId];
  108. [[NCKeyChainController sharedInstance] setToken:token forAccountId:accountId];
  109. TalkAccount *talkAccount = [[NCDatabaseManager sharedInstance] talkAccountForAccountId:accountId];
  110. [[NCAPIController sharedInstance] createAPISessionManagerForAccount:talkAccount];
  111. [self subscribeForPushNotificationsForAccountId:accountId withCompletionBlock:nil];
  112. [self createAccountsFile];
  113. } else {
  114. [self setActiveAccountWithAccountId:accountId];
  115. [[JDStatusBarNotificationPresenter sharedPresenter] presentWithText:NSLocalizedString(@"Account already added", nil) dismissAfterDelay:4.0f includedStyle:JDStatusBarNotificationIncludedStyleSuccess];
  116. }
  117. }
  118. - (void)setActiveAccountWithAccountId:(NSString *)accountId
  119. {
  120. [[NCUserInterfaceController sharedInstance] presentConversationsList];
  121. [[NCDatabaseManager sharedInstance] setActiveAccountWithAccountId:accountId];
  122. [[NCDatabaseManager sharedInstance] resetUnreadBadgeNumberForAccountId:accountId];
  123. [[NCNotificationController sharedInstance] removeAllNotificationsForAccountId:accountId];
  124. [[NCConnectionController sharedInstance] checkAppState];
  125. NSMutableDictionary *userInfo = [NSMutableDictionary new];
  126. [userInfo setObject:accountId forKey:@"accountId"];
  127. [[NSNotificationCenter defaultCenter] postNotificationName:NCSettingsControllerDidChangeActiveAccountNotification
  128. object:self
  129. userInfo:userInfo];
  130. }
  131. - (void)createAccountsFile
  132. {
  133. if (!useAppsGroup) {
  134. return;
  135. }
  136. // Create accounts data
  137. NSURL *appsGroupFolderURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:appsGroupIdentifier];
  138. NSMutableArray *accounts = [NSMutableArray new];
  139. for (TalkAccount *account in [[NCDatabaseManager sharedInstance] allAccounts]) {
  140. UIImage *accountImage = [[NCAPIController sharedInstance] userProfileImageForAccount:account withStyle:UIUserInterfaceStyleLight];
  141. if (accountImage) {
  142. accountImage = [NCUtils roundedImageFromImage:accountImage];
  143. }
  144. DataAccounts *accountData = [[DataAccounts alloc] initWithUrl:account.server user:account.user name:account.userDisplayName image:accountImage];
  145. [accounts addObject:accountData];
  146. }
  147. NKShareAccounts *shareAccounts = [[NKShareAccounts alloc] init];
  148. NSError *error = [shareAccounts putShareAccountsAt:appsGroupFolderURL app:@"nextcloudtalk" dataAccounts:accounts];
  149. NSLog(@"Created accounts file. Error: %@", error);
  150. }
  151. #pragma mark - Notifications
  152. - (void)tokenRevokedResponseReceived:(NSNotification *)notification
  153. {
  154. NSString *accountId = [notification.userInfo objectForKey:@"accountId"];
  155. [self logoutAccountWithAccountId:accountId withCompletionBlock:^(NSError *error) {
  156. [[NCUserInterfaceController sharedInstance] presentConversationsList];
  157. [[NCUserInterfaceController sharedInstance] presentLoggedOutInvalidCredentialsAlert];
  158. [[NCConnectionController sharedInstance] checkAppState];
  159. }];
  160. }
  161. - (void)upgradeRequiredResponseReceived:(NSNotification *)notification
  162. {
  163. NSString *accountId = [notification.userInfo objectForKey:@"accountId"];
  164. if (!_updateAlertController || ![_updateAlertControllerAccountId isEqualToString:accountId]) {
  165. [self createUpdateAlertContollerForAccountId:accountId];
  166. }
  167. [[NCUserInterfaceController sharedInstance] presentAlertIfNotPresentedAlready:_updateAlertController];
  168. }
  169. - (void)createUpdateAlertContollerForAccountId:(NSString *)accountId
  170. {
  171. NSString *appStoreURLString = @"itms-apps://itunes.apple.com/app/id";
  172. BOOL canOpenAppStore = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:appStoreURLString]];
  173. NSString *messageNotification = NSLocalizedString(@"The app is too old and no longer supported by this server.", nil);
  174. NSString *messageAction = canOpenAppStore ? NSLocalizedString(@"Please update.", nil) : NSLocalizedString(@"Please contact your system administrator.", nil);
  175. NSString *message = [NSString stringWithFormat:@"%@ %@", messageNotification, messageAction];
  176. _updateAlertController = [UIAlertController
  177. alertControllerWithTitle:NSLocalizedString(@"App is outdated", nil)
  178. message:message
  179. preferredStyle:UIAlertControllerStyleAlert];
  180. _updateAlertControllerAccountId = accountId;
  181. if (canOpenAppStore) {
  182. UIAlertAction* updateButton = [UIAlertAction
  183. actionWithTitle:NSLocalizedString(@"Update", nil)
  184. style:UIAlertActionStyleDefault
  185. handler:^(UIAlertAction * _Nonnull action) {
  186. [[NCAPIController sharedInstance] getAppStoreAppIdWithCompletionBlock:^(NSString *appId, NSError *error) {
  187. if (appId.length > 0) {
  188. NSURL *appStoreURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", appStoreURLString, appId]];
  189. [[UIApplication sharedApplication] openURL:appStoreURL options:@{} completionHandler:nil];
  190. }
  191. self->_updateAlertControllerAccountId = nil;
  192. }];
  193. }];
  194. [_updateAlertController addAction:updateButton];
  195. }
  196. NSArray *inactiveAccounts = [[NCDatabaseManager sharedInstance] inactiveAccounts];
  197. if (inactiveAccounts.count > 0) {
  198. UIAlertAction* switchAccountButton = [UIAlertAction
  199. actionWithTitle:NSLocalizedString(@"Switch account", nil)
  200. style:UIAlertActionStyleDefault
  201. handler:^(UIAlertAction * _Nonnull action) {
  202. [self switchToAnyInactiveAccount];
  203. self->_updateAlertControllerAccountId = nil;
  204. }];
  205. [_updateAlertController addAction:switchAccountButton];
  206. }
  207. UIAlertAction* logoutButton = [UIAlertAction
  208. actionWithTitle:NSLocalizedString(@"Log out", nil)
  209. style:UIAlertActionStyleDestructive
  210. handler:^(UIAlertAction * _Nonnull action) {
  211. [[NCUserInterfaceController sharedInstance] logOutAccountWithAccountId:accountId];
  212. self->_updateAlertControllerAccountId = nil;
  213. }];
  214. [_updateAlertController addAction:logoutButton];
  215. }
  216. - (void)talkConfigurationHasChanged:(NSNotification *)notification
  217. {
  218. TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
  219. NSString *accountId = [notification.userInfo objectForKey:@"accountId"];
  220. NSString *configurationHash = [notification.userInfo objectForKey:@"configurationHash"];
  221. if (!accountId || !configurationHash || ![activeAccount.accountId isEqualToString:accountId]) {
  222. return;
  223. }
  224. [self getCapabilitiesForAccountId:accountId withCompletionBlock:^(NSError *error) {
  225. if (error) {
  226. return;
  227. }
  228. BGTaskHelper *bgTask = [BGTaskHelper startBackgroundTaskWithName:@"NCUpdateSignalingConfiguration" expirationHandler:nil];
  229. [self updateSignalingConfigurationForAccountId:accountId withCompletionBlock:^(NCExternalSignalingController * _Nullable signalingServer, NSError *error) {
  230. if (!error) {
  231. [[NCDatabaseManager sharedInstance] updateTalkConfigurationHashForAccountId:accountId withHash:configurationHash];
  232. }
  233. [bgTask stopBackgroundTask];
  234. }];
  235. }];
  236. }
  237. #pragma mark - User defaults
  238. - (NCPreferredFileSorting)getPreferredFileSorting
  239. {
  240. NCPreferredFileSorting sorting = (NCPreferredFileSorting)[[[NSUserDefaults standardUserDefaults] objectForKey:kPreferredFileSorting] integerValue];
  241. if (!sorting) {
  242. sorting = NCModificationDateSorting;
  243. [[NSUserDefaults standardUserDefaults] setObject:@(sorting) forKey:kPreferredFileSorting];
  244. }
  245. return sorting;
  246. }
  247. - (void)setPreferredFileSorting:(NCPreferredFileSorting)sorting
  248. {
  249. [[NSUserDefaults standardUserDefaults] setObject:@(sorting) forKey:kPreferredFileSorting];
  250. }
  251. - (BOOL)isContactSyncEnabled
  252. {
  253. // Migration from global setting to per-account setting
  254. if ([[[NSUserDefaults standardUserDefaults] objectForKey:kContactSyncEnabled] boolValue]) {
  255. // If global setting was enabled then we enable contact sync for all accounts
  256. RLMRealm *realm = [RLMRealm defaultRealm];
  257. [realm beginWriteTransaction];
  258. for (TalkAccount *account in [TalkAccount allObjects]) {
  259. account.hasContactSyncEnabled = YES;
  260. }
  261. [realm commitWriteTransaction];
  262. // Remove global setting
  263. [[NSUserDefaults standardUserDefaults] removeObjectForKey:kContactSyncEnabled];
  264. [[NSUserDefaults standardUserDefaults] synchronize];
  265. return YES;
  266. }
  267. return [[NCDatabaseManager sharedInstance] activeAccount].hasContactSyncEnabled;
  268. }
  269. - (void)setContactSync:(BOOL)enabled
  270. {
  271. RLMRealm *realm = [RLMRealm defaultRealm];
  272. [realm beginWriteTransaction];
  273. TalkAccount *account = [TalkAccount objectsWhere:(@"active = true")].firstObject;
  274. account.hasContactSyncEnabled = enabled;
  275. [realm commitWriteTransaction];
  276. }
  277. - (BOOL)didReceiveCallsFromOldAccount
  278. {
  279. BOOL didReceiveCallsFromOldAccount = [[[NSUserDefaults standardUserDefaults] objectForKey:kDidReceiveCallsFromOldAccount] boolValue];
  280. return didReceiveCallsFromOldAccount;
  281. }
  282. - (void)setDidReceiveCallsFromOldAccount:(BOOL)receivedOldCalls
  283. {
  284. [[NSUserDefaults standardUserDefaults] setObject:@(receivedOldCalls) forKey:kDidReceiveCallsFromOldAccount];
  285. }
  286. #pragma mark - User Profile
  287. - (void)getUserProfileForAccountId:(NSString *)accountId withCompletionBlock:(UpdatedProfileCompletionBlock)block
  288. {
  289. TalkAccount *account = [[NCDatabaseManager sharedInstance] talkAccountForAccountId:accountId];
  290. if (!account) {
  291. if (block) {
  292. NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:nil];
  293. block(error);
  294. }
  295. return;
  296. }
  297. [[NCAPIController sharedInstance] getUserProfileForAccount:account withCompletionBlock:^(NSDictionary *userProfile, NSError *error) {
  298. if (!error) {
  299. id emailObject = [userProfile objectForKey:kUserProfileEmail];
  300. NSString *email = emailObject;
  301. if (!emailObject || [emailObject isEqual:[NSNull null]]) {
  302. email = @"";
  303. }
  304. RLMRealm *realm = [RLMRealm defaultRealm];
  305. [realm beginWriteTransaction];
  306. NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", account.accountId];
  307. TalkAccount *managedActiveAccount = [TalkAccount objectsWithPredicate:query].firstObject;
  308. managedActiveAccount.userId = [userProfile objectForKey:kUserProfileUserId];
  309. // "display-name" is returned by /cloud/user endpoint
  310. // change to kUserProfileDisplayName ("displayName") when using /cloud/users/{userId} endpoint
  311. managedActiveAccount.userDisplayName = [userProfile objectForKey:@"display-name"];
  312. managedActiveAccount.userDisplayNameScope = [userProfile objectForKey:kUserProfileDisplayNameScope];
  313. managedActiveAccount.phone = [userProfile objectForKey:kUserProfilePhone];
  314. managedActiveAccount.phoneScope = [userProfile objectForKey:kUserProfilePhoneScope];
  315. managedActiveAccount.email = email;
  316. managedActiveAccount.emailScope = [userProfile objectForKey:kUserProfileEmailScope];
  317. managedActiveAccount.address = [userProfile objectForKey:kUserProfileAddress];
  318. managedActiveAccount.addressScope = [userProfile objectForKey:kUserProfileAddressScope];
  319. managedActiveAccount.website = [userProfile objectForKey:kUserProfileWebsite];
  320. managedActiveAccount.websiteScope = [userProfile objectForKey:kUserProfileWebsiteScope];
  321. managedActiveAccount.twitter = [userProfile objectForKey:kUserProfileTwitter];
  322. managedActiveAccount.twitterScope = [userProfile objectForKey:kUserProfileTwitterScope];
  323. managedActiveAccount.avatarScope = [userProfile objectForKey:kUserProfileAvatarScope];
  324. [realm commitWriteTransaction];
  325. TalkAccount *unmanagedUpdatedAccount = [[TalkAccount alloc] initWithValue:managedActiveAccount];
  326. [[NCAPIController sharedInstance] saveProfileImageForAccount:unmanagedUpdatedAccount];
  327. if (block) {
  328. block(nil);
  329. }
  330. } else {
  331. NSLog(@"Error while getting the user profile");
  332. if (block) {
  333. block(error);
  334. }
  335. }
  336. }];
  337. }
  338. - (void)logoutAccountWithAccountId:(NSString *)accountId withCompletionBlock:(LogoutCompletionBlock)block
  339. {
  340. TalkAccount *removingAccount = [[NCDatabaseManager sharedInstance] talkAccountForAccountId:accountId];
  341. if (removingAccount.deviceIdentifier) {
  342. [[NCAPIController sharedInstance] unsubscribeAccount:removingAccount fromNextcloudServerWithCompletionBlock:^(NSError *error) {
  343. if (!error) {
  344. NSLog(@"Unsubscribed from NC server!!!");
  345. } else {
  346. NSLog(@"Error while unsubscribing from NC server.");
  347. }
  348. }];
  349. [[NCAPIController sharedInstance] unsubscribeAccount:removingAccount fromPushServerWithCompletionBlock:^(NSError *error) {
  350. if (!error) {
  351. NSLog(@"Unsubscribed from Push Notification server!!!");
  352. } else {
  353. NSLog(@"Error while unsubscribing from Push Notification server.");
  354. }
  355. }];
  356. }
  357. NCExternalSignalingController *extSignalingController = [self externalSignalingControllerForAccountId:removingAccount.accountId];
  358. [extSignalingController disconnect];
  359. [[NCAPIController sharedInstance] removeProfileImageForAccount:removingAccount];
  360. [[NCDatabaseManager sharedInstance] removeAccountWithAccountId:removingAccount.accountId];
  361. [[[NCChatFileController alloc] init] deleteDownloadDirectoryForAccount:removingAccount];
  362. [[[NCRoomsManager sharedInstance] chatViewController] leaveChat];
  363. [self createAccountsFile];
  364. // Activate any of the inactive accounts
  365. [self switchToAnyInactiveAccount];
  366. if (block) block(nil);
  367. }
  368. - (void)switchToAnyInactiveAccount
  369. {
  370. NSArray *inactiveAccounts = [[NCDatabaseManager sharedInstance] inactiveAccounts];
  371. if (inactiveAccounts.count > 0) {
  372. TalkAccount *inactiveAccount = [inactiveAccounts objectAtIndex:0];
  373. [self setActiveAccountWithAccountId:inactiveAccount.accountId];
  374. }
  375. }
  376. #pragma mark - Signaling Configuration
  377. - (void)updateSignalingConfigurationForAccountId:(NSString *)accountId withCompletionBlock:(UpdateSignalingConfigCompletionBlock)block
  378. {
  379. TalkAccount *account = [[NCDatabaseManager sharedInstance] talkAccountForAccountId:accountId];
  380. if (!account) {
  381. if (block) {
  382. NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:nil];
  383. block(nil, error);
  384. }
  385. return;
  386. }
  387. [[NCAPIController sharedInstance] getSignalingSettingsFor:account forRoom:nil completionBlock:^(SignalingSettings * _Nullable settings, NSError * _Nullable error) {
  388. if (!error) {
  389. if (settings && account && account.accountId) {
  390. NCExternalSignalingController *extSignalingController = [self setSignalingConfigurationForAccountId:account.accountId withSettings:settings];
  391. if (block) {
  392. block(extSignalingController, nil);
  393. }
  394. } else {
  395. NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:nil];
  396. if (block) {
  397. block(nil, error);
  398. }
  399. }
  400. } else {
  401. NSLog(@"Error while getting signaling configuration");
  402. if (block) {
  403. block(nil, error);
  404. }
  405. }
  406. }];
  407. }
  408. - (NCExternalSignalingController * _Nullable)setSignalingConfigurationForAccountId:(NSString *)accountId withSettings:(SignalingSettings * _Nonnull)signalingSettings
  409. {
  410. [self->_signalingConfigurations setObject:signalingSettings forKey:accountId];
  411. if (signalingSettings.server && signalingSettings.server.length > 0 && signalingSettings.ticket && signalingSettings.ticket.length > 0) {
  412. BGTaskHelper *bgTask = [BGTaskHelper startBackgroundTaskWithName:@"NCSetSignalingConfiguration" expirationHandler:nil];
  413. NCExternalSignalingController *extSignalingController = [self->_externalSignalingControllers objectForKey:accountId];
  414. if (extSignalingController) {
  415. [extSignalingController disconnect];
  416. }
  417. TalkAccount *account = [[NCDatabaseManager sharedInstance] talkAccountForAccountId:accountId];
  418. extSignalingController = [[NCExternalSignalingController alloc] initWithAccount:account server:signalingSettings.server andTicket:signalingSettings.ticket];
  419. [self->_externalSignalingControllers setObject:extSignalingController forKey:accountId];
  420. [bgTask stopBackgroundTask];
  421. return extSignalingController;
  422. }
  423. return nil;
  424. }
  425. - (void)ensureSignalingConfigurationForAccountId:(NSString *)accountId withSettings:(SignalingSettings *)settings withCompletionBlock:(EnsureSignalingConfigCompletionBlock)block
  426. {
  427. SignalingSettings *currentSignalingSettings = [_signalingConfigurations objectForKey:accountId];
  428. if (currentSignalingSettings) {
  429. block([self->_externalSignalingControllers objectForKey:accountId]);
  430. } else {
  431. [NCUtils log:@"Ensure signaling configuration -> Setting configuration"];
  432. if (settings) {
  433. // In case settings are provided, we use these provided settings
  434. NCExternalSignalingController *extSignalingController = [self setSignalingConfigurationForAccountId:accountId withSettings:settings];
  435. block(extSignalingController);
  436. } else {
  437. // There were no settings provided for that call, we have to update the settings
  438. [self updateSignalingConfigurationForAccountId:accountId withCompletionBlock:^(NCExternalSignalingController * _Nullable signalingServer, NSError *error) {
  439. block(signalingServer);
  440. }];
  441. }
  442. }
  443. }
  444. - (NCExternalSignalingController *)externalSignalingControllerForAccountId:(NSString *)accountId
  445. {
  446. return [_externalSignalingControllers objectForKey:accountId];
  447. }
  448. - (void)connectDisconnectedExternalSignalingControllers
  449. {
  450. for (NCExternalSignalingController *extSignalingController in self->_externalSignalingControllers.allValues) {
  451. if (extSignalingController.disconnected) {
  452. [extSignalingController connect];
  453. }
  454. }
  455. }
  456. - (void)disconnectAllExternalSignalingControllers
  457. {
  458. for (NCExternalSignalingController *extSignalingController in self->_externalSignalingControllers.allValues) {
  459. [extSignalingController disconnect];
  460. }
  461. }
  462. #pragma mark - Server Capabilities
  463. - (void)getCapabilitiesForAccountId:(NSString *)accountId withCompletionBlock:(GetCapabilitiesCompletionBlock)block
  464. {
  465. TalkAccount *account = [[NCDatabaseManager sharedInstance] talkAccountForAccountId:accountId];
  466. if (!account) {
  467. if (block) {
  468. NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:nil];
  469. block(error);
  470. }
  471. return;
  472. }
  473. [[NCAPIController sharedInstance] getServerCapabilitiesForAccount:account withCompletionBlock:^(NSDictionary *serverCapabilities, NSError *error) {
  474. if (!error && [serverCapabilities isKindOfClass:[NSDictionary class]]) {
  475. BGTaskHelper *bgTask = [BGTaskHelper startBackgroundTaskWithName:@"NCUpdateCapabilitiesTransaction" expirationHandler:nil];
  476. [[NCDatabaseManager sharedInstance] setServerCapabilities:serverCapabilities forAccountId:account.accountId];
  477. [self checkServerCapabilitiesForAccount:account];
  478. [bgTask stopBackgroundTask];
  479. [[NSNotificationCenter defaultCenter] postNotificationName:NCServerCapabilitiesUpdatedNotification
  480. object:self
  481. userInfo:nil];
  482. if (block) {
  483. block(nil);
  484. }
  485. } else {
  486. NSLog(@"Error while getting server capabilities");
  487. if (block) {
  488. block(error);
  489. }
  490. }
  491. }];
  492. }
  493. - (void)checkServerCapabilitiesForAccount:(TalkAccount *)account
  494. {
  495. ServerCapabilities *serverCapabilities = [[NCDatabaseManager sharedInstance] serverCapabilitiesForAccountId:account.accountId];
  496. if (serverCapabilities) {
  497. NSArray *talkFeatures = [serverCapabilities.talkCapabilities valueForKey:@"self"];
  498. if (!talkFeatures || [talkFeatures count] == 0) {
  499. [[NSNotificationCenter defaultCenter] postNotificationName:NCTalkNotInstalledNotification
  500. object:self
  501. userInfo:nil];
  502. } else if (![talkFeatures containsObject:kMinimumRequiredTalkCapability]) {
  503. [[NSNotificationCenter defaultCenter] postNotificationName:NCOutdatedTalkVersionNotification
  504. object:self
  505. userInfo:nil];
  506. }
  507. }
  508. }
  509. - (BOOL)canCreateGroupAndPublicRooms
  510. {
  511. TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
  512. ServerCapabilities *serverCapabilities = [[NCDatabaseManager sharedInstance] serverCapabilitiesForAccountId:activeAccount.accountId];
  513. if (serverCapabilities) {
  514. return serverCapabilities.canCreate;
  515. }
  516. return YES;
  517. }
  518. - (BOOL)isGuestsAppEnabled
  519. {
  520. TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
  521. ServerCapabilities *serverCapabilities = [[NCDatabaseManager sharedInstance] serverCapabilitiesForAccountId:activeAccount.accountId];
  522. if (serverCapabilities) {
  523. return serverCapabilities.guestsAppEnabled;
  524. }
  525. return NO;
  526. }
  527. - (BOOL)isReferenceApiSupported
  528. {
  529. TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
  530. ServerCapabilities *serverCapabilities = [[NCDatabaseManager sharedInstance] serverCapabilitiesForAccountId:activeAccount.accountId];
  531. if (serverCapabilities) {
  532. return serverCapabilities.referenceApiSupported;
  533. }
  534. return NO;
  535. }
  536. - (BOOL)isRecordingEnabled
  537. {
  538. TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
  539. ServerCapabilities *serverCapabilities = [[NCDatabaseManager sharedInstance] serverCapabilitiesForAccountId:activeAccount.accountId];
  540. if (serverCapabilities && [[NCDatabaseManager sharedInstance] serverHasTalkCapability:kCapabilityRecordingV1]) {
  541. return serverCapabilities.recordingEnabled;
  542. }
  543. return NO;
  544. }
  545. #pragma mark - Push Notifications
  546. - (void)subscribeForPushNotificationsForAccountId:(NSString *)accountId withCompletionBlock:(SubscribeForPushNotificationsCompletionBlock)block;
  547. {
  548. #if !TARGET_IPHONE_SIMULATOR
  549. NCPushNotificationKeyPair *keyPair = nil;
  550. NSData *pushNotificationPublicKey = [[NCKeyChainController sharedInstance] pushNotificationPublicKeyForAccountId:accountId];
  551. NSData *pushNotificationPrivateKey = [[NCKeyChainController sharedInstance] pushNotificationPrivateKeyForAccountId:accountId];
  552. if (pushNotificationPublicKey && pushNotificationPrivateKey) {
  553. keyPair = [[NCPushNotificationKeyPair alloc] init];
  554. keyPair.publicKey = pushNotificationPublicKey;
  555. keyPair.privateKey = pushNotificationPrivateKey;
  556. } else {
  557. keyPair = [self generatePushNotificationsKeyPairForAccountId:accountId];
  558. }
  559. if (!keyPair) {
  560. [NCUtils log:@"Error while subscribing: Unable to generate push notifications key pair."];
  561. if (block) {
  562. block(NO);
  563. }
  564. return;
  565. }
  566. NSString *pushToken = [[NCKeyChainController sharedInstance] combinedPushToken];
  567. if (!pushToken) {
  568. [NCUtils log:@"Error while subscribing: Push token is not available."];
  569. if (block) {
  570. block(NO);
  571. }
  572. return;
  573. }
  574. BGTaskHelper *bgTask = [BGTaskHelper startBackgroundTaskWithName:@"PushProxySubscription" expirationHandler:nil];
  575. [[NCAPIController sharedInstance] subscribeAccount:[[NCDatabaseManager sharedInstance] talkAccountForAccountId:accountId] withPublicKey:keyPair.publicKey toNextcloudServerWithCompletionBlock:^(NSDictionary *responseDict, NSError *error) {
  576. if (!error) {
  577. [NCUtils log:@"Subscribed to NC server successfully."];
  578. NSString *publicKey = [responseDict objectForKey:@"publicKey"];
  579. NSString *deviceIdentifier = [responseDict objectForKey:@"deviceIdentifier"];
  580. NSString *signature = [responseDict objectForKey:@"signature"];
  581. if (!publicKey || !deviceIdentifier || !signature) {
  582. [NCUtils log:@"Something went wrong subscribing to NC server. Aborting subscribe to Push Notification server."];
  583. if (block) {
  584. block(NO);
  585. }
  586. [bgTask stopBackgroundTask];
  587. return;
  588. }
  589. RLMRealm *realm = [RLMRealm defaultRealm];
  590. [realm beginWriteTransaction];
  591. NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", accountId];
  592. TalkAccount *managedAccount = [TalkAccount objectsWithPredicate:query].firstObject;
  593. managedAccount.userPublicKey = publicKey;
  594. managedAccount.deviceIdentifier = deviceIdentifier;
  595. managedAccount.deviceSignature = signature;
  596. [realm commitWriteTransaction];
  597. [[NCAPIController sharedInstance] subscribeAccount:[[NCDatabaseManager sharedInstance] talkAccountForAccountId:accountId] toPushServerWithCompletionBlock:^(NSError *error) {
  598. if (!error) {
  599. RLMRealm *realm = [RLMRealm defaultRealm];
  600. [realm beginWriteTransaction];
  601. NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", accountId];
  602. TalkAccount *managedAccount = [TalkAccount objectsWithPredicate:query].firstObject;
  603. managedAccount.lastPushSubscription = [[NSDate date] timeIntervalSince1970];
  604. [realm commitWriteTransaction];
  605. [[NCKeyChainController sharedInstance] setPushNotificationPublicKey:keyPair.publicKey forAccountId:accountId];
  606. [[NCKeyChainController sharedInstance] setPushNotificationPrivateKey:keyPair.privateKey forAccountId:accountId];
  607. [NCUtils log:@"Subscribed to Push Notification server successfully."];
  608. if (block) {
  609. block(YES);
  610. }
  611. [bgTask stopBackgroundTask];
  612. } else {
  613. [NCUtils log:[NSString stringWithFormat:@"Error while subscribing to Push Notification server. Error: %@", error.description]];
  614. [NCUtils log:[NSString stringWithFormat:@"Push notification, public key: %@", publicKey]];
  615. [NCUtils log:[NSString stringWithFormat:@"Push notification, device signature: %@", signature]];
  616. [NCUtils log:[NSString stringWithFormat:@"Push notification, device identifier: %@", deviceIdentifier]];
  617. [[NCKeyChainController sharedInstance] logCombinedPushToken];
  618. if (block) {
  619. block(NO);
  620. }
  621. [bgTask stopBackgroundTask];
  622. }
  623. }];
  624. } else {
  625. [NCUtils log:[NSString stringWithFormat:@"Error while subscribing to NC server. Error: %@", error.description]];
  626. if (block) {
  627. block(NO);
  628. }
  629. [bgTask stopBackgroundTask];
  630. }
  631. }];
  632. #else
  633. if (block) {
  634. block(YES);
  635. }
  636. #endif
  637. }
  638. - (NCPushNotificationKeyPair *)generatePushNotificationsKeyPairForAccountId:(NSString *)accountId
  639. {
  640. EVP_PKEY *pkey = [self generateRSAKey];
  641. if (!pkey) {
  642. return nil;
  643. }
  644. // Extract publicKey, privateKey
  645. int len;
  646. char *keyBytes;
  647. // PublicKey
  648. BIO *publicKeyBIO = BIO_new(BIO_s_mem());
  649. PEM_write_bio_PUBKEY(publicKeyBIO, pkey);
  650. len = BIO_pending(publicKeyBIO);
  651. keyBytes = malloc(len);
  652. BIO_read(publicKeyBIO, keyBytes, len);
  653. NSData *pnPublicKey = [NSData dataWithBytes:keyBytes length:len];
  654. NSLog(@"Push Notifications Key Pair generated: \n%@", [[NSString alloc] initWithData:pnPublicKey encoding:NSUTF8StringEncoding]);
  655. // PrivateKey
  656. BIO *privateKeyBIO = BIO_new(BIO_s_mem());
  657. PEM_write_bio_PKCS8PrivateKey(privateKeyBIO, pkey, NULL, NULL, 0, NULL, NULL);
  658. len = BIO_pending(privateKeyBIO);
  659. keyBytes = malloc(len);
  660. BIO_read(privateKeyBIO, keyBytes, len);
  661. NSData *pnPrivateKey = [NSData dataWithBytes:keyBytes length:len];
  662. EVP_PKEY_free(pkey);
  663. NCPushNotificationKeyPair *keyPair = [[NCPushNotificationKeyPair alloc] init];
  664. keyPair.publicKey = pnPublicKey;
  665. keyPair.privateKey = pnPrivateKey;
  666. return keyPair;
  667. }
  668. - (EVP_PKEY *)generateRSAKey
  669. {
  670. EVP_PKEY *pkey = EVP_PKEY_new();
  671. if (!pkey) {
  672. return NULL;
  673. }
  674. BIGNUM *bigNumber = BN_new();
  675. int exponent = RSA_F4;
  676. RSA *rsa = RSA_new();
  677. if (BN_set_word(bigNumber, exponent) == 0) {
  678. pkey = NULL;
  679. goto cleanup;
  680. }
  681. if (RSA_generate_key_ex(rsa, 2048, bigNumber, NULL) == 0) {
  682. pkey = NULL;
  683. goto cleanup;
  684. }
  685. if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
  686. pkey = NULL;
  687. goto cleanup;
  688. }
  689. cleanup:
  690. RSA_free(rsa);
  691. BN_free(bigNumber);
  692. return pkey;
  693. }
  694. @end