From d5bc24d9f76fbcef01263f99f39cdc788de7cde7 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Fri, 19 May 2017 09:54:29 +0100 Subject: [PATCH 1/5] Initial implementation of KeyRequestHandler, KeyShareDialog etc --- src/KeyRequestHandler.js | 89 ++++++++++ src/components/structures/MatrixChat.js | 6 + .../views/dialogs/KeyShareDialog.js | 164 ++++++++++++++++++ 3 files changed, 259 insertions(+) create mode 100644 src/KeyRequestHandler.js create mode 100644 src/components/views/dialogs/KeyShareDialog.js diff --git a/src/KeyRequestHandler.js b/src/KeyRequestHandler.js new file mode 100644 index 0000000000..90ff00d47e --- /dev/null +++ b/src/KeyRequestHandler.js @@ -0,0 +1,89 @@ +/* +Copyright 2017 Vector Creations Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import sdk from './index'; +import Modal from './Modal'; + +export default class KeyRequestHandler { + constructor(matrixClient) { + this._matrixClient = matrixClient; + + this._isDialogOpen = false; + + // userId -> deviceId -> [keyRequest] + this._pendingKeyRequests = {}; + } + + + handleKeyRequest(keyRequest) { + const userId = keyRequest.userId; + const deviceId = keyRequest.deviceId; + + if (!this._pendingKeyRequests[userId]) { + this._pendingKeyRequests[userId] = {}; + } + if (!this._pendingKeyRequests[userId][deviceId]) { + this._pendingKeyRequests[userId][deviceId] = []; + } + this._pendingKeyRequests[userId][deviceId].push(keyRequest); + + if (this._isDialogOpen) { + // ignore for now + console.log("Key request, but we already have a dialog open"); + return; + } + + this._processNextRequest(); + } + + _processNextRequest() { + const userId = Object.keys(this._pendingKeyRequests)[0]; + if (!userId) { + return; + } + const deviceId = Object.keys(this._pendingKeyRequests[userId])[0]; + if (!deviceId) { + return; + } + console.log(`Starting KeyShareDialog for ${userId}:${deviceId}`); + + const finished = (r) => { + this._isDialogOpen = false; + + if (r) { + for (const req of this._pendingKeyRequests[userId][deviceId]) { + req.share(); + } + } + delete this._pendingKeyRequests[userId][deviceId]; + if (Object.keys(this._pendingKeyRequests[userId]).length === 0) { + delete this._pendingKeyRequests[userId]; + } + + this._processNextRequest(); + }; + + const KeyShareDialog = sdk.getComponent("dialogs.KeyShareDialog"); + Modal.createDialog(KeyShareDialog, { + matrixClient: this._matrixClient, + userId: userId, + deviceId: deviceId, + onFinished: finished, + }); + this._isDialogOpen = true; + } +} + diff --git a/src/components/structures/MatrixChat.js b/src/components/structures/MatrixChat.js index f53128fba9..544213edc6 100644 --- a/src/components/structures/MatrixChat.js +++ b/src/components/structures/MatrixChat.js @@ -38,6 +38,7 @@ import PageTypes from '../../PageTypes'; import createRoom from "../../createRoom"; import * as UDEHandler from '../../UnknownDeviceErrorHandler'; +import KeyRequestHandler from '../../KeyRequestHandler'; import { _t } from '../../languageHandler'; module.exports = React.createClass({ @@ -914,6 +915,11 @@ module.exports = React.createClass({ } } }); + + const krh = new KeyRequestHandler(cli); + cli.on("crypto.roomKeyRequest", (req) => { + krh.handleKeyRequest(req); + }); }, showScreen: function(screen, params) { diff --git a/src/components/views/dialogs/KeyShareDialog.js b/src/components/views/dialogs/KeyShareDialog.js new file mode 100644 index 0000000000..5b66944dbc --- /dev/null +++ b/src/components/views/dialogs/KeyShareDialog.js @@ -0,0 +1,164 @@ +/* +Copyright 2017 Vector Creations Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import Modal from '../../../Modal'; +import React from 'react'; +import sdk from '../../../index'; + +import { _t } from '../../../languageHandler'; + +export default React.createClass({ + propTypes: { + matrixClient: React.PropTypes.object.isRequired, + userId: React.PropTypes.string.isRequired, + deviceId: React.PropTypes.string.isRequired, + onFinished: React.PropTypes.func.isRequired, + }, + + getInitialState: function() { + return { + deviceInfo: null, + wasNewDevice: false, + }; + }, + + componentDidMount: function() { + this._unmounted = false; + const userId = this.props.userId; + const deviceId = this.props.deviceId; + + // give the client a chance to refresh the device list + this.props.matrixClient.downloadKeys([userId], false).then((r) => { + if (this._unmounted) { return; } + + const deviceInfo = r[userId][deviceId]; + + if(!deviceInfo) { + console.warn(`No details found for device ${userId}:${deviceId}`); + + this.props.onFinished(false); + return; + } + + const wasNewDevice = !deviceInfo.isKnown(); + + this.setState({ + deviceInfo: deviceInfo, + wasNewDevice: wasNewDevice, + }); + + // if the device was new before, it's not any more. + if (wasNewDevice) { + this.props.matrixClient.setDeviceKnown( + userId, + deviceId, + true, + ); + } + }).done(); + }, + + componentWillUnmount: function() { + this._unmounted = true; + }, + + + _onVerifyClicked: function() { + const DeviceVerifyDialog = sdk.getComponent('views.dialogs.DeviceVerifyDialog'); + + console.log("KeyShareDialog: Starting verify dialog"); + Modal.createDialog(DeviceVerifyDialog, { + userId: this.props.userId, + device: this.state.deviceInfo, + onFinished: (verified) => { + if (verified) { + // can automatically share the keys now. + this.props.onFinished(true); + } + }, + }); + }, + + _onShareClicked: function() { + console.log("KeyShareDialog: User clicked 'share'"); + this.props.onFinished(true); + }, + + _onIgnoreClicked: function() { + console.log("KeyShareDialog: User clicked 'ignore'"); + this.props.onFinished(false); + }, + + _renderContent: function() { + const displayName = this.state.deviceInfo.getDisplayName() || + this.state.deviceInfo.deviceId; + + let text; + if (this.state.wasNewDevice) { + text = "You added a new device '%(displayName)s', which is" + + " requesting encryption keys."; + } else { + text = "Your unverified device '%(displayName)s' is requesting" + + " encryption keys."; + } + text = _t(text, {displayName: displayName}); + + return ( +
+

{text}

+ +
+ + + +
+
+ ); + }, + + render: function() { + const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog'); + const Spinner = sdk.getComponent('views.elements.Spinner'); + + let content; + + if (this.state.deviceInfo) { + content = this._renderContent(); + } else { + content = ( +
+

{_t('Loading device info...')}

+ +
+ ); + } + + return ( + + {content} + + ); + }, +}); From 7d55f3e75d55edef5a0303ab8c0f8909cce2d57e Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 1 Jun 2017 17:49:24 +0100 Subject: [PATCH 2/5] handle room key request cancellations --- src/KeyRequestHandler.js | 55 ++++++++++++++++++++++--- src/components/structures/MatrixChat.js | 3 ++ 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/KeyRequestHandler.js b/src/KeyRequestHandler.js index 90ff00d47e..fbb869d468 100644 --- a/src/KeyRequestHandler.js +++ b/src/KeyRequestHandler.js @@ -21,16 +21,18 @@ export default class KeyRequestHandler { constructor(matrixClient) { this._matrixClient = matrixClient; - this._isDialogOpen = false; + // the user/device for which we currently have a dialog open + this._currentUser = null; + this._currentDevice = null; // userId -> deviceId -> [keyRequest] this._pendingKeyRequests = {}; } - handleKeyRequest(keyRequest) { const userId = keyRequest.userId; const deviceId = keyRequest.deviceId; + const requestId = keyRequest.requestId; if (!this._pendingKeyRequests[userId]) { this._pendingKeyRequests[userId] = {}; @@ -38,9 +40,17 @@ export default class KeyRequestHandler { if (!this._pendingKeyRequests[userId][deviceId]) { this._pendingKeyRequests[userId][deviceId] = []; } - this._pendingKeyRequests[userId][deviceId].push(keyRequest); - if (this._isDialogOpen) { + // check if we already have this request + const requests = this._pendingKeyRequests[userId][deviceId]; + if (requests.find((r) => r.requestId === requestId)) { + console.log("Already have this key request, ignoring"); + return; + } + + requests.push(keyRequest); + + if (this._currentUser) { // ignore for now console.log("Key request, but we already have a dialog open"); return; @@ -49,6 +59,37 @@ export default class KeyRequestHandler { this._processNextRequest(); } + handleKeyRequestCancellation(cancellation) { + // see if we can find the request in the queue + const userId = cancellation.userId; + const deviceId = cancellation.deviceId; + const requestId = cancellation.requestId; + + if (userId === this._currentUser && deviceId === this._currentDevice) { + console.log( + "room key request cancellation for the user we currently have a" + + " dialog open for", + ); + // TODO: update the dialog. For now, we just ignore the + // cancellation. + return; + } + + if (!this._pendingKeyRequests[userId]) { + return; + } + const requests = this._pendingKeyRequests[userId][deviceId]; + if (!requests) { + return; + } + const idx = requests.findIndex((r) => r.requestId === requestId); + if (idx < 0) { + return; + } + console.log("Forgetting room key request"); + requests.splice(idx, 1); + } + _processNextRequest() { const userId = Object.keys(this._pendingKeyRequests)[0]; if (!userId) { @@ -61,7 +102,8 @@ export default class KeyRequestHandler { console.log(`Starting KeyShareDialog for ${userId}:${deviceId}`); const finished = (r) => { - this._isDialogOpen = false; + this._currentUser = null; + this._currentDevice = null; if (r) { for (const req of this._pendingKeyRequests[userId][deviceId]) { @@ -83,7 +125,8 @@ export default class KeyRequestHandler { deviceId: deviceId, onFinished: finished, }); - this._isDialogOpen = true; + this._currentUser = userId; + this._currentDevice = deviceId; } } diff --git a/src/components/structures/MatrixChat.js b/src/components/structures/MatrixChat.js index 544213edc6..afb3c57818 100644 --- a/src/components/structures/MatrixChat.js +++ b/src/components/structures/MatrixChat.js @@ -920,6 +920,9 @@ module.exports = React.createClass({ cli.on("crypto.roomKeyRequest", (req) => { krh.handleKeyRequest(req); }); + cli.on("crypto.roomKeyRequestCancellation", (req) => { + krh.handleKeyRequestCancellation(req); + }); }, showScreen: function(screen, params) { From 21277557f80a725ac0ee0175633d198ecbae263a Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Fri, 2 Jun 2017 10:10:40 +0100 Subject: [PATCH 3/5] Add translations for new dialog --- src/i18n/strings/en_EN.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 95baf1f50e..6e7648f00d 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -779,5 +779,10 @@ "Disable URL previews for this room (affects only you)": "Disable URL previews for this room (affects only you)", "$senderDisplayName changed the room avatar to ": "$senderDisplayName changed the room avatar to ", "%(senderDisplayName)s removed the room avatar.": "%(senderDisplayName)s removed the room avatar.", - "%(senderDisplayName)s changed the avatar for %(roomName)s": "%(senderDisplayName)s changed the avatar for %(roomName)s" + "%(senderDisplayName)s changed the avatar for %(roomName)s": "%(senderDisplayName)s changed the avatar for %(roomName)s", + "Start verification": "Start verification", + "Share without verifying": "Share without verifying", + "Ignore request": "Ignore request", + "You added a new device '%(displayName)s', which is requesting encryption keys.": "You added a new device '%(displayName)s', which is requesting encryption keys.", + "Your unverified device '%(displayName)s' is requesting encryption keys.": "Your unverified device '%(displayName)s' is requesting encryption keys." } From 7a9784fd6ef39e4fc214e8416456b9d92cb9f7e8 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 5 Jun 2017 17:26:25 +0100 Subject: [PATCH 4/5] KeyRequestHandler: clear redundant users/devices on cancellations ... otherwise _processNextRequest will get confused --- src/KeyRequestHandler.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/KeyRequestHandler.js b/src/KeyRequestHandler.js index fbb869d468..4354fba269 100644 --- a/src/KeyRequestHandler.js +++ b/src/KeyRequestHandler.js @@ -88,6 +88,12 @@ export default class KeyRequestHandler { } console.log("Forgetting room key request"); requests.splice(idx, 1); + if (requests.length === 0) { + delete this._pendingKeyRequests[userId][deviceId]; + if (Object.keys(this._pendingKeyRequests[userId]).length === 0) { + delete this._pendingKeyRequests[userId]; + } + } } _processNextRequest() { From 32e3ea0601355fc366fddee43194f9c7b1492238 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 5 Jun 2017 17:58:11 +0100 Subject: [PATCH 5/5] Address review comments --- src/KeyRequestHandler.js | 4 ++-- src/components/views/dialogs/KeyShareDialog.js | 10 +++++++++- src/i18n/strings/en_EN.json | 4 +++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/KeyRequestHandler.js b/src/KeyRequestHandler.js index 4354fba269..1da4922153 100644 --- a/src/KeyRequestHandler.js +++ b/src/KeyRequestHandler.js @@ -26,7 +26,7 @@ export default class KeyRequestHandler { this._currentDevice = null; // userId -> deviceId -> [keyRequest] - this._pendingKeyRequests = {}; + this._pendingKeyRequests = Object.create(null); } handleKeyRequest(keyRequest) { @@ -35,7 +35,7 @@ export default class KeyRequestHandler { const requestId = keyRequest.requestId; if (!this._pendingKeyRequests[userId]) { - this._pendingKeyRequests[userId] = {}; + this._pendingKeyRequests[userId] = Object.create(null); } if (!this._pendingKeyRequests[userId][deviceId]) { this._pendingKeyRequests[userId][deviceId] = []; diff --git a/src/components/views/dialogs/KeyShareDialog.js b/src/components/views/dialogs/KeyShareDialog.js index 5b66944dbc..61391d281c 100644 --- a/src/components/views/dialogs/KeyShareDialog.js +++ b/src/components/views/dialogs/KeyShareDialog.js @@ -20,6 +20,14 @@ import sdk from '../../../index'; import { _t } from '../../../languageHandler'; +/** + * Dialog which asks the user whether they want to share their keys with + * an unverified device. + * + * onFinished is called with `true` if the key should be shared, `false` if it + * should not, and `undefined` if the dialog is cancelled. (In other words: + * truthy: do the key share. falsy: don't share the keys). + */ export default React.createClass({ propTypes: { matrixClient: React.PropTypes.object.isRequired, @@ -155,7 +163,7 @@ export default React.createClass({ return ( {content} diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 7421ccad76..f6c044deac 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -809,5 +809,7 @@ "Share without verifying": "Share without verifying", "Ignore request": "Ignore request", "You added a new device '%(displayName)s', which is requesting encryption keys.": "You added a new device '%(displayName)s', which is requesting encryption keys.", - "Your unverified device '%(displayName)s' is requesting encryption keys.": "Your unverified device '%(displayName)s' is requesting encryption keys." + "Your unverified device '%(displayName)s' is requesting encryption keys.": "Your unverified device '%(displayName)s' is requesting encryption keys.", + "Encryption key request": "Encryption key request" + }