From 40b5031550e9720aeac4f9817d109cd1d4b83892 Mon Sep 17 00:00:00 2001 From: teutates <10206665+teutat3s@users.noreply.github.com> Date: Wed, 4 Mar 2020 23:21:36 +0100 Subject: [PATCH] Use proper homeserver URL for API requests --- src/components/LoginPage.js | 2 +- src/synapse/authProvider.js | 34 ++++++++++++++++++++-------- src/synapse/dataProvider.js | 45 ++++++++++++++++++++++--------------- 3 files changed, 53 insertions(+), 28 deletions(-) diff --git a/src/components/LoginPage.js b/src/components/LoginPage.js index 49293db..19280b3 100644 --- a/src/components/LoginPage.js +++ b/src/components/LoginPage.js @@ -70,7 +70,7 @@ const LoginPage = ({ theme }) => { var locale = useLocale(); const setLocale = useSetLocale(); const translate = useTranslate(); - const homeserver = localStorage.getItem("home_server"); + const homeserver = localStorage.getItem("base_url"); const renderInput = ({ meta: { touched, error } = {}, diff --git a/src/synapse/authProvider.js b/src/synapse/authProvider.js index a25a6e8..14ef26c 100644 --- a/src/synapse/authProvider.js +++ b/src/synapse/authProvider.js @@ -1,5 +1,20 @@ import { fetchUtils } from "react-admin"; +const ensureHttpsForUrl = url => { + if (/^https:\/\//i.test(url)) { + return url; + } + const domain = url.replace(/http.?:\/\//g, ""); + return "https://" + domain; +}; + +const stripTrailingSlash = str => { + if (!str) { + return; + } + return str.endsWith("/") ? str.slice(0, -1) : str; +}; + const authProvider = { // called when the user attempts to log in login: ({ homeserver, username, password }) => { @@ -13,16 +28,17 @@ const authProvider = { }), }; - // add 'https://' to homeserver url if its missing - let newUrl = window.decodeURIComponent(homeserver); - newUrl = newUrl.trim().replace(/\s/g, ""); - if (!/^https?:\/\//i.test(newUrl)) { - homeserver = `https://${newUrl}`; - } + const url = window.decodeURIComponent(homeserver); + const trimmed_url = url.trim().replace(/\s/g, ""); + const login_api_url = + ensureHttpsForUrl(trimmed_url) + "/_matrix/client/r0/login"; - const url = homeserver + "/_matrix/client/r0/login"; - return fetchUtils.fetchJson(url, options).then(({ json }) => { - localStorage.setItem("home_server", json.home_server); + return fetchUtils.fetchJson(login_api_url, options).then(({ json }) => { + const normalized_base_url = stripTrailingSlash( + json.well_known["m.homeserver"].base_url + ); + localStorage.setItem("base_url", normalized_base_url); + localStorage.setItem("home_server_url", json.home_server); localStorage.setItem("user_id", json.user_id); localStorage.setItem("access_token", json.access_token); localStorage.setItem("device_id", json.device_id); diff --git a/src/synapse/dataProvider.js b/src/synapse/dataProvider.js index 45ffc3f..6f1334a 100644 --- a/src/synapse/dataProvider.js +++ b/src/synapse/dataProvider.js @@ -63,11 +63,12 @@ const dataProvider = { user_id: user_id, guests: guests, }; - const homeserver = localStorage.getItem("home_server"); + const homeserver = localStorage.getItem("base_url"); if (!homeserver || !(resource in resourceMap)) return Promise.reject(); const res = resourceMap[resource]; - const homeserver_url = "https://" + homeserver + res.path; + + const homeserver_url = homeserver + res.path; const url = `${homeserver_url}?${stringify(query)}`; return jsonClient(url).then(({ json }) => ({ @@ -78,11 +79,12 @@ const dataProvider = { getOne: (resource, params) => { console.log("getOne " + resource); - const homeserver = localStorage.getItem("home_server"); + const homeserver = localStorage.getItem("base_url"); if (!homeserver || !(resource in resourceMap)) return Promise.reject(); const res = resourceMap[resource]; - const homeserver_url = "https://" + homeserver + res.path; + + const homeserver_url = homeserver + res.path; return jsonClient(`${homeserver_url}/${params.id}`).then(({ json }) => ({ data: res.map(json), })); @@ -90,11 +92,12 @@ const dataProvider = { getMany: (resource, params) => { console.log("getMany " + resource); - const homeserver = localStorage.getItem("home_server"); + const homeserver = localStorage.getItem("base_url"); if (!homeserver || !(resource in resourceMap)) return Promise.reject(); const res = resourceMap[resource]; - const homeserver_url = "https://" + homeserver + res.path; + + const homeserver_url = homeserver + res.path; return Promise.all( params.ids.map(id => jsonClient(`${homeserver_url}/${id}`)) ).then(responses => ({ @@ -116,11 +119,12 @@ const dataProvider = { }), }; - const homeserver = localStorage.getItem("home_server"); + const homeserver = localStorage.getItem("base_url"); if (!homeserver || !(resource in resourceMap)) return Promise.reject(); const res = resourceMap[resource]; - const homeserver_url = "https://" + homeserver + res.path; + + const homeserver_url = homeserver + res.path; const url = `${homeserver_url}?${stringify(query)}`; return jsonClient(url).then(({ headers, json }) => ({ @@ -137,11 +141,12 @@ const dataProvider = { update: (resource, params) => { console.log("update " + resource); - const homeserver = localStorage.getItem("home_server"); + const homeserver = localStorage.getItem("base_url"); if (!homeserver || !(resource in resourceMap)) return Promise.reject(); const res = resourceMap[resource]; - const homeserver_url = "https://" + homeserver + res.path; + + const homeserver_url = homeserver + res.path; return jsonClient(`${homeserver_url}/${params.data.id}`, { method: "PUT", body: JSON.stringify(params.data, filterNullValues), @@ -152,11 +157,12 @@ const dataProvider = { updateMany: (resource, params) => { console.log("updateMany " + resource); - const homeserver = localStorage.getItem("home_server"); + const homeserver = localStorage.getItem("base_url"); if (!homeserver || !(resource in resourceMap)) return Promise.reject(); const res = resourceMap[resource]; - const homeserver_url = "https://" + homeserver + res.path; + + const homeserver_url = homeserver + res.path; return Promise.all( params.ids.map(id => jsonClient(`${homeserver_url}/${id}`), { method: "PUT", @@ -169,11 +175,12 @@ const dataProvider = { create: (resource, params) => { console.log("create " + resource); - const homeserver = localStorage.getItem("home_server"); + const homeserver = localStorage.getItem("base_url"); if (!homeserver || !(resource in resourceMap)) return Promise.reject(); const res = resourceMap[resource]; - const homeserver_url = "https://" + homeserver + res.path; + + const homeserver_url = homeserver + res.path; return jsonClient(`${homeserver_url}/${params.data.id}`, { method: "PUT", body: JSON.stringify(params.data, filterNullValues), @@ -184,11 +191,12 @@ const dataProvider = { delete: (resource, params) => { console.log("delete " + resource); - const homeserver = localStorage.getItem("home_server"); + const homeserver = localStorage.getItem("base_url"); if (!homeserver || !(resource in resourceMap)) return Promise.reject(); const res = resourceMap[resource]; - const homeserver_url = "https://" + homeserver + res.path; + + const homeserver_url = homeserver + res.path; return jsonClient(`${homeserver_url}/${params.id}`, { method: "DELETE", }).then(({ json }) => ({ @@ -198,11 +206,12 @@ const dataProvider = { deleteMany: (resource, params) => { console.log("deleteMany " + resource); - const homeserver = localStorage.getItem("home_server"); + const homeserver = localStorage.getItem("base_url"); if (!homeserver || !(resource in resourceMap)) return Promise.reject(); const res = resourceMap[resource]; - const homeserver_url = "https://" + homeserver + res.path; + + const homeserver_url = homeserver + res.path; return Promise.all( params.ids.map(id => jsonClient(`${homeserver_url}/${id}`, {