One-time secrets stay on screen through live updates and reconnects
Browse files at this commit4bff0245012c6437ce3801fc9f0313a835ff917aParent: 03ed0d5- fix
- one-time secrets stay on screen through live updates and reconnects
4 changed files +27 -7
- crates/forge-web/assets/nav.js +15 -2
- crates/forge-web/src/pages/admin_runners.rs +1 -1
- crates/forge-web/src/pages/settings_security.rs +5 -2
- crates/forge-web/src/pages/settings_tokens.rs +6 -2
crates/forge-web/assets/nav.jsmodified+15 -2
| @@ -1474,6 +1474,9 @@ | |||
| 1474 | // `force` re-renders a page that holds no stream of its own (a language change, sent by /-/alive). | 1474 | // `force` re-renders a page that holds no stream of its own (a language change, sent by /-/alive). |
| 1475 | function refreshLive(force) { | 1475 | function refreshLive(force) { |
| 1476 | if (!force && !Object.keys(liveStreams).length) { return; } | 1476 | if (!force && !Object.keys(liveStreams).length) { return; } |
| 1477 | + // A one-time secret (a token, recovery codes) is on screen only in the response that made it: a | ||
| 1478 | + // re-render would take it away. The page is refreshed at the next navigation instead. | ||
| 1479 | + if (document.querySelector('[data-once]')) { return; } | ||
| 1477 | if (userBusy > 0 || liveBusy) { liveDirty = true; if (force) { liveForced = true; } return; } | 1480 | if (userBusy > 0 || liveBusy) { liveDirty = true; if (force) { liveForced = true; } return; } |
| 1478 | liveDirty = false; | 1481 | liveDirty = false; |
| 1479 | liveForced = false; | 1482 | liveForced = false; |
| @@ -1957,7 +1960,16 @@ | |||
| 1957 | .then(function () { done(true); }, function () { done(false); }); | 1960 | .then(function () { done(true); }, function () { done(false); }); |
| 1958 | } | 1961 | } |
| 1959 | 1962 | ||
| 1960 | - // `data-copy="<id>"`: copies the text of that element, with the same Copied/Failed feedback. | 1963 | + // Selects an element's text, so the reader can copy it by hand (a long press on a phone). |
| 1964 | + function selectText(el) { | ||
| 1965 | + var range = document.createRange(); | ||
| 1966 | + range.selectNodeContents(el); | ||
| 1967 | + var sel = window.getSelection(); | ||
| 1968 | + if (sel) { sel.removeAllRanges(); sel.addRange(range); } | ||
| 1969 | + } | ||
| 1970 | + | ||
| 1971 | + // `data-copy="<id>"`: copies the text of that element, with the same Copied/Failed feedback. A | ||
| 1972 | + // `data-lines` element (a list) copies one line per item. When the copy fails the text is selected. | ||
| 1961 | document.addEventListener('click', function (e) { | 1973 | document.addEventListener('click', function (e) { |
| 1962 | var cb = e.target && e.target.closest ? e.target.closest('[data-copy]') : null; | 1974 | var cb = e.target && e.target.closest ? e.target.closest('[data-copy]') : null; |
| 1963 | if (!cb || cb.disabled) { return; } | 1975 | if (!cb || cb.disabled) { return; } |
| @@ -1971,7 +1983,8 @@ | |||
| 1971 | cb.textContent = cb.getAttribute(ok ? 'data-copied' : 'data-failed') || label; | 1983 | cb.textContent = cb.getAttribute(ok ? 'data-copied' : 'data-failed') || label; |
| 1972 | setTimeout(function () { cb.textContent = label; cb.disabled = false; }, 1500); | 1984 | setTimeout(function () { cb.textContent = label; cb.disabled = false; }, 1500); |
| 1973 | }; | 1985 | }; |
| 1974 | - copyText(src.textContent.trim()).then(function () { done(true); }, function () { done(false); }); | 1986 | + var text = cb.hasAttribute('data-lines') ? src.innerText : src.textContent; |
| 1987 | + copyText(text.trim()).then(function () { done(true); }, function () { selectText(src); done(false); }); | ||
| 1975 | }, false); | 1988 | }, false); |
| 1976 | 1989 | ||
| 1977 | document.addEventListener('click', function (e) { | 1990 | document.addEventListener('click', function (e) { |
crates/forge-web/src/pages/admin_runners.rsmodified+1 -1
| @@ -512,7 +512,7 @@ async fn token_view( | |||
| 512 | <p><strong>(g::t(lang, K::CopyTokenNow))</strong></p> | 512 | <p><strong>(g::t(lang, K::CopyTokenNow))</strong></p> |
| 513 | <p>(expiry.as_str())</p> | 513 | <p>(expiry.as_str())</p> |
| 514 | </section> | 514 | </section> |
| 515 | - <section class="card"> | 515 | + <section class="card" data-once=""> |
| 516 | <h2>(g::t(lang, K::Token))</h2> | 516 | <h2>(g::t(lang, K::Token))</h2> |
| 517 | <div class="copy-box"> | 517 | <div class="copy-box"> |
| 518 | <code id="reg-token">(token.as_str())</code> | 518 | <code id="reg-token">(token.as_str())</code> |
crates/forge-web/src/pages/settings_security.rsmodified+5 -2
| @@ -23,6 +23,8 @@ use crate::auth::{ | |||
| 23 | hash_password_async, logout, require_csrf, require_user, two_factor_enabled, unix_now, | 23 | hash_password_async, logout, require_csrf, require_user, two_factor_enabled, unix_now, |
| 24 | verify_password_async, verify_second_factor, | 24 | verify_password_async, verify_second_factor, |
| 25 | }; | 25 | }; |
| 26 | +use crate::i18n::packages::{self as pk, K as PK}; | ||
| 27 | +use crate::i18n::releases_ci::{self as rc, K as RC}; | ||
| 26 | use crate::i18n::{self, Key, Lang, password_too_long, password_too_short, t}; | 28 | use crate::i18n::{self, Key, Lang, password_too_long, password_too_short, t}; |
| 27 | use crate::pages::passkeys::{PasskeyPanel, build_panel, passkeys_panel, s}; | 29 | use crate::pages::passkeys::{PasskeyPanel, build_panel, passkeys_panel, s}; |
| 28 | use crate::state::AppState; | 30 | use crate::state::AppState; |
| @@ -244,14 +246,15 @@ async fn security_body(page: SecurityPage) -> Result<impl View> { | |||
| 244 | </section> | 246 | </section> |
| 245 | } | 247 | } |
| 246 | if let Some(list) = codes { | 248 | if let Some(list) = codes { |
| 247 | - <section class="card"> | 249 | + <section class="card" data-once=""> |
| 248 | <h2>(t(lang, Key::RecoveryCodes))</h2> | 250 | <h2>(t(lang, Key::RecoveryCodes))</h2> |
| 249 | <p>(t(lang, Key::RecoveryCodesSave))</p> | 251 | <p>(t(lang, Key::RecoveryCodesSave))</p> |
| 250 | - <ul class="item-list"> | 252 | + <ul class="item-list" id="recovery-codes"> |
| 251 | for code in list.into_iter() { | 253 | for code in list.into_iter() { |
| 252 | <li><code>(code)</code></li> | 254 | <li><code>(code)</code></li> |
| 253 | } | 255 | } |
| 254 | </ul> | 256 | </ul> |
| 257 | + <p><button type="button" data-copy="recovery-codes" data-lines="" data-copied=(rc::t(lang, RC::CopyDone)) data-failed=(rc::t(lang, RC::CopyFailed))>(pk::t(lang, PK::CopyCommand))</button></p> | ||
| 255 | </section> | 258 | </section> |
| 256 | } | 259 | } |
| 257 | <section class="card security-card"> | 260 | <section class="card security-card"> |
crates/forge-web/src/pages/settings_tokens.rsmodified+6 -2
| @@ -20,6 +20,7 @@ use crate::api::packages::PACKAGES_SCOPE; | |||
| 20 | use crate::audit::{self, Actor, Target}; | 20 | use crate::audit::{self, Actor, Target}; |
| 21 | use crate::auth::{require_csrf, require_user}; | 21 | use crate::auth::{require_csrf, require_user}; |
| 22 | use crate::i18n::packages::{self as pk, K as PK}; | 22 | use crate::i18n::packages::{self as pk, K as PK}; |
| 23 | +use crate::i18n::releases_ci::{self as rc, K as RC}; | ||
| 23 | use crate::i18n::{self, Key, Lang, t, token_meta, token_name_length}; | 24 | use crate::i18n::{self, Key, Lang, t, token_meta, token_name_length}; |
| 24 | use crate::state::AppState; | 25 | use crate::state::AppState; |
| 25 | use crate::views::{alert, shell}; | 26 | use crate::views::{alert, shell}; |
| @@ -73,10 +74,13 @@ async fn tokens_view( | |||
| 73 | <h1>(t(lang, Key::AccessTokens))</h1> | 74 | <h1>(t(lang, Key::AccessTokens))</h1> |
| 74 | </section> | 75 | </section> |
| 75 | if let Some(token) = created { | 76 | if let Some(token) = created { |
| 76 | - <section class="card notice" role="status"> | 77 | + <section class="card notice" role="status" data-once=""> |
| 77 | <h2>(t(lang, Key::YourNewToken))</h2> | 78 | <h2>(t(lang, Key::YourNewToken))</h2> |
| 78 | <p>(t(lang, Key::CopyNow))</p> | 79 | <p>(t(lang, Key::CopyNow))</p> |
| 79 | - <pre class="code"><code>(token)</code></pre> | 80 | + <div class="copy-box"> |
| 81 | + <code id="new-token">(token)</code> | ||
| 82 | + <button type="button" data-copy="new-token" data-copied=(rc::t(lang, RC::CopyDone)) data-failed=(rc::t(lang, RC::CopyFailed))>(pk::t(lang, PK::CopyCommand))</button> | ||
| 83 | + </div> | ||
| 80 | <p class="muted small">(t(lang, Key::UseAsPassword))</p> | 84 | <p class="muted small">(t(lang, Key::UseAsPassword))</p> |
| 81 | </section> | 85 | </section> |
| 82 | } | 86 | } |