Skip to content

Commit

Permalink
fix: differentiate between API and OAuth in whoami (#1203)
Browse files Browse the repository at this point in the history
* fix: differentiate between API and OAuth in whoami

* Create poor-houses-warn.md

* address PR feedback
  • Loading branch information
rozenmd authored Jun 9, 2022
1 parent b44cc26 commit 3b88b9f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
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: "user@example.com" };
});
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: "user@example.com",
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

0 comments on commit 3b88b9f

Please sign in to comment.