Extract helper functions from LoginPage
Change-Id: I507e223d0eff00bac3963d0b71f9bd648b9ab7b1
This commit is contained in:
48
src/synapse/synapse.js
Normal file
48
src/synapse/synapse.js
Normal file
@@ -0,0 +1,48 @@
|
||||
import { fetchUtils } from "react-admin";
|
||||
|
||||
export const splitMxid = mxid => {
|
||||
const re =
|
||||
/^@(?<name>[a-zA-Z0-9._=\-/]+):(?<domain>[a-zA-Z0-9\-.]+\.[a-zA-Z]+)$/;
|
||||
return re.exec(mxid)?.groups;
|
||||
};
|
||||
|
||||
export const isValidBaseUrl = baseUrl =>
|
||||
/^(http|https):\/\/[a-zA-Z0-9\-.]+(:\d{1,5})?$/.test(baseUrl);
|
||||
|
||||
/**
|
||||
* Resolve the homeserver URL using the well-known lookup
|
||||
* @param domain the domain part of an MXID
|
||||
* @returns homeserver base URL
|
||||
*/
|
||||
export const getWellKnownUrl = async domain => {
|
||||
const wellKnownUrl = `https://${domain}/.well-known/matrix/client`;
|
||||
try {
|
||||
const json = await fetchUtils.fetchJson(wellKnownUrl, { method: "GET" });
|
||||
return json["m.homeserver"].base_url;
|
||||
} catch {
|
||||
// if there is no .well-known entry, return the domain itself
|
||||
return `https://${domain}`;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get synapse server version
|
||||
* @param base_url the base URL of the homeserver
|
||||
* @returns server version
|
||||
*/
|
||||
export const getServerVersion = async baseUrl => {
|
||||
const versionUrl = `${baseUrl}/_synapse/admin/v1/server_version`;
|
||||
const response = await fetchUtils.fetchJson(versionUrl, { method: "GET" });
|
||||
return response.json.server_version;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get supported login flows
|
||||
* @param baseUrl the base URL of the homeserver
|
||||
* @returns array of supported login flows
|
||||
*/
|
||||
export const getSupportedLoginFlows = async baseUrl => {
|
||||
const loginFlowsUrl = `${baseUrl}/_matrix/client/r0/login`;
|
||||
const response = await fetchUtils.fetchJson(loginFlowsUrl, { method: "GET" });
|
||||
return response.json.flows;
|
||||
};
|
||||
31
src/synapse/synapse.test.js
Normal file
31
src/synapse/synapse.test.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import { isValidBaseUrl, splitMxid } from "./synapse";
|
||||
|
||||
describe("splitMxid", () => {
|
||||
it("splits valid MXIDs", () =>
|
||||
expect(splitMxid("@name:domain.tld")).toEqual({
|
||||
name: "name",
|
||||
domain: "domain.tld",
|
||||
}));
|
||||
it("rejects invalid MXIDs", () => expect(splitMxid("foo")).toBeUndefined());
|
||||
});
|
||||
|
||||
describe("isValidBaseUrl", () => {
|
||||
it("accepts a http URL", () =>
|
||||
expect(isValidBaseUrl("http://foo.bar")).toBeTruthy());
|
||||
it("accepts a https URL", () =>
|
||||
expect(isValidBaseUrl("https://foo.bar")).toBeTruthy());
|
||||
it("accepts a valid URL with port", () =>
|
||||
expect(isValidBaseUrl("https://foo.bar:1234")).toBeTruthy());
|
||||
it("rejects undefined base URLs", () =>
|
||||
expect(isValidBaseUrl(undefined)).toBeFalsy());
|
||||
it("rejects null base URLs", () => expect(isValidBaseUrl(null)).toBeFalsy());
|
||||
it("rejects empty base URLs", () => expect(isValidBaseUrl("")).toBeFalsy());
|
||||
it("rejects non-string base URLs", () =>
|
||||
expect(isValidBaseUrl({})).toBeFalsy());
|
||||
it("rejects base URLs without protocol", () =>
|
||||
expect(isValidBaseUrl("foo.bar")).toBeFalsy());
|
||||
it("rejects base URLs with path", () =>
|
||||
expect(isValidBaseUrl("http://foo.bar/path")).toBeFalsy());
|
||||
it("rejects invalid base URLs", () =>
|
||||
expect(isValidBaseUrl("http:/foo.bar")).toBeFalsy());
|
||||
});
|
||||
Reference in New Issue
Block a user