/* Copyright 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 React from 'react'; import PropTypes from 'prop-types'; import { _t } from '../../../languageHandler'; import * as sdk from '../../../index'; import { MatrixClientPeg } from '../../../MatrixClientPeg'; import { accessSecretStorage } from '../../../CrossSigningManager'; const PHASE_INTRO = 0; const PHASE_BUSY = 1; const PHASE_DONE = 2; const PHASE_CONFIRM_SKIP = 3; export default class CompleteSecurity extends React.Component { static propTypes = { onFinished: PropTypes.func.isRequired, }; constructor() { super(); this.state = { phase: PHASE_INTRO, // this serves dual purpose as the object for the request logic and // the presence of it insidicating that we're in 'verify mode'. // Because of the latter, it lives in the state. verificationRequest: null, backupInfo: null, }; MatrixClientPeg.get().on("crypto.verification.request", this.onVerificationRequest); } componentWillUnmount() { if (this.state.verificationRequest) { this.state.verificationRequest.off("change", this.onVerificationRequestChange); } if (MatrixClientPeg.get()) { MatrixClientPeg.get().removeListener("crypto.verification.request", this.onVerificationRequest); } } onStartClick = async () => { this.setState({ phase: PHASE_BUSY, }); const cli = MatrixClientPeg.get(); const backupInfo = await cli.getKeyBackupVersion(); this.setState({backupInfo}); try { await accessSecretStorage(async () => { await cli.checkOwnCrossSigningTrust(); if (backupInfo) await cli.restoreKeyBackupWithSecretStorage(backupInfo); }); if (cli.getCrossSigningId()) { this.setState({ phase: PHASE_DONE, }); } } catch (e) { // this will throw if the user hits cancel, so ignore this.setState({ phase: PHASE_INTRO, }); } } onVerificationRequest = (request) => { if (request.otherUserId !== MatrixClientPeg.get().getUserId()) return; if (this.state.verificationRequest) { this.state.verificationRequest.off("change", this.onVerificationRequestChange); } request.on("change", this.onVerificationRequestChange); this.setState({ verificationRequest: request, }); } onVerificationRequestChange = () => { if (this.state.verificationRequest.cancelled) { this.state.verificationRequest.off("change", this.onVerificationRequestChange); this.setState({ verificationRequest: null, }); } } onSkipClick = () => { this.setState({ phase: PHASE_CONFIRM_SKIP, }); } onSkipConfirmClick = () => { this.props.onFinished(); } onSkipBackClick = () => { this.setState({ phase: PHASE_INTRO, }); } onDoneClick = () => { this.props.onFinished(); } render() { const AuthPage = sdk.getComponent("auth.AuthPage"); const CompleteSecurityBody = sdk.getComponent("auth.CompleteSecurityBody"); const AccessibleButton = sdk.getComponent("elements.AccessibleButton"); const { phase, } = this.state; let icon; let title; let body; if (this.state.verificationRequest) { const IncomingSasDialog = sdk.getComponent("views.dialogs.IncomingSasDialog"); body = ; } else if (phase === PHASE_INTRO) { icon = ; title = _t("Complete security"); body = (

{_t( "Verify this session to grant it access to encrypted messages.", )}

{_t("Skip")} {_t("Start")}
); } else if (phase === PHASE_DONE) { icon = ; title = _t("Session verified"); let message; if (this.state.backupInfo) { message =

{_t( "Your new session is now verified. It has access to your " + "encrypted messages, and other users will see it as trusted.", )}

; } else { message =

{_t( "Your new session is now verified. Other users will see it as trusted.", )}

; } body = (
{message}
{_t("Done")}
); } else if (phase === PHASE_CONFIRM_SKIP) { icon = ; title = _t("Are you sure?"); body = (

{_t( "Without completing security on this session, it won’t have " + "access to encrypted messages.", )}

{_t("Skip")} {_t("Go Back")}
); } else if (phase === PHASE_BUSY) { const Spinner = sdk.getComponent('views.elements.Spinner'); icon = ; title = _t("Complete security"); body = ; } else { throw new Error(`Unknown phase ${phase}`); } return (

{icon} {title}

{body}
); } }