Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/docs/commands/registry.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ nf login
nf login --local --api-key "$NF_REGISTRY_KEY"
```

Without `--api-key`, `nf login` asks for the key and shows `*` as you type it, with no way to reveal it.

Without a terminal, pass `--api-key` from an environment variable, as above: the command doesn't
prompt, and a key typed in the command stays in the shell history.

Expand Down
12 changes: 6 additions & 6 deletions src/lib/input/inputs/login-out/api-key.input.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { describe, expect, it, vi } from "vitest";

import { askInput } from "@lib/question";
import { askSecret } from "@lib/question";

import { type Input } from "../../input.type";
import { getLoginApiKeyInputOrAsk } from "./api-key.input";

vi.mock("@lib/question", () => ({
askInput: vi.fn(),
askSecret: vi.fn(),
}));

const createInput = (entries: [string, any][]): Input => {
Expand All @@ -18,14 +18,14 @@ describe("getLoginApiKeyInputOrAsk", () => {
const input = createInput([["apiKey", "my-api-key"]]);

expect(await getLoginApiKeyInputOrAsk(input)).toBe("my-api-key");
expect(askInput).not.toHaveBeenCalled();
expect(askSecret).not.toHaveBeenCalled();
});

it("should call askInput when apiKey is not provided", async () => {
vi.mocked(askInput).mockResolvedValue("asked-api-key");
it("should call askSecret when apiKey is not provided", async () => {
vi.mocked(askSecret).mockResolvedValue("asked-api-key");
const input = createInput([]);

expect(await getLoginApiKeyInputOrAsk(input)).toBe("asked-api-key");
expect(askInput).toHaveBeenCalledOnce();
expect(askSecret).toHaveBeenCalledOnce();
});
});
7 changes: 2 additions & 5 deletions src/lib/input/inputs/login-out/api-key.input.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { askInput } from "@lib/question";
import { askSecret } from "@lib/question";
import { Messages } from "@lib/ui";

import { getInputOrAsk } from "../../ask-inputs";
Expand All @@ -12,10 +12,7 @@ const getApiKeyInput = (inputs: Input) => {
export const getLoginApiKeyInputOrAsk = (inputs: Input) => {
return getInputOrAsk(
getApiKeyInput(inputs),
() =>
askInput(Messages.LOGIN_API_KEY_QUESTION, {
required: true,
}),
() => askSecret(Messages.LOGIN_API_KEY_QUESTION),
"--api-key",
);
};
32 changes: 30 additions & 2 deletions src/lib/question/questions/input.question.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { input } from "@inquirer/prompts";
import { input, password } from "@inquirer/prompts";
import { beforeEach, describe, expect, it, vi } from "vitest";

import { NoTerminalError } from "@utils/errors";

import { askInput, askInputArray } from "./input.question";
import { askInput, askInputArray, askSecret } from "./input.question";

vi.mock("@inquirer/prompts", () => ({
input: vi.fn(),
password: vi.fn(),
}));

// Vitest's stdin is not a terminal: these tests stand for an interactive run.
Expand Down Expand Up @@ -106,3 +107,30 @@ describe("without a terminal", () => {
expect(input).not.toHaveBeenCalled();
});
});

describe("askSecret", () => {
it("should ask with a masked prompt and return the answer", async () => {
vi.mocked(password).mockResolvedValue("nf_key");

expect(await askSecret("API key?")).toBe("nf_key");
expect(password).toHaveBeenCalledWith(
expect.objectContaining({ message: "API key?", mask: "*", toggleMask: false }),
);
});

it("should reject an empty answer", async () => {
vi.mocked(password).mockResolvedValue("nf_key");

await askSecret("API key?");
const validate = vi.mocked(password).mock.calls.at(-1)?.[0].validate;

expect(await validate?.("")).toEqual(expect.any(String));
expect(await validate?.("x")).toBe(true);
});

it("should throw NoTerminalError when stdin is not a terminal", async () => {
process.stdin.isTTY = false;

await expect(askSecret("API key?")).rejects.toThrow(NoTerminalError);
});
});
13 changes: 12 additions & 1 deletion src/lib/question/questions/input.question.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { input } from "@inquirer/prompts";
import { input, password } from "@inquirer/prompts";

import { assertTerminal, promptError } from "@utils/errors";

Expand Down Expand Up @@ -50,3 +50,14 @@ export const askInputArray = async (
).split(options.split);
return options.filter ? res.filter((r) => r.length) : res;
};

/** Like `askInput`, but the answer is masked: for secrets such as API keys. */
export const askSecret = async (question: string): Promise<string | never> => {
assertTerminal();
return await password({
message: question,
mask: "*",
toggleMask: false,
validate: (value) => value.length > 0 || "A value is required.",
}).catch(promptError);
};