2020-03-25 16:05:25 +00:00
|
|
|
"use strict";
|
2020-03-24 21:47:39 +00:00
|
|
|
|
2020-03-25 17:46:28 +00:00
|
|
|
const LOCALSTORAGE_KEY_SERVERS = "mattermostServers";
|
|
|
|
|
2020-03-25 16:05:25 +00:00
|
|
|
function byId(id, nullOk=false) {
|
|
|
|
const el = document.getElementById(id);
|
|
|
|
if (!el && !nullOk) {
|
|
|
|
console.error(`No element #${id}`);
|
2020-03-24 21:47:39 +00:00
|
|
|
}
|
2020-03-25 16:05:25 +00:00
|
|
|
return el;
|
2020-03-24 21:47:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 17:46:28 +00:00
|
|
|
function storeCredentials(endpoint, login_id, token) {
|
|
|
|
let storedServers = JSON.parse(window.localStorage.getItem(LOCALSTORAGE_KEY_SERVERS) || "[]");
|
|
|
|
if (!(endpoint in storedServers)) storedServers.push(endpoint);
|
|
|
|
window.localStorage.setItem(LOCALSTORAGE_KEY_SERVERS, JSON.stringify(storedServers));
|
|
|
|
|
|
|
|
window.localStorage.setItem(`${LOCALSTORAGE_KEY_SERVERS}_${endpoint}`, JSON.stringify({login_id, token}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCredentials(endpoint) {
|
|
|
|
return JSON.parse(window.localStorage.getItem(`${LOCALSTORAGE_KEY_SERVERS}_${endpoint}`) || "null");
|
|
|
|
}
|
|
|
|
|
2020-03-25 16:05:25 +00:00
|
|
|
class MattermostApi {
|
|
|
|
constructor(endpoint) {
|
|
|
|
this.endpoint = endpoint;
|
2020-03-24 21:47:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 16:05:25 +00:00
|
|
|
async validateToken(token) {
|
|
|
|
const response = await ajax.getJson(`${this.endpoint}/users/me`, {
|
|
|
|
headers: {
|
|
|
|
"Token": `Authorization: Bearer ${token}`
|
|
|
|
}
|
|
|
|
});
|
2020-03-25 17:46:28 +00:00
|
|
|
if (!response.ok) {
|
|
|
|
throw response;
|
|
|
|
}
|
|
|
|
return response;
|
2020-03-25 16:05:25 +00:00
|
|
|
}
|
2020-03-24 21:47:39 +00:00
|
|
|
|
2020-03-25 17:46:28 +00:00
|
|
|
logIn(login_id, password) {
|
|
|
|
return ajax.postJson(`${this.endpoint}/users/login`, {login_id, password})
|
2020-03-25 16:05:25 +00:00
|
|
|
.then(response => {
|
|
|
|
let token = response.getHeader("Token");
|
2020-03-25 17:46:28 +00:00
|
|
|
storeCredentials(this.endpoint, login_id, token);
|
2020-03-25 16:05:25 +00:00
|
|
|
return response;
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
document.body.innerHTML = "";
|
|
|
|
const pre = document.createElement("pre");
|
|
|
|
pre.innerText = JSON.stringify(response.json, null, 2);
|
|
|
|
document.body.appendChild(pre);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
document.body.innerText = `An error occurred: ${error}`;
|
|
|
|
});
|
2020-03-24 21:47:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 17:46:28 +00:00
|
|
|
/**
|
|
|
|
* Return an endpoint URL that has a protocol, domain and path
|
|
|
|
*/
|
|
|
|
function normalizedEndpoint(endpoint) {
|
|
|
|
let matches = endpoint.match(/^(https?:\/\/)?([^\/]+)(\/.*)?$/i);
|
|
|
|
if (!matches) throw Error("Invalid endpoint URL");
|
|
|
|
|
|
|
|
let protocol = matches[1] || "https://";
|
|
|
|
let domain = matches[2];
|
|
|
|
let path = matches[3] || "/api/v4";
|
|
|
|
|
|
|
|
return `${protocol}${domain}${path}`;
|
|
|
|
}
|
|
|
|
|
2020-03-25 16:05:25 +00:00
|
|
|
function logIn() {
|
2020-03-25 17:46:28 +00:00
|
|
|
let endpoint = normalizedEndpoint(byId("server").value);
|
2020-03-24 21:47:39 +00:00
|
|
|
|
2020-03-25 16:05:25 +00:00
|
|
|
let api = new MattermostApi(endpoint);
|
|
|
|
api.logIn(byId("username").value, byId("password").value);
|
|
|
|
}
|
2020-03-25 17:46:28 +00:00
|
|
|
|
|
|
|
function validateToken() {
|
|
|
|
let endpoint = normalizedEndpoint(byId("server").value);
|
|
|
|
|
|
|
|
let api = new MattermostApi(endpoint);
|
|
|
|
api.validateToken(getCredentials(endpoint))
|
|
|
|
.then(() => {
|
|
|
|
byId("validate").value = "Validation succeeded";
|
|
|
|
byId("validate").disabled = false;
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
byId("validate").value = "Validation failed";
|
|
|
|
byId("validate").disabled = false;
|
|
|
|
});
|
|
|
|
}
|