Compare commits

...

11 commits

Author SHA1 Message Date
bad
91ac7a6b3e Merge pull request 'Refactor homeserver lookup code' (#12) from refactor-homeserver-lookup into princess
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #12
2020-10-23 08:57:01 +00:00
Bad
aa12cd68e6 Clean up code
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
2020-10-23 10:53:27 +02:00
bad
4382928a93 Merge branch 'princess' into refactor-homeserver-lookup
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
2020-10-23 08:48:37 +00:00
Bad
16de7edd19 Style fixes
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
2020-10-22 09:14:58 +02:00
Bad
0113024be6 Switch to using .catch(()=>{}) instead of try catch
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
2020-10-22 07:50:50 +02:00
Bad
1b97351ca0 Switch to using .catch(()=>{}) instead of try catch 2020-10-22 07:46:32 +02:00
dce4fa6303
Merge branch 'princess' into refactor-homeserver-lookup
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
We need the redirection fix from princess in here to accurately test.
2020-10-22 17:41:16 +13:00
2ff43ea801
Style cleanup
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
2020-10-22 17:35:48 +13:00
Bad
61cc4a19f3 Small cosmetic fixes
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2020-10-21 21:19:17 +02:00
Bad
0c3c06bc0a oops
All checks were successful
continuous-integration/drone/push Build is passing
2020-10-21 21:16:45 +02:00
Bad
6f67ddbce5 Refactor homeserver lookup code
Some checks reported errors
continuous-integration/drone/push Build was killed
2020-10-21 21:11:45 +02:00

View file

@ -77,40 +77,16 @@ class Form extends ElemJS {
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}`)
let domain
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}`)
}
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 +106,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 from servers sending us on a redirect loop
maxDepth--
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) {
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 => {
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)