| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import "./login.css"; |
| 3 |
|
| 4 |
function debug(...args) { |
| 5 |
if (window.debugging === 'all' || window.debugging?.includes('login')) |
| 6 |
console.debug('login:', ...args); |
| 7 |
} |
| 8 |
|
| 9 |
(function() { |
| 10 |
let localStorage; |
| 11 |
|
| 12 |
/* Some browsers fail localStorage access due to corruption, preventing Cockpit login */ |
| 13 |
try { |
| 14 |
localStorage = window.localStorage; |
| 15 |
window.localStorage.removeItem('url-root'); |
| 16 |
window.localStorage.removeItem('standard-login'); |
| 17 |
} catch (ex) { |
| 18 |
localStorage = window.sessionStorage; |
| 19 |
console.warn(String(ex)); |
| 20 |
} |
| 21 |
|
| 22 |
/* Dark mode */ |
| 23 |
const theme = localStorage.getItem('shell:style') || 'auto'; |
| 24 |
if ((window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches && theme === "auto") || theme === "dark") { |
| 25 |
document.documentElement.classList.add('pf-v6-theme-dark'); |
| 26 |
} else { |
| 27 |
document.documentElement.classList.remove('pf-v6-theme-dark'); |
| 28 |
} |
| 29 |
|
| 30 |
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => { |
| 31 |
if ((event.matches && theme === "auto") || theme === "dark") { |
| 32 |
document.documentElement.classList.add('pf-v6-theme-dark'); |
| 33 |
} else { |
| 34 |
document.documentElement.classList.remove('pf-v6-theme-dark'); |
| 35 |
} |
| 36 |
}); |
| 37 |
|
| 38 |
let url_root; |
| 39 |
const environment = window.environment || { }; |
| 40 |
const oauth = environment.OAuth || null; |
| 41 |
if (oauth) { |
| 42 |
if (!oauth.TokenParam) |
| 43 |
oauth.TokenParam = "access_token"; |
| 44 |
if (!oauth.ErrorParam) |
| 45 |
oauth.ErrorParam = "error_description"; |
| 46 |
} |
| 47 |
|
| 48 |
const fmt_re = /\$\{([^}]+)\}|\$([a-zA-Z0-9_]+)/g; |
| 49 |
function format(fmt /* ... */) { |
| 50 |
const args = Array.prototype.slice.call(arguments, 1); |
| 51 |
return fmt.replace(fmt_re, function(m, x, y) { return args[x || y] || "" }); |
| 52 |
} |
| 53 |
|
| 54 |
function gettext(key) { |
| 55 |
if (window.cockpit_po) { |
| 56 |
const translated = window.cockpit_po[key]; |
| 57 |
if (translated && translated[1]) |
| 58 |
return translated[1]; |
| 59 |
} |
| 60 |
return key; |
| 61 |
} |
| 62 |
|
| 63 |
function translate() { |
| 64 |
const list = document.querySelectorAll("[translate]"); |
| 65 |
for (let i = 0; i < list.length; i++) |
| 66 |
list[i].textContent = gettext(list[i].textContent); |
| 67 |
} |
| 68 |
|
| 69 |
const _ = gettext; |
| 70 |
|
| 71 |
let login_path; |
| 72 |
let application; |
| 73 |
let org_login_path; |
| 74 |
let org_application; |
| 75 |
const qs_re = /[?&]?([^=]+)=([^&]*)/g; |
| 76 |
let oauth_redirect_to = null; |
| 77 |
|
| 78 |
function QueryParams(qs) { |
| 79 |
qs = qs.split('+').join(' '); |
| 80 |
|
| 81 |
const params = {}; |
| 82 |
|
| 83 |
for (;;) { |
| 84 |
const tokens = qs_re.exec(qs); |
| 85 |
if (!tokens) |
| 86 |
break; |
| 87 |
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]); |
| 88 |
} |
| 89 |
return params; |
| 90 |
} |
| 91 |
|
| 92 |
function id(name) { |
| 93 |
return document.getElementById(name); |
| 94 |
} |
| 95 |
|
| 96 |
// strip off "user@", "*:port", and IPv6 brackets from login target (but keep two :: intact for IPv6) |
| 97 |
function parseHostname(ssh_target) { |
| 98 |
return ssh_target |
| 99 |
.replace(/^.*@/, '') |
| 100 |
.replace(/(?<!:):[0-9]+$/, '') |
| 101 |
.replace(/^\[/, '') |
| 102 |
.replace(/\]$/, ''); |
| 103 |
} |
| 104 |
|
| 105 |
// Hide an element (or set of elements) based on a boolean |
| 106 |
// true: element is hidden, false: element is shown |
| 107 |
function hideToggle(elements, toggle) { |
| 108 |
// If it's a single selector, convert it to an array for the loop |
| 109 |
if (typeof elements === "string") |
| 110 |
elements = [elements]; |
| 111 |
|
| 112 |
// >= 1 arguments (of type element or string (for CSS selectors)) |
| 113 |
// (passed in "arguments" isn't a a true array, so forEach wouldn't always work) |
| 114 |
for (let i = 0; i < elements.length; i++) { |
| 115 |
if (typeof elements[i] === "string") { |
| 116 |
// Support CSS selectors as a string |
| 117 |
const els = document.querySelectorAll(elements[i]); |
| 118 |
|
| 119 |
if (els) |
| 120 |
els.forEach(function(element) { |
| 121 |
if (element.hidden !== !!toggle) |
| 122 |
element.hidden = !!toggle; |
| 123 |
}); |
| 124 |
} else { |
| 125 |
// Hide specific elements |
| 126 |
if (elements[i].hidden !== !!toggle) |
| 127 |
elements[i].hidden = !!toggle; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
// Show >=1 arguments (element or CSS selector) |
| 133 |
function show() { |
| 134 |
hideToggle(arguments, false); |
| 135 |
} |
| 136 |
|
| 137 |
// Hide >=1 arguments (element or CSS selector) |
| 138 |
function hide() { |
| 139 |
hideToggle(arguments, true); |
| 140 |
} |
| 141 |
|
| 142 |
function show_captured_stderr(msg) { |
| 143 |
console.warn("stderr:", msg); |
| 144 |
|
| 145 |
hide("#login-wait-validating"); |
| 146 |
|
| 147 |
hide("#login", "#login-details"); |
| 148 |
show("#login-fatal"); |
| 149 |
|
| 150 |
id("login-again").onclick = () => { hide('#login-fatal'); show_login() }; |
| 151 |
show("#login-again"); |
| 152 |
|
| 153 |
const el = id("login-fatal-message"); |
| 154 |
el.textContent = ""; |
| 155 |
el.appendChild(document.createTextNode(msg)); |
| 156 |
} |
| 157 |
|
| 158 |
function fatal(msg) { |
| 159 |
console.warn("fatal:", msg); |
| 160 |
|
| 161 |
hide("#login-again", "#login-wait-validating"); |
| 162 |
|
| 163 |
if (oauth_redirect_to) { |
| 164 |
id("login-again").href = oauth_redirect_to; |
| 165 |
show("#login-again"); |
| 166 |
} |
| 167 |
|
| 168 |
hide("#login", "#login-details"); |
| 169 |
show("#login-fatal"); |
| 170 |
|
| 171 |
const el = id("login-fatal-message"); |
| 172 |
el.textContent = ""; |
| 173 |
el.appendChild(document.createTextNode(msg)); |
| 174 |
} |
| 175 |
|
| 176 |
function brand(_id, def) { |
| 177 |
const elt = id(_id); |
| 178 |
const style = (elt && window.getComputedStyle) ? window.getComputedStyle(elt, ":before") : null; |
| 179 |
|
| 180 |
if (!style) |
| 181 |
return; |
| 182 |
|
| 183 |
let content = style.content; |
| 184 |
if (content && content != "none" && content != "normal") { |
| 185 |
const len = content.length; |
| 186 |
if ((content[0] === '"' || content[0] === '\'') && |
| 187 |
len > 2 && content[len - 1] === content[0]) |
| 188 |
content = content.substring(1, len - 1); |
| 189 |
elt.innerHTML = content || def; |
| 190 |
} else { |
| 191 |
elt.removeAttribute("class"); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
function requisites() { |
| 196 |
function showBypass(bypass) { |
| 197 |
if (bypass) { |
| 198 |
// Selectively show and hide elements |
| 199 |
show("#login", "#login-details", "#login-override"); |
| 200 |
hide("#get-out-link"); |
| 201 |
|
| 202 |
// Reparent login form to the expander |
| 203 |
id("login-override-content").appendChild(id("login")); |
| 204 |
|
| 205 |
// Change the state of the button from primary to warning |
| 206 |
id("login-button").classList.add("pf-m-warning"); |
| 207 |
|
| 208 |
// Render a "helper text" warning above the log in button |
| 209 |
document.querySelector("#login .login-actions").insertAdjacentHTML( |
| 210 |
"beforebegin", |
| 211 |
"<div class='pf-v6-c-helper-text pf-m-warning' id='bypass-warning'>" + |
| 212 |
_("Cockpit might not render correctly in your browser") + |
| 213 |
"</div>" |
| 214 |
); |
| 215 |
} else { |
| 216 |
hide("#login", "#login-details", "#login-override"); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
function disableLogin(name, bypass) { |
| 221 |
if (name === "supports") |
| 222 |
name = "@supports API"; |
| 223 |
const errorString = format(_("This web browser is too old to run the Web Console (missing $0)"), name); |
| 224 |
|
| 225 |
console.warn(errorString); |
| 226 |
id("login-error-message").textContent = errorString; |
| 227 |
show("#unsupported-browser", "#error-group"); |
| 228 |
document.body.classList.add("unsupported-browser"); |
| 229 |
|
| 230 |
showBypass(bypass); |
| 231 |
} |
| 232 |
|
| 233 |
function req(name, obj) { |
| 234 |
let ret; |
| 235 |
try { |
| 236 |
ret = (obj && obj[name]); |
| 237 |
} catch (ex) { |
| 238 |
fatal(format(_("The web browser configuration prevents Cockpit from running (inaccessible $0)"), name)); |
| 239 |
throw ex; |
| 240 |
} |
| 241 |
if (ret === undefined) { |
| 242 |
disableLogin(name); |
| 243 |
return false; |
| 244 |
} |
| 245 |
return true; |
| 246 |
} |
| 247 |
|
| 248 |
function css() { |
| 249 |
/* |
| 250 |
* Be certain to use parenthesis when checking CSS strings |
| 251 |
* as Edge is oddly particular. |
| 252 |
* |
| 253 |
* Instead of "display: inline", use: |
| 254 |
* "(display: inline)" |
| 255 |
* or |
| 256 |
* "display", "inline" |
| 257 |
*/ |
| 258 |
const args = [].join.call(arguments, ": "); |
| 259 |
|
| 260 |
if (!window.CSS || !window.CSS.supports.apply(this, arguments)) { |
| 261 |
disableLogin(args, "bypass"); |
| 262 |
return false; |
| 263 |
} |
| 264 |
return true; |
| 265 |
} |
| 266 |
|
| 267 |
const hard_req = req("WebSocket", window) && |
| 268 |
req("XMLHttpRequest", window) && |
| 269 |
req("sessionStorage", window) && |
| 270 |
req("JSON", window) && |
| 271 |
req("defineProperty", Object) && |
| 272 |
req("pushState", window.history) && |
| 273 |
req("textContent", document) && |
| 274 |
req("replaceAll", String.prototype) && |
| 275 |
req("finally", Promise.prototype) && |
| 276 |
req("supports", window.CSS); |
| 277 |
|
| 278 |
if (hard_req) { |
| 279 |
css("display", "flex") && |
| 280 |
css("display", "grid") && |
| 281 |
css("selector(test)") && |
| 282 |
css("selector(:is(*):where(*))"); |
| 283 |
return true; |
| 284 |
} else { |
| 285 |
return false; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
/* Sets values for application, url_root and login_path */ |
| 290 |
function setup_path_globals (path) { |
| 291 |
const parser = document.createElement('a'); |
| 292 |
// send_login_html() sets <base> to UrlRoot |
| 293 |
const base = document.baseURI; |
| 294 |
|
| 295 |
path = path || "/"; |
| 296 |
parser.href = base; |
| 297 |
if (parser.pathname != "/") { |
| 298 |
url_root = parser.pathname.replace(/^\/+|\/+$/g, ''); |
| 299 |
// deprecated: for connecting to cockpit.js < 272 |
| 300 |
localStorage.setItem('url-root', url_root); |
| 301 |
if (url_root && path.indexOf('/' + url_root) === 0) |
| 302 |
path = path.replace('/' + url_root, '') || '/'; |
| 303 |
} |
| 304 |
|
| 305 |
if (path.indexOf("/=") === 0) { |
| 306 |
environment.hostname = path.substring(2).split("/")[0]; |
| 307 |
id("server-field").value = environment.hostname; |
| 308 |
toggle_options(null, true); |
| 309 |
path = "/cockpit+" + path.split("/")[1]; |
| 310 |
} else if (path.indexOf("/cockpit/") !== 0 && path.indexOf("/cockpit+") !== 0) { |
| 311 |
path = "/cockpit"; |
| 312 |
} |
| 313 |
|
| 314 |
application = path.split("/")[1]; |
| 315 |
login_path = "/" + application + "/login"; |
| 316 |
if (url_root) |
| 317 |
login_path = "/" + url_root + login_path; |
| 318 |
|
| 319 |
org_application = application; |
| 320 |
org_login_path = login_path; |
| 321 |
} |
| 322 |
|
| 323 |
function toggle_options(ev, show) { |
| 324 |
// On keypress, only accept spacebar (enter acts as a click) |
| 325 |
if (ev && ev.type === 'keypress' && ev.key !== ' ') |
| 326 |
return; |
| 327 |
// Stop the <a>'s click handler, otherwise it causes a page reload |
| 328 |
if (ev && ev.type === 'click') |
| 329 |
ev.preventDefault(); |
| 330 |
|
| 331 |
if (show === undefined) |
| 332 |
show = id("server-group").hidden; |
| 333 |
|
| 334 |
hideToggle("#server-group", !show); |
| 335 |
|
| 336 |
id("option-group").setAttribute("data-state", show); |
| 337 |
} |
| 338 |
|
| 339 |
function toggle_password(event) { |
| 340 |
const input = id("login-password-input"); |
| 341 |
|
| 342 |
input.setAttribute("type", (input.getAttribute("type") === "password") ? "text" : "password"); |
| 343 |
event.stopPropagation(); |
| 344 |
} |
| 345 |
|
| 346 |
function deal_with_multihost() { |
| 347 |
// If we are currently logged in to some machine, but still |
| 348 |
// end up on the login page, we are about to load resources |
| 349 |
// from two machines into the same browser origin. This needs |
| 350 |
// to be allowed explicitly via a configuration setting. |
| 351 |
|
| 352 |
const logged_into = environment.logged_into || []; |
| 353 |
const cur_machine = logged_into.length > 0 ? logged_into[0] : null; |
| 354 |
|
| 355 |
function redirect_to_current_machine() { |
| 356 |
if (cur_machine === ".") { |
| 357 |
login_reload("/"); |
| 358 |
} else { |
| 359 |
login_reload("/=" + cur_machine); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
if (cur_machine && !environment.page.allow_multihost) |
| 364 |
redirect_to_current_machine(); |
| 365 |
} |
| 366 |
|
| 367 |
function boot() { |
| 368 |
window.onload = null; |
| 369 |
|
| 370 |
translate(); |
| 371 |
if (window.cockpit_po && window.cockpit_po[""]) { |
| 372 |
document.documentElement.lang = window.cockpit_po[""].language; |
| 373 |
if (window.cockpit_po[""]["language-direction"]) |
| 374 |
document.documentElement.dir = window.cockpit_po[""]["language-direction"]; |
| 375 |
} |
| 376 |
|
| 377 |
deal_with_multihost(); |
| 378 |
|
| 379 |
setup_path_globals(window.location.pathname); |
| 380 |
|
| 381 |
/* Determine if we are nested or not, and switch styles */ |
| 382 |
if (window.location.pathname.indexOf("/" + url_root + "/cockpit/") === 0 || |
| 383 |
window.location.pathname.indexOf("/" + url_root + "/cockpit+") === 0) |
| 384 |
document.documentElement.setAttribute("class", "inline"); |
| 385 |
|
| 386 |
// Setup title |
| 387 |
let title = environment.page.title; |
| 388 |
if (environment.is_cockpit_client) |
| 389 |
title = _("Login"); |
| 390 |
if (!title || application.indexOf("cockpit+=") === 0) |
| 391 |
title = environment.hostname; |
| 392 |
document.title = title; |
| 393 |
|
| 394 |
if (application.indexOf("cockpit+=") === 0) { |
| 395 |
hide("#brand", "#badge"); |
| 396 |
} else { |
| 397 |
brand("badge", ""); |
| 398 |
brand("brand", "Cockpit"); |
| 399 |
} |
| 400 |
|
| 401 |
if (!requisites()) |
| 402 |
return; |
| 403 |
|
| 404 |
if (environment.banner) { |
| 405 |
show("#banner"); |
| 406 |
id("banner-message").textContent = environment.banner.trimEnd(); |
| 407 |
} |
| 408 |
|
| 409 |
id("bypass-browser-check").addEventListener("click", toggle_options); |
| 410 |
id("bypass-browser-check").addEventListener("keypress", toggle_options); |
| 411 |
id("show-other-login-options").addEventListener("click", toggle_options); |
| 412 |
id("show-other-login-options").addEventListener("keypress", toggle_options); |
| 413 |
id("server-clear").addEventListener("click", function () { |
| 414 |
const el = id("server-field"); |
| 415 |
el.value = ""; |
| 416 |
el.focus(); |
| 417 |
}); |
| 418 |
|
| 419 |
const logout_intent = window.sessionStorage.getItem("logout-intent") == "explicit"; |
| 420 |
if (logout_intent) |
| 421 |
window.sessionStorage.removeItem("logout-intent"); |
| 422 |
|
| 423 |
const logout_reason = window.sessionStorage.getItem("logout-reason"); |
| 424 |
if (logout_reason) |
| 425 |
window.sessionStorage.removeItem("logout-reason"); |
| 426 |
|
| 427 |
/* Try automatic/kerberos authentication? */ |
| 428 |
if (oauth) { |
| 429 |
hide("#login-details", "#login"); |
| 430 |
if (logout_intent) { |
| 431 |
build_oauth_redirect_to(); |
| 432 |
id("login-again").textContent = _("Login again"); |
| 433 |
fatal(_("Logout successful")); |
| 434 |
} else { |
| 435 |
oauth_auto_login(); |
| 436 |
} |
| 437 |
} else if (logout_intent) { |
| 438 |
show_login(logout_reason); |
| 439 |
} else if (need_host()) { |
| 440 |
show_login(); |
| 441 |
} else { |
| 442 |
standard_auto_login(); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
function standard_auto_login() { |
| 447 |
const xhr = new XMLHttpRequest(); |
| 448 |
xhr.open("GET", login_path, true); |
| 449 |
xhr.onreadystatechange = function () { |
| 450 |
if (xhr.readyState == 4) { |
| 451 |
if (xhr.status == 200) { |
| 452 |
run(JSON.parse(xhr.responseText)); |
| 453 |
} else if (xhr.status == 401) { |
| 454 |
show_login(); |
| 455 |
} else if (xhr.statusText) { |
| 456 |
fatal(decodeURIComponent(xhr.statusText)); |
| 457 |
} else if (xhr.status === 0) { |
| 458 |
show_login(); |
| 459 |
} else { |
| 460 |
fatal(format(_("$0 error"), xhr.status)); |
| 461 |
} |
| 462 |
} |
| 463 |
}; |
| 464 |
xhr.send(); |
| 465 |
} |
| 466 |
|
| 467 |
function build_oauth_redirect_to() { |
| 468 |
const url_parts = window.location.href.split('#', 2); |
| 469 |
oauth_redirect_to = oauth.URL; |
| 470 |
if (oauth.URL.indexOf("?") > -1) |
| 471 |
oauth_redirect_to += "&"; |
| 472 |
else |
| 473 |
oauth_redirect_to += "?"; |
| 474 |
oauth_redirect_to += "redirect_uri=" + encodeURIComponent(url_parts[0]); |
| 475 |
} |
| 476 |
|
| 477 |
function oauth_auto_login() { |
| 478 |
const parser = document.createElement('a'); |
| 479 |
if (!oauth.URL) |
| 480 |
return fatal(_("Cockpit authentication is configured incorrectly.")); |
| 481 |
|
| 482 |
const query = (!window.location.search && window.location.hash) |
| 483 |
? QueryParams(window.location.hash.slice(1)) |
| 484 |
: QueryParams(window.location.search); |
| 485 |
|
| 486 |
/* Not all providers allow hashes in redirect urls */ |
| 487 |
|
| 488 |
build_oauth_redirect_to(); |
| 489 |
|
| 490 |
if (query[oauth.TokenParam]) { |
| 491 |
if (window.sessionStorage.getItem('login-wanted')) { |
| 492 |
parser.href = window.sessionStorage.getItem('login-wanted'); |
| 493 |
setup_path_globals(parser.pathname); |
| 494 |
} |
| 495 |
|
| 496 |
const token_val = query[oauth.TokenParam]; |
| 497 |
show("#login-wait-validating"); |
| 498 |
const xhr = new XMLHttpRequest(); |
| 499 |
xhr.open("GET", login_path, true); |
| 500 |
xhr.setRequestHeader("Authorization", "Bearer " + token_val); |
| 501 |
xhr.onreadystatechange = function () { |
| 502 |
if (xhr.readyState == 4) { |
| 503 |
if (xhr.status == 200) { |
| 504 |
run(JSON.parse(xhr.responseText)); |
| 505 |
} else { |
| 506 |
const prompt_data = get_prompt_from_challenge(xhr.getResponseHeader("WWW-Authenticate"), xhr.responseText); |
| 507 |
if (prompt_data) |
| 508 |
show_converse(prompt_data); |
| 509 |
else |
| 510 |
fatal(decodeURIComponent(xhr.statusText)); |
| 511 |
} |
| 512 |
} |
| 513 |
}; |
| 514 |
xhr.send(); |
| 515 |
} else if (query[oauth.ErrorParam]) { |
| 516 |
fatal(query[oauth.ErrorParam]); |
| 517 |
} else { |
| 518 |
/* Store url we originally wanted in case we |
| 519 |
* had to strip a hash or query params |
| 520 |
*/ |
| 521 |
window.sessionStorage.setItem('login-wanted', |
| 522 |
window.location.href); |
| 523 |
window.location = oauth_redirect_to; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
function clear_errors() { |
| 528 |
hide("#error-group"); |
| 529 |
id("login-error-message").textContent = ""; |
| 530 |
} |
| 531 |
|
| 532 |
function clear_info() { |
| 533 |
hide("#info-group"); |
| 534 |
id("login-info-message").textContent = ""; |
| 535 |
} |
| 536 |
|
| 537 |
function login_failure(title, msg, form) { |
| 538 |
clear_errors(); |
| 539 |
if (title) { |
| 540 |
/* OAuth failures are always fatal */ |
| 541 |
if (oauth) { |
| 542 |
fatal(title); |
| 543 |
} else { |
| 544 |
show_form(form || "login"); |
| 545 |
id("login-error-title").textContent = title; |
| 546 |
id("login-error-message").textContent = msg; |
| 547 |
hideToggle("#error-group .pf-v6-c-alert__description", !msg); |
| 548 |
show("#error-group"); |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
function login_info(msg) { |
| 554 |
clear_info(); |
| 555 |
if (msg) { |
| 556 |
id("login-info-message").textContent = msg; |
| 557 |
show("#info-group"); |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
function host_failure(title, msg) { |
| 562 |
if (!login_machine) { |
| 563 |
login_failure(msg); |
| 564 |
} else { |
| 565 |
clear_errors(); |
| 566 |
id("login-error-title").textContent = title; |
| 567 |
id("login-error-message").textContent = msg; |
| 568 |
hideToggle("#error-group .pf-v6-c-alert__description", !msg); |
| 569 |
show("#error-group"); |
| 570 |
toggle_options(null, true); |
| 571 |
show_form("login"); |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
function login_note(msg) { |
| 576 |
const el = id("login-note"); |
| 577 |
if (msg) { |
| 578 |
show(el); |
| 579 |
el.textContent = msg; |
| 580 |
} else { |
| 581 |
el.innerHTML = ' '; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
function need_host() { |
| 586 |
return environment.page.require_host && |
| 587 |
org_application.indexOf("cockpit+=") === -1; |
| 588 |
} |
| 589 |
|
| 590 |
function get_recent_hosts() { |
| 591 |
let hosts = []; |
| 592 |
try { |
| 593 |
hosts = JSON.parse(localStorage.getItem("cockpit-client-sessions") || "[]"); |
| 594 |
} catch (e) { |
| 595 |
console.log("Failed to parse 'cockpit-client-sessions':", e); |
| 596 |
} |
| 597 |
|
| 598 |
return hosts; |
| 599 |
} |
| 600 |
|
| 601 |
// value of #server-field at the time of clicking "Login" |
| 602 |
let login_machine = null; |
| 603 |
/* set by do_hostkey_verification() for a confirmed unknown host fingerprint; |
| 604 |
* setup_localstorage() will then write the received full known_hosts entry to the known_hosts |
| 605 |
* database for this host */ |
| 606 |
let login_data_host = null; |
| 607 |
/* set if our known_host database has a non-matching host key, and we re-attempt the login |
| 608 |
* with asking the user for confirmation */ |
| 609 |
let ssh_host_key_change_host = null; |
| 610 |
|
| 611 |
function call_login() { |
| 612 |
login_failure(null); |
| 613 |
login_machine = id("server-field").value; |
| 614 |
login_data_host = null; |
| 615 |
const user = id("login-user-input").value.trim(); |
| 616 |
if (user === "" && !environment.is_cockpit_client) { |
| 617 |
login_failure(_("User name cannot be empty")); |
| 618 |
} else if (need_host() && login_machine === "") { |
| 619 |
login_failure(_("Please specify the host to connect to")); |
| 620 |
} else { |
| 621 |
if (login_machine) { |
| 622 |
application = "cockpit+=" + login_machine; |
| 623 |
login_path = org_login_path.replace("/" + org_application + "/", "/" + application + "/"); |
| 624 |
id("brand").style.display = "none"; |
| 625 |
id("badge").style.visibility = "hidden"; |
| 626 |
} else { |
| 627 |
application = org_application; |
| 628 |
login_path = org_login_path; |
| 629 |
brand("badge", ""); |
| 630 |
brand("brand", "Cockpit"); |
| 631 |
} |
| 632 |
|
| 633 |
id("server-name").textContent = login_machine || environment.hostname; |
| 634 |
id("login-button").removeEventListener("click", call_login); |
| 635 |
|
| 636 |
const password = id("login-password-input").value; |
| 637 |
|
| 638 |
const superuser_key = "superuser:" + user + (login_machine ? ":" + login_machine : ""); |
| 639 |
const superuser = localStorage.getItem(superuser_key) || "none"; |
| 640 |
localStorage.setItem("superuser-key", superuser_key); |
| 641 |
localStorage.setItem(superuser_key, superuser); |
| 642 |
|
| 643 |
/* Keep information if login page was used */ |
| 644 |
localStorage.setItem('standard-login', true); |
| 645 |
|
| 646 |
let known_hosts = ''; |
| 647 |
if (login_machine) { |
| 648 |
if (ssh_host_key_change_host == login_machine) { |
| 649 |
/* We came here because logging in ran into invalid-hostkey; so try the next |
| 650 |
* round without sending the key. do_hostkey_verification() will notice the |
| 651 |
change and show the correct dialog. */ |
| 652 |
debug("call_login(): previous login attempt into", login_machine, "failed due to changed key"); |
| 653 |
} else { |
| 654 |
// If we have a known host key, send it to ssh |
| 655 |
const keys = get_hostkeys(login_machine); |
| 656 |
if (keys) { |
| 657 |
debug("call_login(): sending known_host key", keys, "for logging into", login_machine); |
| 658 |
known_hosts = keys; |
| 659 |
} else { |
| 660 |
debug("call_login(): no known_hosts entry for logging into", login_machine); |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
const headers = { |
| 666 |
Authorization: "Basic " + window.btoa(utf8(user + ":" + password + '\0' + known_hosts)), |
| 667 |
"X-Superuser": superuser, |
| 668 |
}; |
| 669 |
// allow unknown remote hosts with interactive logins with "Connect to:" |
| 670 |
if (login_machine) |
| 671 |
headers["X-SSH-Connect-Unknown-Hosts"] = "yes"; |
| 672 |
|
| 673 |
send_login_request("GET", headers, false); |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
function render_recent_hosts() { |
| 678 |
const hosts = get_recent_hosts(); |
| 679 |
|
| 680 |
const list = id("recent-hosts-list"); |
| 681 |
list.innerHTML = ""; |
| 682 |
hosts.forEach(host => { |
| 683 |
const wrapper = document.createElement("div"); |
| 684 |
wrapper.classList.add("host-line"); |
| 685 |
wrapper.setAttribute("data-host-id", host); |
| 686 |
|
| 687 |
const b1 = document.createElement("button"); |
| 688 |
b1.textContent = host; |
| 689 |
b1.classList.add("pf-v6-c-button", "pf-m-tertiary", "host-name"); |
| 690 |
b1.addEventListener("click", () => { |
| 691 |
id("server-field").value = host; |
| 692 |
call_login(); |
| 693 |
}); |
| 694 |
|
| 695 |
const b2 = document.createElement("button"); |
| 696 |
b2.title = _("Remove host"); |
| 697 |
b2.ariaLabel = b2.title; |
| 698 |
b2.classList.add("host-remove"); |
| 699 |
b2.addEventListener("click", () => { |
| 700 |
const i = hosts.indexOf(host); |
| 701 |
hosts.splice(i, 1); |
| 702 |
localStorage.setItem('cockpit-client-sessions', JSON.stringify(hosts)); |
| 703 |
render_recent_hosts(); |
| 704 |
}); |
| 705 |
|
| 706 |
wrapper.append(b1, b2); |
| 707 |
list.append(wrapper); |
| 708 |
}); |
| 709 |
hideToggle("#recent-hosts", hosts.length == 0); |
| 710 |
} |
| 711 |
|
| 712 |
function show_form(form) { |
| 713 |
const connectable = environment.page.connect; |
| 714 |
let expanded = id("option-group").getAttribute("data-state"); |
| 715 |
|
| 716 |
hide("#login-wait-validating"); |
| 717 |
show("#login"); |
| 718 |
hideToggle("#login-details", environment.is_cockpit_client); |
| 719 |
hideToggle("#server-field-label", environment.is_cockpit_client); |
| 720 |
if (environment.is_cockpit_client) { |
| 721 |
const brand = id("brand"); |
| 722 |
brand.textContent = _("Connect to:"); |
| 723 |
brand.classList.add("text-brand"); |
| 724 |
} |
| 725 |
|
| 726 |
hideToggle(["#user-group", "#password-group"], form != "login" || environment.is_cockpit_client); |
| 727 |
hideToggle("#conversation-group", form != "conversation"); |
| 728 |
hideToggle("#hostkey-group", form != "hostkey"); |
| 729 |
|
| 730 |
id("login-button-text").textContent = (form == "hostkey") ? _("Accept key and log in") : _("Log in"); |
| 731 |
if (form != "login") |
| 732 |
id("login-password-input").value = ''; |
| 733 |
|
| 734 |
if (environment.page.require_host) { |
| 735 |
hide("#option-group"); |
| 736 |
expanded = true; |
| 737 |
} else { |
| 738 |
hideToggle("#option-group", !connectable || form != "login"); |
| 739 |
} |
| 740 |
|
| 741 |
if (!connectable || form != "login") { |
| 742 |
hide("#server-group"); |
| 743 |
} else { |
| 744 |
hideToggle("#server-group", !expanded); |
| 745 |
} |
| 746 |
|
| 747 |
id("login-button").removeAttribute('disabled'); |
| 748 |
id("login-button").removeAttribute('spinning'); |
| 749 |
id("login-button").classList.remove("pf-m-danger"); |
| 750 |
id("login-button").classList.add("pf-m-primary"); |
| 751 |
hide("#get-out-link"); |
| 752 |
|
| 753 |
if (form == "login") |
| 754 |
id("login-button").addEventListener("click", call_login); |
| 755 |
|
| 756 |
if (environment.is_cockpit_client) { |
| 757 |
render_recent_hosts(); |
| 758 |
document.body.classList.add("cockpit-client"); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
function show_login(message) { |
| 763 |
/* Show the login screen */ |
| 764 |
login_info(message); |
| 765 |
id("server-name").textContent = document.title; |
| 766 |
login_note(_("Log in with your server user account.")); |
| 767 |
id("login-user-input").addEventListener("keydown", function(e) { |
| 768 |
login_failure(null); |
| 769 |
clear_info(); |
| 770 |
if (e.which == 13) |
| 771 |
id("login-password-input").focus(); |
| 772 |
}, false); |
| 773 |
|
| 774 |
const do_login = function(e) { |
| 775 |
login_failure(null); |
| 776 |
if (e.which == 13) |
| 777 |
call_login(); |
| 778 |
}; |
| 779 |
|
| 780 |
id("login-password-input").addEventListener("keydown", do_login); |
| 781 |
id("login-password-toggle").addEventListener("click", toggle_password); |
| 782 |
|
| 783 |
show_form("login"); |
| 784 |
|
| 785 |
if (!environment.is_cockpit_client) { |
| 786 |
id("login-user-input").focus(); |
| 787 |
} else if (environment.page.require_host) { |
| 788 |
id("server-field").focus(); |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
function get_known_hosts_db() { |
| 793 |
try { |
| 794 |
return JSON.parse(localStorage.getItem("known_hosts") || "{ }"); |
| 795 |
} catch (ex) { |
| 796 |
console.warn("Can't parse known_hosts database in localStorage", ex); |
| 797 |
return { }; |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
function get_hostkeys(host) { |
| 802 |
return get_known_hosts_db()[parseHostname(host)]; |
| 803 |
} |
| 804 |
|
| 805 |
function set_hostkeys(host, keys) { |
| 806 |
try { |
| 807 |
const db = get_known_hosts_db(); |
| 808 |
db[parseHostname(host)] = keys; |
| 809 |
localStorage.setItem("known_hosts", JSON.stringify(db)); |
| 810 |
} catch (ex) { |
| 811 |
console.warn("Can't write known_hosts database to localStorage", ex); |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
function do_hostkey_verification(data) { |
| 816 |
const key = data["host-key"]; |
| 817 |
const key_host = key.split(" ")[0]; |
| 818 |
const key_type = key.split(" ")[1]; |
| 819 |
const db_keys = get_hostkeys(key_host); |
| 820 |
|
| 821 |
if (db_keys) { |
| 822 |
debug("do_hostkey_verification: received key fingerprint", data.default, "for host", key_host, |
| 823 |
"does not match key in known_hosts database:", db_keys, "; treating as changed"); |
| 824 |
id("hostkey-title").textContent = format(_("$0 key changed"), login_machine); |
| 825 |
show("#hostkey-warning-group"); |
| 826 |
id("hostkey-message-1").textContent = ""; |
| 827 |
} else { |
| 828 |
debug("do_hostkey_verification: received key fingerprint", data.default, "for host", key_host, |
| 829 |
"not in known_hosts database; treating as new host"); |
| 830 |
id("hostkey-title").textContent = _("New host"); |
| 831 |
hide("#hostkey-warning-group"); |
| 832 |
id("hostkey-message-1").textContent = format(_("You are connecting to $0 for the first time."), login_machine); |
| 833 |
} |
| 834 |
|
| 835 |
id("hostkey-verify-help-1").textContent = format(_("To verify a fingerprint, run the following on $0 while physically sitting at the machine or through a trusted network:"), login_machine); |
| 836 |
id("hostkey-verify-help-cmds").textContent = format("ssh-keyscan$0 localhost | ssh-keygen -lf -", |
| 837 |
key_type ? " -t " + key_type : ""); |
| 838 |
|
| 839 |
id("hostkey-fingerprint").textContent = data.default; |
| 840 |
|
| 841 |
if (key_type) { |
| 842 |
id("hostkey-type").textContent = format("($0)", key_type); |
| 843 |
show("#hostkey-type"); |
| 844 |
} else { |
| 845 |
hide("#hostkey-type"); |
| 846 |
} |
| 847 |
|
| 848 |
login_failure(""); |
| 849 |
|
| 850 |
function call_converse() { |
| 851 |
id("login-button").removeEventListener("click", call_converse); |
| 852 |
login_failure(null, null, "hostkey"); |
| 853 |
// cockpit-beiboot sends only a placeholder, defer to login-data in setup_localstorage() |
| 854 |
if (key.endsWith(" login-data")) { |
| 855 |
login_data_host = key_host; |
| 856 |
debug("call_converse(): got placeholder host keyfor", login_data_host, ", deferring db update"); |
| 857 |
converse(data.id, data.default); |
| 858 |
} else { |
| 859 |
console.error("login: got unexpected host key prompt, expecting login-data placeholder:", key); |
| 860 |
fatal(_("Internal protocol error")); |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
id("login-button").addEventListener("click", call_converse); |
| 865 |
|
| 866 |
show_form("hostkey"); |
| 867 |
show("#get-out-link"); |
| 868 |
|
| 869 |
if (db_keys) { |
| 870 |
id("login-button").classList.add("pf-m-danger"); |
| 871 |
id("login-button").classList.remove("pf-m-primary"); |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
function show_converse(prompt_data) { |
| 876 |
if (prompt_data["host-key"]) { |
| 877 |
do_hostkey_verification(prompt_data); |
| 878 |
return; |
| 879 |
} |
| 880 |
|
| 881 |
const type = prompt_data.echo ? "text" : "password"; |
| 882 |
id("conversation-prompt").textContent = prompt_data.prompt; |
| 883 |
|
| 884 |
const em = id("conversation-message"); |
| 885 |
const msg = prompt_data.error || prompt_data.message; |
| 886 |
if (msg) { |
| 887 |
em.textContent = msg; |
| 888 |
show(em); |
| 889 |
} else { |
| 890 |
hide(em); |
| 891 |
} |
| 892 |
|
| 893 |
const ei = id("conversation-input"); |
| 894 |
ei.value = ""; |
| 895 |
if (prompt_data.default) |
| 896 |
ei.value = prompt_data.default; |
| 897 |
ei.setAttribute('type', type); |
| 898 |
|
| 899 |
login_failure(""); |
| 900 |
|
| 901 |
function call_converse() { |
| 902 |
id("conversation-input").removeEventListener("keydown", key_down); |
| 903 |
id("login-button").removeEventListener("click", call_converse); |
| 904 |
login_failure(null, null, "conversation"); |
| 905 |
converse(prompt_data.id, id("conversation-input").value); |
| 906 |
} |
| 907 |
|
| 908 |
function key_down(e) { |
| 909 |
login_failure(null, null, "conversation"); |
| 910 |
if (e.which == 13) { |
| 911 |
call_converse(); |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
id("conversation-input").addEventListener("keydown", key_down); |
| 916 |
id("login-button").addEventListener("click", call_converse); |
| 917 |
show_form("conversation"); |
| 918 |
ei.focus(); |
| 919 |
} |
| 920 |
|
| 921 |
function utf8(str) { |
| 922 |
return window.unescape(encodeURIComponent(str)); |
| 923 |
} |
| 924 |
|
| 925 |
function get_prompt_from_challenge (header, body) { |
| 926 |
if (!header) |
| 927 |
return null; |
| 928 |
|
| 929 |
const parts = header.split(' '); |
| 930 |
if (parts[0].toLowerCase() !== 'x-conversation' || parts.length !== 3) |
| 931 |
return null; |
| 932 |
|
| 933 |
const id = parts[1]; |
| 934 |
let prompt; |
| 935 |
try { |
| 936 |
prompt = window.atob(parts[2]); |
| 937 |
} catch (err) { |
| 938 |
console.error("Invalid prompt data", err); |
| 939 |
return null; |
| 940 |
} |
| 941 |
|
| 942 |
let resp; |
| 943 |
try { |
| 944 |
resp = JSON.parse(body); |
| 945 |
} catch (err) { |
| 946 |
console.log("Got invalid JSON response for prompt data", err); |
| 947 |
resp = {}; |
| 948 |
} |
| 949 |
|
| 950 |
resp.id = id; |
| 951 |
resp.prompt = prompt; |
| 952 |
return resp; |
| 953 |
} |
| 954 |
|
| 955 |
function send_login_request(method, headers, is_conversation) { |
| 956 |
debug("send_login_request():", method, "headers:", JSON.stringify(headers)); |
| 957 |
id("login-button").setAttribute('disabled', "true"); |
| 958 |
id("login-button").setAttribute('spinning', "true"); |
| 959 |
const xhr = new XMLHttpRequest(); |
| 960 |
xhr.open(method, login_path, true); |
| 961 |
|
| 962 |
for (const k in headers) |
| 963 |
xhr.setRequestHeader(k, headers[k]); |
| 964 |
|
| 965 |
xhr.onreadystatechange = function () { |
| 966 |
if (xhr.readyState != 4) { |
| 967 |
return; |
| 968 |
} |
| 969 |
if (xhr.status == 200) { |
| 970 |
const resp = JSON.parse(xhr.responseText); |
| 971 |
run(resp); |
| 972 |
} else if (xhr.status == 401) { |
| 973 |
debug("send_login_request():", method, "got 401, status:", xhr.statusText, "; response:", xhr.responseText); |
| 974 |
const challenge = xhr.getResponseHeader("WWW-Authenticate"); |
| 975 |
if (challenge && challenge.toLowerCase().indexOf("x-conversation") === 0) { |
| 976 |
const prompt_data = get_prompt_from_challenge(challenge, xhr.responseText); |
| 977 |
if (prompt_data) |
| 978 |
show_converse(prompt_data); |
| 979 |
else |
| 980 |
fatal(_("Internal error: Invalid challenge header")); |
| 981 |
} else { |
| 982 |
console.log(xhr.statusText); |
| 983 |
/* did the user confirm a changed SSH host key? If so, update database */ |
| 984 |
if (ssh_host_key_change_host) { |
| 985 |
try { |
| 986 |
const keys = JSON.parse(xhr.responseText)["known-hosts"]; |
| 987 |
if (keys) { |
| 988 |
debug("send_login_request(): got updated known-hosts for changed host keys of", ssh_host_key_change_host, ":", keys); |
| 989 |
set_hostkeys(ssh_host_key_change_host, keys); |
| 990 |
ssh_host_key_change_host = null; |
| 991 |
} else { |
| 992 |
debug("send_login_request():", ssh_host_key_change_host, "changed key, but did not get an updated key from response"); |
| 993 |
} |
| 994 |
} catch (ex) { |
| 995 |
console.error("Failed to parse response text as JSON:", xhr.responseText, ":", JSON.stringify(ex)); |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
if (xhr.statusText.startsWith("captured-stderr:")) { |
| 1000 |
show_captured_stderr(decodeURIComponent(xhr.statusText.replace(/^captured-stderr:/, ''))); |
| 1001 |
} else if (xhr.statusText.indexOf("authentication-not-supported") > -1) { |
| 1002 |
const user = id("login-user-input").value.trim(); |
| 1003 |
fatal(format(_("The server refused to authenticate '$0' using password authentication, and no other supported authentication methods are available."), user)); |
| 1004 |
} else if (xhr.statusText.indexOf("terminated") > -1) { |
| 1005 |
login_failure(_("Authentication failed"), _("Server closed connection")); |
| 1006 |
} else if (xhr.statusText.indexOf("no-host") > -1) { |
| 1007 |
host_failure(_("Unable to connect to that address")); |
| 1008 |
} else if (xhr.statusText.indexOf("unknown-hostkey") > -1) { |
| 1009 |
host_failure(_("Refusing to connect"), _("Hostkey is unknown")); |
| 1010 |
} else if (xhr.statusText.indexOf("unknown-host") > -1) { |
| 1011 |
host_failure(_("Refusing to connect"), _("Host is unknown")); |
| 1012 |
} else if (xhr.statusText.indexOf("invalid-hostkey") > -1) { |
| 1013 |
/* ssh/ferny/beiboot immediately fail in this case, it's not a conversation; |
| 1014 |
* ask the user for confirmation and try again */ |
| 1015 |
if (ssh_host_key_change_host === null) { |
| 1016 |
debug("send_login_request(): invalid-hostkey, trying again to let the user confirm"); |
| 1017 |
ssh_host_key_change_host = login_machine; |
| 1018 |
call_login(); |
| 1019 |
} else { |
| 1020 |
// but only once, to avoid loops |
| 1021 |
debug("send_login_request(): invalid-hostkey, and already retried, giving up"); |
| 1022 |
host_failure(_("Refusing to connect"), _("Hostkey does not match")); |
| 1023 |
} |
| 1024 |
} else if (is_conversation) { |
| 1025 |
login_failure(_("Authentication failed")); |
| 1026 |
} else { |
| 1027 |
login_failure(_("Authentication failed"), _("Wrong user name or password")); |
| 1028 |
} |
| 1029 |
} |
| 1030 |
} else if (xhr.status == 403) { |
| 1031 |
const status = decodeURIComponent(xhr.statusText).trim(); |
| 1032 |
login_failure(_("Permission denied"), status === "Permission denied" ? "" : status); |
| 1033 |
} else if (xhr.status == 500 && xhr.statusText.indexOf("no-cockpit") > -1) { |
| 1034 |
const message = format( |
| 1035 |
_("Install the cockpit-system package (and optionally other cockpit packages) on $0 to enable web console access."), |
| 1036 |
login_machine || "localhost"); |
| 1037 |
|
| 1038 |
login_failure(_("Packageless session unavailable"), message); |
| 1039 |
} else if (xhr.status == 500 && xhr.statusText.indexOf("unsupported-shell") > -1) { |
| 1040 |
login_failure(_("Authentication failed"), |
| 1041 |
_("Unsupported shell. Check the journal for details.")); |
| 1042 |
} else if (xhr.statusText) { |
| 1043 |
fatal(decodeURIComponent(xhr.statusText)); |
| 1044 |
} else { |
| 1045 |
fatal(format(_("$0 error"), xhr.status)); |
| 1046 |
} |
| 1047 |
}; |
| 1048 |
xhr.send(); |
| 1049 |
} |
| 1050 |
|
| 1051 |
function converse(id, msg) { |
| 1052 |
const headers = { |
| 1053 |
Authorization: "X-Conversation " + id + " " + window.btoa(utf8(msg)) |
| 1054 |
}; |
| 1055 |
send_login_request("GET", headers, true); |
| 1056 |
} |
| 1057 |
|
| 1058 |
function login_reload (wanted) { |
| 1059 |
// Force a reload if not triggered below |
| 1060 |
// because only the hash part of the url |
| 1061 |
// changed |
| 1062 |
let timer = window.setTimeout(function() { |
| 1063 |
timer = null; |
| 1064 |
window.location.reload(true); |
| 1065 |
}, 100); |
| 1066 |
|
| 1067 |
if (wanted && wanted != window.location.href) |
| 1068 |
window.location = wanted; |
| 1069 |
|
| 1070 |
// cancel forced reload if we are reloading |
| 1071 |
window.onbeforeunload = function() { |
| 1072 |
if (timer) |
| 1073 |
window.clearTimeout(timer); |
| 1074 |
timer = null; |
| 1075 |
}; |
| 1076 |
} |
| 1077 |
|
| 1078 |
function clear_storage (storage, prefix, full) { |
| 1079 |
let i = 0; |
| 1080 |
while (i < storage.length) { |
| 1081 |
const k = storage.key(i); |
| 1082 |
if (full && k.indexOf("cockpit") !== 0) |
| 1083 |
storage.removeItem(k); |
| 1084 |
else if (k.indexOf(prefix) === 0) |
| 1085 |
storage.removeItem(k); |
| 1086 |
else |
| 1087 |
i++; |
| 1088 |
} |
| 1089 |
} |
| 1090 |
|
| 1091 |
function setup_localstorage (response) { |
| 1092 |
/* Clear anything not prefixed with |
| 1093 |
* different application from sessionStorage |
| 1094 |
*/ |
| 1095 |
clear_storage(window.sessionStorage, application, true); |
| 1096 |
|
| 1097 |
/* Clear anything prefixed with our application |
| 1098 |
* and login-data, but not other non-application values. |
| 1099 |
*/ |
| 1100 |
localStorage.removeItem('login-data'); |
| 1101 |
clear_storage(localStorage, application, false); |
| 1102 |
|
| 1103 |
if (response && response["login-data"]) { |
| 1104 |
const str = JSON.stringify(response["login-data"]); |
| 1105 |
/* login-data is tied to the auth cookie, since |
| 1106 |
* cookies are available after the page |
| 1107 |
* session ends login-data should be too. |
| 1108 |
*/ |
| 1109 |
localStorage.setItem(application + 'login-data', str); |
| 1110 |
/* Backwards compatibility for packages that aren't application prefixed */ |
| 1111 |
localStorage.setItem('login-data', str); |
| 1112 |
|
| 1113 |
/* When confirming a host key with cockpit-beiboot, login-data contains the known_hosts pubkey; |
| 1114 |
* update our database */ |
| 1115 |
if (login_data_host) { |
| 1116 |
const hostkey = response["login-data"]["known-hosts"]; |
| 1117 |
if (hostkey) { |
| 1118 |
debug("setup_localstorage(): updating known_hosts database for deferred host key for", login_data_host, ":", hostkey); |
| 1119 |
set_hostkeys(login_data_host, hostkey); |
| 1120 |
} else { |
| 1121 |
console.error("login.js internal error: setup_localstorage() received a pending login-data host, but login-data does not contain known-hosts"); |
| 1122 |
} |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
/* URL Root is set by cockpit ws and shouldn't be prefixed |
| 1127 |
* by application |
| 1128 |
* deprecated: for connecting to cockpit.js < 272 |
| 1129 |
*/ |
| 1130 |
if (url_root) |
| 1131 |
localStorage.setItem('url-root', url_root); |
| 1132 |
|
| 1133 |
const ca_cert_url = environment.CACertUrl; |
| 1134 |
if (ca_cert_url) |
| 1135 |
window.sessionStorage.setItem('CACertUrl', ca_cert_url); |
| 1136 |
} |
| 1137 |
|
| 1138 |
function run(response) { |
| 1139 |
let wanted = window.sessionStorage.getItem('login-wanted'); |
| 1140 |
const machine = id("server-field").value; |
| 1141 |
|
| 1142 |
/* When using cockpit client remember all the addresses being used */ |
| 1143 |
if (machine && environment.is_cockpit_client) { |
| 1144 |
const hosts = get_recent_hosts(); |
| 1145 |
if (hosts.indexOf(machine) < 0) { |
| 1146 |
hosts.push(machine); |
| 1147 |
localStorage.setItem('cockpit-client-sessions', JSON.stringify(hosts)); |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
if (machine && application != org_application) { |
| 1152 |
wanted = "/=" + machine; |
| 1153 |
if (url_root) |
| 1154 |
wanted = "/" + url_root + wanted; |
| 1155 |
} |
| 1156 |
|
| 1157 |
/* clean up sessionStorage. clear anything that isn't prefixed |
| 1158 |
* with an application and anything prefixed with our application. |
| 1159 |
*/ |
| 1160 |
clear_storage(window.sessionStorage, application, false); |
| 1161 |
|
| 1162 |
setup_localstorage(response); |
| 1163 |
login_reload(wanted); |
| 1164 |
} |
| 1165 |
|
| 1166 |
window.onload = boot; |
| 1167 |
})(); |