Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Commit

Permalink
element-hq/element-ios/issues/4255 - Fully exposed hydration methods …
Browse files Browse the repository at this point in the history
…and made them depend on secret key data being passed as a parameter.
  • Loading branch information
stefanceriu committed Sep 17, 2021
1 parent b71d74e commit 0a98e06
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 20 deletions.
14 changes: 14 additions & 0 deletions MatrixKit/Controllers/MXKAuthenticationViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,26 @@
/// @param parameters Login parameters
- (void)loginWithParameters:(NSDictionary*)parameters;

/// Create an account with the given credentials
/// @param credentials Account credentials
- (void)createAccountWithCredentials:(MXCredentials *)credentials;

#pragma mark - Authentication Fallback

/**
Display the fallback URL within a webview.
*/
- (void)showAuthenticationFallBackView;

#pragma mark - Device rehydration

/**
Call this method at an appropriate time to attempt rehydrating from an existing backup device
@param keyData Secret key data
@param credentials Account credentials
*/

- (void)attemptDeviceRehydrationWithKeyData:(NSData *)keyData credentials:(MXCredentials *)credentials;

@end

35 changes: 25 additions & 10 deletions MatrixKit/Controllers/MXKAuthenticationViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ - (void)onSuccessfulLogin:(MXCredentials*)credentials
credentials.identityServer = _identityServerTextField.text;
}

[self attemptDeviceRehydrationWithCredentials:credentials retry:YES];
[self createAccountWithCredentials:credentials];
}
}

Expand All @@ -1436,25 +1436,40 @@ - (MXHTTPOperation *)currentHttpOperation

#pragma mark - Privates

- (void)attemptDeviceRehydrationWithCredentials:(MXCredentials *)credentials retry:(BOOL)retry
// Hook point for triggering device rehydration in subclasses
// Avoid cycles by using a separate private method do to the actual work
- (void)createAccountWithCredentials:(MXCredentials *)credentials
{
[self _createAccountWithCredentials:credentials];
}

- (void)attemptDeviceRehydrationWithKeyData:(NSData *)keyData
credentials:(MXCredentials *)credentials
{
[self attemptDeviceRehydrationWithKeyData:keyData
credentials:credentials
retry:YES];
}

- (void)attemptDeviceRehydrationWithKeyData:(NSData *)keyData
credentials:(MXCredentials *)credentials
retry:(BOOL)retry
{
MXLogDebug(@"[MXKAuthenticationViewController] attemptDeviceRehydration: starting device rehydration");

MXKeyData * keyData = [[MXKeyProvider sharedInstance] requestKeyForDataOfType:MXDehydrationServiceKeyDataType isMandatory:NO expectedKeyType:kRawData];
if (keyData == nil)
{
MXLogError(@"[MXKAuthenticationViewController] attemptDeviceRehydration: no key provided for device rehydration");
[self createAccountWithCredentials:credentials];
[self _createAccountWithCredentials:credentials];
return;
}

NSData *key = ((MXRawDataKey*) keyData).key;
MXRestClient *mxRestClient = [[MXRestClient alloc] initWithCredentials:credentials andOnUnrecognizedCertificateBlock:^BOOL(NSData *certificate) {
return NO;
}];

MXWeakify(self);
[[MXKAccountManager sharedManager].dehydrationService rehydrateDeviceWithMatrixRestClient:mxRestClient dehydrationKey:key success:^(NSString * deviceId) {
[[MXKAccountManager sharedManager].dehydrationService rehydrateDeviceWithMatrixRestClient:mxRestClient dehydrationKey:keyData success:^(NSString * deviceId) {
MXStrongifyAndReturnIfNil(self);

if (deviceId)
Expand All @@ -1467,24 +1482,24 @@ - (void)attemptDeviceRehydrationWithCredentials:(MXCredentials *)credentials ret
MXLogDebug(@"[MXKAuthenticationViewController] attemptDeviceRehydration: device rehydration has been canceled.");
}

[self createAccountWithCredentials:credentials];
[self _createAccountWithCredentials:credentials];
} failure:^(NSError *error) {
MXStrongifyAndReturnIfNil(self);

if (retry)
{
MXLogError(@"[MXKAuthenticationViewController] attemptDeviceRehydration: device rehydration failed due to error: %@. Retrying", error);
[self attemptDeviceRehydrationWithCredentials:credentials retry:NO];
[self attemptDeviceRehydrationWithKeyData:keyData credentials:credentials retry:NO];
return;
}

MXLogError(@"[MXKAuthenticationViewController] attemptDeviceRehydration: device rehydration failed due to error: %@", error);

[self createAccountWithCredentials:credentials];
[self _createAccountWithCredentials:credentials];
}];
}

- (void)createAccountWithCredentials:(MXCredentials *)credentials
- (void)_createAccountWithCredentials:(MXCredentials *)credentials
{
MXKAccount *account = [[MXKAccount alloc] initWithCredentials:credentials];
account.identityServerURL = credentials.identityServer;
Expand Down
6 changes: 4 additions & 2 deletions MatrixKit/Models/Account/MXKAccount.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,10 @@ typedef BOOL (^MXKAccountOnCertificateChange)(MXKAccount *mxAccount, NSData *cer
- (void)supportLazyLoadOfRoomMembers:(void (^)(BOOL supportLazyLoadOfRoomMembers))completion;

/**
Call this method at an appropriate time to attempt dehydrating an existing backup device
Call this method at an appropriate time to attempt dehydrating to a new backup device
*/
- (void)attemptDeviceDehydrationWithSuccess:(void (^)(void))success failure:(void (^)(NSError *error))failure;
- (void)attemptDeviceDehydrationWithKeyData:(NSData *)keyData
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure;

@end
18 changes: 10 additions & 8 deletions MatrixKit/Models/Account/MXKAccount.m
Original file line number Diff line number Diff line change
Expand Up @@ -1673,14 +1673,18 @@ - (void)launchInitialServerSync
}];
}

- (void)attemptDeviceDehydrationWithSuccess:(void (^)(void))success failure:(void (^)(NSError *error))failure
- (void)attemptDeviceDehydrationWithKeyData:(NSData *)keyData
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure
{
[self attemptDeviceDehydrationWithRetry:YES success:success failure:failure];
[self attemptDeviceDehydrationWithKeyData:keyData retry:YES success:success failure:failure];
}

- (void)attemptDeviceDehydrationWithRetry:(BOOL)retry success:(void (^)(void))success failure:(void (^)(NSError *error))failure
- (void)attemptDeviceDehydrationWithKeyData:(NSData *)keyData
retry:(BOOL)retry
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure
{
MXKeyData * keyData = [[MXKeyProvider sharedInstance] requestKeyForDataOfType:MXDehydrationServiceKeyDataType isMandatory:NO expectedKeyType:kRawData];
if (keyData == nil)
{
MXLogWarning(@"[MXKAccount] attemptDeviceDehydrationWithRetry: no key provided for device dehydration");
Expand All @@ -1693,10 +1697,8 @@ - (void)attemptDeviceDehydrationWithRetry:(BOOL)retry success:(void (^)(void))su
return;
}

NSData *key = ((MXRawDataKey*) keyData).key;

MXLogDebug(@"[MXKAccount] attemptDeviceDehydrationWithRetry: starting device dehydration");
[[MXKAccountManager sharedManager].dehydrationService dehydrateDeviceWithMatrixRestClient:mxRestClient crypto:mxSession.crypto dehydrationKey:key success:^(NSString *deviceId) {
[[MXKAccountManager sharedManager].dehydrationService dehydrateDeviceWithMatrixRestClient:mxRestClient crypto:mxSession.crypto dehydrationKey:keyData success:^(NSString *deviceId) {
MXLogDebug(@"[MXKAccount] attemptDeviceDehydrationWithRetry: device successfully dehydrated");

if (success)
Expand All @@ -1706,7 +1708,7 @@ - (void)attemptDeviceDehydrationWithRetry:(BOOL)retry success:(void (^)(void))su
} failure:^(NSError *error) {
if (retry)
{
[self attemptDeviceDehydrationWithRetry:NO success:success failure:failure];
[self attemptDeviceDehydrationWithKeyData:keyData retry:NO success:success failure:failure];
MXLogError(@"[MXKAccount] attemptDeviceDehydrationWithRetry: device dehydration failed due to error: %@. Retrying.", error);
}
else
Expand Down

0 comments on commit 0a98e06

Please sign in to comment.