Skip to content

Commit

Permalink
#4917 fix: initialize external service at init (#4927)
Browse files Browse the repository at this point in the history
* fix: initialize external servie at init

* fix: account server init
  • Loading branch information
ioanmo226 committed Feb 1, 2023
1 parent cece7da commit d5ee302
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
1 change: 1 addition & 0 deletions extension/chrome/elements/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export class ComposeView extends View {
this.renderModule = new ComposeRenderModule(this);
this.myPubkeyModule = new ComposeMyPubkeyModule(this);
this.storageModule = new ComposeStorageModule(this);
await this.acctServer.initialize();
if (!this.isReplyBox) {
await Assert.abortAndRenderErrOnUnprotectedKey(this.acctEmail);
}
Expand Down
1 change: 1 addition & 0 deletions extension/chrome/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ View.run(
}
this.tabId = await BrowserMsg.requiredTabId();
this.notifications = new Notifications();
await this.acctServer?.initialize();
if (this.acctEmail) {
this.clientConfiguration = await ClientConfiguration.newInstance(this.acctEmail);
}
Expand Down
1 change: 1 addition & 0 deletions extension/chrome/settings/modules/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ View.run(
}

public render = async () => {
await this.acctServer.initialize();
await initPassphraseToggle(['passphrase_entry']);
this.prvs = await KeyStoreUtil.parse(await KeyStore.getRequired(this.acctEmail));
const storage = await AcctStore.get(this.acctEmail, ['hide_message_password', 'outgoing_language']);
Expand Down
38 changes: 18 additions & 20 deletions extension/js/common/api/account-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,27 @@ export type UploadedMessageData = {
* whether FES is deployed on the customer domain or not.
*/
export class AccountServer extends Api {
private readonly potentialCustomerUrlFes: ExternalService;
private readonly sharedTenantFes: ExternalService;
private readonly externalService: ExternalService;

public constructor(private acctEmail: string) {
super();
this.potentialCustomerUrlFes = new ExternalService(this.acctEmail);
this.sharedTenantFes = new ExternalService(this.acctEmail);
this.sharedTenantFes.url = SHARED_TENANT_API_HOST;
this.externalService = new ExternalService(this.acctEmail);
}

public static init = async (acctEmail: string) => {
const acctServer = new AccountServer(acctEmail);
await acctServer.initialize();
return acctServer;
};

public initialize = async () => {
if (!(await isCustomerUrlFesUsed(this.acctEmail))) {
this.externalService.url = SHARED_TENANT_API_HOST;
}
};

public fetchAndSaveClientConfiguration = async (): Promise<ClientConfigurationJson> => {
const service = await this.getExternalService();
return await service.fetchAndSaveClientConfiguration();
return await this.externalService.fetchAndSaveClientConfiguration();
};

public getWebPortalMessageExpireDays = async (): Promise<number> => {
Expand All @@ -46,24 +54,14 @@ export class AccountServer extends Api {
recipients: ParsedRecipients,
progressCb: ProgressCb
): Promise<UploadedMessageData> => {
const service = await this.getExternalService();
return await service.webPortalMessageUpload(encrypted, replyToken, from, recipients, progressCb);
return await this.externalService.webPortalMessageUpload(encrypted, replyToken, from, recipients, progressCb);
};

public messageGatewayUpdate = async (externalId: string, emailGatewayMessageId: string) => {
const service = await this.getExternalService();
return await service.messageGatewayUpdate(externalId, emailGatewayMessageId);
return await this.externalService.messageGatewayUpdate(externalId, emailGatewayMessageId);
};

public messageToken = async (): Promise<{ replyToken: string }> => {
const service = await this.getExternalService();
return await service.webPortalMessageNewReplyToken();
};

private getExternalService = async (): Promise<ExternalService> => {
if (await isCustomerUrlFesUsed(this.acctEmail)) {
return this.potentialCustomerUrlFes;
}
return this.sharedTenantFes;
return await this.externalService.webPortalMessageNewReplyToken();
};
}
3 changes: 1 addition & 2 deletions extension/js/common/api/email-provider/gmail/google-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,8 @@ export class GoogleAuth {
if (await potentialFes.isFesInstalledAndAvailable()) {
await AcctStore.set(authRes.acctEmail, { fesUrl: potentialFes.url });
}
const acctServer = new AccountServer(authRes.acctEmail);
// fetch and store ClientConfiguration (not authenticated)
await acctServer.fetchAndSaveClientConfiguration();
await (await AccountServer.init(authRes.acctEmail)).fetchAndSaveClientConfiguration();
} catch (e) {
if (GoogleAuth.isFesUnreachableErr(e, authRes.acctEmail)) {
const error = `Cannot reach your company's FlowCrypt External Service (FES). Contact your Help Desk when unsure. (${String(e)})`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export const contentScriptSetupIfVacant = async (webmailSpecific: WebmailSpecifi

const updateClientConfiguration = async (acctEmail: string) => {
try {
await new AccountServer(acctEmail).fetchAndSaveClientConfiguration();
await (await AccountServer.init(acctEmail)).fetchAndSaveClientConfiguration();
} catch (e) {
if (e instanceof BackendAuthErr) {
// user will see a prompt to log in during some other actions that involve backend
Expand Down

0 comments on commit d5ee302

Please sign in to comment.