From e14056812dcb95b19c0c795925342c12ba1fb1db Mon Sep 17 00:00:00 2001 From: Ming Date: Thu, 20 Oct 2022 18:44:21 -0400 Subject: [PATCH] Auth fix (#7460) * auth success only if online * fix reconnect * lint/format Co-authored-by: Roshan Padaki --- .../cloud_tabs_extension/src/@core-ts/api.ts | 34 ++++++++++++------- .../src/worker/events/auth.ts | 4 ++- .../src/worker/events/idle.ts | 8 ++++- .../src/worker/events/mandelbox.ts | 11 ++++-- .../src/worker/events/socketio.ts | 3 ++ 5 files changed, 42 insertions(+), 18 deletions(-) diff --git a/browser/hybrid/components/cloud_tabs_extension/src/@core-ts/api.ts b/browser/hybrid/components/cloud_tabs_extension/src/@core-ts/api.ts index 19cfb7fbdce..d9a75135d1f 100644 --- a/browser/hybrid/components/cloud_tabs_extension/src/@core-ts/api.ts +++ b/browser/hybrid/components/cloud_tabs_extension/src/@core-ts/api.ts @@ -9,27 +9,35 @@ import stringify from "json-stringify-safe" const httpConfig = (method: string) => async (args: { body: object; url: string; accessToken?: string }) => { - const response = await fetch(args.url, { - method, - headers: { - "Content-Type": "application/json", - ...(args.accessToken !== undefined && { - Authorization: `Bearer ${args.accessToken}`, - }), - }, - body: stringify(args.body), - }) + let response = null try { - const json = await response.json() + response = await fetch(args.url, { + method, + headers: { + "Content-Type": "application/json", + ...(args.accessToken !== undefined && { + Authorization: `Bearer ${args.accessToken}`, + }), + }, + body: stringify(args.body), + }) + } catch (err) { + return { + status: 500, + json: {}, + } + } + try { + const json = await response.json() return { - status: response.status, + status: response?.status ?? 500, json, } } catch (err) { return { - status: response.status, + status: response?.status ?? 500, json: {}, } } diff --git a/browser/hybrid/components/cloud_tabs_extension/src/worker/events/auth.ts b/browser/hybrid/components/cloud_tabs_extension/src/worker/events/auth.ts index 1d2f3ce9fb3..8750586def8 100644 --- a/browser/hybrid/components/cloud_tabs_extension/src/worker/events/auth.ts +++ b/browser/hybrid/components/cloud_tabs_extension/src/worker/events/auth.ts @@ -3,7 +3,7 @@ import { filter, share, switchMap, take, throttle, map } from "rxjs/operators" import { NodeEventHandler } from "rxjs/internal/observable/fromEvent" import { webpageLoaded } from "@app/worker/events/webRequest" -import { activatedFromSleep } from "@app/worker/events/idle" +import { activatedFromSleep, networkOnline } from "@app/worker/events/idle" import { authSuccess as _authSuccess, authFailure as _authFailure, @@ -43,9 +43,11 @@ const authInfo = merge( merge( webpageLoaded.pipe(take(1)), activatedFromSleep, + networkOnline, timer(0, DAY_MS / 2) ).pipe( // We throttle by 10s so there's no race condition of two simultaneous auth refresh requests + filter(() => navigator.onLine), throttle(() => interval(SEC_MS * 10)), switchMap(() => from(initWhistAuth())) ), diff --git a/browser/hybrid/components/cloud_tabs_extension/src/worker/events/idle.ts b/browser/hybrid/components/cloud_tabs_extension/src/worker/events/idle.ts index d132364c3aa..4d58047d366 100644 --- a/browser/hybrid/components/cloud_tabs_extension/src/worker/events/idle.ts +++ b/browser/hybrid/components/cloud_tabs_extension/src/worker/events/idle.ts @@ -7,4 +7,10 @@ const activatedFromSleep = fromEventPattern( (newState: chrome.idle.IdleState) => newState ).pipe(filter((newState: chrome.idle.IdleState) => newState === "active")) -export { activatedFromSleep } +const networkOnline = fromEventPattern( + (handler: any) => window.addEventListener("online", handler), + (handler: any) => window.removeEventListener("online", handler), + (args: any) => args +) + +export { activatedFromSleep, networkOnline } diff --git a/browser/hybrid/components/cloud_tabs_extension/src/worker/events/mandelbox.ts b/browser/hybrid/components/cloud_tabs_extension/src/worker/events/mandelbox.ts index a523686e8cb..5d38e211763 100644 --- a/browser/hybrid/components/cloud_tabs_extension/src/worker/events/mandelbox.ts +++ b/browser/hybrid/components/cloud_tabs_extension/src/worker/events/mandelbox.ts @@ -1,4 +1,4 @@ -import { from, of } from "rxjs" +import { from, of, merge } from "rxjs" import { switchMap, map, filter, share } from "rxjs/operators" import { getStorage } from "@app/worker/utils/storage" @@ -7,6 +7,7 @@ import { mandelboxCreateSuccess, } from "@app/worker/utils/mandelbox" import { stateDidChange, whistState } from "@app/worker/utils/state" +import { authSuccess } from "./auth" import { MandelboxState, Storage } from "@app/constants/storage" import { config } from "@app/constants/app" @@ -14,8 +15,12 @@ import { AsyncReturnType } from "@app/@types/api" import { AWSRegion, AWSRegionOrdering } from "@app/constants/location" import { AuthInfo } from "@app/@types/payload" -const mandelboxNeeded = stateDidChange("waitingCloudTabs").pipe( - filter((change: any) => change?.applyData?.name === "push"), +const mandelboxNeeded = merge( + stateDidChange("waitingCloudTabs").pipe( + filter((change: any) => change?.applyData?.name === "push") + ), + authSuccess.pipe(filter(() => whistState.waitingCloudTabs.length > 0)) +).pipe( filter( () => whistState.mandelboxState === MandelboxState.MANDELBOX_NONEXISTENT && diff --git a/browser/hybrid/components/cloud_tabs_extension/src/worker/events/socketio.ts b/browser/hybrid/components/cloud_tabs_extension/src/worker/events/socketio.ts index c9ab81c9a14..954012e4c43 100644 --- a/browser/hybrid/components/cloud_tabs_extension/src/worker/events/socketio.ts +++ b/browser/hybrid/components/cloud_tabs_extension/src/worker/events/socketio.ts @@ -53,4 +53,7 @@ const socketReconnectFailed = merge(socket, socketConnected).pipe( ) ) +// I'm not sure why this fixes things but it does +socket.subscribe() + export { socket, socketConnected, socketDisconnected, socketReconnectFailed }