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-03-27 10:44:13 -04:00
|
|
|
var title_pre_auth = "Log in with one of the following methods";
|
|
|
|
var title_post_auth = "Logging in...";
|
|
|
|
|
2015-10-26 13:35:24 -04:00
|
|
|
var submitPassword = function(user, pwd) {
|
|
|
|
console.log("Logging in with password...");
|
2020-03-27 10:44:13 -04:00
|
|
|
set_title(title_post_auth);
|
2015-10-26 13:35:24 -04:00
|
|
|
var data = {
|
|
|
|
type: "m.login.password",
|
|
|
|
user: user,
|
|
|
|
password: pwd,
|
|
|
|
};
|
|
|
|
$.post(matrixLogin.endpoint, JSON.stringify(data), function(response) {
|
|
|
|
matrixLogin.onLogin(response);
|
|
|
|
}).error(errorFunc);
|
|
|
|
};
|
|
|
|
|
2015-11-05 16:32:47 -05:00
|
|
|
var submitToken = function(loginToken) {
|
|
|
|
console.log("Logging in with login token...");
|
2020-03-27 10:44:13 -04:00
|
|
|
set_title(title_post_auth);
|
2015-10-26 13:35:24 -04:00
|
|
|
var data = {
|
2015-11-05 16:32:47 -05:00
|
|
|
type: "m.login.token",
|
|
|
|
token: loginToken
|
2015-10-26 13:35:24 -04:00
|
|
|
};
|
|
|
|
$.post(matrixLogin.endpoint, JSON.stringify(data), function(response) {
|
|
|
|
matrixLogin.onLogin(response);
|
|
|
|
}).error(errorFunc);
|
|
|
|
};
|
|
|
|
|
|
|
|
var errorFunc = function(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.
|
|
|
|
show_login(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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var setFeedbackString = function(text) {
|
|
|
|
$("#feedback").text(text);
|
|
|
|
};
|
|
|
|
|
2020-03-27 10:44:13 -04:00
|
|
|
var show_login = function(inhibit_redirect) {
|
2018-11-27 02:51:52 -05:00
|
|
|
var this_page = window.location.origin + window.location.pathname;
|
2019-04-04 07:05:56 -04:00
|
|
|
$("#sso_redirect_url").val(this_page);
|
2018-11-27 02:51:52 -05:00
|
|
|
|
2020-03-27 10:44:13 -04:00
|
|
|
// If inhibit_redirect is false, and SSO is the only supported login method, we can
|
|
|
|
// redirect straight to the SSO page
|
|
|
|
if (matrixLogin.serverAcceptsSso) {
|
|
|
|
if (!inhibit_redirect && !matrixLogin.serverAcceptsPassword) {
|
|
|
|
$("#sso_form").submit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, show the SSO form
|
|
|
|
$("#sso_form").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-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
|
|
|
|
|
|
|
set_title(title_pre_auth);
|
|
|
|
|
|
|
|
$("#loading").hide();
|
2015-10-26 13:35:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
var show_spinner = function() {
|
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-03-27 10:44:13 -04:00
|
|
|
var set_title = function(title) {
|
|
|
|
$("#title").text(title);
|
|
|
|
};
|
2015-10-26 13:35:24 -04:00
|
|
|
|
|
|
|
var fetch_info = function(cb) {
|
|
|
|
$.get(matrixLogin.endpoint, function(response) {
|
|
|
|
var serverAcceptsPassword = false;
|
|
|
|
for (var i=0; i<response.flows.length; i++) {
|
|
|
|
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();
|
|
|
|
}).error(errorFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
matrixLogin.onLoad = function() {
|
|
|
|
fetch_info(function() {
|
2015-11-05 16:32:47 -05:00
|
|
|
if (!try_token()) {
|
2020-03-27 10:44:13 -04:00
|
|
|
show_login(false);
|
2015-10-26 13:35:24 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
matrixLogin.password_login = function() {
|
|
|
|
var user = $("#user_id").val();
|
|
|
|
var pwd = $("#password").val();
|
|
|
|
|
|
|
|
setFeedbackString("");
|
|
|
|
|
|
|
|
show_spinner();
|
|
|
|
submitPassword(user, pwd);
|
|
|
|
};
|
|
|
|
|
|
|
|
matrixLogin.onLogin = function(response) {
|
|
|
|
// clobber this function
|
|
|
|
console.log("onLogin - This function should be replaced to proceed.");
|
|
|
|
console.log(response);
|
|
|
|
};
|
|
|
|
|
|
|
|
var parseQsFromUrl = function(query) {
|
|
|
|
var result = {};
|
|
|
|
query.split("&").forEach(function(part) {
|
|
|
|
var item = part.split("=");
|
|
|
|
var key = item[0];
|
|
|
|
var val = item[1];
|
|
|
|
|
|
|
|
if (val) {
|
|
|
|
val = decodeURIComponent(val);
|
|
|
|
}
|
|
|
|
result[key] = val
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2015-11-05 16:32:47 -05:00
|
|
|
var try_token = function() {
|
2015-10-26 13:35:24 -04:00
|
|
|
var pos = window.location.href.indexOf("?");
|
|
|
|
if (pos == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var qs = parseQsFromUrl(window.location.href.substr(pos+1));
|
|
|
|
|
2015-11-05 16:32:47 -05:00
|
|
|
var loginToken = qs.loginToken;
|
2015-10-26 13:35:24 -04:00
|
|
|
|
2015-11-05 16:32:47 -05:00
|
|
|
if (!loginToken) {
|
2015-10-26 13:35:24 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-05 16:32:47 -05:00
|
|
|
submitToken(loginToken);
|
2015-10-26 13:35:24 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|