Skip to content

Commit

Permalink
Privacy: Store Identity Server in Account Data (MSC2230)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuroe committed Sep 5, 2019
1 parent 28eb330 commit 9f0a52c
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Improvements:
* MXIdentityService: Support identity server v2 API. Handle identity server v2 API authentification and use the hashed v2 lookup API for 3PIDs (vector-im/riot-ios#2603 and /vector-im/riot-ios#2652).
* MXHTTPClient: Add access token renewal plus request retry mechanism.
* MXHTTPClient: Do not retry requests if the host is not valid.
* MXAutoDiscovery: Add initWithUrl contructor
* MXAutoDiscovery: Add initWithUrl contructor.
* Privacy: Store Identity Server in Account Data ([MSC2230](https://github.com/matrix-org/matrix-doc/pull/2230))(vector-im/riot-ios#2665).

API break:
* MXRestClient: Remove identity server requests. Now MXIdentityService is used to perform identity server requests.
Expand Down
3 changes: 3 additions & 0 deletions MatrixSDK/MXRestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ FOUNDATION_EXPORT NSString *const kMXAccountDataTypeDirect;
FOUNDATION_EXPORT NSString *const kMXAccountDataTypePushRules;
FOUNDATION_EXPORT NSString *const kMXAccountDataTypeIgnoredUserList;
FOUNDATION_EXPORT NSString *const kMXAccountDataTypeUserWidgets;
FOUNDATION_EXPORT NSString *const kMXAccountDataTypeIdentityServer;
FOUNDATION_EXPORT NSString *const kMXAccountDataTypeAcceptedTerms;
FOUNDATION_EXPORT NSString *const kMXAccountDataTypeAcceptedTermsKey;

/**
Account data keys
*/
FOUNDATION_EXPORT NSString *const kMXAccountDataKeyIgnoredUser;
FOUNDATION_EXPORT NSString *const kMXAccountDataKeyIdentityServer;


/**
MXRestClient error domain
Expand Down
4 changes: 3 additions & 1 deletion MatrixSDK/MXRestClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@
NSString *const kMXAccountDataTypePushRules = @"m.push_rules";
NSString *const kMXAccountDataTypeDirect = @"m.direct";
NSString *const kMXAccountDataTypeUserWidgets = @"m.widgets";
NSString *const kMXAccountDataTypeIdentityServer = @"m.identity_server";
NSString *const kMXAccountDataTypeAcceptedTerms = @"m.accepted_terms";
NSString *const kMXAccountDataTypeAcceptedTermsKey = @"accepted";

/**
Account data keys
*/
NSString *const kMXAccountDataKeyIgnoredUser = @"ignored_users";
NSString *const kMXAccountDataKeyIdentityServer = @"base_url";
NSString *const kMXAccountDataTypeAcceptedTermsKey = @"accepted";

/**
Types of third party media.
Expand Down
38 changes: 38 additions & 0 deletions MatrixSDK/MXSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ FOUNDATION_EXPORT NSString *const kMXSessionDirectRoomsDidChangeNotification;
*/
FOUNDATION_EXPORT NSString *const kMXSessionAccountDataDidChangeNotification;

/**
Posted when the identity server in the user account data has changed.
The notification object is the concerned session (MXSession instance).
*/
FOUNDATION_EXPORT NSString *const kMXSessionAccountDataDidChangeIdentityServerNotification;

/**
Posted when MXSession data have been corrupted. The listener must reload the session data with a full server sync.
Expand Down Expand Up @@ -1306,6 +1313,37 @@ typedef void (^MXOnBackgroundSyncFail)(NSError *error);
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure;

/**
Set the identity server in the user's account data.
`kMXSessionAccountDataDidChangeIdentityServerNotification` will be sent once the
user's account data is updated with the new value.
@param identityServer the base url of the identity server (ex: "https://vector.im"). Nil to indicate no IS.
@param success A block object called when the operation succeeds.
@param failure A block object called when the operation fails.
@return a MXHTTPOperation instance.
*/
- (MXHTTPOperation*)setAccountDataIdentityServer:(NSString*)identityServer
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure;
/**
Indicate if an IS is set in the user's account data.
@return YES if YES.
*/
- (BOOL)hasAccountDataIdentityServer;

/**
The IS set in the user's account data.
@return the identity server. Nil means either the user have not set yet a IS
or they do not want to use an IS. Use [self hasAccountDataIdentityServer]
to differentiate the 2 options.
*/
- (NSString*)accountDataIdentityServer;


#pragma mark - Matrix filters
/**
Expand Down
102 changes: 102 additions & 0 deletions MatrixSDK/MXSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
NSString *const kMXSessionIgnoredUsersDidChangeNotification = @"kMXSessionIgnoredUsersDidChangeNotification";
NSString *const kMXSessionDirectRoomsDidChangeNotification = @"kMXSessionDirectRoomsDidChangeNotification";
NSString *const kMXSessionAccountDataDidChangeNotification = @"kMXSessionAccountDataDidChangeNotification";
NSString *const kMXSessionAccountDataDidChangeIdentityServerNotification = @"kMXSessionAccountDataDidChangeIdentityServerNotification";
NSString *const kMXSessionDidCorruptDataNotification = @"kMXSessionDidCorruptDataNotification";
NSString *const kMXSessionCryptoDidCorruptDataNotification = @"kMXSessionCryptoDidCorruptDataNotification";
NSString *const kMXSessionNewGroupInviteNotification = @"kMXSessionNewGroupInviteNotification";
Expand Down Expand Up @@ -390,6 +391,8 @@ -(void)setStore:(id<MXStore>)store success:(void (^)(void))onStoreDataReady fail

- (void)setIdentityServer:(NSString *)identityServer andAccessToken:(NSString *)accessToken
{
NSLog(@"[MXSession] setIdentityServer: %@", identityServer);

matrixRestClient.identityServer = identityServer;

if (identityServer)
Expand Down Expand Up @@ -1486,6 +1489,24 @@ - (void)handleAccountData:(NSDictionary*)accountDataUpdate

// Update the corresponding part of account data
[_accountData updateWithEvent:event];

if ([event[@"type"] isEqualToString:kMXAccountDataTypeIdentityServer])
{
NSString *identityServer = self.accountDataIdentityServer;
if (identityServer != self.identityService.identityServer
&& ![identityServer isEqualToString:self.identityService.identityServer])
{
NSLog(@"[MXSession] handleAccountData: Update identity server: %@ -> %@", self.identityService.identityServer, identityServer);

// Use the IS from the account data
[self setIdentityServer:identityServer andAccessToken:nil];

// And notify
[[NSNotificationCenter defaultCenter] postNotificationName:kMXSessionAccountDataDidChangeIdentityServerNotification
object:self
userInfo:nil];
}
}
}

_store.userAccountData = _accountData.accountData;
Expand Down Expand Up @@ -3407,6 +3428,87 @@ - (MXHTTPOperation*)setAccountData:(NSDictionary*)data
return [matrixRestClient setAccountData:data forType:type success:success failure:failure];
}

- (MXHTTPOperation *)setAccountDataIdentityServer:(NSString *)identityServer
success:(void (^)(void))success
failure:(void (^)(NSError *))failure
{
// Sanitise the passed URL
if (!identityServer.length)
{
identityServer = nil;
}
if (identityServer)
{
if (![identityServer hasPrefix:@"http"])
{
identityServer = [NSString stringWithFormat:@"https://%@", identityServer];
}
if ([identityServer hasSuffix:@"/"])
{
identityServer = [identityServer substringToIndex:identityServer.length - 1];
}
}

NSLog(@"[MXSession] setAccountDataIdentityServer: %@", identityServer);

MXHTTPOperation *operation;
if (identityServer)
{
// Does the URL point to a true IS
__block MXIdentityService *identityService = [[MXIdentityService alloc] initWithIdentityServer:identityServer accessToken:nil andHomeserverRestClient:matrixRestClient];

operation = [identityService pingIdentityServer:^{
identityService = nil;

MXHTTPOperation *operation2 = [self setAccountData:@{
kMXAccountDataKeyIdentityServer:identityServer
}
forType:kMXAccountDataTypeIdentityServer
success:success failure:failure];

if (operation2)
{
[operation mutateTo:operation2];
}

} failure:^(NSError * _Nonnull error) {
identityService = nil;

NSLog(@"[MXSession] setAccountDataIdentityServer: Invalid identity server. Error: %@", error);

if (failure)
{
failure(error);
}
}];
}
else
{
operation = [self setAccountData:@{
kMXAccountDataKeyIdentityServer:NSNull.null
}
forType:kMXAccountDataTypeIdentityServer
success:success failure:failure];
}

return operation;
}

- (BOOL)hasAccountDataIdentityServer
{
return ([self.accountData accountDataForEventType:kMXAccountDataTypeIdentityServer] != nil);
}

- (NSString *)accountDataIdentityServer
{
NSString *accountDataIdentityServer;

NSDictionary *content = [self.accountData accountDataForEventType:kMXAccountDataTypeIdentityServer];
MXJSONModelSetString(accountDataIdentityServer, content[kMXAccountDataKeyIdentityServer]);

return accountDataIdentityServer;
}


#pragma mark - Matrix filters
- (MXHTTPOperation*)setFilter:(MXFilterJSONModel*)filter
Expand Down

0 comments on commit 9f0a52c

Please sign in to comment.