From 6f67ddbce57d6c80263d1a5d6741e06ddbeed6c1 Mon Sep 17 00:00:00 2001 From: Bad Date: Wed, 21 Oct 2020 21:11:45 +0200 Subject: [PATCH 1/7] Refactor homeserver lookup code --- src/js/login.js | 82 ++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 31 deletions(-) diff --git a/src/js/login.js b/src/js/login.js index 55caa66..c285898 100644 --- a/src/js/login.js +++ b/src/js/login.js @@ -71,46 +71,23 @@ class Form extends ElemJS { this.on("submit", this.submit.bind(this)) } + async submit() { if (this.processing) return this.processing = true if (!username.isValid()) return this.cancel("Username is not valid.") // Resolve homeserver address - let currentAddress = homeserver.value - let ok = false - while (!ok) { - if (!currentAddress.match(/^https?:\/\//)) { - console.warn(`${currentAddress} doesn't specify the protocol, assuming https`) - currentAddress = "https://" + currentAddress - } - currentAddress = currentAddress.replace(/\/*$/, "") - this.status(`Looking up homeserver... trying ${currentAddress}`) - try { - // check if we found the actual matrix server - try { - const versions = await fetch(`${currentAddress}/_matrix/client/versions`).then(res => res.json()) - if (Array.isArray(versions.versions)) { - ok = true - break - } - } catch (e) {} - // find the next matrix server in the chain - const root = await fetch(`${currentAddress}/.well-known/matrix/client`).then(res => res.json()) - let nextAddress = root["m.homeserver"].base_url - nextAddress = nextAddress.replace(/\/*$/, "") - if (currentAddress === nextAddress) { - ok = true - } - currentAddress = nextAddress - } catch (e) { - return this.cancel(`Failed to look up server ${currentAddress}`) - } + let domain + try { + domain = await this.findHomeserver(homeserver.value) + } catch(e) { + return this.cancel(e.message) } // Request access token this.status("Logging in...") - const root = await fetch(`${currentAddress}/_matrix/client/r0/login`, { + const root = await fetch(`${domain}/_matrix/client/r0/login`, { method: "POST", body: JSON.stringify({ type: "m.login.password", @@ -130,12 +107,52 @@ class Form extends ElemJS { } localStorage.setItem("mx_user_id", root.user_id) - localStorage.setItem("domain", currentAddress) + localStorage.setItem("domain", domain) localStorage.setItem("access_token", root.access_token) location.assign("../") } + async findHomeserver(address, maxDepth = 5) { + + //Protects us from servers sending us on a redirect loop + maxDepth-- + if(maxDepth<=0) throw new Error(`Failed to look up homeserver, maximum search depth reached`) + + if (!address.match(/^https?:\/\//)) { + console.warn(`${address} doesn't specify the protocol, assuming https`) + address = "https://" + address + } + + address = address.replace(/\/*$/, "") + this.status(`Looking up homeserver... trying ${address}`) + + // check if we found the actual matrix server + const versionsReq = await fetch(`${address}/_matrix/client/versions`).catch(()=>{}); + if(versionsReq?.ok) { + const versions = await versionsReq.json().catch(()=>{}) + if (Array.isArray(versions.versions)) { + return address + } + } + + // find the next matrix server in the chain + const root = await fetch(`${address}/.well-known/matrix/client`).then(res => res.json()).catch(e => { + console.error(e) + throw new Error(`Failed to look up server ${address}`) + }) + + let nextAddress = root["m.homeserver"].base_url + nextAddress = nextAddress.replace(/\/*$/, "") + + if (address === nextAddress) { + throw new Error(`Failed to look up server ${address}, /.well-known/matrix/client found a redirect loop`); + } + + return this.findHomeserver(nextAddress, maxDepth) + } + + status(message) { feedback.setLoading(true) feedback.message(message) @@ -149,3 +166,6 @@ class Form extends ElemJS { } const form = new Form() + + + -- 2.34.1 From 0c3c06bc0af1ab93232e7cdd02751181fbcd7765 Mon Sep 17 00:00:00 2001 From: Bad Date: Wed, 21 Oct 2020 21:16:45 +0200 Subject: [PATCH 2/7] oops --- src/js/login.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/js/login.js b/src/js/login.js index c285898..52ec151 100644 --- a/src/js/login.js +++ b/src/js/login.js @@ -128,13 +128,15 @@ class Form extends ElemJS { this.status(`Looking up homeserver... trying ${address}`) // check if we found the actual matrix server - const versionsReq = await fetch(`${address}/_matrix/client/versions`).catch(()=>{}); - if(versionsReq?.ok) { - const versions = await versionsReq.json().catch(()=>{}) - if (Array.isArray(versions.versions)) { - return address + try { + const versionsReq = await fetch(`${address}/_matrix/client/versions`) + if(versionsReq.ok) { + const versions = await versionsReq.json().catch(()=>{}) + if (Array.isArray(versions.versions)) { + return address + } } - } + } catch(e) {} // find the next matrix server in the chain const root = await fetch(`${address}/.well-known/matrix/client`).then(res => res.json()).catch(e => { -- 2.34.1 From 61cc4a19f3a420c93ec3d411b776fbc4ae331c65 Mon Sep 17 00:00:00 2001 From: Bad Date: Wed, 21 Oct 2020 21:19:17 +0200 Subject: [PATCH 3/7] Small cosmetic fixes --- src/js/login.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/js/login.js b/src/js/login.js index 52ec151..725ef4e 100644 --- a/src/js/login.js +++ b/src/js/login.js @@ -119,19 +119,20 @@ class Form extends ElemJS { maxDepth-- if(maxDepth<=0) throw new Error(`Failed to look up homeserver, maximum search depth reached`) + //Preprocess the address if (!address.match(/^https?:\/\//)) { console.warn(`${address} doesn't specify the protocol, assuming https`) address = "https://" + address } - address = address.replace(/\/*$/, "") + this.status(`Looking up homeserver... trying ${address}`) - // check if we found the actual matrix server + // Check if we found the actual matrix server try { const versionsReq = await fetch(`${address}/_matrix/client/versions`) if(versionsReq.ok) { - const versions = await versionsReq.json().catch(()=>{}) + const versions = await versionsReq.json() if (Array.isArray(versions.versions)) { return address } -- 2.34.1 From 2ff43ea801822b914117710751d90d808e26e1fd Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 22 Oct 2020 17:35:48 +1300 Subject: [PATCH 4/7] Style cleanup --- src/js/login.js | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/js/login.js b/src/js/login.js index 725ef4e..3bf23d0 100644 --- a/src/js/login.js +++ b/src/js/login.js @@ -71,7 +71,6 @@ class Form extends ElemJS { this.on("submit", this.submit.bind(this)) } - async submit() { if (this.processing) return this.processing = true @@ -114,32 +113,31 @@ class Form extends ElemJS { } async findHomeserver(address, maxDepth = 5) { - - //Protects us from servers sending us on a redirect loop + // Protects us from servers sending us on a redirect loop maxDepth-- - if(maxDepth<=0) throw new Error(`Failed to look up homeserver, maximum search depth reached`) - - //Preprocess the address + if (maxDepth <= 0) throw new Error(`Failed to look up homeserver, maximum search depth reached`) + + // Normalise the address if (!address.match(/^https?:\/\//)) { console.warn(`${address} doesn't specify the protocol, assuming https`) address = "https://" + address } address = address.replace(/\/*$/, "") - + this.status(`Looking up homeserver... trying ${address}`) - + // Check if we found the actual matrix server try { const versionsReq = await fetch(`${address}/_matrix/client/versions`) - if(versionsReq.ok) { + if (versionsReq.ok) { const versions = await versionsReq.json() if (Array.isArray(versions.versions)) { return address } } } catch(e) {} - - // find the next matrix server in the chain + + // Find the next matrix server in the chain const root = await fetch(`${address}/.well-known/matrix/client`).then(res => res.json()).catch(e => { console.error(e) throw new Error(`Failed to look up server ${address}`) @@ -155,7 +153,6 @@ class Form extends ElemJS { return this.findHomeserver(nextAddress, maxDepth) } - status(message) { feedback.setLoading(true) feedback.message(message) @@ -169,6 +166,3 @@ class Form extends ElemJS { } const form = new Form() - - - -- 2.34.1 From 1b97351ca063a332fd92f93fff64da5253c2994e Mon Sep 17 00:00:00 2001 From: Bad Date: Thu, 22 Oct 2020 07:46:32 +0200 Subject: [PATCH 5/7] Switch to using .catch(()=>{}) instead of try catch --- src/js/login.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/js/login.js b/src/js/login.js index 725ef4e..83353cd 100644 --- a/src/js/login.js +++ b/src/js/login.js @@ -129,15 +129,15 @@ class Form extends ElemJS { this.status(`Looking up homeserver... trying ${address}`) // Check if we found the actual matrix server - try { - const versionsReq = await fetch(`${address}/_matrix/client/versions`) - if(versionsReq.ok) { - const versions = await versionsReq.json() - if (Array.isArray(versions.versions)) { - return address - } - } - } catch(e) {} + const versionsReq = await fetch(`${address}/_matrix/client/versions`).catch(()=>{}) + /* jshint ignore:start */ + //JsHint doesn't support optional chaining + // https://github.com/jshint/jshint/issues/3448 + if(versionsReq?.ok) { + const versions = await versionsReq.json().catch(()=>{}) + if (Array.isArray(versions?.versions)) return address + } + /* jshint ignore:end */ // find the next matrix server in the chain const root = await fetch(`${address}/.well-known/matrix/client`).then(res => res.json()).catch(e => { -- 2.34.1 From 16de7edd19d864045b5af9f2e5195edd86ad7bfd Mon Sep 17 00:00:00 2001 From: Bad Date: Thu, 22 Oct 2020 09:14:58 +0200 Subject: [PATCH 6/7] Style fixes --- src/js/login.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/js/login.js b/src/js/login.js index 83353cd..68d4683 100644 --- a/src/js/login.js +++ b/src/js/login.js @@ -71,7 +71,6 @@ class Form extends ElemJS { this.on("submit", this.submit.bind(this)) } - async submit() { if (this.processing) return this.processing = true @@ -114,12 +113,12 @@ class Form extends ElemJS { } async findHomeserver(address, maxDepth = 5) { - - //Protects us from servers sending us on a redirect loop + + //Protects from servers sending us on a redirect loop maxDepth-- - if(maxDepth<=0) throw new Error(`Failed to look up homeserver, maximum search depth reached`) + if (maxDepth <= 0) throw new Error(`Failed to look up homeserver, maximum search depth reached`) - //Preprocess the address + //Normalise the address if (!address.match(/^https?:\/\//)) { console.warn(`${address} doesn't specify the protocol, assuming https`) address = "https://" + address @@ -133,13 +132,13 @@ class Form extends ElemJS { /* jshint ignore:start */ //JsHint doesn't support optional chaining // https://github.com/jshint/jshint/issues/3448 - if(versionsReq?.ok) { + if (versionsReq?.ok) { const versions = await versionsReq.json().catch(()=>{}) if (Array.isArray(versions?.versions)) return address } /* jshint ignore:end */ - // find the next matrix server in the chain + // Find the next matrix server in the chain const root = await fetch(`${address}/.well-known/matrix/client`).then(res => res.json()).catch(e => { console.error(e) throw new Error(`Failed to look up server ${address}`) @@ -155,7 +154,6 @@ class Form extends ElemJS { return this.findHomeserver(nextAddress, maxDepth) } - status(message) { feedback.setLoading(true) feedback.message(message) @@ -169,6 +167,3 @@ class Form extends ElemJS { } const form = new Form() - - - -- 2.34.1 From aa12cd68e67a2ecbf0ef591abdbff152242b08de Mon Sep 17 00:00:00 2001 From: Bad Date: Fri, 23 Oct 2020 10:51:04 +0200 Subject: [PATCH 7/7] Clean up code --- src/js/login.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/js/login.js b/src/js/login.js index 68d4683..4754bef 100644 --- a/src/js/login.js +++ b/src/js/login.js @@ -128,15 +128,13 @@ class Form extends ElemJS { this.status(`Looking up homeserver... trying ${address}`) // Check if we found the actual matrix server - const versionsReq = await fetch(`${address}/_matrix/client/versions`).catch(()=>{}) - /* jshint ignore:start */ - //JsHint doesn't support optional chaining - // https://github.com/jshint/jshint/issues/3448 - if (versionsReq?.ok) { - const versions = await versionsReq.json().catch(()=>{}) - if (Array.isArray(versions?.versions)) return address - } - /* jshint ignore:end */ + try { + const versionsReq = await fetch(`${address}/_matrix/client/versions`) + if (versionsReq.ok) { + const versions = await versionsReq.json() + if (Array.isArray(versions.versions)) return address + } + } catch(e) {} // Find the next matrix server in the chain const root = await fetch(`${address}/.well-known/matrix/client`).then(res => res.json()).catch(e => { -- 2.34.1