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

Merge pull request #145 from matrix-org/rav/crypto_event

Configure encryption on m.room.encryption events
This commit is contained in:
David Baker
2016-06-23 18:25:15 +01:00
committed by GitHub

View File

@@ -148,6 +148,7 @@ function MatrixClient(opts) {
this.sessionStore.storeEndToEndDevicesForUser( this.sessionStore.storeEndToEndDevicesForUser(
opts.userId, myDevices opts.userId, myDevices
); );
setupCryptoEventHandler(this);
} }
this.scheduler = opts.scheduler; this.scheduler = opts.scheduler;
if (this.scheduler) { if (this.scheduler) {
@@ -676,6 +677,44 @@ MatrixClient.prototype.isEventSenderVerified = function(event) {
return false; return false;
}; };
/**
* Register a listener for m.room.encryption events which will enable encryption
* for a room.
*
* @param {MatrixClient} client
*/
function setupCryptoEventHandler(client) {
client.on("event", function(event) {
if (!event.isState() || event.getType() != "m.room.encryption") {
return;
}
onCryptoEvent(client, event);
});
}
function onCryptoEvent(client, event) {
var roomId = event.getRoomId();
// if we already have encryption in this room, we should ignore this event
// (for now at least. maybe we should alert the user somehow?)
var content = event.getContent();
var existingConfig = client.sessionStore.getEndToEndRoom(roomId);
if (existingConfig) {
if (JSON.stringify(existingConfig) != JSON.stringify(content)) {
console.error("Ignoring m.room.encryption event which requests " +
"a change of config in " + roomId);
return;
}
}
try {
client.setRoomEncryption(roomId, content).done();
} catch (e) {
console.error("Error configuring encryption in room " + roomId +
": " + e);
}
}
/** /**
* Enable end-to-end encryption for a room. * Enable end-to-end encryption for a room.
* @param {string} roomId The room ID to enable encryption in. * @param {string} roomId The room ID to enable encryption in.
@@ -684,12 +723,16 @@ MatrixClient.prototype.isEventSenderVerified = function(event) {
*/ */
MatrixClient.prototype.setRoomEncryption = function(roomId, config) { MatrixClient.prototype.setRoomEncryption = function(roomId, config) {
if (!this._olmDevice) { if (!this._olmDevice) {
return q.reject(new Error("End-to-End encryption disabled")); throw new Error("End-to-End encryption disabled");
} }
var self = this; var self = this;
if (config.algorithm === OLM_ALGORITHM) { if (config.algorithm === OLM_ALGORITHM) {
// remove spurious keys
config = {
algorithm: OLM_ALGORITHM,
};
this.sessionStore.storeEndToEndRoom(roomId, config); this.sessionStore.storeEndToEndRoom(roomId, config);
var room = this.getRoom(roomId); var room = this.getRoom(roomId);