Skip to content

Commit

Permalink
Crypto: Remove m.new_device support
Browse files Browse the repository at this point in the history
  • Loading branch information
manuroe committed Nov 16, 2017
1 parent 8f3b3f4 commit cc70fc8
Show file tree
Hide file tree
Showing 11 changed files with 10 additions and 240 deletions.
2 changes: 0 additions & 2 deletions MatrixSDK/Contrib/Swift/JSONModels/MXEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public enum MXEventType {
case roomTag
case presence
case typing
case newDevice
case callInvite
case callCandidates
case callAnswer
Expand Down Expand Up @@ -84,7 +83,6 @@ public enum MXEventType {
case .roomThirdPartyInvite: return kMXEventTypeStringRoomThirdPartyInvite
case .roomTag: return kMXEventTypeStringRoomTag
case .presence: return kMXEventTypeStringPresence
case .newDevice: return kMXEventTypeStringNewDevice
case .callInvite: return kMXEventTypeStringCallInvite
case .callCandidates: return kMXEventTypeStringCallCandidates
case .callAnswer: return kMXEventTypeStringCallAnswer
Expand Down
10 changes: 0 additions & 10 deletions MatrixSDK/Crypto/Data/Store/MXCryptoStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,6 @@
*/
- (OLMAccount*)account;

/**
Store a flag indicating that we have announced the new device.
*/
- (void)storeDeviceAnnounced;

/**
Check if the "device announced" flag is set.
*/
- (BOOL)deviceAnnounced;

/**
Store the sync token corresponding to the device list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ @interface MXRealmOlmAccount : RLMObject
*/
@property NSData *olmAccountData;

/**
Has this device been annonced to others?
*/
@property (nonatomic) BOOL deviceAnnounced;

/**
The sync token corresponding to the device list.
*/
Expand Down Expand Up @@ -363,20 +358,6 @@ - (OLMAccount*)account
return nil;
}

- (void)storeDeviceAnnounced
{
MXRealmOlmAccount *account = self.accountInCurrentThread;
[account.realm transactionWithBlock:^{
account.deviceAnnounced = YES;
}];
}

- (BOOL)deviceAnnounced
{
MXRealmOlmAccount *account = self.accountInCurrentThread;
return account.deviceAnnounced;
}

- (void)storeDeviceSyncToken:(NSString*)deviceSyncToken
{
MXRealmOlmAccount *account = self.accountInCurrentThread;
Expand Down
4 changes: 1 addition & 3 deletions MatrixSDK/Crypto/MXCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ FOUNDATION_EXPORT NSString *const kMXCryptoRoomKeyRequestCancellationNotificatio
/**
Start the crypto module.
Device keys will be uploaded, then one time keys if there are not enough on the homeserver
and, then, if this is the first time, this new device will be announced to all other users
devices.
Device keys will be uploaded, then one time keys if there are not enough on the homeserver.
@param onComplete A block object called when the operation succeeds.
@param failure A block object called when the operation fails.
Expand Down
201 changes: 6 additions & 195 deletions MatrixSDK/Crypto/MXCrypto.m
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,6 @@ - (void)start:(void (^)())success
return;
}

// Check if announcement must be done and to who
NSMutableDictionary *roomsByUser = [self usersToMakeAnnouncement];

// Start uploading user device keys
startOperation = [self uploadDeviceKeys:^(MXKeysUploadResponse *keysUploadResponse) {

Expand All @@ -230,55 +227,12 @@ - (void)start:(void (^)())success
NSLog(@"Store: %@", _store);
NSLog(@"");

// Anounce ourselves if not already done
if (roomsByUser)
{
// But upload our one-time keys before.
// Thus, other devices can download them once they receive our to-device annoucement event
[self maybeUploadOneTimeKeys:^{

// Once keys are uploaded, announce ourselves
MXHTTPOperation *operation2 = [self makeAnnoucement:roomsByUser success:^{

// Make sure we are refreshing devices lists right instead
// of waiting for the next /sync that may occur in 30s.
[_deviceList refreshOutdatedDeviceLists];

[outgoingRoomKeyRequestManager start];

dispatch_async(dispatch_get_main_queue(), ^{
startOperation = nil;
success();
});

} failure:^(NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
startOperation = nil;
failure(error);
});
}];

if (operation2)
{
[startOperation mutateTo:operation2];
}
} failure:^(NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
startOperation = nil;
failure(error);
});
}];
}
else
{
// No annoucement require
[outgoingRoomKeyRequestManager start];
[outgoingRoomKeyRequestManager start];

dispatch_async(dispatch_get_main_queue(), ^{
startOperation = nil;
success();
});
}
dispatch_async(dispatch_get_main_queue(), ^{
startOperation = nil;
success();
});

} failure:^(NSError *error) {
NSLog(@"[MXCrypto] start. Error in uploadDeviceKeys");
Expand Down Expand Up @@ -639,9 +593,7 @@ - (void)onSyncCompleted:(NSString *)oldSyncToken nextSyncToken:(NSString *)nextS
// to-device messages, to help us avoid throwing away one-time-keys that we
// are about to receive messages for
// (https://github.com/vector-im/riot-web/issues/2782).
// Also, do not upload them if we have not announced our device yet.
// They will be uploaded just before the announcement in [self start].
if (!catchingUp && _store.deviceAnnounced)
if (!catchingUp)
{
[self maybeUploadOneTimeKeys:nil failure:nil];
[incomingRoomKeyRequestManager processReceivedRoomKeyRequests];
Expand Down Expand Up @@ -1833,100 +1785,6 @@ - (void)registerEventHandlers
});
}

/**
Get the list of users and rooms where to announce a new device.
@return the list of rooms ids ordered by user id. nil if annoucement is not required.
*/
- (NSMutableDictionary<NSString*, NSMutableArray*> *)usersToMakeAnnouncement
{
// Following operations must be called from the main thread
NSParameterAssert([NSThread currentThread].isMainThread);

if (_store.deviceAnnounced)
{
NSLog(@"[MXCrypto] usersToMakeAnnouncement: Device announcement already done");
return nil;
}

// We need to tell all the devices in all the rooms we are members of that
// we have arrived.
// Build a list of rooms for each user.
return self.e2eRoomMembers;
}

/**
Make device annoucement if required.
Send m.new_device messages to any devices we share a room with.
(TODO: we can get rid of this once a suitable number of homeservers and
clients support the more reliable device list update stream mechanism)
@param roomsByUser the rooms and users to announce the device to.
@param success A block object called when the operation succeeds.
@param failure A block object called when the operation fails.
*/
- (MXHTTPOperation*)makeAnnoucement:(NSMutableDictionary<NSString*, NSMutableArray*> *)roomsByUser
success:(void (^)())success
failure:(void (^)(NSError *error))failure
{
// This method is called when the initialSync was done or the session was resumed

if (!roomsByUser)
{
// Catch up on any m.new_device events which arrived during the initial sync.
[_deviceList refreshOutdatedDeviceLists];

NSLog(@"[MXCrypto] makeAnnoucementTo: Already done");
success();
return nil;
}

// Catch up on any m.new_device events which arrived during the initial sync.
// And force download all devices keys the user already has.
[_deviceList invalidateUserDeviceList:myDevice.userId];
[_deviceList refreshOutdatedDeviceLists];

// Build a per-device message for each user
MXUsersDevicesMap<NSDictionary*> *contentMap = [[MXUsersDevicesMap alloc] init];
for (NSString *userId in roomsByUser)
{
[contentMap setObjects:@{
@"*": @{
@"device_id": myDevice.deviceId,
@"rooms": roomsByUser[userId],
}
} forUser:userId];
}

NSLog(@"[MXCrypto] checkDeviceAnnounced: Make annoucements to %tu users and %tu devices: %@", contentMap.userIds.count, contentMap.count, contentMap);

if (contentMap.userIds.count)
{
return [_matrixRestClient sendToDevice:kMXEventTypeStringNewDevice contentMap:contentMap txnId:nil success:^{

NSLog(@"[MXCrypto] checkDeviceAnnounced: Annoucements done");

[_store storeDeviceAnnounced];
success();

} failure:^(NSError *error) {
NSLog(@"[MXCrypto] checkDeviceAnnounced: Annoucements failed.");
failure(error);
}];
}
else
{
NSLog(@"[MXCrypto] checkDeviceAnnounced: Annoucements done 2");
[_store storeDeviceAnnounced];
}

success();
return nil;
}

/**
Handle a to-device event.
Expand All @@ -1953,14 +1811,6 @@ - (void)onToDeviceEvent:(NSNotification *)notification
break;
}

case MXEventTypeNewDevice:
{
dispatch_async(_cryptoQueue, ^{
[weakSelf onNewDeviceEvent:event];
});
break;
}

case MXEventTypeRoomKeyRequest:
{
dispatch_async(_cryptoQueue, ^{
Expand Down Expand Up @@ -2002,45 +1852,6 @@ - (void)onRoomKeyEvent:(MXEvent*)event
[alg onRoomKeyEvent:event];
}

/**
Called when a new device announces itself.
@param event the announcement event.
*/
- (void)onNewDeviceEvent:(MXEvent*)event
{
NSString *userId = event.sender;

NSString *deviceId;
NSArray<NSString*> *rooms;

MXJSONModelSetString(deviceId, event.content[@"device_id"]);
MXJSONModelSetArray(rooms, event.content[@"rooms"]);

if (!rooms || !deviceId)
{
NSLog(@"[MXCrypto] onNewDeviceEvent: new_device event missing keys");
return;
}

NSLog(@"[MXCrypto] onNewDeviceEvent: m.new_device event from %@:%@ for rooms %@", userId, deviceId, rooms);

if ([_store deviceWithDeviceId:deviceId forUser:userId])
{
NSLog(@"[MXCrypto] onNewDeviceEvent: known device; ignoring");
return;
}

[_deviceList invalidateUserDeviceList:userId];

// We delay handling these until the intialsync has completed, so that we
// can do all of them together.
if (mxSession.state == MXSessionStateRunning)
{
[_deviceList refreshOutdatedDeviceLists];
}
}

/**
Handle an m.room.encryption event.
Expand Down
2 changes: 1 addition & 1 deletion MatrixSDK/Data/Store/MXFileStore/MXFileStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#import "MXFileStoreMetaData.h"
#import "MXSDKOptions.h"

static NSUInteger const kMXFileVersion = 48;
static NSUInteger const kMXFileVersion = 49;

static NSString *const kMXFileStoreFolder = @"MXFileStore";
static NSString *const kMXFileStoreMedaDataFile = @"MXFileStore";
Expand Down
2 changes: 0 additions & 2 deletions MatrixSDK/JSONModels/MXEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ typedef enum : NSUInteger
MXEventTypeReceipt,
MXEventTypeRead,
MXEventTypeReadMarker,
MXEventTypeNewDevice,
MXEventTypeCallInvite,
MXEventTypeCallCandidates,
MXEventTypeCallAnswer,
Expand Down Expand Up @@ -99,7 +98,6 @@ FOUNDATION_EXPORT NSString *const kMXEventTypeStringTypingNotification;
FOUNDATION_EXPORT NSString *const kMXEventTypeStringReceipt;
FOUNDATION_EXPORT NSString *const kMXEventTypeStringRead;
FOUNDATION_EXPORT NSString *const kMXEventTypeStringReadMarker;
FOUNDATION_EXPORT NSString *const kMXEventTypeStringNewDevice;
FOUNDATION_EXPORT NSString *const kMXEventTypeStringCallInvite;
FOUNDATION_EXPORT NSString *const kMXEventTypeStringCallCandidates;
FOUNDATION_EXPORT NSString *const kMXEventTypeStringCallAnswer;
Expand Down
1 change: 0 additions & 1 deletion MatrixSDK/JSONModels/MXEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
NSString *const kMXEventTypeStringReceipt = @"m.receipt";
NSString *const kMXEventTypeStringRead = @"m.read";
NSString *const kMXEventTypeStringReadMarker = @"m.fully_read";
NSString *const kMXEventTypeStringNewDevice = @"m.new_device";
NSString *const kMXEventTypeStringCallInvite = @"m.call.invite";
NSString *const kMXEventTypeStringCallCandidates = @"m.call.candidates";
NSString *const kMXEventTypeStringCallAnswer = @"m.call.answer";
Expand Down
1 change: 0 additions & 1 deletion MatrixSDK/Utils/MXTools.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ + (void)initialize
kMXEventTypeStringReceipt,
kMXEventTypeStringRead,
kMXEventTypeStringReadMarker,
kMXEventTypeStringNewDevice,
kMXEventTypeStringCallInvite,
kMXEventTypeStringCallCandidates,
kMXEventTypeStringCallAnswer,
Expand Down
4 changes: 0 additions & 4 deletions MatrixSDKTests/MXCryptoTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ - (void)testEnableCrypto
XCTAssert(mxSession.crypto);
XCTAssert([mxSession.crypto.store.class hasDataForCredentials:mxSession.matrixRestClient.credentials]);

XCTAssert(mxSession.crypto.store.deviceAnnounced, @"The device must have been announced when enableCrypto completes");

[mxSession enableCrypto:NO success:^{

XCTAssertNil(mxSession.crypto);
Expand Down Expand Up @@ -145,8 +143,6 @@ - (void)testMXSDKOptionsEnableCryptoWhenOpeningMXSession
XCTAssert(mxSession.crypto);
XCTAssert([mxSession.crypto.store.class hasDataForCredentials:mxSession.matrixRestClient.credentials]);

XCTAssert(mxSession.crypto.store.deviceAnnounced, @"The device must have been announced when [MXSession start] completes");

[mxSession enableCrypto:NO success:^{

XCTAssertNil(mxSession.crypto);
Expand Down
4 changes: 2 additions & 2 deletions MatrixSDKTests/MXSessionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ - (void)testToDeviceEvents
XCTAssert(toDeviceEvent);

XCTAssertEqualObjects(toDeviceEvent.sender, aliceRestClient.credentials.userId);
XCTAssertEqual(toDeviceEvent.eventType, MXEventTypeNewDevice);
XCTAssertEqual(toDeviceEvent.eventType, MXEventTypeRoomKeyRequest);

[[NSNotificationCenter defaultCenter] removeObserver:observer];

Expand All @@ -1246,7 +1246,7 @@ - (void)testToDeviceEvents
}
} forUser:mxSession.myUser.userId];

[aliceRestClient sendToDevice:kMXEventTypeStringNewDevice contentMap:contentMap txnId:nil success:^{
[aliceRestClient sendToDevice:kMXEventTypeStringRoomKeyRequest contentMap:contentMap txnId:nil success:^{

} failure:^(NSError *error) {
XCTFail(@"Cannot set up intial test conditions - error: %@", error);
Expand Down

0 comments on commit cc70fc8

Please sign in to comment.