|
| 1 | +/* |
| 2 | +Copyright 2019 New Vector Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import request from 'browser-request'; |
| 18 | +import url from 'url'; |
| 19 | +import React from 'react'; |
| 20 | +import {_t} from "../../../languageHandler"; |
| 21 | +import sdk from '../../../index'; |
| 22 | +import MatrixClientPeg from "../../../MatrixClientPeg"; |
| 23 | +import SdkConfig from "../../../SdkConfig"; |
| 24 | +import Field from "../elements/Field"; |
| 25 | + |
| 26 | +/** |
| 27 | + * If a url has no path component, etc. abbreviate it to just the hostname |
| 28 | + * |
| 29 | + * @param {string} u The url to be abbreviated |
| 30 | + * @returns {string} The abbreviated url |
| 31 | + */ |
| 32 | +function abbreviateUrl(u) { |
| 33 | + if (!u) return ''; |
| 34 | + |
| 35 | + const parsedUrl = url.parse(u); |
| 36 | + // if it's something we can't parse as a url then just return it |
| 37 | + if (!parsedUrl) return u; |
| 38 | + |
| 39 | + if (parsedUrl.path == '/') { |
| 40 | + // we ignore query / hash parts: these aren't relevant for IS server URLs |
| 41 | + return parsedUrl.host; |
| 42 | + } |
| 43 | + |
| 44 | + return u; |
| 45 | +} |
| 46 | + |
| 47 | +function unabbreviateUrl(u) { |
| 48 | + if (!u) return ''; |
| 49 | + |
| 50 | + let longUrl = u; |
| 51 | + if (!u.startsWith('https://')) longUrl = 'https://' + u; |
| 52 | + const parsed = url.parse(longUrl); |
| 53 | + if (parsed.hostname === null) return u; |
| 54 | + |
| 55 | + return longUrl; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Check an IS URL is valid, including liveness check |
| 60 | + * |
| 61 | + * @param {string} isUrl The url to check |
| 62 | + * @returns {string} null if url passes all checks, otherwise i18ned error string |
| 63 | + */ |
| 64 | +async function checkIsUrl(isUrl) { |
| 65 | + const parsedUrl = url.parse(isUrl); |
| 66 | + |
| 67 | + if (parsedUrl.protocol !== 'https:') return _t("Identity Server URL must be HTTPS"); |
| 68 | + |
| 69 | + // XXX: duplicated logic from js-sdk but it's quite tied up in the validation logic in the |
| 70 | + // js-sdk so probably as easy to duplicate it than to separate it out so we can reuse it |
| 71 | + return new Promise((resolve) => { |
| 72 | + request( |
| 73 | + // also XXX: we don't really know whether to hit /v1 or /v2 for this: we |
| 74 | + // probably want a /versions endpoint like the C/S API. |
| 75 | + { method: "GET", url: isUrl + '/_matrix/identity/api/v1' }, |
| 76 | + (err, response, body) => { |
| 77 | + if (err) { |
| 78 | + resolve(_t("Could not connect to ID Server")); |
| 79 | + } else if (response.status < 200 || response.status >= 300) { |
| 80 | + resolve(_t("Not a valid ID Server (status code %(code)s)", {code: response.status})); |
| 81 | + } else { |
| 82 | + resolve(null); |
| 83 | + } |
| 84 | + }, |
| 85 | + ); |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +export default class SetIdServer extends React.Component { |
| 90 | + constructor() { |
| 91 | + super(); |
| 92 | + |
| 93 | + let defaultIdServer = abbreviateUrl(MatrixClientPeg.get().getIdentityServerUrl()); |
| 94 | + if (!defaultIdServer) { |
| 95 | + defaultIdServer = abbreviateUrl(SdkConfig.get()['validated_server_config']['idServer']) || ''; |
| 96 | + } |
| 97 | + |
| 98 | + this.state = { |
| 99 | + currentClientIdServer: MatrixClientPeg.get().getIdentityServerUrl(), |
| 100 | + idServer: defaultIdServer, |
| 101 | + error: null, |
| 102 | + busy: false, |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + _onIdentityServerChanged = (ev) => { |
| 107 | + const u = ev.target.value; |
| 108 | + |
| 109 | + this.setState({idServer: u}); |
| 110 | + }; |
| 111 | + |
| 112 | + _getTooltip = () => { |
| 113 | + if (this.state.busy) { |
| 114 | + const InlineSpinner = sdk.getComponent('views.elements.InlineSpinner'); |
| 115 | + return <div> |
| 116 | + <InlineSpinner /> |
| 117 | + { _t("Checking Server") } |
| 118 | + </div>; |
| 119 | + } else if (this.state.error) { |
| 120 | + return this.state.error; |
| 121 | + } else { |
| 122 | + return null; |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + _idServerChangeEnabled = () => { |
| 127 | + return !!this.state.idServer && !this.state.busy; |
| 128 | + }; |
| 129 | + |
| 130 | + _saveIdServer = async () => { |
| 131 | + this.setState({busy: true}); |
| 132 | + |
| 133 | + const fullUrl = unabbreviateUrl(this.state.idServer); |
| 134 | + |
| 135 | + const errStr = await checkIsUrl(fullUrl); |
| 136 | + |
| 137 | + let newFormValue = this.state.idServer; |
| 138 | + if (!errStr) { |
| 139 | + MatrixClientPeg.get().setIdentityServerUrl(fullUrl); |
| 140 | + localStorage.removeItem("mx_is_access_token"); |
| 141 | + localStorage.setItem("mx_is_url", fullUrl); |
| 142 | + newFormValue = ''; |
| 143 | + } |
| 144 | + this.setState({ |
| 145 | + busy: false, |
| 146 | + error: errStr, |
| 147 | + currentClientIdServer: MatrixClientPeg.get().getIdentityServerUrl(), |
| 148 | + idServer: newFormValue, |
| 149 | + }); |
| 150 | + }; |
| 151 | + |
| 152 | + render() { |
| 153 | + const idServerUrl = this.state.currentClientIdServer; |
| 154 | + let sectionTitle; |
| 155 | + let bodyText; |
| 156 | + if (idServerUrl) { |
| 157 | + sectionTitle = _t("Identity Server (%(server)s)", { server: abbreviateUrl(idServerUrl) }); |
| 158 | + bodyText = _t( |
| 159 | + "You are currently using <server></server> to discover and be discoverable by " + |
| 160 | + "existing contacts you know. You can change your identity server below.", |
| 161 | + {}, |
| 162 | + { server: sub => <b>{abbreviateUrl(idServerUrl)}</b> }, |
| 163 | + ); |
| 164 | + } else { |
| 165 | + sectionTitle = _t("Identity Server"); |
| 166 | + bodyText = _t( |
| 167 | + "You are not currently using an Identity Server. " + |
| 168 | + "To discover and be discoverable by existing contacts you know, " + |
| 169 | + "add one below", |
| 170 | + ); |
| 171 | + } |
| 172 | + |
| 173 | + return ( |
| 174 | + <form className="mx_SettingsTab_section mx_SetIdServer" onSubmit={this._saveIdServer}> |
| 175 | + <span className="mx_SettingsTab_subheading"> |
| 176 | + {sectionTitle} |
| 177 | + </span> |
| 178 | + <span className="mx_SettingsTab_subsectionText"> |
| 179 | + {bodyText} |
| 180 | + </span> |
| 181 | + <Field label={_t("Identity Server")} |
| 182 | + id="mx_SetIdServer_idServer" |
| 183 | + type="text" value={this.state.idServer} autoComplete="off" |
| 184 | + onChange={this._onIdentityServerChanged} |
| 185 | + tooltip={this._getTooltip()} |
| 186 | + /> |
| 187 | + <input className="mx_Dialog_primary" |
| 188 | + type="submit" value={_t("Change")} |
| 189 | + disabled={!this._idServerChangeEnabled()} |
| 190 | + /> |
| 191 | + </form> |
| 192 | + ); |
| 193 | + } |
| 194 | +} |
0 commit comments