/** * Tells if an user is in the ignored user ids list * * @param userId the user id to test * @return true if the user is ignored */ public boolean isUserIgnored(String userId) { if (null != userId) { return getDataHandler().getIgnoredUserIds().indexOf(userId) >= 0; } return false; }
@Override public String toString() { return getMyUserId() + " " + super.toString(); } }
@Override public void onStoreReady(String s) { session.getDataHandler().checkPermanentStorageData(); session.startEventStream(store.getEventStreamToken()); session.getDataHandler().addListener(evLis); }
@Override public void initWithMatrixSession(MXSession matrixSession, String roomId) { mSession = matrixSession; mCrypto = matrixSession.getCrypto(); mRoomId = roomId; mDeviceId = matrixSession.getCredentials().deviceId; // Default rotation periods // TODO: Make it configurable via parameters mSessionRotationPeriodMsgs = 100; mSessionRotationPeriodMs = 7 * 24 * 3600 * 1000; }
/** * Shorthand for {@link #startEventStream(EventsThreadListener, NetworkConnectivityReceiver, String)} with no eventListener * using a DataHandler and no specific failure callback. * * @param initialToken the initial sync token (null to sync from scratch). */ public void startEventStream(String initialToken) { checkIfAlive(); startEventStream(null, mNetworkConnectivityReceiver, initialToken); }
/** * Constructor * * @param session the session * @param networkConnectivityReceiver the network events listener */ public BingRulesManager(MXSession session, NetworkConnectivityReceiver networkConnectivityReceiver) { mSession = session; mApiClient = session.getBingRulesApiClient(); mMyUserId = session.getCredentials().userId; mDataHandler = session.getDataHandler(); mNetworkListener = new IMXNetworkEventListener() { @Override public void onNetworkConnectionUpdate(boolean isConnected) { // mLoadRulesCallback is set when a loadRules failed // so when a network is available, trigger again loadRules if (isConnected && (null != mLoadRulesCallback)) { loadRules(mLoadRulesCallback); } } }; mNetworkConnectivityReceiver = networkConnectivityReceiver; networkConnectivityReceiver.addEventListener(mNetworkListener); }
bobSession.getCredentials().deviceId = "BobDevice"; Assert.assertNull(bobSession.getCrypto()); bobSession.enableCrypto(true, new TestApiCallback<Void>(lock0) { @Override public void onSuccess(Void info) { Assert.assertNotNull(bobSession.getCrypto()); final String deviceCurve25519Key = bobSession.getCrypto().getOlmDevice().getDeviceCurve25519Key(); final String deviceEd25519Key = bobSession.getCrypto().getOlmDevice().getDeviceEd25519Key(); final List<MXDeviceInfo> myUserDevices = bobSession.getCrypto().getUserDevices(bobSession.getMyUserId()); bobSession2.getDataHandler().getStore().addMXStoreListener(listener); bobSession2.getDataHandler().getStore().open(); mTestHelper.await(lock1); Assert.assertTrue(bobSession2.isCryptoEnabled()); bobSession2.getDataHandler().addListener(eventsListener); bobSession2.startEventStream(null); mTestHelper.await(lock2); Assert.assertTrue(results.containsKey("onInitialSyncComplete")); Assert.assertTrue(results.containsKey("onCryptoSyncComplete")); MXCrypto crypto = bobSession2.getCrypto();
aliceSession.enableCrypto(true, new TestApiCallback<Void>(lock00b) { @Override public void onSuccess(Void info) { bobSession.enableCrypto(true, new TestApiCallback<Void>(lock00b) { @Override public void onSuccess(Void info) { aliceSession.getCrypto().setWarnOnUnknownDevices(false); bobSession.getCrypto().setWarnOnUnknownDevices(false); aliceSession.createRoom(null, null, RoomDirectoryVisibility.DIRECTORY_VISIBILITY_PUBLIC, null, null, new TestApiCallback<String>(lock0) { @Override bobSession.joinRoom(aliceRoomId, new TestApiCallback<String>(lock1) { @Override public void onSuccess(String info) { Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId); Credentials bobCredentials = bobSession.getCredentials(); bobSession.clear(context); MXSession bobSession2 = mTestHelper.logIntoAccount(bobSession.getMyUserId(), mCryptoTestHelper.getEncryptedSessionParams()); Assert.assertNotNull(bobSession2); Assert.assertTrue(bobSession2.isCryptoEnabled()); Assert.assertNotEquals(bobSession2.getCrypto().getMyDevice().deviceId, bobCredentials.deviceId); bobSession2.getCrypto().setWarnOnUnknownDevices(false);
final MXSession bobSession = cryptoTestData.getSecondSession(); Credentials bobCredentials = bobSession.getCredentials(); bobSession.clear(context); bobSession2.getDataHandler().addListener(eventListener); bobSession2.getDataHandler().getStore().open(); bobSession2.startEventStream(null); Assert.assertTrue(results.containsKey("onCryptoSyncComplete")); Assert.assertNotNull(bobSession2.getCrypto()); Room roomFromBobPOV = bobSession2.getDataHandler().getRoom(aliceRoomId); bobSession2.clear(context);
MXSession samSession = mTestHelper.createAccount(TestConstants.USER_SAM, createSessionParams); final String aliceId = aliceSession.getMyUserId(); final String bobId = bobSession.getMyUserId(); final String samId = samSession.getMyUserId(); bobSession.createRoom(new TestApiCallback<String>(latch) { @Override public void onSuccess(String info) { final Room bobRoom = bobSession.getDataHandler().getRoom(roomId); samSession.joinRoom(roomId, new TestApiCallback<String>(latch)); mTestHelper.await(latch); aliceSession.joinRoom(roomId, new TestApiCallback<String>(latch)); mTestHelper.await(latch); final Room aliceRoom = aliceSession.getDataHandler().getStore().getRoom(roomId); aliceSession.clear(context); bobSession.clear(context); samSession.clear(context);
final MXSession bobSession = cryptoTestData.getSecondSession(); bobSession.getCrypto().setWarnOnUnknownDevices(false); aliceSession.getCrypto().setWarnOnUnknownDevices(false); final Room roomFromBobPOV = bobSession.getDataHandler().getRoom(aliceRoomId); final Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId); String bobDeviceId1 = bobSession.getCredentials().deviceId; bobSession.getDataHandler().addListener(bobEventListener); String bobId = bobSession.getCredentials().userId; bobSession.logout(context, new TestApiCallback<Void>(lock2) { @Override public void onSuccess(Void info) { aliceSession.getDataHandler().addListener(aliceEventListener); MXSession bobSession2 = mTestHelper.logIntoAccount(bobSession.getMyUserId(), mCryptoTestHelper.getEncryptedSessionParams()); String bobDeviceId2 = bobSession2.getCredentials().deviceId; Assert.assertNotEquals(bobDeviceId2, bobDeviceId1); final Room roomFromBobPOV2 = bobSession2.getDataHandler().getRoom(aliceRoomId); Assert.assertNotNull(roomFromBobPOV2);
session = new MXSession(hsConfig, dh, context); session.setSyncDelay(syncDelay * 1000); session.setSyncTimeout(syncTimeout * 60 * 1000); Log.e(TAG, "onLogin:" + session.getSyncTimeout()); session.getDataHandler().checkPermanentStorageData(); session.startEventStream(store.getEventStreamToken()); session.getDataHandler().addListener(evLis); } else { store.addMXStoreListener(new IMXStoreListener() {
bobSession.enableCrypto(true, new TestApiCallback<Void>(lock0) { @Override public void onSuccess(Void info) { bobSession.createRoom(new TestApiCallback<String>(lock1) { @Override public void onSuccess(String info) { Assert.assertNotNull(roomId[0]); Room room = bobSession.getDataHandler().getRoom(roomId[0]); Assert.assertNull(bobSession.getCrypto().getCryptoStore().getRoomAlgorithm(room.getRoomId())); bobSession.getDataHandler().addListener(new MXEventListener() { @Override public void onLiveEvent(Event event, RoomState roomState) { Assert.assertEquals(CryptoConstantsKt.MXCRYPTO_ALGORITHM_MEGOLM, bobSession.getCrypto().getCryptoStore().getRoomAlgorithm(room.getRoomId())); bobSession.clear(context);
bobSession.getCrypto().setWarnOnUnknownDevices(false); aliceSession.getCrypto().setWarnOnUnknownDevices(false); final Room roomFromBobPOV = bobSession.getDataHandler().getRoom(aliceRoomId); final Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId); String bobUserId1 = bobSession.getMyUserId(); String aliceUserId1 = aliceSession.getMyUserId(); bobSession.getDataHandler().addListener(bobEventListener); bobSession.logout(context, new TestApiCallback<Void>(lock2) { @Override public void onSuccess(Void info) { aliceSession.logout(context, new TestApiCallback<Void>(lock3) { @Override public void onSuccess(Void info) { MXSession bobSession2 = mTestHelper.logIntoAccount(bobSession.getMyUserId(), mCryptoTestHelper.getEncryptedSessionParams()); Assert.assertNotNull(bobSession2); bobSession2.getCrypto().setWarnOnUnknownDevices(false); MXSession aliceSession2 = mTestHelper.logIntoAccount(aliceSession.getMyUserId(), mCryptoTestHelper.getEncryptedSessionParams()); Assert.assertNotNull(aliceSession2); aliceSession2.getCrypto().setWarnOnUnknownDevices(false); Room roomFromBob2POV = bobSession2.getDataHandler().getRoom(aliceRoomId); Room roomFromAlice2POV = aliceSession2.getDataHandler().getRoom(aliceRoomId);
bobSession.getCryptoRestClient().uploadKeys(null, otks, "dev1", new TestApiCallback<KeysUploadResponse>(lock1) { @Override public void onSuccess(KeysUploadResponse keysUploadResponse) { usersDevicesKeyTypesMap.setObject("curve25519", bobSession.getMyUserId(), "dev1"); aliceSession.getCryptoRestClient().claimOneTimeKeysForUsersDevices(usersDevicesKeyTypesMap, new TestApiCallback<MXUsersDevicesMap<MXKey>>(lock2) { @Override public void onSuccess(MXUsersDevicesMap<MXKey> usersDevicesMap) { Assert.assertEquals(1, oneTimeKeys.getMap().size()); MXKey bobOtk = oneTimeKeys.getObject("dev1", bobSession.getMyUserId()); Assert.assertNotNull(bobOtk); Assert.assertEquals(1, keys.size()); bobSession.clear(context); aliceSession.clear(context);
mRoomDecryptors = new HashMap<>(); String deviceId = mSession.getCredentials().deviceId; mSession.getCredentials().deviceId = deviceId = mCryptoStore.getDeviceId(); mSession.getCredentials().deviceId = deviceId = UUID.randomUUID().toString(); Log.d(LOG_TAG, "Warning: No device id in MXCredentials. An id was created. Think of storing it"); mCryptoStore.storeDeviceId(deviceId); mMyDevice.userId = mSession.getMyUserId(); keys.put("ed25519:" + mSession.getCredentials().deviceId, mOlmDevice.getDeviceEd25519Key()); keys.put("curve25519:" + mSession.getCredentials().deviceId, mOlmDevice.getDeviceCurve25519Key()); Map<String, MXDeviceInfo> endToEndDevicesForUser = mCryptoStore.getUserDevices(mSession.getMyUserId()); mCryptoStore.storeUserDevices(mSession.getMyUserId(), myDevices); mSession.getDataHandler().setCryptoEventsListener(mEventListener); mEncryptingHandlerThread = new HandlerThread("MXCrypto_encrypting_" + mSession.getMyUserId(), Thread.MIN_PRIORITY); mEncryptingHandlerThread.start(); mDecryptingHandlerThread = new HandlerThread("MXCrypto_decrypting_" + mSession.getMyUserId(), Thread.MIN_PRIORITY); mDecryptingHandlerThread.start(); mDevicesList.handleDeviceListsChanges(Arrays.asList(mSession.getMyUserId()), null);
final MXSession bobSession = cryptoTestData.getSecondSession(); bobSession.getCrypto().setWarnOnUnknownDevices(false); aliceSession.getCrypto().setWarnOnUnknownDevices(false); final Room roomFromBobPOV = bobSession.getDataHandler().getRoom(aliceRoomId); final Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId); bobSession.getDataHandler().addListener(bobEventListener); String newSessionKey = aliceSession.getCrypto().getOlmDevice().getSessionKey(sessionId); bobSession.getDataHandler().onToDeviceEvent(toDeviceEvent); bobSession.getDataHandler().decryptEvent(event, null); mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, messageFromAlice, aliceSession);
@Test public void test01_testCryptoNoDeviceId() throws Exception { Log.e(LOG_TAG, "test01_testCryptoNoDeviceId"); Context context = InstrumentationRegistry.getContext(); final Map<String, Object> results = new HashMap<>(); MXSession bobSession = mTestHelper.createAccount(TestConstants.USER_BOB, mCryptoTestHelper.getDefaultSessionParams()); Assert.assertNull(bobSession.getCrypto()); bobSession.getCredentials().deviceId = null; CountDownLatch lock1 = new CountDownLatch(1); bobSession.enableCrypto(true, new TestApiCallback<Void>(lock1) { @Override public void onSuccess(Void info) { results.put("enableCrypto", "enableCrypto"); super.onSuccess(info); } }); mTestHelper.await(lock1); Assert.assertTrue(results.containsKey("enableCrypto")); Assert.assertNotNull(bobSession.getCrypto()); Assert.assertNotNull(bobSession.getCredentials().deviceId); bobSession.clear(context); }
/** * Convert a filter to a filterId * Either it is already known to the server, or send the filter to the server to get a filterId */ private void convertFilterToFilterId() { // Ensure mCurrentFilter has not been updated in the same time final String wantedJsonFilter = mCurrentFilter.toJSONString(); // Check if the current filter is known by the server, to directly use the filterId String filterId = getDataHandler().getStore().getFilters().get(wantedJsonFilter); if (TextUtils.isEmpty(filterId)) { // enable the filter in JSON representation so do not block sync until the filter response is there mEventsThread.setFilterOrFilterId(wantedJsonFilter); // Send the filter to the server mFilterRestClient.uploadFilter(getMyUserId(), mCurrentFilter, new SimpleApiCallback<FilterResponse>() { @Override public void onSuccess(FilterResponse filter) { // Store the couple filter/filterId getDataHandler().getStore().addFilter(wantedJsonFilter, filter.filterId); // Ensure the filter is still corresponding to the current filter if (TextUtils.equals(wantedJsonFilter, mCurrentFilter.toJSONString())) { // Tell the event thread to use the id now mEventsThread.setFilterOrFilterId(filter.filterId); } } }); } else { // Tell the event thread to use the id now mEventsThread.setFilterOrFilterId(filterId); } }
final MXSession bobSession = cryptoTestData.getSecondSession(); bobSession.getCrypto().setWarnOnUnknownDevices(false); aliceSession.getCrypto().setWarnOnUnknownDevices(false); final Room roomFromBobPOV = bobSession.getDataHandler().getRoom(aliceRoomId); final Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId); aliceSession.getCrypto().getDeviceList() .downloadKeys(Arrays.asList(bobSession.getMyUserId(), "@pppppppppppp:matrix.org"), false, new TestApiCallback<MXUsersDevicesMap<MXDeviceInfo>>(lock1) { @Override Assert.assertEquals(1, usersDevicesInfoMap.getMap().get(bobSession.getMyUserId()).size()); List<String> bobDevices = usersDevicesInfoMap.getUserDeviceIds(bobSession.getMyUserId());