Skip to content

Commit

Permalink
fix: handle error response with 200 status code from OAuth endpoint (#27
Browse files Browse the repository at this point in the history
)
  • Loading branch information
frangio authored and gr2m committed Nov 3, 2019
1 parent 81a29be commit 01b32fd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dependencies": {
"@octokit/request": "^5.3.0",
"@octokit/types": "^2.0.0",
"@octokit/request-error": "^1.1.0",
"@types/lru-cache": "^5.1.0",
"lru-cache": "^5.1.1",
"universal-github-app-jwt": "^1.0.1",
Expand Down
12 changes: 11 additions & 1 deletion src/get-oauth-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
StrategyOptionsWithDefaults,
OAuthAccesTokenAuthentication
} from "./types";
import { RequestError } from '@octokit/request-error';

export async function getOAuthAuthentication(
state: StrategyOptionsWithDefaults,
Expand Down Expand Up @@ -34,9 +35,18 @@ export async function getOAuthAuthentication(
redirect_uri: options.redirectUrl
};

const response = await request(route, parameters);

if (response.data.error !== undefined) {
throw new RequestError(`${response.data.error_description} (${response.data.error})`, response.status, {
headers: response.headers,
request: request.endpoint(route, parameters)
});
}

const {
data: { access_token: token, scope }
} = await request(route, parameters);
} = response;

return {
type: "token",
Expand Down
38 changes: 38 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1049,3 +1049,41 @@ test("auth.hook() creates token and uses it for succeeding requests", async () =

expect(mock.done()).toBe(true);
});


test("oauth endpoint error", async () => {
const requestMock = request.defaults({
headers: {
"user-agent": "test"
},
request: {
fetch: fetchMock.sandbox().post(
"https://github.com/login/oauth/access_token", {
status: 200,
body: JSON.stringify({
error: "incorrect_client_credentials",
error_description: "The client_id and/or client_secret passed are incorrect.",
}),
headers: {
"Content-Type": "application/json; charset=utf-8"
}
}),
},
});

const auth = createAppAuth({
id: APP_ID,
privateKey: PRIVATE_KEY,
clientId: "12345678901234567890",
clientSecret: "1234567890123456789012345678901234567890",
request: requestMock,
});

await expect(
auth({
type: 'oauth',
code: '12345678901234567890',
redirectUrl: 'https://example.com/login',
})
).rejects.toThrow('client_id');
});

0 comments on commit 01b32fd

Please sign in to comment.