2015-10-26 13:35:24 -04:00
|
|
|
window.matrixLogin = {
|
2018-11-27 02:51:52 -05:00
|
|
|
endpoint: location.origin + "/_matrix/client/r0/login",
|
2015-10-26 13:35:24 -04:00
|
|
|
serverAcceptsPassword: false,
|
2018-11-27 02:51:52 -05:00
|
|
|
serverAcceptsSso: false,
|
2015-10-26 13:35:24 -04:00
|
|
|
};
|
|
|
|
|
2020-06-08 10:13:24 -04:00
|
|
|
// Titles get updated through the process to give users feedback.
|
2020-06-10 09:50:39 -04:00
|
|
|
const TITLE_PRE_AUTH = "Log in with one of the following methods";
|
|
|
|
const TITLE_POST_AUTH = "Logging in...";
|
2020-06-08 10:13:24 -04:00
|
|
|
|
|
|
|
// The cookie used to store the original query parameters when using SSO.
|
2020-06-10 09:50:39 -04:00
|
|
|
const COOKIE_KEY = "synapse_login_fallback_qs";
|
2020-06-08 10:13:24 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Submit a login request.
|
|
|
|
*
|
|
|
|
* type: The login type as a string (e.g. "m.login.foo").
|
|
|
|
* data: An object of data specific to the login type.
|
|
|
|
* extra: (Optional) An object to search for extra information to send with the
|
|
|
|
* login request, e.g. device_id.
|
|
|
|
* callback: (Optional) Function to call on successful login.
|
|
|
|
*/
|
2020-06-10 09:50:39 -04:00
|
|
|
function submitLogin(type, data, extra, callback) {
|
2020-06-08 10:13:24 -04:00
|
|
|
console.log("Logging in with " + type);
|
2020-06-10 09:50:39 -04:00
|
|
|
setTitle(TITLE_POST_AUTH);
|
2020-06-08 10:13:24 -04:00
|
|
|
|
|
|
|
// Add the login type.
|
|
|
|
data.type = type;
|
|
|
|
|
|
|
|
// Add the device information, if it was provided.
|
|
|
|
if (extra.device_id) {
|
|
|
|
data.device_id = extra.device_id;
|
|
|
|
}
|
|
|
|
if (extra.initial_device_display_name) {
|
|
|
|
data.initial_device_display_name = extra.initial_device_display_name;
|
|
|
|
}
|
2015-10-26 13:35:24 -04:00
|
|
|
|
|
|
|
$.post(matrixLogin.endpoint, JSON.stringify(data), function(response) {
|
2020-06-08 10:13:24 -04:00
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
2015-10-26 13:35:24 -04:00
|
|
|
matrixLogin.onLogin(response);
|
2020-04-09 07:23:30 -04:00
|
|
|
}).fail(errorFunc);
|
2020-06-10 09:50:39 -04:00
|
|
|
}
|
2015-10-26 13:35:24 -04:00
|
|
|
|
2020-06-10 09:50:39 -04:00
|
|
|
/*
|
|
|
|
* Display an error to the user and show the login form again.
|
|
|
|
*/
|
|
|
|
function errorFunc(err) {
|
2020-03-27 10:44:13 -04:00
|
|
|
// We want to show the error to the user rather than redirecting immediately to the
|
|
|
|
// SSO portal (if SSO is the only login option), so we inhibit the redirect.
|
2020-06-10 09:50:39 -04:00
|
|
|
showLogin(true);
|
2015-10-26 13:35:24 -04:00
|
|
|
|
|
|
|
if (err.responseJSON && err.responseJSON.error) {
|
|
|
|
setFeedbackString(err.responseJSON.error + " (" + err.responseJSON.errcode + ")");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setFeedbackString("Request failed: " + err.status);
|
|
|
|
}
|
2020-06-10 09:50:39 -04:00
|
|
|
}
|
2015-10-26 13:35:24 -04:00
|
|
|
|
2020-06-10 09:50:39 -04:00
|
|
|
/*
|
|
|
|
* Display an error to the user.
|
|
|
|
*/
|
|
|
|
function setFeedbackString(text) {
|
2015-10-26 13:35:24 -04:00
|
|
|
$("#feedback").text(text);
|
2020-06-10 09:50:39 -04:00
|
|
|
}
|
2015-10-26 13:35:24 -04:00
|
|
|
|
2020-06-10 09:50:39 -04:00
|
|
|
/*
|
|
|
|
* (Maybe) Show the login forms.
|
|
|
|
*
|
|
|
|
* This actually does a few unrelated functions:
|
|
|
|
*
|
|
|
|
* * Configures the SSO redirect URL to come back to this page.
|
|
|
|
* * Configures and shows the SSO form, if the server supports SSO.
|
|
|
|
* * Otherwise, shows the password form.
|
|
|
|
*/
|
|
|
|
function showLogin(inhibitRedirect) {
|
|
|
|
setTitle(TITLE_PRE_AUTH);
|
2018-11-27 02:51:52 -05:00
|
|
|
|
2020-06-10 09:50:39 -04:00
|
|
|
// If inhibitRedirect is false, and SSO is the only supported login method,
|
2020-06-08 10:13:24 -04:00
|
|
|
// we can redirect straight to the SSO page.
|
2020-03-27 10:44:13 -04:00
|
|
|
if (matrixLogin.serverAcceptsSso) {
|
2020-06-10 09:50:39 -04:00
|
|
|
// Set the redirect to come back to this page, a login token will get
|
|
|
|
// added as a query parameter and handled after the redirect.
|
|
|
|
$("#sso_redirect_url").val(window.location.origin + window.location.pathname);
|
|
|
|
|
2020-06-08 10:13:24 -04:00
|
|
|
// Before submitting SSO, set the current query parameters into a cookie
|
|
|
|
// for retrieval later.
|
|
|
|
var qs = parseQsFromUrl();
|
|
|
|
setCookie(COOKIE_KEY, JSON.stringify(qs));
|
|
|
|
|
2020-06-10 09:50:39 -04:00
|
|
|
// If password is not supported and redirects are allowed, then submit
|
|
|
|
// the form (redirecting to the SSO provider).
|
|
|
|
if (!inhibitRedirect && !matrixLogin.serverAcceptsPassword) {
|
2020-03-27 10:44:13 -04:00
|
|
|
$("#sso_form").submit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, show the SSO form
|
2020-04-07 12:31:50 -04:00
|
|
|
$("#sso_flow").show();
|
2015-10-26 13:35:24 -04:00
|
|
|
}
|
|
|
|
|
2020-03-27 10:44:13 -04:00
|
|
|
if (matrixLogin.serverAcceptsPassword) {
|
|
|
|
$("#password_flow").show();
|
2015-10-26 13:35:24 -04:00
|
|
|
}
|
|
|
|
|
2020-06-10 09:50:39 -04:00
|
|
|
// If neither password or SSO are supported, show an error to the user.
|
2020-03-27 10:44:13 -04:00
|
|
|
if (!matrixLogin.serverAcceptsPassword && !matrixLogin.serverAcceptsSso) {
|
2015-10-26 13:35:24 -04:00
|
|
|
$("#no_login_types").show();
|
|
|
|
}
|
2020-03-27 10:44:13 -04:00
|
|
|
|
|
|
|
$("#loading").hide();
|
2020-06-10 09:50:39 -04:00
|
|
|
}
|
2015-10-26 13:35:24 -04:00
|
|
|
|
2020-06-10 09:50:39 -04:00
|
|
|
/*
|
|
|
|
* Hides the forms and shows a loading throbber.
|
|
|
|
*/
|
|
|
|
function showSpinner() {
|
2018-11-27 02:51:52 -05:00
|
|
|
$("#password_flow").hide();
|
|
|
|
$("#sso_flow").hide();
|
2015-10-26 13:35:24 -04:00
|
|
|
$("#no_login_types").hide();
|
|
|
|
$("#loading").show();
|
2020-06-10 09:50:39 -04:00
|
|
|
}
|
2015-10-26 13:35:24 -04:00
|
|
|
|
2020-06-10 09:50:39 -04:00
|
|
|
/*
|
|
|
|
* Helper to show the page's main title.
|
|
|
|
*/
|
|
|
|
function setTitle(title) {
|
2020-03-27 10:44:13 -04:00
|
|
|
$("#title").text(title);
|
2020-06-10 09:50:39 -04:00
|
|
|
}
|
2015-10-26 13:35:24 -04:00
|
|
|
|
2020-06-10 09:50:39 -04:00
|
|
|
/*
|
|
|
|
* Query the login endpoint for the homeserver's supported flows.
|
|
|
|
*
|
|
|
|
* This populates matrixLogin.serverAccepts* variables.
|
|
|
|
*/
|
|
|
|
function fetchLoginFlows(cb) {
|
2015-10-26 13:35:24 -04:00
|
|
|
$.get(matrixLogin.endpoint, function(response) {
|
2020-06-10 09:50:39 -04:00
|
|
|
for (var i = 0; i < response.flows.length; i++) {
|
2015-10-26 13:35:24 -04:00
|
|
|
var flow = response.flows[i];
|
2018-11-27 02:51:52 -05:00
|
|
|
if ("m.login.sso" === flow.type) {
|
|
|
|
matrixLogin.serverAcceptsSso = true;
|
|
|
|
console.log("Server accepts SSO");
|
|
|
|
}
|
2015-10-26 13:35:24 -04:00
|
|
|
if ("m.login.password" === flow.type) {
|
|
|
|
matrixLogin.serverAcceptsPassword = true;
|
|
|
|
console.log("Server accepts password");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cb();
|
2020-04-09 07:23:30 -04:00
|
|
|
}).fail(errorFunc);
|
2015-10-26 13:35:24 -04:00
|
|
|
}
|
|
|
|
|
2020-06-10 09:50:39 -04:00
|
|
|
/*
|
|
|
|
* Called on load to fetch login flows and attempt SSO login (if a token is available).
|
|
|
|
*/
|
2015-10-26 13:35:24 -04:00
|
|
|
matrixLogin.onLoad = function() {
|
2020-06-10 09:50:39 -04:00
|
|
|
fetchLoginFlows(function() {
|
|
|
|
// (Maybe) attempt logging in via SSO if a token is available.
|
|
|
|
if (!tryTokenLogin()) {
|
|
|
|
showLogin(false);
|
2015-10-26 13:35:24 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-06-10 09:50:39 -04:00
|
|
|
/*
|
|
|
|
* Submit simple user & password login.
|
|
|
|
*/
|
|
|
|
matrixLogin.passwordLogin = function() {
|
2015-10-26 13:35:24 -04:00
|
|
|
var user = $("#user_id").val();
|
|
|
|
var pwd = $("#password").val();
|
|
|
|
|
|
|
|
setFeedbackString("");
|
|
|
|
|
2020-06-10 09:50:39 -04:00
|
|
|
showSpinner();
|
2020-06-08 10:13:24 -04:00
|
|
|
submitLogin(
|
|
|
|
"m.login.password",
|
|
|
|
{user: user, password: pwd},
|
|
|
|
parseQsFromUrl());
|
2015-10-26 13:35:24 -04:00
|
|
|
};
|
|
|
|
|
2020-06-10 09:50:39 -04:00
|
|
|
/*
|
2020-10-23 12:38:40 -04:00
|
|
|
* The onLogin function gets called after a successful login.
|
2020-06-10 09:50:39 -04:00
|
|
|
*
|
|
|
|
* It is expected that implementations override this to be notified when the
|
|
|
|
* login is complete. The response to the login call is provided as the single
|
|
|
|
* parameter.
|
|
|
|
*/
|
2015-10-26 13:35:24 -04:00
|
|
|
matrixLogin.onLogin = function(response) {
|
|
|
|
// clobber this function
|
2020-04-09 07:38:38 -04:00
|
|
|
console.warn("onLogin - This function should be replaced to proceed.");
|
2015-10-26 13:35:24 -04:00
|
|
|
};
|
|
|
|
|
2020-06-08 10:13:24 -04:00
|
|
|
/*
|
|
|
|
* Process the query parameters from the current URL into an object.
|
|
|
|
*/
|
2020-06-10 09:50:39 -04:00
|
|
|
function parseQsFromUrl() {
|
2020-06-08 10:13:24 -04:00
|
|
|
var pos = window.location.href.indexOf("?");
|
|
|
|
if (pos == -1) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
var query = window.location.href.substr(pos + 1);
|
|
|
|
|
2015-10-26 13:35:24 -04:00
|
|
|
var result = {};
|
|
|
|
query.split("&").forEach(function(part) {
|
|
|
|
var item = part.split("=");
|
|
|
|
var key = item[0];
|
|
|
|
var val = item[1];
|
|
|
|
|
|
|
|
if (val) {
|
|
|
|
val = decodeURIComponent(val);
|
|
|
|
}
|
2020-06-08 10:13:24 -04:00
|
|
|
result[key] = val;
|
2015-10-26 13:35:24 -04:00
|
|
|
});
|
|
|
|
return result;
|
2020-06-10 09:50:39 -04:00
|
|
|
}
|
2015-10-26 13:35:24 -04:00
|
|
|
|
2020-06-08 10:13:24 -04:00
|
|
|
/*
|
|
|
|
* Process the cookies and return an object.
|
|
|
|
*/
|
2020-06-10 09:50:39 -04:00
|
|
|
function parseCookies() {
|
2020-06-08 10:13:24 -04:00
|
|
|
var allCookies = document.cookie;
|
|
|
|
var result = {};
|
|
|
|
allCookies.split(";").forEach(function(part) {
|
|
|
|
var item = part.split("=");
|
|
|
|
// Cookies might have arbitrary whitespace between them.
|
|
|
|
var key = item[0].trim();
|
|
|
|
// You can end up with a broken cookie that doesn't have an equals sign
|
|
|
|
// in it. Set to an empty value.
|
|
|
|
var val = (item[1] || "").trim();
|
|
|
|
// Values might be URI encoded.
|
|
|
|
if (val) {
|
|
|
|
val = decodeURIComponent(val);
|
|
|
|
}
|
|
|
|
result[key] = val;
|
|
|
|
});
|
|
|
|
return result;
|
2020-06-10 09:50:39 -04:00
|
|
|
}
|
2020-06-08 10:13:24 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set a cookie that is valid for 1 hour.
|
|
|
|
*/
|
2020-06-10 09:50:39 -04:00
|
|
|
function setCookie(key, value) {
|
2020-06-08 10:13:24 -04:00
|
|
|
// The maximum age is set in seconds.
|
|
|
|
var maxAge = 60 * 60;
|
|
|
|
// Set the cookie, this defaults to the current domain and path.
|
|
|
|
document.cookie = key + "=" + encodeURIComponent(value) + ";max-age=" + maxAge + ";sameSite=lax";
|
2020-06-10 09:50:39 -04:00
|
|
|
}
|
2020-06-08 10:13:24 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Removes a cookie by key.
|
|
|
|
*/
|
2020-06-10 09:50:39 -04:00
|
|
|
function deleteCookie(key) {
|
2020-06-08 10:13:24 -04:00
|
|
|
// Delete a cookie by setting the expiration to 0. (Note that the value
|
|
|
|
// doesn't matter.)
|
|
|
|
document.cookie = key + "=deleted;expires=0";
|
2020-06-10 09:50:39 -04:00
|
|
|
}
|
2020-06-08 10:13:24 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Submits the login token if one is found in the query parameters. Returns a
|
|
|
|
* boolean of whether the login token was found or not.
|
|
|
|
*/
|
2020-06-10 09:50:39 -04:00
|
|
|
function tryTokenLogin() {
|
2020-06-08 10:13:24 -04:00
|
|
|
// Check if the login token is in the query parameters.
|
|
|
|
var qs = parseQsFromUrl();
|
2015-10-26 13:35:24 -04:00
|
|
|
|
2015-11-05 16:32:47 -05:00
|
|
|
var loginToken = qs.loginToken;
|
|
|
|
if (!loginToken) {
|
2015-10-26 13:35:24 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-08 10:13:24 -04:00
|
|
|
// Retrieve the original query parameters (from before the SSO redirect).
|
|
|
|
// They are stored as JSON in a cookie.
|
|
|
|
var cookies = parseCookies();
|
2020-06-10 09:50:39 -04:00
|
|
|
var originalQueryParams = JSON.parse(cookies[COOKIE_KEY] || "{}")
|
2020-06-08 10:13:24 -04:00
|
|
|
|
|
|
|
// If the login is successful, delete the cookie.
|
2020-06-10 09:50:39 -04:00
|
|
|
function callback() {
|
2020-06-08 10:13:24 -04:00
|
|
|
deleteCookie(COOKIE_KEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
submitLogin(
|
|
|
|
"m.login.token",
|
|
|
|
{token: loginToken},
|
2020-06-10 09:50:39 -04:00
|
|
|
originalQueryParams,
|
2020-06-08 10:13:24 -04:00
|
|
|
callback);
|
2015-10-26 13:35:24 -04:00
|
|
|
|
|
|
|
return true;
|
2020-06-10 09:50:39 -04:00
|
|
|
}
|