Refactor homeserver lookup code #12
1 changed files with 51 additions and 31 deletions
|
@ -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()
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue