2019-08-12 13:51:44 +03:00
|
|
|
/*
|
2019-08-13 12:46:42 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-08-12 13:51:44 +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 url from 'url';
|
|
|
|
import React from 'react';
|
|
|
|
import {_t} from "../../../languageHandler";
|
|
|
|
import sdk from '../../../index';
|
|
|
|
import MatrixClientPeg from "../../../MatrixClientPeg";
|
|
|
|
import SdkConfig from "../../../SdkConfig";
|
2019-08-12 16:36:48 +03:00
|
|
|
import Modal from '../../../Modal';
|
2019-08-13 20:40:27 +03:00
|
|
|
import dis from "../../../dispatcher";
|
2019-08-15 14:04:07 +03:00
|
|
|
import { getThreepidBindStatus } from '../../../boundThreepids';
|
2019-08-12 13:51:44 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If a url has no path component, etc. abbreviate it to just the hostname
|
|
|
|
*
|
|
|
|
* @param {string} u The url to be abbreviated
|
|
|
|
* @returns {string} The abbreviated url
|
|
|
|
*/
|
|
|
|
function abbreviateUrl(u) {
|
|
|
|
if (!u) return '';
|
|
|
|
|
|
|
|
const parsedUrl = url.parse(u);
|
|
|
|
// if it's something we can't parse as a url then just return it
|
|
|
|
if (!parsedUrl) return u;
|
|
|
|
|
|
|
|
if (parsedUrl.path == '/') {
|
|
|
|
// we ignore query / hash parts: these aren't relevant for IS server URLs
|
|
|
|
return parsedUrl.host;
|
|
|
|
}
|
|
|
|
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
|
|
|
function unabbreviateUrl(u) {
|
|
|
|
if (!u) return '';
|
|
|
|
|
|
|
|
let longUrl = u;
|
|
|
|
if (!u.startsWith('https://')) longUrl = 'https://' + u;
|
|
|
|
const parsed = url.parse(longUrl);
|
|
|
|
if (parsed.hostname === null) return u;
|
|
|
|
|
|
|
|
return longUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check an IS URL is valid, including liveness check
|
|
|
|
*
|
2019-08-13 14:59:34 +03:00
|
|
|
* @param {string} u The url to check
|
2019-08-12 13:51:44 +03:00
|
|
|
* @returns {string} null if url passes all checks, otherwise i18ned error string
|
|
|
|
*/
|
2019-08-13 14:59:34 +03:00
|
|
|
async function checkIdentityServerUrl(u) {
|
|
|
|
const parsedUrl = url.parse(u);
|
2019-08-12 13:51:44 +03:00
|
|
|
|
|
|
|
if (parsedUrl.protocol !== 'https:') return _t("Identity Server URL must be HTTPS");
|
|
|
|
|
|
|
|
// XXX: duplicated logic from js-sdk but it's quite tied up in the validation logic in the
|
|
|
|
// js-sdk so probably as easy to duplicate it than to separate it out so we can reuse it
|
2019-08-13 18:37:56 +03:00
|
|
|
try {
|
|
|
|
const response = await fetch(u + '/_matrix/identity/api/v1');
|
|
|
|
if (response.ok) {
|
|
|
|
return null;
|
|
|
|
} else if (response.status < 200 || response.status >= 300) {
|
|
|
|
return _t("Not a valid Identity Server (status code %(code)s)", {code: response.status});
|
|
|
|
} else {
|
|
|
|
return _t("Could not connect to Identity Server");
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return _t("Could not connect to Identity Server");
|
|
|
|
}
|
2019-08-12 13:51:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class SetIdServer extends React.Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2019-08-13 19:55:10 +03:00
|
|
|
let defaultIdServer = '';
|
|
|
|
if (!MatrixClientPeg.get().getIdentityServerUrl() && SdkConfig.get()['validated_server_config']['isUrl']) {
|
|
|
|
// If no ID server is configured but there's one in the config, prepopulate
|
|
|
|
// the field to help the user.
|
|
|
|
defaultIdServer = abbreviateUrl(SdkConfig.get()['validated_server_config']['isUrl']);
|
2019-08-12 13:51:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
currentClientIdServer: MatrixClientPeg.get().getIdentityServerUrl(),
|
|
|
|
idServer: defaultIdServer,
|
|
|
|
error: null,
|
|
|
|
busy: false,
|
2019-08-15 14:04:07 +03:00
|
|
|
disconnectBusy: false,
|
2019-08-12 13:51:44 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_onIdentityServerChanged = (ev) => {
|
|
|
|
const u = ev.target.value;
|
|
|
|
|
|
|
|
this.setState({idServer: u});
|
|
|
|
};
|
|
|
|
|
|
|
|
_getTooltip = () => {
|
|
|
|
if (this.state.busy) {
|
|
|
|
const InlineSpinner = sdk.getComponent('views.elements.InlineSpinner');
|
|
|
|
return <div>
|
|
|
|
<InlineSpinner />
|
2019-08-13 12:51:26 +03:00
|
|
|
{ _t("Checking server") }
|
2019-08-12 13:51:44 +03:00
|
|
|
</div>;
|
|
|
|
} else if (this.state.error) {
|
|
|
|
return this.state.error;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_idServerChangeEnabled = () => {
|
|
|
|
return !!this.state.idServer && !this.state.busy;
|
|
|
|
};
|
|
|
|
|
2019-08-13 18:20:30 +03:00
|
|
|
_saveIdServer = async (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2019-08-12 13:51:44 +03:00
|
|
|
this.setState({busy: true});
|
|
|
|
|
|
|
|
const fullUrl = unabbreviateUrl(this.state.idServer);
|
|
|
|
|
2019-08-13 14:59:34 +03:00
|
|
|
const errStr = await checkIdentityServerUrl(fullUrl);
|
2019-08-12 15:29:52 +03:00
|
|
|
|
|
|
|
let newFormValue = this.state.idServer;
|
2019-08-12 13:51:44 +03:00
|
|
|
if (!errStr) {
|
|
|
|
MatrixClientPeg.get().setIdentityServerUrl(fullUrl);
|
2019-08-12 16:46:04 +03:00
|
|
|
localStorage.removeItem("mx_is_access_token");
|
2019-08-12 13:51:44 +03:00
|
|
|
localStorage.setItem("mx_is_url", fullUrl);
|
2019-08-13 20:40:27 +03:00
|
|
|
dis.dispatch({action: 'id_server_changed'});
|
2019-08-12 15:29:52 +03:00
|
|
|
newFormValue = '';
|
2019-08-12 13:51:44 +03:00
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
error: errStr,
|
|
|
|
currentClientIdServer: MatrixClientPeg.get().getIdentityServerUrl(),
|
2019-08-12 15:29:52 +03:00
|
|
|
idServer: newFormValue,
|
2019-08-12 13:51:44 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-08-15 14:04:07 +03:00
|
|
|
_onDisconnectClicked = async () => {
|
|
|
|
this.setState({disconnectBusy: true});
|
|
|
|
try {
|
|
|
|
const threepids = await getThreepidBindStatus(MatrixClientPeg.get());
|
|
|
|
|
|
|
|
const boundThreepids = threepids.filter(tp => tp.bound);
|
|
|
|
let message;
|
|
|
|
if (boundThreepids.length) {
|
|
|
|
message = _t(
|
|
|
|
"You are currently sharing email addresses or phone numbers on the identity " +
|
|
|
|
"server <idserver />. You will need to reconnect to <idserver2 /> to stop " +
|
|
|
|
"sharing them.", {},
|
|
|
|
{
|
|
|
|
idserver: sub => <b>{abbreviateUrl(this.state.currentClientIdServer)}</b>,
|
2019-08-15 17:39:15 +03:00
|
|
|
// XXX: https://github.com/vector-im/riot-web/issues/9086
|
2019-08-15 14:04:07 +03:00
|
|
|
idserver2: sub => <b>{abbreviateUrl(this.state.currentClientIdServer)}</b>,
|
2019-08-15 15:10:05 +03:00
|
|
|
},
|
2019-08-15 14:04:07 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
message = _t(
|
|
|
|
"Disconnect from the identity server <idserver />?", {},
|
|
|
|
{idserver: sub => <b>{abbreviateUrl(this.state.currentClientIdServer)}</b>},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const QuestionDialog = sdk.getComponent("dialogs.QuestionDialog");
|
|
|
|
Modal.createTrackedDialog('Identity Server Disconnect Warning', '', QuestionDialog, {
|
|
|
|
title: _t("Disconnect Identity Server"),
|
|
|
|
description: message,
|
|
|
|
button: _t("Disconnect"),
|
|
|
|
onFinished: (confirmed) => {
|
|
|
|
if (confirmed) {
|
|
|
|
this._disconnectIdServer();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
this.setState({disconnectBusy: false});
|
|
|
|
}
|
2019-08-12 16:36:48 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
_disconnectIdServer = () => {
|
|
|
|
MatrixClientPeg.get().setIdentityServerUrl(null);
|
2019-08-12 16:45:15 +03:00
|
|
|
localStorage.removeItem("mx_is_access_token");
|
2019-08-12 16:36:48 +03:00
|
|
|
localStorage.removeItem("mx_is_url");
|
2019-08-14 12:06:05 +03:00
|
|
|
|
|
|
|
let newFieldVal = '';
|
|
|
|
if (SdkConfig.get()['validated_server_config']['isUrl']) {
|
|
|
|
// Prepopulate the client's default so the user at least has some idea of
|
|
|
|
// a valid value they might enter
|
|
|
|
newFieldVal = abbreviateUrl(SdkConfig.get()['validated_server_config']['isUrl']);
|
|
|
|
}
|
|
|
|
|
2019-08-12 16:36:48 +03:00
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
error: null,
|
|
|
|
currentClientIdServer: MatrixClientPeg.get().getIdentityServerUrl(),
|
2019-08-14 12:06:05 +03:00
|
|
|
idServer: newFieldVal,
|
2019-08-12 16:36:48 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-08-12 13:51:44 +03:00
|
|
|
render() {
|
2019-08-12 16:36:48 +03:00
|
|
|
const AccessibleButton = sdk.getComponent('views.elements.AccessibleButton');
|
2019-08-14 12:03:32 +03:00
|
|
|
const Field = sdk.getComponent('elements.Field');
|
2019-08-12 13:51:44 +03:00
|
|
|
const idServerUrl = this.state.currentClientIdServer;
|
|
|
|
let sectionTitle;
|
|
|
|
let bodyText;
|
|
|
|
if (idServerUrl) {
|
|
|
|
sectionTitle = _t("Identity Server (%(server)s)", { server: abbreviateUrl(idServerUrl) });
|
|
|
|
bodyText = _t(
|
|
|
|
"You are currently using <server></server> to discover and be discoverable by " +
|
|
|
|
"existing contacts you know. You can change your identity server below.",
|
|
|
|
{},
|
|
|
|
{ server: sub => <b>{abbreviateUrl(idServerUrl)}</b> },
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
sectionTitle = _t("Identity Server");
|
|
|
|
bodyText = _t(
|
2019-08-13 12:51:26 +03:00
|
|
|
"You are not currently using an identity server. " +
|
2019-08-12 13:51:44 +03:00
|
|
|
"To discover and be discoverable by existing contacts you know, " +
|
2019-08-13 12:51:26 +03:00
|
|
|
"add one below.",
|
2019-08-12 13:51:44 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-12 16:36:48 +03:00
|
|
|
let discoSection;
|
|
|
|
if (idServerUrl) {
|
2019-08-15 14:04:07 +03:00
|
|
|
let discoButtonContent = _t("Disconnect");
|
|
|
|
if (this.state.disconnectBusy) {
|
|
|
|
const InlineSpinner = sdk.getComponent('views.elements.InlineSpinner');
|
|
|
|
discoButtonContent = <InlineSpinner />;
|
|
|
|
}
|
2019-08-12 16:36:48 +03:00
|
|
|
discoSection = <div>
|
|
|
|
<span className="mx_SettingsTab_subsectionText">{_t(
|
|
|
|
"Disconnecting from your identity server will mean you " +
|
2019-08-14 12:07:50 +03:00
|
|
|
"won't be discoverable by other users and you won't be " +
|
2019-08-12 16:36:48 +03:00
|
|
|
"able to invite others by email or phone.",
|
|
|
|
)}</span>
|
|
|
|
<AccessibleButton onClick={this._onDisconnectClicked} kind="danger">
|
2019-08-15 14:04:07 +03:00
|
|
|
{discoButtonContent}
|
2019-08-12 16:36:48 +03:00
|
|
|
</AccessibleButton>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:51:44 +03:00
|
|
|
return (
|
|
|
|
<form className="mx_SettingsTab_section mx_SetIdServer" onSubmit={this._saveIdServer}>
|
|
|
|
<span className="mx_SettingsTab_subheading">
|
|
|
|
{sectionTitle}
|
|
|
|
</span>
|
|
|
|
<span className="mx_SettingsTab_subsectionText">
|
|
|
|
{bodyText}
|
|
|
|
</span>
|
|
|
|
<Field label={_t("Identity Server")}
|
|
|
|
id="mx_SetIdServer_idServer"
|
|
|
|
type="text" value={this.state.idServer} autoComplete="off"
|
|
|
|
onChange={this._onIdentityServerChanged}
|
2019-08-13 13:01:04 +03:00
|
|
|
tooltipContent={this._getTooltip()}
|
2019-08-12 13:51:44 +03:00
|
|
|
/>
|
2019-08-13 18:20:30 +03:00
|
|
|
<AccessibleButton type="submit" kind="primary_sm"
|
|
|
|
onClick={this._saveIdServer}
|
2019-08-12 13:51:44 +03:00
|
|
|
disabled={!this._idServerChangeEnabled()}
|
2019-08-13 18:20:30 +03:00
|
|
|
>{_t("Change")}</AccessibleButton>
|
2019-08-12 16:36:48 +03:00
|
|
|
{discoSection}
|
2019-08-12 13:51:44 +03:00
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|