Skip to content
This repository was archived by the owner on May 18, 2025. It is now read-only.

Commit 366f343

Browse files
authored
Merge pull request matrix-org#2327 from matrix-org/travis/well-known-improvements
Introduce a default_server_name for aesthetics and rework .well-known
2 parents 15366fb + 5f434cd commit 366f343

File tree

6 files changed

+190
-122
lines changed

6 files changed

+190
-122
lines changed

src/components/structures/MatrixChat.js

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ import SettingsStore, {SettingLevel} from "../../settings/SettingsStore";
4848
import { startAnyRegistrationFlow } from "../../Registration.js";
4949
import { messageForSyncError } from '../../utils/ErrorUtils';
5050

51+
const AutoDiscovery = Matrix.AutoDiscovery;
52+
5153
// Disable warnings for now: we use deprecated bluebird functions
5254
// and need to migrate, but they spam the console with warnings.
5355
Promise.config({warnings: false});
@@ -181,6 +183,12 @@ export default React.createClass({
181183
register_is_url: null,
182184
register_id_sid: null,
183185

186+
// Parameters used for setting up the login/registration views
187+
defaultServerName: this.props.config.default_server_name,
188+
defaultHsUrl: this.props.config.default_hs_url,
189+
defaultIsUrl: this.props.config.default_is_url,
190+
defaultServerDiscoveryError: null,
191+
184192
// When showing Modal dialogs we need to set aria-hidden on the root app element
185193
// and disable it when there are no dialogs
186194
hideToSRUsers: false,
@@ -199,6 +207,10 @@ export default React.createClass({
199207
};
200208
},
201209

210+
getDefaultServerName: function() {
211+
return this.state.defaultServerName;
212+
},
213+
202214
getCurrentHsUrl: function() {
203215
if (this.state.register_hs_url) {
204216
return this.state.register_hs_url;
@@ -209,8 +221,10 @@ export default React.createClass({
209221
}
210222
},
211223

212-
getDefaultHsUrl() {
213-
return this.props.config.default_hs_url || "https://matrix.org";
224+
getDefaultHsUrl(defaultToMatrixDotOrg) {
225+
defaultToMatrixDotOrg = typeof(defaultToMatrixDotOrg) !== 'boolean' ? true : defaultToMatrixDotOrg;
226+
if (!this.state.defaultHsUrl && defaultToMatrixDotOrg) return "https://matrix.org";
227+
return this.state.defaultHsUrl;
214228
},
215229

216230
getFallbackHsUrl: function() {
@@ -228,7 +242,7 @@ export default React.createClass({
228242
},
229243

230244
getDefaultIsUrl() {
231-
return this.props.config.default_is_url || "https://vector.im";
245+
return this.state.defaultIsUrl || "https://vector.im";
232246
},
233247

234248
componentWillMount: function() {
@@ -278,6 +292,20 @@ export default React.createClass({
278292
console.info(`Team token set to ${this._teamToken}`);
279293
}
280294

295+
// Set up the default URLs (async)
296+
if (this.getDefaultServerName() && !this.getDefaultHsUrl(false)) {
297+
this.setState({loadingDefaultHomeserver: true});
298+
this._tryDiscoverDefaultHomeserver(this.getDefaultServerName());
299+
} else if (this.getDefaultServerName() && this.getDefaultHsUrl(false)) {
300+
// Ideally we would somehow only communicate this to the server admins, but
301+
// given this is at login time we can't really do much besides hope that people
302+
// will check their settings.
303+
this.setState({
304+
defaultServerName: null, // To un-hide any secrets people might be keeping
305+
defaultServerDiscoveryError: _t("Invalid configuration: Cannot supply a default homeserver URL and a default server name"),
306+
});
307+
}
308+
281309
// Set a default HS with query param `hs_url`
282310
const paramHs = this.props.startingFragmentQueryParams.hs_url;
283311
if (paramHs) {
@@ -1728,6 +1756,36 @@ export default React.createClass({
17281756
this.setState(newState);
17291757
},
17301758

1759+
_tryDiscoverDefaultHomeserver: async function(serverName) {
1760+
try {
1761+
const discovery = await AutoDiscovery.findClientConfig(serverName);
1762+
const state = discovery["m.homeserver"].state;
1763+
if (state !== AutoDiscovery.SUCCESS) {
1764+
console.error("Failed to discover homeserver on startup:", discovery);
1765+
this.setState({
1766+
defaultServerDiscoveryError: discovery["m.homeserver"].error,
1767+
loadingDefaultHomeserver: false,
1768+
});
1769+
} else {
1770+
const hsUrl = discovery["m.homeserver"].base_url;
1771+
const isUrl = discovery["m.identity_server"].state === AutoDiscovery.SUCCESS
1772+
? discovery["m.identity_server"].base_url
1773+
: "https://vector.im";
1774+
this.setState({
1775+
defaultHsUrl: hsUrl,
1776+
defaultIsUrl: isUrl,
1777+
loadingDefaultHomeserver: false,
1778+
});
1779+
}
1780+
} catch (e) {
1781+
console.error(e);
1782+
this.setState({
1783+
defaultServerDiscoveryError: _t("Unknown error discovering homeserver"),
1784+
loadingDefaultHomeserver: false,
1785+
});
1786+
}
1787+
},
1788+
17311789
_makeRegistrationUrl: function(params) {
17321790
if (this.props.startingFragmentQueryParams.referrer) {
17331791
params.referrer = this.props.startingFragmentQueryParams.referrer;
@@ -1742,7 +1800,7 @@ export default React.createClass({
17421800
render: function() {
17431801
// console.log(`Rendering MatrixChat with view ${this.state.view}`);
17441802

1745-
if (this.state.view === VIEWS.LOADING || this.state.view === VIEWS.LOGGING_IN) {
1803+
if (this.state.view === VIEWS.LOADING || this.state.view === VIEWS.LOGGING_IN || this.state.loadingDefaultHomeserver) {
17461804
const Spinner = sdk.getComponent('elements.Spinner');
17471805
return (
17481806
<div className="mx_MatrixChat_splash">
@@ -1816,6 +1874,8 @@ export default React.createClass({
18161874
idSid={this.state.register_id_sid}
18171875
email={this.props.startingFragmentQueryParams.email}
18181876
referrer={this.props.startingFragmentQueryParams.referrer}
1877+
defaultServerName={this.getDefaultServerName()}
1878+
defaultServerDiscoveryError={this.state.defaultServerDiscoveryError}
18191879
defaultHsUrl={this.getDefaultHsUrl()}
18201880
defaultIsUrl={this.getDefaultIsUrl()}
18211881
brand={this.props.config.brand}
@@ -1838,6 +1898,8 @@ export default React.createClass({
18381898
const ForgotPassword = sdk.getComponent('structures.login.ForgotPassword');
18391899
return (
18401900
<ForgotPassword
1901+
defaultServerName={this.getDefaultServerName()}
1902+
defaultServerDiscoveryError={this.state.defaultServerDiscoveryError}
18411903
defaultHsUrl={this.getDefaultHsUrl()}
18421904
defaultIsUrl={this.getDefaultIsUrl()}
18431905
customHsUrl={this.getCurrentHsUrl()}
@@ -1854,6 +1916,8 @@ export default React.createClass({
18541916
<Login
18551917
onLoggedIn={Lifecycle.setLoggedIn}
18561918
onRegisterClick={this.onRegisterClick}
1919+
defaultServerName={this.getDefaultServerName()}
1920+
defaultServerDiscoveryError={this.state.defaultServerDiscoveryError}
18571921
defaultHsUrl={this.getDefaultHsUrl()}
18581922
defaultIsUrl={this.getDefaultIsUrl()}
18591923
customHsUrl={this.getCurrentHsUrl()}

src/components/structures/login/ForgotPassword.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ module.exports = React.createClass({
3636
onLoginClick: PropTypes.func,
3737
onRegisterClick: PropTypes.func,
3838
onComplete: PropTypes.func.isRequired,
39+
40+
// The default server name to use when the user hasn't specified
41+
// one. This is used when displaying the defaultHsUrl in the UI.
42+
defaultServerName: PropTypes.string,
43+
44+
// An error passed along from higher up explaining that something
45+
// went wrong when finding the defaultHsUrl.
46+
defaultServerDiscoveryError: PropTypes.string,
3947
},
4048

4149
getInitialState: function() {
@@ -45,6 +53,7 @@ module.exports = React.createClass({
4553
progress: null,
4654
password: null,
4755
password2: null,
56+
errorText: null,
4857
};
4958
},
5059

@@ -81,6 +90,13 @@ module.exports = React.createClass({
8190
onSubmitForm: function(ev) {
8291
ev.preventDefault();
8392

93+
// Don't allow the user to register if there's a discovery error
94+
// Without this, the user could end up registering on the wrong homeserver.
95+
if (this.props.defaultServerDiscoveryError) {
96+
this.setState({errorText: this.props.defaultServerDiscoveryError});
97+
return;
98+
}
99+
84100
if (!this.state.email) {
85101
this.showErrorDialog(_t('The email address linked to your account must be entered.'));
86102
} else if (!this.state.password || !this.state.password2) {
@@ -200,6 +216,12 @@ module.exports = React.createClass({
200216
);
201217
}
202218

219+
let errorText = null;
220+
const err = this.state.errorText || this.props.defaultServerDiscoveryError;
221+
if (err) {
222+
errorText = <div className="mx_Login_error">{ err }</div>;
223+
}
224+
203225
const LanguageSelector = sdk.getComponent('structures.login.LanguageSelector');
204226

205227
resetPasswordJsx = (
@@ -230,6 +252,7 @@ module.exports = React.createClass({
230252
<input className="mx_Login_submit" type="submit" value={_t('Send Reset Email')} />
231253
</form>
232254
{ serverConfigSection }
255+
{ errorText }
233256
<a className="mx_Login_create" onClick={this.props.onLoginClick} href="#">
234257
{ _t('Return to login screen') }
235258
</a>

0 commit comments

Comments
 (0)