Skip to content

Commit e10cf12

Browse files
authored
fix: do not set auth on OAuth Web/Device flow requests (#7)
1 parent 288cef2 commit e10cf12

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/hook.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ export async function hook(
3939
parameters
4040
) as EndpointDefaults & { url: string };
4141

42+
// Do not intercept OAuth Web/Device flow request
43+
if (
44+
/\/login\/(oauth\/access_token|device\/code)$/.test(endpoint.url as string)
45+
) {
46+
return request(endpoint);
47+
}
48+
4249
if (requiresBasicAuth(endpoint.url)) {
4350
const credentials = btoa(`${state.clientId}:${state.clientSecret}`);
4451
endpoint.headers.authorization = `basic ${credentials}`;

test/octokit.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,42 @@ test("Sets clientId/clientSecret as Basic auth for /authentication/{clientId}/*
164164

165165
expect(data).toEqual({ ok: true });
166166
});
167+
168+
test("Sets no auth auth for OAuth Web flow requests", async () => {
169+
const matchCreateTokenRequest: MockMatcherFunction = (url, options) => {
170+
expect(url).toEqual("https://github.com/login/oauth/access_token");
171+
// @ts-ignore
172+
expect(options.headers.authorization).toBeUndefined();
173+
174+
return true;
175+
};
176+
177+
const mock = fetchMock
178+
.sandbox()
179+
.postOnce(matchCreateTokenRequest, { ok: true });
180+
181+
const octokit = new Octokit({
182+
authStrategy: createOAuthUserAuth,
183+
auth: {
184+
clientId: "1234567890abcdef1234",
185+
clientSecret: "1234567890abcdef1234567890abcdef12345678",
186+
code: "code123",
187+
},
188+
request: {
189+
fetch: mock,
190+
},
191+
});
192+
193+
// Exchanges the code for the user access token authentication on first request
194+
// and caches the authentication for successive requests
195+
const { data } = await octokit.request(
196+
"POST https://github.com/login/oauth/access_token",
197+
{
198+
client_id: "1234567890abcdef1234",
199+
client_secret: "client_secret",
200+
code: "code123",
201+
}
202+
);
203+
204+
expect(data).toEqual({ ok: true });
205+
});

0 commit comments

Comments
 (0)