Skip to content

Commit

Permalink
get registry auth
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeVanti committed Aug 11, 2024
1 parent eea3d5f commit ba755dd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 37 deletions.
5 changes: 2 additions & 3 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { npmDebugLog } from "../logging";
import { makeCheckIsBuiltInPackage } from "../services/built-in-package-check";
import { makeResolveDependency } from "../services/dependency-resolving";
import { makeDetermineEditorVersion } from "../services/determine-editor-version";
import { makeLoadRegistryAuth } from "../services/load-registry-auth";
import { getRegistryAuth } from "../services/get-registry-auth";
import { makeLogin } from "../services/login";
import { makeNpmLogin } from "../services/npm-login";
import { makeAuthNpmrc } from "../services/npmrc-auth";
Expand Down Expand Up @@ -54,11 +54,10 @@ import {

const log = npmlog;

const loadRegistryAuth = makeLoadRegistryAuth(loadUpmConfig);
const parseEnv = makeParseEnv(
log,
getUpmConfigPath,
loadRegistryAuth,
getRegistryAuth,
getProcessCwd,
npmDebugLog
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import { addAuth, emptyUpmConfig, UpmConfig } from "../domain/upm-config";
import { LoadUpmConfig, UpmAuth, UpmConfigContent } from "../io/upm-config-io";
import { NpmAuth } from "another-npm-registry-client";
import { decodeBase64 } from "../domain/base64";
import { coerceRegistryUrl } from "../domain/registry-url";
import { addAuth, emptyUpmConfig, UpmConfig } from "../domain/upm-config";
import {
loadUpmConfig,
LoadUpmConfig,
UpmAuth,
UpmConfigContent,
} from "../io/upm-config-io";
import { recordEntries } from "../utils/record-utils";
import { trySplitAtFirstOccurrenceOf } from "../utils/string-utils";
import { removeExplicitUndefined } from "../utils/zod-utils";
import { recordEntries } from "../utils/record-utils";
import { coerceRegistryUrl } from "../domain/registry-url";

/**
* Service function for loading registry authentication. Internally this
* will load it from the upmconfig.toml.
* Service function for getting registry authentication.
* @param upmConfigPath The path to the upmconfig.toml file.
*/
export type LoadRegistryAuth = (upmConfigPath: string) => Promise<UpmConfig>;
export type GetRegistryAuth = (upmConfigPath: string) => Promise<UpmConfig>;

/**
* Makes a {@link LoadRegistryAuth} function.
* Makes a {@link GetRegistryAuth} function which gets it's information from
* the users upm config.
*/
export function makeLoadRegistryAuth(
export function LoadRegistryAuthFromUpmConfig(
loadUpmConfig: LoadUpmConfig
): LoadRegistryAuth {
): GetRegistryAuth {
return async (upmConfigPath) => {
function importNpmAuth(input: UpmAuth): NpmAuth {
// Basic auth
Expand Down Expand Up @@ -62,3 +67,8 @@ export function makeLoadRegistryAuth(
return content !== null ? importUpmConfig(content) : emptyUpmConfig;
};
}

/**
* Default {@link GetRegistryAuth} function. Uses {@link LoadRegistryAuthFromUpmConfig}.
*/
export const getRegistryAuth = LoadRegistryAuthFromUpmConfig(loadUpmConfig);
6 changes: 3 additions & 3 deletions src/services/parse-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { GetCwd } from "../io/special-paths";
import { CustomError } from "ts-custom-error";
import { DebugLog } from "../logging";
import { assertIsError } from "../utils/error-type-guards";
import { LoadRegistryAuth } from "./load-registry-auth";
import { GetRegistryAuth } from "./get-registry-auth";

/**
* Error for when auth information for a registry could not be loaded.
Expand Down Expand Up @@ -64,7 +64,7 @@ export type ParseEnv = (options: CmdOptions) => Promise<Env>;
export function makeParseEnv(
log: Logger,
getUpmConfigPath: GetUpmConfigPath,
loadRegistryAuth: LoadRegistryAuth,
getRegistryAuth: GetRegistryAuth,
getCwd: GetCwd,
debugLog: DebugLog
): ParseEnv {
Expand Down Expand Up @@ -145,7 +145,7 @@ export function makeParseEnv(
let registry: Registry;
let upstreamRegistry: Registry;
try {
const upmConfig = await loadRegistryAuth(upmConfigPath);
const upmConfig = await getRegistryAuth(upmConfigPath);
registry = determinePrimaryRegistry(options, upmConfig);
upstreamRegistry = determineUpstreamRegistry();
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
import { Base64 } from "../../src/domain/base64";
import { emptyUpmConfig } from "../../src/domain/upm-config";
import { LoadUpmConfig } from "../../src/io/upm-config-io";
import { LoadRegistryAuthFromUpmConfig } from "../../src/services/get-registry-auth";
import { exampleRegistryUrl } from "../domain/data-registry";
import { makeLoadRegistryAuth } from "../../src/services/load-registry-auth";
import { mockService } from "./service.mock";
import { LoadUpmConfig } from "../../src/io/upm-config-io";
import { emptyUpmConfig } from "../../src/domain/upm-config";
import { Base64 } from "../../src/domain/base64";

describe("load registry auth", () => {
describe("get registry auth from upm config", () => {
const someConfigPath = "/home/user/.upmconfig.toml";
const someEmail = "user@mail.com";
const someToken = "isehusehgusheguszg8gshg";

function makeDependencies() {
const loadUpmConfig = mockService<LoadUpmConfig>();

const loadRegistryAuth = makeLoadRegistryAuth(loadUpmConfig);
return { loadRegistryAuth, loadUpmConfig } as const;
const loadRegistryAuthFromUpmConfig =
LoadRegistryAuthFromUpmConfig(loadUpmConfig);
return { loadRegistryAuthFromUpmConfig, loadUpmConfig } as const;
}

it("should be empty if there is no upm config", async () => {
const { loadRegistryAuth, loadUpmConfig } = makeDependencies();
const { loadRegistryAuthFromUpmConfig, loadUpmConfig } = makeDependencies();
loadUpmConfig.mockResolvedValue(null);

const actual = await loadRegistryAuth(someConfigPath);
const actual = await loadRegistryAuthFromUpmConfig(someConfigPath);

expect(actual).toEqual(emptyUpmConfig);
});

it("should import empty", async () => {
const { loadRegistryAuth, loadUpmConfig } = makeDependencies();
const { loadRegistryAuthFromUpmConfig, loadUpmConfig } = makeDependencies();
loadUpmConfig.mockResolvedValue({});

const actual = await loadRegistryAuth(someConfigPath);
const actual = await loadRegistryAuthFromUpmConfig(someConfigPath);

expect(actual).toEqual(emptyUpmConfig);
});

it("should remove trailing slash on registry urls", async () => {
const { loadRegistryAuth, loadUpmConfig } = makeDependencies();
const { loadRegistryAuthFromUpmConfig, loadUpmConfig } = makeDependencies();
loadUpmConfig.mockResolvedValue({
npmAuth: {
[exampleRegistryUrl + "/"]: {
Expand All @@ -47,7 +48,7 @@ describe("load registry auth", () => {
},
});

const actual = await loadRegistryAuth(someConfigPath);
const actual = await loadRegistryAuthFromUpmConfig(someConfigPath);

expect(actual).toEqual({
[exampleRegistryUrl]: {
Expand All @@ -60,7 +61,7 @@ describe("load registry auth", () => {
});

it("should import valid basic auth", async () => {
const { loadRegistryAuth, loadUpmConfig } = makeDependencies();
const { loadRegistryAuthFromUpmConfig, loadUpmConfig } = makeDependencies();
loadUpmConfig.mockResolvedValue({
npmAuth: {
[exampleRegistryUrl]: {
Expand All @@ -71,7 +72,7 @@ describe("load registry auth", () => {
},
});

const actual = await loadRegistryAuth(someConfigPath);
const actual = await loadRegistryAuthFromUpmConfig(someConfigPath);

expect(actual).toEqual({
[exampleRegistryUrl]: {
Expand All @@ -84,12 +85,12 @@ describe("load registry auth", () => {
});

it("should import valid token auth", async () => {
const { loadRegistryAuth, loadUpmConfig } = makeDependencies();
const { loadRegistryAuthFromUpmConfig, loadUpmConfig } = makeDependencies();
loadUpmConfig.mockResolvedValue({
npmAuth: { [exampleRegistryUrl]: { token: someToken, alwaysAuth: true } },
});

const actual = await loadRegistryAuth(someConfigPath);
const actual = await loadRegistryAuthFromUpmConfig(someConfigPath);

expect(actual).toEqual({
[exampleRegistryUrl]: {
Expand All @@ -100,7 +101,7 @@ describe("load registry auth", () => {
});

it("should ignore email when importing token auth", async () => {
const { loadRegistryAuth, loadUpmConfig } = makeDependencies();
const { loadRegistryAuthFromUpmConfig, loadUpmConfig } = makeDependencies();
loadUpmConfig.mockResolvedValue({
npmAuth: {
[exampleRegistryUrl]: {
Expand All @@ -110,7 +111,7 @@ describe("load registry auth", () => {
},
});

const actual = await loadRegistryAuth(someConfigPath);
const actual = await loadRegistryAuthFromUpmConfig(someConfigPath);

expect(actual).toEqual({
[exampleRegistryUrl]: {
Expand Down
4 changes: 2 additions & 2 deletions test/services/parse-env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { mockService } from "./service.mock";
import { GetCwd } from "../../src/io/special-paths";
import path from "path";
import { noopLogger } from "../../src/logging";
import { LoadRegistryAuth } from "../../src/services/load-registry-auth";
import { GetRegistryAuth } from "../../src/services/get-registry-auth";

const testRootPath = "/users/some-user/projects/MyUnityProject";

Expand All @@ -28,7 +28,7 @@ function makeDependencies() {
// The root directory does not contain an upm-config
getUpmConfigPath.mockResolvedValue(testRootPath);

const loadRegistryAuth = mockService<LoadRegistryAuth>();
const loadRegistryAuth = mockService<GetRegistryAuth>();
loadRegistryAuth.mockResolvedValue(emptyUpmConfig);

// process.cwd is in the root directory.
Expand Down

0 comments on commit ba755dd

Please sign in to comment.