2020-03-10 15:31:40 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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 {MatrixClientPeg} from './MatrixClientPeg';
|
2020-05-14 02:41:41 +00:00
|
|
|
import dis from "./dispatcher/dispatcher";
|
2020-03-10 15:31:40 +00:00
|
|
|
import Modal from './Modal';
|
|
|
|
import * as sdk from './index';
|
2020-07-18 11:08:20 +00:00
|
|
|
import {RightPanelPhases} from "./stores/RightPanelStorePhases";
|
2020-03-10 15:31:40 +00:00
|
|
|
import {findDMForUser} from './createRoom';
|
2020-09-03 13:50:49 +00:00
|
|
|
import {accessSecretStorage} from './SecurityManager';
|
2020-03-11 14:52:48 +00:00
|
|
|
import {verificationMethods} from 'matrix-js-sdk/src/crypto';
|
2020-07-18 11:08:20 +00:00
|
|
|
import {Action} from './dispatcher/actions';
|
2021-05-12 16:04:24 +00:00
|
|
|
import UntrustedDeviceDialog from "./components/views/dialogs/UntrustedDeviceDialog";
|
2020-03-10 15:31:40 +00:00
|
|
|
|
2020-03-11 09:11:54 +00:00
|
|
|
async function enable4SIfNeeded() {
|
2020-03-10 15:31:40 +00:00
|
|
|
const cli = MatrixClientPeg.get();
|
2020-05-27 09:28:25 +00:00
|
|
|
if (!cli.isCryptoEnabled()) {
|
2020-03-10 15:31:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-10 17:40:24 +00:00
|
|
|
const usk = cli.getCrossSigningId("user_signing");
|
|
|
|
if (!usk) {
|
2020-03-10 15:31:40 +00:00
|
|
|
await accessSecretStorage();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function verifyDevice(user, device) {
|
2020-04-14 15:53:33 +00:00
|
|
|
const cli = MatrixClientPeg.get();
|
2021-01-26 11:00:56 +00:00
|
|
|
if (cli.isGuest()) {
|
|
|
|
dis.dispatch({action: 'require_registration'});
|
|
|
|
return;
|
|
|
|
}
|
2020-04-14 15:53:33 +00:00
|
|
|
// if cross-signing is not explicitly disabled, check if it should be enabled first.
|
|
|
|
if (cli.getCryptoTrustCrossSignedDevices()) {
|
|
|
|
if (!await enable4SIfNeeded()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-10 15:31:40 +00:00
|
|
|
}
|
2020-04-27 19:31:14 +00:00
|
|
|
|
2020-04-28 17:35:16 +00:00
|
|
|
Modal.createTrackedDialog("Verification warning", "unverified session", UntrustedDeviceDialog, {
|
|
|
|
user,
|
|
|
|
device,
|
|
|
|
onFinished: async (action) => {
|
|
|
|
if (action === "sas") {
|
|
|
|
const verificationRequestPromise = cli.legacyDeviceVerification(
|
|
|
|
user.userId,
|
|
|
|
device.deviceId,
|
|
|
|
verificationMethods.SAS,
|
|
|
|
);
|
|
|
|
dis.dispatch({
|
2020-07-18 11:08:20 +00:00
|
|
|
action: Action.SetRightPanelPhase,
|
|
|
|
phase: RightPanelPhases.EncryptionPanel,
|
2020-04-28 17:35:16 +00:00
|
|
|
refireParams: {member: user, verificationRequestPromise},
|
|
|
|
});
|
|
|
|
} else if (action === "legacy") {
|
|
|
|
const ManualDeviceKeyVerificationDialog =
|
|
|
|
sdk.getComponent("dialogs.ManualDeviceKeyVerificationDialog");
|
|
|
|
Modal.createTrackedDialog("Legacy verify session", "legacy verify session",
|
|
|
|
ManualDeviceKeyVerificationDialog,
|
|
|
|
{
|
|
|
|
userId: user.userId,
|
|
|
|
device,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2020-03-10 15:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function legacyVerifyUser(user) {
|
|
|
|
const cli = MatrixClientPeg.get();
|
2021-01-26 11:00:56 +00:00
|
|
|
if (cli.isGuest()) {
|
|
|
|
dis.dispatch({action: 'require_registration'});
|
|
|
|
return;
|
|
|
|
}
|
2020-04-14 15:53:33 +00:00
|
|
|
// if cross-signing is not explicitly disabled, check if it should be enabled first.
|
|
|
|
if (cli.getCryptoTrustCrossSignedDevices()) {
|
|
|
|
if (!await enable4SIfNeeded()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 08:32:27 +00:00
|
|
|
const verificationRequestPromise = cli.requestVerification(user.userId);
|
2020-03-10 15:31:40 +00:00
|
|
|
dis.dispatch({
|
2020-07-18 11:08:20 +00:00
|
|
|
action: Action.SetRightPanelPhase,
|
|
|
|
phase: RightPanelPhases.EncryptionPanel,
|
2020-03-10 15:31:40 +00:00
|
|
|
refireParams: {member: user, verificationRequestPromise},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function verifyUser(user) {
|
2021-01-26 11:00:56 +00:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
if (cli.isGuest()) {
|
|
|
|
dis.dispatch({action: 'require_registration'});
|
|
|
|
return;
|
|
|
|
}
|
2020-03-10 15:31:40 +00:00
|
|
|
if (!await enable4SIfNeeded()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-26 16:31:31 +00:00
|
|
|
const existingRequest = pendingVerificationRequestForUser(user);
|
2020-03-10 15:31:40 +00:00
|
|
|
dis.dispatch({
|
2020-07-18 11:08:20 +00:00
|
|
|
action: Action.SetRightPanelPhase,
|
|
|
|
phase: RightPanelPhases.EncryptionPanel,
|
2020-03-10 15:31:40 +00:00
|
|
|
refireParams: {
|
|
|
|
member: user,
|
|
|
|
verificationRequest: existingRequest,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2020-03-26 16:31:31 +00:00
|
|
|
|
|
|
|
export function pendingVerificationRequestForUser(user) {
|
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
const dmRoom = findDMForUser(cli, user.userId);
|
|
|
|
if (dmRoom) {
|
|
|
|
return cli.findVerificationRequestDMInProgress(dmRoom.roomId);
|
|
|
|
}
|
|
|
|
}
|