Skip to content

Commit

Permalink
refactor: extract local helper function (#85)
Browse files Browse the repository at this point in the history
Extract helper functions for getting auth-information from UPMConfig objects into the upm-config module, so they are close to other  UPMConfig related functions.
  • Loading branch information
ComradeVanti authored Dec 29, 2023
1 parent e4d9971 commit 60f7608
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 47 deletions.
44 changes: 44 additions & 0 deletions src/types/upm-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { trySplitAtFirstOccurrenceOf } from "../utils/string-utils";
import { Base64, decodeBase64, encodeBase64 } from "./base64";
import { RegistryUrl } from "./registry-url";
import log from "../logger";
import { NpmAuth } from "another-npm-registry-client";

/**
* Authentication information that is shared between different authentication methods
Expand Down Expand Up @@ -95,3 +97,45 @@ export function decodeBasicAuth(base64: Base64): [string, string] {
export function shouldAlwaysAuth(auth: UpmAuth): boolean {
return auth.alwaysAuth || false;
}

function tryToNpmAuth(upmAuth: UpmAuth): NpmAuth | null {
if (isTokenAuth(upmAuth)) {
return {
token: upmAuth.token,
alwaysAuth: shouldAlwaysAuth(upmAuth),
};
} else if (isBasicAuth(upmAuth)) {
const [username, password] = decodeBasicAuth(upmAuth._auth);
return {
username,
password: encodeBase64(password),
email: upmAuth.email,
alwaysAuth: shouldAlwaysAuth(upmAuth),
};
}
return null;
}

/**
* Attempts to get the {@link NpmAuth} information for a specific registry
* from a {@link UPMConfig} object.
* @param upmConfig The config
* @param registry The registry
* @returns The auth information or null if the registry does not exist
* in the config
*/
export function tryGetAuthForRegistry(
upmConfig: UPMConfig,
registry: RegistryUrl
): NpmAuth | null {
const upmAuth = upmConfig.npmAuth![registry];
if (upmAuth === undefined) return null;
const npmAuth = tryToNpmAuth(upmAuth);
if (npmAuth === null) {
log.warn(
"env.auth",
`failed to parse auth info for ${registry} in .upmconfig.toml: missing token or _auth fields`
);
}
return npmAuth;
}
54 changes: 7 additions & 47 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,11 @@ import { getUpmConfigDir, loadUpmConfig } from "./upm-config-io";
import path from "path";
import fs from "fs";
import yaml from "yaml";
import {
coerceRegistryUrl,
RegistryUrl,
registryUrl,
} from "../types/registry-url";
import {
decodeBasicAuth,
isBasicAuth,
isTokenAuth,
shouldAlwaysAuth,
UpmAuth,
} from "../types/upm-config";
import { encodeBase64 } from "../types/base64";
import { coerceRegistryUrl, registryUrl } from "../types/registry-url";
import { tryGetAuthForRegistry } from "../types/upm-config";
import { CmdOptions } from "../types/options";
import { manifestPathFor } from "../types/pkg-manifest";
import { Registry } from "../registry-client";
import { NpmAuth } from "another-npm-registry-client";

export type Env = {
cwd: string;
Expand Down Expand Up @@ -86,46 +74,18 @@ export const parseEnv = async function (
};
}

function tryToNpmAuth(upmAuth: UpmAuth): NpmAuth | null {
if (isTokenAuth(upmAuth)) {
return {
token: upmAuth.token,
alwaysAuth: shouldAlwaysAuth(upmAuth),
};
} else if (isBasicAuth(upmAuth)) {
const [username, password] = decodeBasicAuth(upmAuth._auth);
return {
username,
password: encodeBase64(password),
email: upmAuth.email,
alwaysAuth: shouldAlwaysAuth(upmAuth),
};
}
return null;
}

// auth
if (options._global.systemUser) env.systemUser = true;
if (options._global.wsl) env.wsl = true;
const configDir = await getUpmConfigDir(env.wsl, env.systemUser);
const upmConfig = await loadUpmConfig(configDir);

function tryGetAuthForRegistry(registry: RegistryUrl): NpmAuth | null {
const upmAuth = upmConfig!.npmAuth![registry];
if (upmAuth === undefined) return null;
const npmAuth = tryToNpmAuth(upmAuth);
if (npmAuth === null) {
log.warn(
"env.auth",
`failed to parse auth info for ${registry} in .upmconfig.toml: missing token or _auth fields`
);
}
return npmAuth;
}

if (upmConfig !== undefined && upmConfig.npmAuth !== undefined) {
env.registry.auth = tryGetAuthForRegistry(env.registry.url);
env.upstreamRegistry.auth = tryGetAuthForRegistry(env.upstreamRegistry.url);
env.registry.auth = tryGetAuthForRegistry(upmConfig, env.registry.url);
env.upstreamRegistry.auth = tryGetAuthForRegistry(
upmConfig,
env.upstreamRegistry.url
);
}
// return if no need to check path
if (!checkPath) return env;
Expand Down

0 comments on commit 60f7608

Please sign in to comment.