Skip to content

Commit

Permalink
Added authentication config
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Dec 4, 2023
1 parent 4d8d36a commit 4ddcf60
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
7 changes: 6 additions & 1 deletion src/tooling/piral-cli/src/common/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defaultRegistry } from './constants';
import { rc } from '../external';
import { SourceLanguage, NpmClientType, PiletSchemaVersion } from '../types';
import { AuthConfig, SourceLanguage, NpmClientType, PiletSchemaVersion } from '../types';

export interface PiralCliConfig {
/**
Expand All @@ -12,6 +12,10 @@ export interface PiralCliConfig {
* Feed URL to API key specifications.
*/
apiKeys?: Record<string, string>;
/**
* Emulator URL to auth options mapping.
*/
auth?: Record<string, AuthConfig>;
/**
* URL to be used for publishing a pilet in case
* there is no specialized key in url specified.
Expand Down Expand Up @@ -69,6 +73,7 @@ export const config: PiralCliConfig = rc(
{
apiKey: undefined,
apiKeys: {},
auth: {},
url: undefined,
cert: undefined,
npmClient: 'npm' as const,
Expand Down
27 changes: 25 additions & 2 deletions src/tooling/piral-cli/src/common/website.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { join, relative, resolve } from 'path';
import { createPiralStubIndexIfNotExists } from './template';
import { config } from './config';
import { packageJson } from './constants';
import { ForceOverwrite } from './enums';
import { createDirectory, readJson, writeBinary } from './io';
Expand All @@ -8,6 +9,28 @@ import { progress, log } from './log';
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);
}
}

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

Expand Down Expand Up @@ -69,7 +92,7 @@ export async function updateFromEmulatorWebsite(targetDir: string, manifestUrl:
progress(`Updating emulator from %s ...`, manifestUrl);

try {
const response = await axios.default.get(manifestUrl);
const response = await requestManifest(manifestUrl);
const nextEmulator: EmulatorWebsiteManifest = response.data;
const currentEmulator = await readJson(targetDir, packageJson);

Expand All @@ -90,7 +113,7 @@ export async function updateFromEmulatorWebsite(targetDir: string, manifestUrl:

export async function scaffoldFromEmulatorWebsite(rootDir: string, manifestUrl: string) {
progress(`Downloading emulator from %s ...`, manifestUrl);
const response = await axios.default.get(manifestUrl);
const response = await requestManifest(manifestUrl);
const emulatorJson: EmulatorWebsiteManifest = response.data;
const targetDir = resolve(rootDir, 'node_modules', emulatorJson.name);
const appDir = resolve(targetDir, 'app');
Expand Down
34 changes: 27 additions & 7 deletions src/tooling/piral-cli/src/injectors/pilet-injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,31 @@ export default class PiletInjector implements KrasInjector {
return this.sendContent(content, mime.getType(target), url);
}

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' });
}
}

private async shouldLoad(target: string, path: string) {
if (this.proxyInfo) {
if (!this.proxyInfo.files.includes(path)) {
Expand All @@ -342,17 +367,12 @@ export default class PiletInjector implements KrasInjector {
const fileInfo = await stat(target).catch(() => undefined);

if (!fileInfo || fileInfo.mtime < this.proxyInfo.date) {
const url = new URL(path, this.proxyInfo.source);

try {
const response = await axios.default.get(url.href, { responseType: 'arraybuffer' });
const response = await this.download(path);
await writeFile(target, response.data);
} catch (ex) {
log('generalDebug_0003', `HTTP request for emulator asset retrieval failed: ${ex}`);
log(
fileInfo ? 'optionalEmulatorAssetUpdateSkipped_0122' : 'requiredEmulatorAssetDownloadSkipped_0123',
url.href,
);
log(fileInfo ? 'optionalEmulatorAssetUpdateSkipped_0122' : 'requiredEmulatorAssetDownloadSkipped_0123', path);
return !!fileInfo;
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/tooling/piral-cli/src/types/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,20 @@ export type ImportmapMode = 'host' | 'remote';

export type PiletSchemaVersion = 'none' | 'v0' | 'v1' | 'v2' | 'v3' | 'mf';

export interface HeaderAuthConfig {
mode: 'header';
key: string;
value: string;
}

export interface HttpAuthConfig {
mode: 'http';
username: string;
password: string;
}

export type AuthConfig = HeaderAuthConfig | HttpAuthConfig;

export type SourceLanguage = 'js' | 'ts';

export type PiletPublishScheme = 'none' | 'digest' | 'bearer' | 'basic';
Expand Down

0 comments on commit 4ddcf60

Please sign in to comment.