diff --git a/src/gui/src/UI/Dashboard/UIAccountDialogs.test.js b/src/gui/src/UI/Dashboard/UIAccountDialogs.test.js index 1f779835b..9fde7b52b 100644 --- a/src/gui/src/UI/Dashboard/UIAccountDialogs.test.js +++ b/src/gui/src/UI/Dashboard/UIAccountDialogs.test.js @@ -478,7 +478,7 @@ describe('disable 2FA', () => { it('sends nothing when the provider sign-in does not finish', async () => { openRevalidatePopup.mockRejectedValueOnce(new Error('closed')); - const result = open({ oidc_only: true }); + const result = open({ oidc_only: true, oidc_revalidate_url: 'https://puter.test/revalidate' }); await submitWith({ code: '000123' }); diff --git a/src/gui/src/UI/Dashboard/UIDashboard2FASetupDialog.test.js b/src/gui/src/UI/Dashboard/UIDashboard2FASetupDialog.test.js index d3ea5c627..eb4334503 100644 --- a/src/gui/src/UI/Dashboard/UIDashboard2FASetupDialog.test.js +++ b/src/gui/src/UI/Dashboard/UIDashboard2FASetupDialog.test.js @@ -204,7 +204,7 @@ describe('2FA setup: confirming identity', () => { it('sends nothing when the provider sign-in does not finish', async () => { openRevalidatePopup.mockRejectedValueOnce(new Error('closed')); - await open({ oidc_only: true }); + await open({ oidc_only: true, oidc_revalidate_url: 'https://puter.test/revalidate' }); action(0).trigger('click'); await settle(); expect(fetch).not.toHaveBeenCalled(); diff --git a/src/gui/src/helpers/postUserProtected.js b/src/gui/src/helpers/postUserProtected.js index 6eff68dfb..e813bb4ef 100644 --- a/src/gui/src/helpers/postUserProtected.js +++ b/src/gui/src/helpers/postUserProtected.js @@ -49,8 +49,8 @@ const CODE_MESSAGES = { * @param {string} route The part after `/user-protected/`. * @param {Record} body The change itself, sent on every attempt. * @param {object} [opts] - * @param {string} [opts.password] Sent on the first attempt only; signing in - * with the provider stands in for it on a retry. + * @param {string} [opts.password] Sent on the first attempt only, and never + * for an account without one: signing in with the provider stands in for it. * @param {() => void} [opts.onRevalidate] Runs just before the sign-in popup opens. * @returns {Promise} */ @@ -74,30 +74,42 @@ export default async function postUserProtected (route, body, { password, onReva return { ok: false, data, message: known ? i18n(known, [], false) : data.message || res.statusText || i18n('error_unknown_cause', [], false) }; }; + /** Resolves to null once signed in, or to the i18n key of why it didn't finish. */ const revalidate = async (url) => { onRevalidate?.(); try { - await openRevalidatePopup(url || window.user?.oidc_revalidate_url); - return true; - } catch { - return false; + await openRevalidatePopup(url); + return null; + } catch ( err ) { + return err?.code === 'popup_blocked' ? 'revalidate_popup_blocked' : 'revalidate_failed'; } }; - const revalidateFailed = () => ({ ok: false, data: {}, message: i18n('revalidate_failed', [], false) }); + const revalidateFailed = (key) => ({ ok: false, data: {}, message: i18n(key, [], false) }); - // A temporary account has no password either, but no linked provider: the - // routes that admit it (deleting it) need no proof of identity at all. - if ( window.user?.oidc_only && ! window.user.is_temp && ! password ) { - if ( ! await revalidate() ) return revalidateFailed(); + // The popup opens straight from the click while the sign-in URL is known; + // without one, the server says where to sign in. A temporary account has + // no password either, but no linked provider: the routes that admit it + // (deleting it) need no proof of identity at all. + const user = window.user; + if ( user?.oidc_only && ! user.is_temp && user.oidc_revalidate_url ) { + const failed = await revalidate(user.oidc_revalidate_url); + if ( failed ) return revalidateFailed(failed); return settle(await post(body)); } - const result = await settle(await post(password ? { ...body, password } : body)); - // `oidc_only` on the cached user can lag the account's real state, so - // the server has the last word on whether a provider sign-in is needed. - if ( result.data.code === 'oidc_revalidation_required' && result.data.revalidate_url ) { - if ( ! await revalidate(result.data.revalidate_url) ) return revalidateFailed(); - return settle(await post(body)); + const sendPassword = password && ! user?.oidc_only; + const result = await settle(await post(sendPassword ? { ...body, password } : body)); + if ( result.data.code !== 'oidc_revalidation_required' ) return result; + // The server answers this only for an account without a password, and + // it has the last word over a cached `oidc_only` that lags. + if ( ! result.data.revalidate_url ) { + return { ...result, message: i18n('revalidate_unavailable', [], false) }; } - return result; + if ( user ) { + user.oidc_only = true; + user.oidc_revalidate_url = result.data.revalidate_url; + } + const failed = await revalidate(result.data.revalidate_url); + if ( failed ) return revalidateFailed(failed); + return settle(await post(body)); } diff --git a/src/gui/src/helpers/postUserProtected.test.js b/src/gui/src/helpers/postUserProtected.test.js index d99cc7201..7a6ab8c51 100644 --- a/src/gui/src/helpers/postUserProtected.test.js +++ b/src/gui/src/helpers/postUserProtected.test.js @@ -113,6 +113,57 @@ describe('postUserProtected', () => { expect(fetch).not.toHaveBeenCalled(); }); + it('asks the server where to sign in when the cached user has no sign-in URL', async () => { + window.user = { oidc_only: true }; + fetch + .mockResolvedValueOnce(reply(403, { code: 'oidc_revalidation_required', revalidate_url: 'https://puter.test/r3' })) + .mockResolvedValueOnce(reply(200, { success: true })); + + const result = await postUserProtected('delete-own-user', {}); + + expect(openRevalidatePopup).toHaveBeenCalledWith('https://puter.test/r3'); + expect(fetch).toHaveBeenCalledTimes(2); + expect(result.ok).toBe(true); + }); + + it('says there is nothing to sign in with when the server offers no sign-in URL', async () => { + window.user = { oidc_only: true }; + fetch.mockResolvedValueOnce(reply(403, { code: 'oidc_revalidation_required', message: 'OIDC revalidation required' })); + + const result = await postUserProtected('change-username', { new_username: 'bob' }); + + expect(openRevalidatePopup).not.toHaveBeenCalled(); + expect(result).toMatchObject({ ok: false, message: 'revalidate_unavailable' }); + }); + + it('says the popup was blocked when the browser blocks it', async () => { + window.user = { oidc_only: true, oidc_revalidate_url: 'https://puter.test/revalidate' }; + openRevalidatePopup.mockRejectedValueOnce(Object.assign(new Error('Popup blocked'), { code: 'popup_blocked' })); + + const result = await postUserProtected('delete-own-user', {}); + + expect(result).toMatchObject({ ok: false, message: 'revalidate_popup_blocked' }); + expect(fetch).not.toHaveBeenCalled(); + }); + + // A popup opened after a round trip can fall outside the click that + // allowed it, so the next try has to open it straight away. + it('opens the popup first on the next try once the server asked for a sign-in', async () => { + fetch.mockResolvedValueOnce(reply(403, { code: 'oidc_revalidation_required', revalidate_url: 'https://puter.test/r4' })); + openRevalidatePopup.mockRejectedValueOnce(Object.assign(new Error('Popup blocked'), { code: 'popup_blocked' })); + + const first = await postUserProtected('change-username', { new_username: 'bob' }, { password: 'pw' }); + expect(first).toMatchObject({ ok: false, message: 'revalidate_popup_blocked' }); + + fetch.mockResolvedValueOnce(reply(200)); + const second = await postUserProtected('change-username', { new_username: 'bob' }, { password: 'pw' }); + + expect(openRevalidatePopup).toHaveBeenLastCalledWith('https://puter.test/r4'); + expect(fetch).toHaveBeenCalledTimes(2); + expect(bodyOf(1)).toEqual({ new_username: 'bob' }); + expect(second.ok).toBe(true); + }); + it('says the current password is wrong in the reader\'s language', async () => { fetch.mockResolvedValueOnce(reply(400, { message: 'Password mismatch', code: 'password_mismatch' })); diff --git a/src/gui/src/i18n/translations/cs.js b/src/gui/src/i18n/translations/cs.js index c7f957946..fbbe91eca 100644 --- a/src/gui/src/i18n/translations/cs.js +++ b/src/gui/src/i18n/translations/cs.js @@ -70,7 +70,6 @@ const cs = { "reauth_required_message": "Vaše relace byla z důvodu zabezpečení aktualizována – přihlaste se prosím znovu.", "close": "Zavřít", "change_email": "Změnit e-mail", - "revalidated": "Znovu ověřeno.", "confirm": "Potvrdit", "revalidate_sign_in_popup": "Ve vyskakovacím okně se přihlaste pomocí svého propojeného účtu.", "confirm_code_generic_incorrect": "Nesprávný kód.", diff --git a/src/gui/src/i18n/translations/en.js b/src/gui/src/i18n/translations/en.js index 500073577..1b59f1f96 100644 --- a/src/gui/src/i18n/translations/en.js +++ b/src/gui/src/i18n/translations/en.js @@ -82,10 +82,11 @@ const en = { change_username: 'Change Username', username_set_by_team: 'Set by {{team}}. Ask an admin to change it.', username_set_by_team_generic: 'Set by your team. Ask an admin to change it.', - revalidated: 'Re-validated.', revalidate_sign_in_popup: 'Sign in with your linked account in the popup.', revalidate_flow_notice: 'You will be asked to sign in with your linked account when you continue.', revalidate_failed: 'Signing in with your linked account didn\u2019t finish. Please try again.', + revalidate_popup_blocked: 'Your browser blocked the sign-in popup. Allow popups for this site and try again.', + revalidate_unavailable: 'Your account has no password or linked sign-in account to confirm it\u2019s you. Sign out and use \u201cForgot password?\u201d to set a password.', reauth_required_message: 'Your session was updated for security — please sign in again.', close: 'Close', close_all_windows_and_log_out: 'Close Windows and Log Out', diff --git a/src/gui/src/util/openid.js b/src/gui/src/util/openid.js index ff838619a..cc05d10b9 100644 --- a/src/gui/src/util/openid.js +++ b/src/gui/src/util/openid.js @@ -55,7 +55,7 @@ export const openRevalidatePopup = async (revalidateUrl) => { const popup = window.open(url, 'puter-revalidate', POPUP_FEATURES); // A blocked popup never closes, so waiting for it would never end. if ( ! popup ) { - throw new Error('Popup blocked'); + throw Object.assign(new Error('Popup blocked'), { code: 'popup_blocked' }); } const onMessage = ev => { if ( (ev.origin !== window.gui_origin) && (ev.origin !== window.location.origin) ) return;