Embed auth token into HTML

Summary:
Auth token used be injected in the manifest file. Instead, have the server injected into the main HTML page.

The main driver to this change are:
- Simplify
- There are instances in which for some reason reading/writing the token from the manifest fails. This will address that problem.

Reviewed By: lblasa

Differential Revision: D51160521

fbshipit-source-id: 4626fd8f56bc8b61182a53a5d9cf5acad1e723bc
This commit is contained in:
Andrey Goncharov
2023-11-09 14:05:43 -08:00
committed by Facebook GitHub Bot
parent 69378c4b09
commit 8ef29c8160
6 changed files with 25 additions and 69 deletions

View File

@@ -16,11 +16,10 @@ import {
} from './openssl-wrapper-with-promises';
import path from 'path';
import tmp, {FileOptions} from 'tmp';
import {FlipperServerConfig, reportPlatformFailures} from 'flipper-common';
import {reportPlatformFailures} from 'flipper-common';
import {isTest} from 'flipper-common';
import {flipperDataFolder} from '../../utils/paths';
import * as jwt from 'jsonwebtoken';
import {getFlipperServerConfig} from '../../FlipperServerConfig';
import {Mutex} from 'async-mutex';
import {createSecureContext} from 'tls';
@@ -288,52 +287,6 @@ const writeToTempFile = async (content: string): Promise<string> => {
await fs.writeFile(path, content);
return path;
};
const manifestFilename = 'manifest.json';
const getManifestPath = (config: FlipperServerConfig): string => {
return path.resolve(config.paths.staticPath, manifestFilename);
};
const exportTokenToManifest = async (token: string) => {
console.info('Export token to manifest');
let config: FlipperServerConfig | undefined;
try {
config = getFlipperServerConfig();
} catch {
console.warn(
'Unable to obtain server configuration whilst exporting token to manifest',
);
}
if (!config || !config.environmentInfo.isHeadlessBuild) {
console.warn(
'No configuration or not headless build detected, skipping exporting token to manifest',
config,
);
return;
}
const manifestPath = getManifestPath(config);
try {
console.info('Reading manifest at path', manifestPath);
const manifestData = await fs.readFile(manifestPath, {
encoding: 'utf-8',
});
const manifest = JSON.parse(manifestData);
manifest.token = token;
const newManifestData = JSON.stringify(manifest, null, 4);
console.info('Export token to manifest at path', manifestPath);
await fs.writeFile(manifestPath, newManifestData);
} catch (e) {
console.error(
'Unable to export authentication token to manifest, may be non existent.',
e,
);
}
};
export const generateAuthToken = async () => {
console.info('Generate client authentication token');
@@ -347,8 +300,6 @@ export const generateAuthToken = async () => {
await fs.writeFile(serverAuthToken, token);
await exportTokenToManifest(token);
return token;
};
@@ -382,8 +333,6 @@ export const getAuthToken = async (): Promise<string> => {
return generateAuthToken();
}
await exportTokenToManifest(token);
return token;
};