Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Improve handling of cookies #962

Merged
merged 5 commits into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 26 additions & 27 deletions Owncloud iOs Client/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

[self showSplashScreenFake];

//Check if the server support shared api
[CheckFeaturesSupported updateServerFeaturesAndCapabilitiesOfActiveUser];


//Needed to use on background tasks
if (!k_is_sso_active) {
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
Expand Down Expand Up @@ -180,10 +176,13 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

if (user) {
self.activeUser = user;
[UtilsCookies deleteCurrentSystemCookieStorageAndRestoreTheCookiesOfActiveUser];

((CheckAccessToServer*)[CheckAccessToServer sharedManager]).delegate = self;
[[CheckAccessToServer sharedManager] isConnectionToTheServerByUrl:user.url withTimeout:k_timeout_fast];

[CheckFeaturesSupported updateServerFeaturesAndCapabilitiesOfActiveUser];

ManageAccounts *manageAccounts = [ManageAccounts new];
[manageAccounts updateDisplayNameOfUserWithUser:self.activeUser];

Expand Down Expand Up @@ -589,14 +588,16 @@ + (OCCommunication*)sharedOCCommunication
//Cookies is allways available in current supported Servers
[sharedOCCommunication setIsCookiesAvailable:YES];

[sharedOCCommunication setOauth2Configuration: [[OCOAuth2Configuration alloc]
initWithClientId:k_oauth2_client_id
clientSecret:k_oauth2_client_secret
redirectUri:k_oauth2_redirect_uri
authorizationEndpoint:k_oauth2_authorization_endpoint
tokenEndpoint:k_oauth2_token_endpoint]];
OCOAuth2Configuration *ocOAuth2conf = [[OCOAuth2Configuration alloc]
initWithClientId:k_oauth2_client_id
clientSecret:k_oauth2_client_secret
redirectUri:k_oauth2_redirect_uri
authorizationEndpoint:k_oauth2_authorization_endpoint
tokenEndpoint:k_oauth2_token_endpoint];

[sharedOCCommunication setUserAgent:[UtilsUrls getUserAgent]];
[sharedOCCommunication setValueOauth2Configuration: ocOAuth2conf];

[sharedOCCommunication setValueOfUserAgent:[UtilsUrls getUserAgent]];

OCKeychain *oKeychain = [[OCKeychain alloc] init];
[sharedOCCommunication setValueCredentialsStorage:oKeychain];
Expand Down Expand Up @@ -631,14 +632,16 @@ + (OCCommunication*)sharedOCCommunicationDownloadFolder {
//Cookies is allways available in current supported Servers
[sharedOCCommunicationDownloadFolder setIsCookiesAvailable:YES];

[sharedOCCommunicationDownloadFolder setOauth2Configuration: [[OCOAuth2Configuration alloc]
initWithClientId:k_oauth2_client_id
clientSecret:k_oauth2_client_secret
redirectUri:k_oauth2_redirect_uri
authorizationEndpoint:k_oauth2_authorization_endpoint
tokenEndpoint:k_oauth2_token_endpoint]];
OCOAuth2Configuration *ocOAuth2conf = [[OCOAuth2Configuration alloc]
initWithClientId:k_oauth2_client_id
clientSecret:k_oauth2_client_secret
redirectUri:k_oauth2_redirect_uri
authorizationEndpoint:k_oauth2_authorization_endpoint
tokenEndpoint:k_oauth2_token_endpoint];

[sharedOCCommunicationDownloadFolder setValueOauth2Configuration:ocOAuth2conf];

[sharedOCCommunicationDownloadFolder setUserAgent:[UtilsUrls getUserAgent]];
[sharedOCCommunicationDownloadFolder setValueOfUserAgent:[UtilsUrls getUserAgent]];

OCKeychain *oKeychain = [[OCKeychain alloc] init];
[sharedOCCommunicationDownloadFolder setValueCredentialsStorage:oKeychain];
Expand Down Expand Up @@ -974,6 +977,10 @@ - (void)applicationWillTerminate:(UIApplication *)application
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

//Set on the user defaults that the app has been killed by user

//Store active user cookies on the Database
[UtilsCookies saveCurrentOfActiveUserAndClean];

NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setBool:YES forKey:k_app_killed_by_user];
[standardUserDefaults synchronize];
Expand Down Expand Up @@ -2809,15 +2816,7 @@ - (void) switchActiveUserTo:(UserDto *)user isNewAccount:(BOOL)isNewAccount with
[ManageUsersDB setActiveAccountByUserId:user.userId];
user.activeaccount = YES;

//Restore the cookies of the future activeUser
//1- Store the new cookies on the Database
[UtilsCookies setOnDBStorageCookiesByUser:self.activeUser];
//2- Clean the cookies storage
[UtilsFramework deleteAllCookies];
//3- We restore the previous cookies of the active user on the System cookies storage
[UtilsCookies setOnSystemStorageCookiesByUser:user];
//4- We delete the cookies of the active user on the database because it could change and it is not necessary keep them there
[ManageCookiesStorageDB deleteCookiesByUser:user];
[UtilsCookies saveActiveUserCookiesAndRestoreCookiesOfUser:user];

//Change the active user in appDelegate global variable
self.activeUser = user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public enum TextfieldType: String {
self.checkCurrentUrl()
}

UtilsCookies.clear() // network requests from log-in view need to be independent of existing sessions
UtilsCookies.saveCurrentOfActiveUserAndClean() // network requests from log-in view need to be independent of existing sessions

print("Init login with loginMode: \(loginMode.rawValue) (0=Create,1=Update,2=Expire,3=Migrate)")
}
Expand All @@ -159,8 +159,9 @@ public enum TextfieldType: String {
super.viewWillDisappear(animated)

self.removeNotificationsAboutKeyboard()

if self.loginMode == .update || self.loginMode == .migrate {
UtilsCookies.restoreTheCookiesOfActiveUser()
UtilsCookies.deleteCurrentSystemCookieStorageAndRestoreTheCookiesOfActiveUser()

}
}
Expand Down Expand Up @@ -868,6 +869,9 @@ public enum TextfieldType: String {

if tryingToUpdateDifferentUser {
self.showCredentialsError(NSLocalizedString("credentials_different_user", comment: "") )
//Delete current wrong cookies and relaunch check url to get correct ones
UtilsFramework.deleteAllCookies()
self.checkCurrentUrl()

} else {

Expand All @@ -884,6 +888,9 @@ public enum TextfieldType: String {

if (ManageUsersDB.isExistUser(self.user)) {
self.showURLError(NSLocalizedString("account_not_new", comment: ""))
//Delete current wrong cookies and relaunch check url to get correct ones
UtilsFramework.deleteAllCookies()
self.checkCurrentUrl()

} else {

Expand Down
10 changes: 3 additions & 7 deletions Owncloud iOs Client/Network/UserData/DetectUserData.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@ @implementation DetectUserData

+ (void) getUserDisplayNameOfServer:(NSString*)path credentials:(OCCredentialsDto *)credentials
withCompletion:(void(^)(NSString *displayName, NSError *error))completion {
OCCommunication *sharedCommunication;

sharedCommunication = [AppDelegate sharedOCCommunication];


[sharedCommunication setCredentials:credentials];
[[AppDelegate sharedOCCommunication] setCredentials:credentials];

[sharedCommunication setValueOfUserAgent:[UtilsUrls getUserAgent]];
[[AppDelegate sharedOCCommunication] setValueOfUserAgent:[UtilsUrls getUserAgent]];

[sharedCommunication getUserDisplayNameOfServer:path onCommunication:sharedCommunication
[[AppDelegate sharedOCCommunication] getUserDisplayNameOfServer:path onCommunication:[AppDelegate sharedOCCommunication]
success:^(NSHTTPURLResponse *response, NSString *displayName, NSString *redirectedServer) {
if (displayName && ![displayName isEqualToString:@""]) {
completion(displayName, nil);
Expand Down
24 changes: 1 addition & 23 deletions Owncloud iOs Client/Tabs/SettingTab/SettingsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1134,27 +1134,6 @@ - (void) refreshTable {

#pragma mark - Manage Accounts Methods

//-----------------------------------
/// @name setCookiesOfActiveAccount
///-----------------------------------

/**
* Method to delete the current cookies and add the cookies of the active account
*
* @warning we have to take in account that the cookies of the active account must to be in the database
*/
- (void) setCookiesOfActiveAccount {

AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];

//1- Delete the current cookies because we delete the current active user
[UtilsFramework deleteAllCookies];
//2- We restore the previous cookies of the active user on the System cookies storage
[UtilsCookies setOnSystemStorageCookiesByUser:app.activeUser];
//3- We delete the cookies of the active user on the databse because it could change and it is not necessary keep them there
[ManageCookiesStorageDB deleteCookiesByUser:app.activeUser];
}

///-----------------------------------
/// @name cancelAndRemoveFromTabRecentsAllInfoByUser
///-----------------------------------
Expand Down Expand Up @@ -1741,10 +1720,9 @@ - (void) didSelectLogOutAccount:(UserDto *)user {

[ManageUsersDB setActiveAccountAutomatically];

//Update in appDelegate the active user
APP_DELEGATE.activeUser = [ManageUsersDB getActiveUser];

[self setCookiesOfActiveAccount];
[UtilsCookies deleteCurrentSystemCookieStorageAndRestoreTheCookiesOfActiveUser];

[UtilsFileSystem createFolderForUser:APP_DELEGATE.activeUser];

Expand Down
11 changes: 8 additions & 3 deletions Owncloud iOs Client/Utils/UtilsCookies.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@
@interface UtilsCookies : NSObject

+ (void) setOnDBStorageCookiesByUser:(UserDto *) user;
+ (void) setOnSystemStorageCookiesByUser:(UserDto *) user;
+ (void) setOnSystemCookieStorageDBCookiesOfUser:(UserDto *) user;

+ (void) eraseCredentialsWithURL:(NSString *)connectURL;
+ (void) eraseURLCache;
+ (void) eraseCredentialsAndUrlCacheOfActiveUser;

+ (void) clearCookies;

+ (void) restoreTheCookiesOfActiveUser;
+ (void) saveCurrentOfActiveUserAndClean;

+ (void) restoreCookiesOfUser:(UserDto *)user;

+ (void) deleteCurrentSystemCookieStorageAndRestoreTheCookiesOfActiveUser;

+ (void) saveActiveUserCookiesAndRestoreCookiesOfUser:(UserDto *)user;

@end
55 changes: 28 additions & 27 deletions Owncloud iOs Client/Utils/UtilsCookies.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,8 @@ + (void) setOnDBStorageCookiesByUser:(UserDto *) user {
}
}

//-----------------------------------
/// @name setOnDBStorageCookiesByUser
///-----------------------------------

/**
* Method set on the System storage the cookies that are on Database of a user
*
* @param UserDto -> user
*
*/
+ (void) setOnSystemStorageCookiesByUser:(UserDto *) user {
+ (void) setOnSystemCookieStorageDBCookiesOfUser:(UserDto *) user {

NSArray *listOfCookiesStorageDto = [ManageCookiesStorageDB getCookiesByUser:user];

Expand Down Expand Up @@ -109,39 +100,49 @@ + (void) eraseCredentialsAndUrlCacheOfActiveUser {
}


+ (void) clearCookies {

+ (void) saveCurrentOfActiveUserAndClean {
DLog(@"_saveAndCleanCookies_");

AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];

//Clear the cookies before to try to do login
//1- Storage the new cookies on the Database

//1- Store the current cookies on the Database
if (app.activeUser != nil) {
[UtilsCookies setOnDBStorageCookiesByUser:app.activeUser];
}
//2- Clean the cookies storage
[UtilsFramework deleteAllCookies];
}

//-----------------------------------
/// @name restoreTheCookiesOfActiveUserByNewUser
///-----------------------------------
+ (void) restoreCookiesOfUser:(UserDto *)user {
DLog(@"_srestoreCookiesOfUser_ %ld", (long)user.userId);

//1-Restore the previous cookies of user on the System Cookie Storage
[UtilsCookies setOnSystemCookieStorageDBCookiesOfUser:user];

//2-Delete the cookies of the active user on the database because it could change and it is not necessary keep them there
[ManageCookiesStorageDB deleteCookiesByUser:user];
}

/**
* Method to restore the cookies of the active after add a new user
*
* @param UserDto -> user
*
*/
+ (void) restoreTheCookiesOfActiveUser {
DLog(@"_restoreTheCookiesOfActiveUser_");
+ (void) deleteCurrentSystemCookieStorageAndRestoreTheCookiesOfActiveUser {
DLog(@"_deleteCurrentSystemCookieStorageAndRestoreTheCookiesOfActiveUser_");
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];

//1- Clean the cookies storage
[UtilsFramework deleteAllCookies];
//2- We restore the previous cookies of the active user on the System cookies storage
[UtilsCookies setOnSystemStorageCookiesByUser:app.activeUser];
//3- We delete the cookies of the active user on the databse because it could change and it is not necessary keep them there
[ManageCookiesStorageDB deleteCookiesByUser:app.activeUser];

//2- Restore cookies of active user
[self restoreCookiesOfUser:app.activeUser];
}

+ (void) saveActiveUserCookiesAndRestoreCookiesOfUser:(UserDto *)user {
DLog(@"_saveActiveUserCookiesAndRestoreCookiesOfUser_ %ld", (long)user.userId);

[self saveCurrentOfActiveUserAndClean];

[self restoreCookiesOfUser:user];
}

@end
18 changes: 10 additions & 8 deletions ownCloudExtApp/DocumentPickerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,16 @@ + (OCCommunication*)sharedOCCommunication
//Cookies is allways available in current supported Servers
[sharedOCCommunication setIsCookiesAvailable:YES];

[sharedOCCommunication setOauth2Configuration: [[OCOAuth2Configuration alloc]
initWithClientId:k_oauth2_client_id
clientSecret:k_oauth2_client_secret
redirectUri:k_oauth2_redirect_uri
authorizationEndpoint:k_oauth2_authorization_endpoint
tokenEndpoint:k_oauth2_token_endpoint]];

[sharedOCCommunication setUserAgent:[UtilsUrls getUserAgent]];
OCOAuth2Configuration *ocOAuth2conf = [[OCOAuth2Configuration alloc]
initWithClientId:k_oauth2_client_id
clientSecret:k_oauth2_client_secret
redirectUri:k_oauth2_redirect_uri
authorizationEndpoint:k_oauth2_authorization_endpoint
tokenEndpoint:k_oauth2_token_endpoint];

[sharedOCCommunication setValueOauth2Configuration: ocOAuth2conf];

[sharedOCCommunication setValueOfUserAgent:[UtilsUrls getUserAgent]];

OCKeychain *oKeychain = [[OCKeychain alloc] init];
[sharedOCCommunication setValueCredentialsStorage:oKeychain];
Expand Down