Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: differentiate between API and OAuth in whoami #1203

Merged
merged 3 commits into from
Jun 9, 2022
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
7 changes: 7 additions & 0 deletions .changeset/poor-houses-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: differentiate between API and OAuth in whoami

Closes #1198
34 changes: 34 additions & 0 deletions packages/wrangler/src/__tests__/whoami.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { runInTempDir } from "./helpers/run-in-tmp";
import type { UserInfo } from "../whoami";

describe("getUserInfo()", () => {
const ENV_COPY = process.env;

runInTempDir({ homedir: "./home" });
const std = mockConsoleMethods();
const { setIsTTY } = useMockIsTTY();
Expand All @@ -17,6 +19,10 @@ describe("getUserInfo()", () => {
setIsTTY(true);
});

afterEach(() => {
process.env = ENV_COPY;
});

it("should return undefined if there is no config file", async () => {
const userInfo = await getUserInfo();
expect(userInfo).toBeUndefined();
Expand All @@ -28,6 +34,34 @@ describe("getUserInfo()", () => {
expect(userInfo).toBeUndefined();
});

it("should say it's using an API token when one is set", async () => {
process.env = {
CLOUDFLARE_API_TOKEN: "123456789",
};
setMockResponse("/user", () => {
return { email: "[email protected]" };
});
setMockResponse("/accounts", () => {
return [
{ name: "Account One", id: "account-1" },
{ name: "Account Two", id: "account-2" },
{ name: "Account Three", id: "account-3" },
];
});

const userInfo = await getUserInfo();
expect(userInfo).toEqual({
authType: "API",
apiToken: "123456789",
email: "[email protected]",
accounts: [
{ name: "Account One", id: "account-1" },
{ name: "Account Two", id: "account-2" },
{ name: "Account Three", id: "account-3" },
],
});
});

it("should return the user's email and accounts if authenticated via config token", async () => {
writeAuthConfigFile({ oauth_token: "some-oauth-token" });

Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ import type { Response } from "undici";
/**
* Try to read the API token from the environment.
*/
const getCloudflareAPITokenFromEnv = getEnvironmentVariableFactory({
export const getCloudflareAPITokenFromEnv = getEnvironmentVariableFactory({
variableName: "CLOUDFLARE_API_TOKEN",
deprecatedName: "CF_API_TOKEN",
});
Expand Down
5 changes: 3 additions & 2 deletions packages/wrangler/src/whoami.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Table from "ink-table";
import React from "react";
import { fetchListResult, fetchResult } from "./cfetch";
import { logger } from "./logger";
import { getAPIToken } from "./user";
import { getAPIToken, getCloudflareAPITokenFromEnv } from "./user";

export async function whoami() {
logger.log("Getting User settings...");
Expand Down Expand Up @@ -48,10 +48,11 @@ export interface UserInfo {

export async function getUserInfo(): Promise<UserInfo | undefined> {
const apiToken = getAPIToken();
const apiTokenFromEnv = getCloudflareAPITokenFromEnv();
return apiToken
? {
apiToken,
authType: "OAuth",
authType: apiTokenFromEnv ? "API" : "OAuth",
email: await getEmail(),
accounts: await getAccounts(),
}
Expand Down