element-web/src/components/views/right_panel/VerificationPanel.js

246 lines
8.6 KiB
JavaScript
Raw Normal View History

2019-12-16 20:16:22 +03:00
/*
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
2019-12-16 20:16:22 +03:00
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 React from "react";
import PropTypes from "prop-types";
import * as sdk from '../../../index';
import {verificationMethods} from 'matrix-js-sdk/src/crypto';
2020-01-18 05:53:33 +03:00
import VerificationQRCode from "../elements/crypto/VerificationQRCode";
import {MatrixClientPeg} from "../../../MatrixClientPeg";
import {_t} from "../../../languageHandler";
import E2EIcon from "../rooms/E2EIcon";
2020-01-28 14:13:09 +03:00
import {
PHASE_UNSENT,
PHASE_REQUESTED,
2020-01-28 14:13:09 +03:00
PHASE_READY,
PHASE_DONE,
PHASE_STARTED,
PHASE_CANCELLED,
} from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest";
import Spinner from "../elements/Spinner";
2019-12-16 20:16:22 +03:00
export default class VerificationPanel extends React.PureComponent {
static propTypes = {
request: PropTypes.object.isRequired,
member: PropTypes.object.isRequired,
phase: PropTypes.oneOf([
PHASE_UNSENT,
PHASE_REQUESTED,
PHASE_READY,
PHASE_STARTED,
PHASE_CANCELLED,
PHASE_DONE,
]).isRequired,
onClose: PropTypes.func.isRequired,
};
2019-12-16 20:16:22 +03:00
constructor(props) {
super(props);
this.state = {};
this._hasVerifier = false;
2019-12-16 20:16:22 +03:00
}
2020-01-28 14:13:09 +03:00
renderQRPhase(pending) {
const {member, request} = this.props;
const AccessibleButton = sdk.getComponent('elements.AccessibleButton');
2020-01-24 19:41:43 +03:00
2020-01-28 14:13:09 +03:00
let button;
if (pending) {
button = <Spinner />;
} else {
const disabled = this.state.emojiButtonClicked;
2020-01-28 14:13:09 +03:00
button = (
<AccessibleButton disabled={disabled} kind="primary" className="mx_UserInfo_wideButton" onClick={this._startSAS}>
2020-01-28 14:13:09 +03:00
{_t("Verify by emoji")}
</AccessibleButton>
);
}
2020-01-27 18:48:19 +03:00
const cli = MatrixClientPeg.get();
const crossSigningInfo = cli.getStoredCrossSigningForUser(request.otherUserId);
if (!crossSigningInfo || !request.requestEvent || !request.requestEvent.getId()) {
// for whatever reason we can't generate a QR code, offer only SAS Verification
return <div className="mx_UserInfo_container">
<h3>Verify by emoji</h3>
<p>{_t("Verify by comparing unique emoji.")}</p>
2020-01-28 14:13:09 +03:00
{ button }
</div>;
2020-01-24 19:41:43 +03:00
}
2020-01-27 18:48:19 +03:00
const myKeyId = cli.getCrossSigningId();
2020-01-24 19:41:43 +03:00
const qrCodeKeys = [
2020-01-27 18:48:19 +03:00
[cli.getDeviceId(), cli.getDeviceEd25519Key()],
[myKeyId, myKeyId],
2020-01-24 19:41:43 +03:00
];
2020-01-27 20:17:05 +03:00
// TODO: add way to open camera to scan a QR code
return <React.Fragment>
2020-01-03 15:40:20 +03:00
<div className="mx_UserInfo_container">
<h3>Verify by scanning</h3>
2020-01-27 20:17:05 +03:00
<p>{_t("Ask %(displayName)s to scan your code:", {
displayName: member.displayName || member.name || member.userId,
})}</p>
2020-01-24 19:41:43 +03:00
2020-01-27 20:17:05 +03:00
<div className="mx_VerificationPanel_qrCode">
<VerificationQRCode
keyholderUserId={MatrixClientPeg.get().getUserId()}
requestEventId={request.requestEvent.getId()}
otherUserKey={crossSigningInfo.getId("master")}
secret={request.encodedSharedSecret}
keys={qrCodeKeys}
/>
</div>
</div>
<div className="mx_UserInfo_container">
<h3>Verify by emoji</h3>
<p>{_t("If you can't scan the code above, verify by comparing unique emoji.")}</p>
2020-01-24 19:41:43 +03:00
2020-01-28 14:13:09 +03:00
{ button }
</div>
</React.Fragment>;
}
renderVerifiedPhase() {
const {member} = this.props;
2019-12-16 20:16:22 +03:00
const AccessibleButton = sdk.getComponent('elements.AccessibleButton');
return (
<div className="mx_UserInfo_container mx_VerificationPanel_verified_section">
<h3>Verified</h3>
<p>{_t("You've successfully verified %(displayName)s!", {
displayName: member.displayName || member.name || member.userId,
})}</p>
<E2EIcon isUser={true} status="verified" size={128} />
<p>Verify all users in a room to ensure it's secure.</p>
2020-01-28 14:13:09 +03:00
<AccessibleButton kind="primary" className="mx_UserInfo_wideButton" onClick={this.props.onClose}>
{_t("Got it")}
</AccessibleButton>
</div>
);
}
2020-01-28 14:13:09 +03:00
renderCancelledPhase() {
const {member, request} = this.props;
2020-01-28 14:13:09 +03:00
const AccessibleButton = sdk.getComponent('elements.AccessibleButton');
let text;
if (request.cancellationCode === "m.timeout") {
text = _t("Verification timed out. Start verification again from their profile.");
} else if (request.cancellingUserId === request.otherUserId) {
text = _t("%(displayName)s cancelled verification. Start verification again from their profile.", {
displayName: member.displayName || member.name || member.userId,
});
} else {
text = _t("You cancelled verification. Start verification again from their profile.");
}
return (
<div className="mx_UserInfo_container">
<h3>Verification cancelled</h3>
<p>{ text }</p>
<AccessibleButton kind="primary" className="mx_UserInfo_wideButton" onClick={this.props.onClose}>
2020-01-28 14:13:09 +03:00
{_t("Got it")}
</AccessibleButton>
</div>
);
}
render() {
const {member, phase} = this.props;
2020-01-28 14:13:09 +03:00
const displayName = member.displayName || member.name || member.userId;
2019-12-16 20:16:22 +03:00
switch (phase) {
2020-01-28 14:13:09 +03:00
case PHASE_READY:
return this.renderQRPhase();
case PHASE_STARTED:
if (this.state.sasEvent) {
const VerificationShowSas = sdk.getComponent('views.verification.VerificationShowSas');
return <div className="mx_UserInfo_container">
<h3>Compare emoji</h3>
<VerificationShowSas
displayName={displayName}
sas={this.state.sasEvent.sas}
onCancel={this._onSasMismatchesClick}
onDone={this._onSasMatchesClick}
/>
</div>;
} else {
return this.renderQRPhase(true); // keep showing same phase but with a spinner
}
case PHASE_DONE:
return this.renderVerifiedPhase();
case PHASE_CANCELLED:
return this.renderCancelledPhase();
2019-12-16 20:16:22 +03:00
}
2020-01-29 11:08:52 +03:00
console.error("VerificationPanel unhandled phase:", phase);
return null;
2019-12-16 20:16:22 +03:00
}
_startSAS = async () => {
this.setState({emojiButtonClicked: true});
2019-12-16 20:16:22 +03:00
const verifier = this.props.request.beginKeyVerification(verificationMethods.SAS);
try {
await verifier.verify();
} catch (err) {
console.error(err);
2019-12-16 20:16:22 +03:00
}
};
_onSasMatchesClick = () => {
this.state.sasEvent.confirm();
};
_onSasMismatchesClick = () => {
2020-01-28 14:13:09 +03:00
this.state.sasEvent.mismatch();
2019-12-16 20:16:22 +03:00
};
_onVerifierShowSas = (sasEvent) => {
this.setState({sasEvent});
};
_onRequestChange = async () => {
const {request} = this.props;
const hadVerifier = this._hasVerifier;
this._hasVerifier = !!request.verifier;
if (!hadVerifier && this._hasVerifier) {
request.verifier.once('show_sas', this._onVerifierShowSas);
try {
// on the requester side, this is also awaited in _startSAS,
// but that's ok as verify should return the same promise.
await request.verifier.verify();
} catch (err) {
console.error("error verify", err);
}
}
2019-12-16 20:16:22 +03:00
};
componentDidMount() {
this.props.request.on("change", this._onRequestChange);
this._onRequestChange();
2019-12-16 20:16:22 +03:00
}
componentWillUnmount() {
this.props.request.off("change", this._onRequestChange);
}
}