A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(app): ask for 2FA and show pending approval after SSO
Hampusa00add3ec0d4
diff --git a/fluxer_app/src/features/auth/components/pages/SsoCallbackPage.tsx b/fluxer_app/src/features/auth/components/pages/SsoCallbackPage.tsxindex cc290963a..6d6b3d614 100644--- a/fluxer_app/src/features/auth/components/pages/SsoCallbackPage.tsx+++ b/fluxer_app/src/features/auth/components/pages/SsoCallbackPage.tsx@@ -1,11 +1,19 @@// SPDX-License-Identifier: AGPL-3.0-or-later+import {Routes} from '@app/app/Routes';+import type {RuntimeConfigSnapshot} from '@app/features/app/state/InstanceSnapshotStore';import RuntimeConfig from '@app/features/app/state/RuntimeConfig';import * as AuthenticationCommands from '@app/features/auth/commands/AuthenticationCommands';import styles from '@app/features/auth/components/pages/LoginPage.module.css';import {useAuthSingleUseRequest} from '@app/features/auth/flow/AuthSingleUseRequest';+import MfaScreen from '@app/features/auth/flow/MfaScreen';import {useAuthPresentation} from '@app/features/auth/flow/useAuthPresentation';-import {completeSsoLogin, startSsoLogin} from '@app/features/auth/state/AuthFlow';+import {+ completeSsoLogin,+ type LoginSuccessPayload,+ type MfaChallenge,+ startSsoLogin,+} from '@app/features/auth/state/AuthFlow';import {AuthCardVariant} from '@app/features/auth/state/AuthLayoutContext';import {type AuthRuntimeTarget, useAuthRuntimeTarget} from '@app/features/auth/state/AuthRuntimeTarget';import {@@ -20,11 +28,13 @@ import {TRY_AGAIN_DESCRIPTOR,} from '@app/features/i18n/utils/CommonMessageDescriptors';import * as RouterUtils from '@app/features/navigation/utils/RouterUtils';+import {failureCode} from '@app/features/platform/utils/ResponseInspection';import {Spinner} from '@app/features/ui/components/Spinner';import {navigateToExternalURL} from '@app/features/ui/utils/NativeUtils';import {useFluxerDocumentTitle} from '@app/features/window/hooks/useFluxerDocumentTitle';import * as FormUtils from '@app/lib/forms';import {flxElementClassName} from '@app/lib/react';+import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';import {SSO_MOBILE_CALLBACK_URI, SSO_MOBILE_STATE_PREFIX} from '@fluxer/constants/src/SsoConstants';import type {I18n} from '@lingui/core';import {msg} from '@lingui/core/macro';@@ -32,6 +42,17 @@ import {Trans, useLingui} from '@lingui/react/macro';import {observer} from 'mobx-react-lite';import {useCallback, useState} from 'react';+interface SsoCallbackError {+ readonly message: string;+ readonly awaitingApproval: boolean;+}++interface SsoCallbackMfa {+ readonly challenge: MfaChallenge;+ readonly redirectTo: string;+ readonly runtimeSnapshot: RuntimeConfigSnapshot;+}+const SSO_SIGN_IN_TIMED_OUT_PLEASE_TRY_AGAIN_DESCRIPTOR = msg({message: 'SSO sign-in timed out. Try again.',comment: 'SSO callback page error shown when the SSO sign-in timed out before the callback arrived.',@@ -60,7 +81,8 @@ interface SsoCallbackAttemptDependencies {callbackParameters: SsoCallbackParameters | null;i18n: I18n;isCurrent: () => boolean;- onError: (error: string) => void;+ onError: (error: SsoCallbackError) => void;+ onRequireMfa: (mfa: SsoCallbackMfa) => void;onProcessingChange: (isProcessing: boolean) => void;providerError: string | null;providerErrorDescription: string | null;@@ -78,8 +100,16 @@ function pendingRedirectTo(context: SsoPendingContext | null): string | null {return context?.redirectTo ?? null;}-function resolveSsoRedirectTarget(payloadRedirect: string | undefined, context: SsoPendingContext | null): string {- return safeRedirectTarget(payloadRedirect) ?? safeRedirectTarget(pendingRedirectTo(context)) ?? '/';+function resolveSsoRedirectTarget(+ payloadRedirect: string | undefined,+ context: SsoPendingContext | null,+ runtimeSnapshot: RuntimeConfigSnapshot,+): string {+ const target = safeRedirectTarget(payloadRedirect) ?? safeRedirectTarget(pendingRedirectTo(context)) ?? Routes.ME;+ const singleCommunityGuildId = runtimeSnapshot.community.single_community+ ? runtimeSnapshot.community.single_community_guild_id+ : null;+ return target === Routes.ME && singleCommunityGuildId ? Routes.guildChannel(singleCommunityGuildId) : target;}class SsoCallbackAttempt {@@ -122,16 +152,30 @@ class SsoCallbackAttempt {return;}this.stopTimeout();- const redirectTo = resolveSsoRedirectTarget(result.redirect_to, this.dependencies.callbackContext);- await AuthenticationCommands.completeLogin({- ...result,+ if (result.type === 'mfa') {+ this.settled = true;+ AuthenticationCommands.setMfaTicket({...result.challenge, runtimeSnapshot});+ this.dependencies.onRequireMfa({+ challenge: result.challenge,+ redirectTo: resolveSsoRedirectTarget(result.redirectTo, this.dependencies.callbackContext, runtimeSnapshot),+ runtimeSnapshot,+ });+ return;+ }+ const redirectTo = resolveSsoRedirectTarget(+ result.payload.redirect_to,+ this.dependencies.callbackContext,runtimeSnapshot,- });+ );+ await AuthenticationCommands.completeLogin(+ {+ ...result.payload,+ runtimeSnapshot,+ },+ {redirectPath: redirectTo},+ );this.dependencies.runtimeTarget.reset();this.settled = true;- if (this.dependencies.isCurrent()) {- RouterUtils.replaceWith(redirectTo);- }}private handleTimeout(): void {@@ -149,10 +193,10 @@ class SsoCallbackAttempt {error != null && typeof error === 'object' && 'body' in error? FormUtils.extractErrorMessage(this.dependencies.i18n, error): this.dependencies.i18n._(FAILED_TO_COMPLETE_SSO_SIGN_IN_DESCRIPTOR);- this.finishWithError(message);+ this.finishWithError(message, failureCode(error) === APIErrorCodes.REGISTRATION_PENDING_APPROVAL);}- private finishWithError(error: string): void {+ private finishWithError(message: string, awaitingApproval = false): void {if (this.settled) {return;}@@ -161,7 +205,7 @@ class SsoCallbackAttempt {if (!this.dependencies.isCurrent()) {return;}- this.dependencies.onError(error);+ this.dependencies.onError({message, awaitingApproval});this.dependencies.onProcessingChange(false);}@@ -198,9 +242,10 @@ const SsoCallbackPage = observer(function SsoCallbackPage() {const mobileCallbackUrl = state?.startsWith(SSO_MOBILE_STATE_PREFIX)? `${SSO_MOBILE_CALLBACK_URI}${window.location.search}`: null;- const [error, setError] = useState<string | null>(null);+ const [error, setError] = useState<SsoCallbackError | null>(null);const [isProcessing, setIsProcessing] = useState(true);const [pendingContext, setPendingContext] = useState<SsoPendingContext | null>(null);+ const [mfa, setMfa] = useState<SsoCallbackMfa | null>(null);const handleBackToLogin = useCallback(() => {if (state != null && state.length > 0) {consumeSsoPendingContext(state);@@ -231,6 +276,25 @@ const SsoCallbackPage = observer(function SsoCallbackPage() {RouterUtils.replaceWith('/login');}}, [pendingContext, runtimeTarget]);+ const handleMfaSuccess = useCallback(+ async (payload: LoginSuccessPayload) => {+ if (mfa == null) {+ return;+ }+ await AuthenticationCommands.completeLogin(+ {...payload, runtimeSnapshot: mfa.runtimeSnapshot},+ {redirectPath: mfa.redirectTo},+ );+ AuthenticationCommands.clearMfaTicket();+ runtimeTarget.reset();+ },+ [mfa, runtimeTarget],+ );+ const handleMfaCancel = useCallback(() => {+ AuthenticationCommands.clearMfaTicket();+ setMfa(null);+ handleBackToLogin();+ }, [handleBackToLogin]);useAuthSingleUseRequest(`${code ?? ''}:${state ?? ''}`, (request) => {if (mobileCallbackUrl != null) {window.location.replace(mobileCallbackUrl);@@ -244,6 +308,7 @@ const SsoCallbackPage = observer(function SsoCallbackPage() {i18n,isCurrent: () => request.isCurrent(),onError: setError,+ onRequireMfa: setMfa,onProcessingChange: setIsProcessing,providerError,providerErrorDescription,@@ -261,7 +326,7 @@ const SsoCallbackPage = observer(function SsoCallbackPage() {<Trans>Completing sign-in…</Trans></h1><p className={styles.ssoProcessingHint} data-flx="auth.sso-callback-page.sso-processing-hint--mobile">- <Trans>Jump straight to the app to continue.</Trans>+ <Trans>Continue in the desktop app.</Trans></p><flx-auth-sso-callback-page-actionsclassName={flxElementClassName(styles.ssoCallbackActions)}@@ -278,6 +343,16 @@ const SsoCallbackPage = observer(function SsoCallbackPage() {</flx-auth-sso-callback-page>);}+ if (mfa != null) {+ return (+ <MfaScreen+ challenge={mfa.challenge}+ onSuccess={handleMfaSuccess}+ onCancel={handleMfaCancel}+ data-flx="auth.sso-callback-page.mfa-screen"+ />+ );+ }if (error != null) {return (<flx-auth-sso-callback-page@@ -285,27 +360,35 @@ const SsoCallbackPage = observer(function SsoCallbackPage() {data-flx="auth.sso-callback-page.login-container"><h1 className={styles.title} data-flx="auth.sso-callback-page.title">- <Trans>SSO sign-in failed</Trans>+ {error.awaitingApproval ? (+ <Trans comment="Heading on the SSO callback page when the new account is waiting for an admin to approve it.">+ Waiting for approval+ </Trans>+ ) : (+ <Trans>SSO sign-in failed</Trans>+ )}</h1><flx-auth-sso-callback-page-noticearia-live="assertive"className={flxElementClassName(styles.loginNotice)}data-flx="auth.sso-callback-page.login-notice">- {error}+ {error.message}</flx-auth-sso-callback-page-notice><flx-auth-sso-callback-page-actionsclassName={flxElementClassName(styles.ssoCallbackActions)}data-flx="auth.sso-callback-page.sso-callback-actions">- <button- type="button"- onClick={handleRetry}- className={styles.ssoRetryButton}- data-flx="auth.sso-callback-page.sso-retry-button"- >- {i18n._(TRY_AGAIN_DESCRIPTOR)}- </button>+ {error.awaitingApproval ? null : (+ <button+ type="button"+ onClick={handleRetry}+ className={styles.ssoRetryButton}+ data-flx="auth.sso-callback-page.sso-retry-button"+ >+ {i18n._(TRY_AGAIN_DESCRIPTOR)}+ </button>+ )}<buttontype="button"onClick={handleBackToLogin}diff --git a/fluxer_app/src/features/auth/state/AuthFlow.ts b/fluxer_app/src/features/auth/state/AuthFlow.tsindex 7a53379d4..8adda8750 100644--- a/fluxer_app/src/features/auth/state/AuthFlow.ts+++ b/fluxer_app/src/features/auth/state/AuthFlow.ts@@ -209,6 +209,10 @@ export async function startSsoLogin({return {authorizationUrl: result.authorization_url, redirectUri: result.redirect_uri, state};}+export type SsoLoginResult =+ | {type: 'success'; payload: LoginSuccessPayload}+ | {type: 'mfa'; challenge: MfaChallenge; redirectTo: string};+export async function completeSsoLogin({code,state,@@ -217,15 +221,21 @@ export async function completeSsoLogin({code: string;state: string;runtimeSnapshot: RuntimeConfigSnapshot;-}): Promise<LoginSuccessPayload> {+}): Promise<SsoLoginResult> {const result = await AuthenticationCommands.completeSso({code,state,target: instanceTargetFromSnapshot(runtimeSnapshot),});+ if ('mfa' in result) {+ return {type: 'mfa', challenge: mfaChallengeFromResponse(result), redirectTo: result.redirect_to};+ }return {- ...toLoginSuccessPayload(result),- redirect_to: result.redirect_to,+ type: 'success',+ payload: {+ ...toLoginSuccessPayload(result),+ redirect_to: result.redirect_to,+ },};}