Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3158445
D10884.id36621.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
D10884.id36621.diff
View Options
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
@@ -34,6 +34,7 @@
};
const AUTH_RETRY_DELAY_MS = 60_000;
+const CANCELLED_ERROR = 'cancelled';
function KeyserverConnectionHandler(props: Props) {
const { socketComponent: Socket, keyserverID, ...rest } = props;
@@ -85,38 +86,40 @@
const olmSessionCreator = React.useContext(OlmSessionCreatorContext);
invariant(olmSessionCreator, 'Olm session creator should be set');
- const [canPerformAuth, setCanPerformAuth] = React.useState(true);
- const isUserAuthenticated = useSelector(isLoggedInToKeyserver(keyserverID));
+ const [authInProgress, setAuthInProgress] = React.useState(false);
+ const performAuth = React.useCallback(() => {
+ setAuthInProgress(true);
- React.useEffect(() => {
- if (
- !usingCommServicesAccessToken ||
- !canPerformAuth ||
- isUserAuthenticated
- ) {
- return;
- }
- setCanPerformAuth(false);
+ let cancelled = false;
+ const cancel = () => {
+ cancelled = true;
+ setAuthInProgress(false);
+ };
- void (async () => {
+ const promise = (async () => {
try {
const keyserverKeys =
await identityClient.getKeyserverKeys(keyserverID);
- const [notifsSession, contentSession] = await Promise.all([
- olmSessionCreator.notificationsSessionCreator(
- cookie,
- keyserverKeys.identityKeysBlob.notificationIdentityPublicKeys,
- keyserverKeys.notifInitializationInfo,
- keyserverID,
- ),
- olmSessionCreator.contentSessionCreator(
- keyserverKeys.identityKeysBlob.primaryIdentityPublicKeys,
- keyserverKeys.contentInitializationInfo,
- ),
- ]);
-
- const { userID, deviceID } = await getAuthMetadata();
+ if (cancelled) {
+ throw new Error(CANCELLED_ERROR);
+ }
+
+ const [notifsSession, contentSession, { userID, deviceID }] =
+ await Promise.all([
+ olmSessionCreator.notificationsSessionCreator(
+ cookie,
+ keyserverKeys.identityKeysBlob.notificationIdentityPublicKeys,
+ keyserverKeys.notifInitializationInfo,
+ keyserverID,
+ ),
+ olmSessionCreator.contentSessionCreator(
+ keyserverKeys.identityKeysBlob.primaryIdentityPublicKeys,
+ keyserverKeys.contentInitializationInfo,
+ ),
+ getAuthMetadata(),
+ ]);
+
invariant(userID, 'userID should be set');
invariant(deviceID, 'deviceID should be set');
@@ -124,54 +127,102 @@
? { [keyserverID]: { deviceToken } }
: {};
+ if (cancelled) {
+ throw new Error(CANCELLED_ERROR);
+ }
+
await dispatchActionPromise(
keyserverAuthActionTypes,
- keyserverAuth({
- userID,
- deviceID,
- doNotRegister: false,
- calendarQuery,
- deviceTokenUpdateInput,
- logInActionSource: process.env.BROWSER
- ? logInActionSources.keyserverAuthFromWeb
- : logInActionSources.keyserverAuthFromNative,
- keyserverData: {
- [keyserverID]: {
- initialContentEncryptedMessage: contentSession,
- initialNotificationsEncryptedMessage: notifsSession,
+ (async () => {
+ await keyserverAuth({
+ userID,
+ deviceID,
+ doNotRegister: false,
+ calendarQuery,
+ deviceTokenUpdateInput,
+ logInActionSource: process.env.BROWSER
+ ? logInActionSources.keyserverAuthFromWeb
+ : logInActionSources.keyserverAuthFromNative,
+ keyserverData: {
+ [keyserverID]: {
+ initialContentEncryptedMessage: contentSession,
+ initialNotificationsEncryptedMessage: notifsSession,
+ },
},
- },
- }),
+ });
+ if (cancelled) {
+ throw new Error(CANCELLED_ERROR);
+ }
+ })(),
);
} catch (e) {
+ if (cancelled) {
+ return;
+ }
+
console.log(
`Error while authenticating to keyserver with id ${keyserverID}`,
e,
);
+
if (!dataLoaded && keyserverID === ashoatKeyserverID) {
await dispatchActionPromise(logOutActionTypes, callLogOut());
}
} finally {
- await sleep(AUTH_RETRY_DELAY_MS);
- setCanPerformAuth(true);
+ if (!cancelled) {
+ await sleep(AUTH_RETRY_DELAY_MS);
+ setAuthInProgress(false);
+ }
}
})();
+ return [promise, cancel];
}, [
- keyserverID,
- identityClient,
- olmSessionCreator,
- cookie,
- getAuthMetadata,
- dispatchActionPromise,
- keyserverAuth,
- deviceToken,
calendarQuery,
- isUserAuthenticated,
callLogOut,
+ cookie,
dataLoaded,
- canPerformAuth,
+ deviceToken,
+ dispatchActionPromise,
+ getAuthMetadata,
+ identityClient,
+ keyserverAuth,
+ keyserverID,
+ olmSessionCreator,
]);
+ const cancelPendingAuth = React.useRef<?() => void>(null);
+ const prevPerformAuth = React.useRef(performAuth);
+ const isUserAuthenticated = useSelector(isLoggedInToKeyserver(keyserverID));
+ const hasAccessToken = useSelector(state => !!state.commServicesAccessToken);
+
+ React.useEffect(() => {
+ if (!hasAccessToken) {
+ cancelPendingAuth.current?.();
+ cancelPendingAuth.current = null;
+ }
+
+ if (
+ !usingCommServicesAccessToken ||
+ isUserAuthenticated ||
+ !hasAccessToken
+ ) {
+ return;
+ }
+
+ if (prevPerformAuth.current !== performAuth) {
+ cancelPendingAuth.current?.();
+ cancelPendingAuth.current = null;
+ }
+ prevPerformAuth.current = performAuth;
+
+ if (authInProgress) {
+ return;
+ }
+
+ const [, cancel] = performAuth();
+ cancelPendingAuth.current = cancel;
+ }, [authInProgress, hasAccessToken, isUserAuthenticated, performAuth]);
+
if (keyserverID !== ashoatKeyserverID) {
return null;
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Nov 6, 9:50 PM (21 h, 6 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2432648
Default Alt Text
D10884.id36621.diff (6 KB)
Attached To
Mode
D10884: [lib] Cancel an auth every time dependencies change
Attached
Detach File
Event Timeline
Log In to Comment