Fixes an issue whereas server configuration is not yet set

Summary: ^

Reviewed By: aigoncharov

Differential Revision: D50470060

fbshipit-source-id: cc59ac7cace092addbf48dfa16219983bd129cb0
This commit is contained in:
Lorenzo Blasa
2023-10-19 13:57:36 -07:00
committed by Facebook GitHub Bot
parent 64d97998fd
commit 450e6f2d7c

View File

@@ -286,10 +286,21 @@ const getManifestPath = (config: FlipperServerConfig): string => {
return path.resolve(config.paths.staticPath, manifestFilename); return path.resolve(config.paths.staticPath, manifestFilename);
}; };
const exportTokenToManifest = async ( const exportTokenToManifest = async (token: string) => {
config: FlipperServerConfig, console.info('Export token to manifest');
token: string, 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) {
return;
}
const manifestPath = getManifestPath(config); const manifestPath = getManifestPath(config);
try { try {
const manifestData = await fs.readFile(manifestPath, { const manifestData = await fs.readFile(manifestPath, {
@@ -313,8 +324,6 @@ export const generateAuthToken = async () => {
await ensureServerCertExists(); await ensureServerCertExists();
const config = getFlipperServerConfig();
const privateKey = await fs.readFile(serverKey); const privateKey = await fs.readFile(serverKey);
const token = jwt.sign({unixname: os.userInfo().username}, privateKey, { const token = jwt.sign({unixname: os.userInfo().username}, privateKey, {
algorithm: 'RS256', algorithm: 'RS256',
@@ -323,11 +332,7 @@ export const generateAuthToken = async () => {
await fs.writeFile(serverAuthToken, token); await fs.writeFile(serverAuthToken, token);
console.info('Token generated and saved to disk'); await exportTokenToManifest(token);
if (config.environmentInfo.isHeadlessBuild) {
console.info('Token exported to manifest');
await exportTokenToManifest(config, token);
}
return token; return token;
}; };
@@ -349,25 +354,22 @@ export const getAuthToken = async (): Promise<string> => {
return generateAuthToken(); return generateAuthToken();
} }
const token = await fs.readFile(serverAuthToken); const tokenBuffer = await fs.readFile(serverAuthToken);
const token = tokenBuffer.toString();
try { try {
console.info('Verify authentication token'); console.info('Verify authentication token');
const serverCertificate = await fs.readFile(serverCert); const serverCertificate = await fs.readFile(serverCert);
jwt.verify(token.toString(), serverCertificate); jwt.verify(token, serverCertificate);
console.info('Token verification succeeded'); console.info('Token verification succeeded');
} catch (_) { } catch (_) {
console.warn('Either token has expired or is invalid'); console.warn('Either token has expired or is invalid');
return generateAuthToken(); return generateAuthToken();
} }
const config = getFlipperServerConfig(); await exportTokenToManifest(token);
if (config.environmentInfo.isHeadlessBuild) {
console.info('Token exported to manifest');
await exportTokenToManifest(config, token.toString());
}
return token.toString(); return token;
}; };
export const hasAuthToken = async (): Promise<boolean> => { export const hasAuthToken = async (): Promise<boolean> => {