diff --git a/lib/components/keyserver-connection-handler.js b/lib/components/keyserver-connection-handler.js --- a/lib/components/keyserver-connection-handler.js +++ b/lib/components/keyserver-connection-handler.js @@ -13,6 +13,7 @@ import { setNewSession, CANCELLED_ERROR, + type CallKeyserverEndpoint, } from '../keyserver-conn/keyserver-conn-types.js'; import { resolveKeyserverSessionInvalidation } from '../keyserver-conn/recovery-utils.js'; import { filterThreadIDsInFilterList } from '../reducers/calendar-filters-reducer.js'; @@ -34,6 +35,8 @@ } from '../types/account-types.js'; import { genericCookieInvalidation } from '../types/session-types.js'; import { authoritativeKeyserverID } from '../utils/authoritative-keyserver.js'; +import type { CallSingleKeyserverEndpoint } from '../utils/call-single-keyserver-endpoint.js'; +import { getConfig } from '../utils/config.js'; import { useDispatchActionPromise } from '../utils/redux-promise-utils.js'; import { useSelector, useDispatch } from '../utils/redux-utils.js'; import { usingCommServicesAccessToken } from '../utils/services-utils.js'; @@ -231,6 +234,38 @@ const getInitialNotificationsEncryptedMessageForRecovery = useInitialNotificationsEncryptedMessage(keyserverID); + const { resolveKeyserverSessionInvalidationUsingNativeCredentials } = + getConfig(); + const innerPerformRecovery = React.useCallback( + ( + recoveryActionSource: RecoveryActionSource, + hasBeenCancelled: () => boolean, + ) => + async ( + callSingleKeyserverEndpoint: CallSingleKeyserverEndpoint, + callKeyserverEndpoint: CallKeyserverEndpoint, + ) => { + if (!resolveKeyserverSessionInvalidationUsingNativeCredentials) { + return; + } + await resolveKeyserverSessionInvalidationUsingNativeCredentials( + callSingleKeyserverEndpoint, + callKeyserverEndpoint, + dispatchActionPromise, + recoveryActionSource, + keyserverID, + getInitialNotificationsEncryptedMessageForRecovery, + hasBeenCancelled, + ); + }, + [ + resolveKeyserverSessionInvalidationUsingNativeCredentials, + dispatchActionPromise, + keyserverID, + getInitialNotificationsEncryptedMessageForRecovery, + ], + ); + const dispatch = useDispatch(); const urlPrefix = useSelector(urlPrefixSelector(keyserverID)); const performRecovery = React.useCallback( @@ -255,13 +290,11 @@ const recoverySessionChange = await resolveKeyserverSessionInvalidation( dispatch, - dispatchActionPromise, cookie, urlPrefix, recoveryActionSource, keyserverID, - getInitialNotificationsEncryptedMessageForRecovery, - hasBeenCancelled, + innerPerformRecovery(recoveryActionSource, hasBeenCancelled), ); const sessionChange = recoverySessionChange ?? genericCookieInvalidation; @@ -305,14 +338,7 @@ })(); return [promise, cancel]; }, - [ - dispatch, - dispatchActionPromise, - cookie, - urlPrefix, - keyserverID, - getInitialNotificationsEncryptedMessageForRecovery, - ], + [dispatch, cookie, urlPrefix, keyserverID, innerPerformRecovery], ); const cancelPendingAuth = React.useRef void>(null); diff --git a/lib/keyserver-conn/recovery-utils.js b/lib/keyserver-conn/recovery-utils.js --- a/lib/keyserver-conn/recovery-utils.js +++ b/lib/keyserver-conn/recovery-utils.js @@ -1,16 +1,20 @@ // @flow -import { setNewSession } from './keyserver-conn-types.js'; -import type { InitialNotifMessageOptions } from '../shared/crypto-utils.js'; +import { + setNewSession, + type CallKeyserverEndpoint, +} from './keyserver-conn-types.js'; import type { RecoveryActionSource } from '../types/account-types.js'; import type { Endpoint } from '../types/endpoints.js'; import type { Dispatch } from '../types/redux-types.js'; import type { ClientSessionChange } from '../types/session-types.js'; import callSingleKeyServerEndpoint from '../utils/call-single-keyserver-endpoint.js'; -import type { CallSingleKeyserverEndpointOptions } from '../utils/call-single-keyserver-endpoint.js'; +import type { + CallSingleKeyserverEndpoint, + CallSingleKeyserverEndpointOptions, +} from '../utils/call-single-keyserver-endpoint.js'; import { getConfig } from '../utils/config.js'; import { promiseAll } from '../utils/promises.js'; -import type { DispatchActionPromise } from '../utils/redux-promise-utils.js'; import { usingCommServicesAccessToken } from '../utils/services-utils.js'; // This function is a shortcut that tells us whether it's worth even trying to @@ -38,21 +42,15 @@ // legacy auth responder, which won't work on web and won't work for ETH users. async function resolveKeyserverSessionInvalidation( dispatch: Dispatch, - dispatchActionPromise: DispatchActionPromise, cookie: ?string, urlPrefix: string, recoveryActionSource: RecoveryActionSource, keyserverID: string, - getInitialNotificationsEncryptedMessage: ( - options?: ?InitialNotifMessageOptions, - ) => Promise, - hasBeenCancelled: () => boolean, + actionFunc: ( + callSingleKeyserverEndpoint: CallSingleKeyserverEndpoint, + callKeyserverEndpoint: CallKeyserverEndpoint, + ) => Promise, ): Promise { - const { resolveKeyserverSessionInvalidationUsingNativeCredentials } = - getConfig(); - if (!resolveKeyserverSessionInvalidationUsingNativeCredentials) { - return null; - } let newSessionChange = null; const boundCallSingleKeyserverEndpoint = async ( endpoint: Endpoint, @@ -110,14 +108,9 @@ return Promise.resolve({}); }; - await resolveKeyserverSessionInvalidationUsingNativeCredentials( + await actionFunc( boundCallSingleKeyserverEndpoint, boundCallKeyserverEndpoint, - dispatchActionPromise, - recoveryActionSource, - keyserverID, - getInitialNotificationsEncryptedMessage, - hasBeenCancelled, ); return newSessionChange; } diff --git a/native/data/sqlite-data-handler.js b/native/data/sqlite-data-handler.js --- a/native/data/sqlite-data-handler.js +++ b/native/data/sqlite-data-handler.js @@ -6,6 +6,7 @@ import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions.js'; import { MediaCacheContext } from 'lib/components/media-cache-provider.react.js'; import { useStaffContext } from 'lib/components/staff-provider.react.js'; +import type { CallKeyserverEndpoint } from 'lib/keyserver-conn/keyserver-conn-types.js'; import { resolveKeyserverSessionInvalidation } from 'lib/keyserver-conn/recovery-utils.js'; import { keyserverStoreOpsHandlers } from 'lib/ops/keyserver-store-ops.js'; import { reportStoreOpsHandlers } from 'lib/ops/report-store-ops.js'; @@ -21,10 +22,12 @@ recoveryActionSources, type RecoveryActionSource, } from 'lib/types/account-types.js'; +import type { CallSingleKeyserverEndpoint } from 'lib/utils/call-single-keyserver-endpoint.js'; import { getMessageForException } from 'lib/utils/errors.js'; import { useDispatchActionPromise } from 'lib/utils/redux-promise-utils.js'; import { useDispatch } from 'lib/utils/redux-utils.js'; +import { resolveKeyserverSessionInvalidationUsingNativeCredentials } from '../account/legacy-recover-keyserver-session.js'; import { authoritativeKeyserverID } from '../authoritative-keyserver.js'; import { filesystemMediaCache } from '../media/media-cache.js'; import { commCoreModule } from '../native-modules.js'; @@ -65,18 +68,29 @@ const getInitialNotificationsEncryptedMessage = useInitialNotificationsEncryptedMessage(authoritativeKeyserverID); - const callFetchNewCookieFromNativeCredentials = React.useCallback( + const recoverDataFromAuthoritativeKeyserver = React.useCallback( async (source: RecoveryActionSource) => { + const innerRecoverDataFromAuthoritativeKeyserver = ( + callSingleKeyserverEndpoint: CallSingleKeyserverEndpoint, + callKeyserverEndpoint: CallKeyserverEndpoint, + ) => + resolveKeyserverSessionInvalidationUsingNativeCredentials( + callSingleKeyserverEndpoint, + callKeyserverEndpoint, + dispatchActionPromise, + source, + authoritativeKeyserverID, + getInitialNotificationsEncryptedMessage, + () => false, + ); try { await resolveKeyserverSessionInvalidation( dispatch, - dispatchActionPromise, cookie, urlPrefix, source, authoritativeKeyserverID, - getInitialNotificationsEncryptedMessage, - () => false, + innerRecoverDataFromAuthoritativeKeyserver, ); dispatch({ type: setStoreLoadedActionType }); } catch (fetchCookieException) { @@ -155,7 +169,7 @@ commCoreModule.terminate(); } } - await callFetchNewCookieFromNativeCredentials( + await recoverDataFromAuthoritativeKeyserver( recoveryActionSources.corruptedDatabaseDeletion, ); })(); @@ -218,7 +232,7 @@ '{no exception message}', ); } - await callFetchNewCookieFromNativeCredentials( + await recoverDataFromAuthoritativeKeyserver( recoveryActionSources.sqliteLoadFailure, ); } @@ -234,7 +248,7 @@ storeLoaded, urlPrefix, staffUserHasBeenLoggedIn, - callFetchNewCookieFromNativeCredentials, + recoverDataFromAuthoritativeKeyserver, callClearSensitiveData, mediaCacheContext, ]);