1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

Reset megolm session when people join/leave the room

This commit is contained in:
Richard van der Hoff
2016-08-26 11:23:01 +01:00
parent 2da70ca024
commit b4f22310ea
7 changed files with 91 additions and 14 deletions

View File

@@ -90,6 +90,13 @@ EncryptionAlgorithm.prototype.initRoomEncryption = function(roomMembers) {
* @return {module:client.Promise} Promise which resolves to the new event body
*/
/**
* Called when the membership of a member of the room changes.
*
* @param {module:models/event.MatrixEvent} event event causing the change
* @param {module:models/room-member} member user whose membership changed
*/
EncryptionAlgorithm.prototype.onRoomMembership = function(event, member) {};
/**
* base type for decryption implementations
@@ -125,7 +132,7 @@ module.exports.DecryptionAlgorithm = DecryptionAlgorithm;
*
* @method module:crypto-algorithms/base.DecryptionAlgorithm#onRoomKeyEvent
*
* @param {module:modules/event~MatrixEvent} event key event
* @param {module:models/event.MatrixEvent} event key event
*/
DecryptionAlgorithm.prototype.onRoomKeyEvent = function(params) {
// ignore by default

View File

@@ -40,6 +40,7 @@ function MegolmEncryption(params) {
base.EncryptionAlgorithm.call(this, params);
this._prepPromise = null;
this._outboundSessionId = null;
this._discardNewSession = false;
}
utils.inherits(MegolmEncryption, base.EncryptionAlgorithm);
@@ -59,7 +60,7 @@ MegolmEncryption.prototype._ensureOutboundSession = function(room) {
if (this._outboundSessionId) {
// prep already done
return q();
return q(this._outboundSessionId);
}
var session_id = this._olmDevice.createOutboundGroupSession();
@@ -138,11 +139,19 @@ MegolmEncryption.prototype._ensureOutboundSession = function(room) {
undefined, "PUT", path, undefined, encryptedContent
);
}).then(function() {
// don't set this until the keys are sent successfully; if we get an
// error, the user can restart by resending the message.
self._outboundSessionId = session_id;
if (self._discardNewSession) {
// we've had cause to reset the session_id since starting this process.
// we'll use the current session for any currently pending events, but
// don't save it as the current _outboundSessionId, so that new events
// will use a new session.
console.log("Session generation complete, but discarding");
} else {
self._outboundSessionId = session_id;
}
return session_id;
}).finally(function() {
self._prepPromise = null;
self._discardNewSession = false;
});
return this._prepPromise;
@@ -159,7 +168,7 @@ MegolmEncryption.prototype._ensureOutboundSession = function(room) {
*/
MegolmEncryption.prototype.encryptMessage = function(room, eventType, content) {
var self = this;
return this._ensureOutboundSession(room).then(function() {
return this._ensureOutboundSession(room).then(function(session_id) {
var payloadJson = {
room_id: self._roomId,
type: eventType,
@@ -167,14 +176,14 @@ MegolmEncryption.prototype.encryptMessage = function(room, eventType, content) {
};
var ciphertext = self._olmDevice.encryptGroupMessage(
self._outboundSessionId, JSON.stringify(payloadJson)
session_id, JSON.stringify(payloadJson)
);
var encryptedContent = {
algorithm: olmlib.MEGOLM_ALGORITHM,
sender_key: self._olmDevice.deviceCurve25519Key,
body: ciphertext,
session_id: self._outboundSessionId,
session_id: session_id,
signature: "FIXME",
};
@@ -182,6 +191,36 @@ MegolmEncryption.prototype.encryptMessage = function(room, eventType, content) {
});
};
/**
* @inheritdoc
*
* @param {module:models/event.MatrixEvent} event event causing the change
* @param {module:models/room-member} member user whose membership changed
*/
MegolmEncryption.prototype.onRoomMembership = function(event, member) {
// start a new outbound session whenever someone joins or leaves the room.
//
// technically we don't need to reset on all membership transitions (eg,
// leave->ban), but we might as well.
// when people join the room, we could get away with sharing the current
// state of the ratchet with them; however, it's somewhat easier for now
// just to reset the session and start a new one.
if (this._outboundSessionId) {
console.log("Discarding outbound megolm session due to change in " +
"membership of " + member.userId);
this._outboundSessionId = null;
}
if (this._prepPromise) {
console.log("Discarding as-yet-incomplete megolm session due to " +
"change in membership of " + member.userId);
this._discardNewSession = true;
}
};
/**
* Megolm decryption implementation
*
@@ -231,7 +270,7 @@ MegolmDecryption.prototype.decryptEvent = function(event) {
/**
* @inheritdoc
*
* @param {module:modules/event~MatrixEvent} event key event
* @param {module:models/event.MatrixEvent} event key event
*/
MegolmDecryption.prototype.onRoomKeyEvent = function(event) {
console.log("Adding key from ", event);