Skip to content

Commit

Permalink
Improved emulator website auth
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Feb 17, 2024
1 parent 8a7c763 commit fd36b69
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 44 deletions.
23 changes: 23 additions & 0 deletions src/tooling/piral-cli/src/common/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Stream } from 'stream';
import { tmpdir } from 'os';
import { createWriteStream } from 'fs';
import { log } from './log';
import { config } from './config';
import { standardHeaders } from './info';
import { getTokenInteractively } from './interactive';
import { axios, FormData } from '../external';
Expand Down Expand Up @@ -37,6 +38,28 @@ function streamToFile(source: Stream, target: string) {
});
}

export function getAxiosOptions(url: string) {
const auth = config.auth?.[url];

switch (auth?.mode) {
case 'header':
return {
headers: {
[auth.key]: auth.value,
},
};
case 'http':
return {
auth: {
username: auth.username,
password: auth.password,
},
};
default:
return {};
}
}

export function downloadFile(target: string, ca?: Buffer): Promise<Array<string>> {
const httpsAgent = ca ? new Agent({ ca }) : undefined;
return axios.default
Expand Down
26 changes: 5 additions & 21 deletions src/tooling/piral-cli/src/common/website.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join, relative, resolve } from 'path';
import { createPiralStubIndexIfNotExists } from './template';
import { config } from './config';
import { getAxiosOptions } from './http';
import { packageJson } from './constants';
import { ForceOverwrite } from './enums';
import { createDirectory, readJson, writeBinary } from './io';
Expand All @@ -10,34 +10,18 @@ import { axios } from '../external';
import { EmulatorWebsiteManifestFiles, EmulatorWebsiteManifest } from '../types';

function requestManifest(url: string) {
const auth = config.auth?.[url];

switch (auth?.mode) {
case 'header':
return axios.default.get(url, {
headers: {
[auth.key]: auth.value,
},
});
case 'http':
return axios.default.get(url, {
auth: {
username: auth.username,
password: auth.password,
},
});
default:
return axios.default.get(url);
}
const opts = getAxiosOptions(url);
return axios.default.get(url, opts);
}

async function downloadEmulatorFiles(manifestUrl: string, target: string, files: EmulatorWebsiteManifestFiles) {
const requiredFiles = [files.typings, files.main, files.app];
const opts = getAxiosOptions(manifestUrl);

await Promise.all(
requiredFiles.map(async (file) => {
const url = new URL(file, manifestUrl);
const res = await axios.default.get(url.href, { responseType: 'arraybuffer' });
const res = await axios.default.get(url.href, { ...opts, responseType: 'arraybuffer' });
const data: Buffer = res.data;
await writeBinary(target, file, data);
}),
Expand Down
29 changes: 6 additions & 23 deletions src/tooling/piral-cli/src/injectors/pilet-injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { existsSync } from 'fs';
import { readFile, stat, writeFile } from 'fs/promises';
import { KrasInjector, KrasRequest, KrasInjectorConfig, KrasConfiguration, KrasResult } from 'kras';
import { log } from '../common/log';
import { getAxiosOptions } from '../common/http';
import { getPiletSpecMeta } from '../common/spec';
import { config as commonConfig } from '../common/config';
import { axios, mime, jju } from '../external';
Expand Down Expand Up @@ -146,7 +147,7 @@ export default class PiletInjector implements KrasInjector {
if (existsSync(path)) {
try {
const packageJson = require(path);

if (typeof packageJson.piralCLI.source === 'string') {
this.proxyInfo = {
source: packageJson.piralCLI.source,
Expand Down Expand Up @@ -343,28 +344,10 @@ export default class PiletInjector implements KrasInjector {
}

private download(path: string) {
const url = new URL(path, this.proxyInfo.source);
const auth = commonConfig.auth?.[this.proxyInfo.source];

switch (auth?.mode) {
case 'header':
return axios.default.get(url.href, {
responseType: 'arraybuffer',
headers: {
[auth.key]: auth.value,
},
});
case 'http':
return axios.default.get(url.href, {
responseType: 'arraybuffer',
auth: {
username: auth.username,
password: auth.password,
},
});
default:
return axios.default.get(url.href, { responseType: 'arraybuffer' });
}
const manifestUrl = this.proxyInfo.source;
const url = new URL(path, manifestUrl);
const opts = getAxiosOptions(manifestUrl);
return axios.default.get(url.href, { ...opts, responseType: 'arraybuffer' });
}

private async shouldLoad(target: string, path: string) {
Expand Down

0 comments on commit fd36b69

Please sign in to comment.