From 01b32fdd17642690a6bd9fb9c0a5122ffb61f192 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Sun, 3 Nov 2019 18:18:15 -0300 Subject: [PATCH] fix: handle error response with 200 status code from OAuth endpoint (#27) --- package-lock.json | 7 +++--- package.json | 1 + src/get-oauth-authentication.ts | 12 ++++++++++- test/index.test.ts | 38 +++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 61e05ba29..b3df8bca6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1136,10 +1136,11 @@ } }, "@octokit/request-error": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-1.0.4.tgz", - "integrity": "sha512-L4JaJDXn8SGT+5G0uX79rZLv0MNJmfGa4vb4vy1NnpjSnWDLJRy6m90udGwvMmavwsStgbv2QNkPzzTCMmL+ig==", + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-1.1.0.tgz", + "integrity": "sha512-06lt8PulL3rKpmwzYLCeLEt1iHFoj8l0PLkObAtp5Cx0Wwd1+5FAa9u6UXjA0kzYsfbjBKF9TtO9CuXelKiYlw==", "requires": { + "@octokit/types": "^2.0.0", "deprecation": "^2.0.0", "once": "^1.4.0" } diff --git a/package.json b/package.json index 88425eb81..f112d4747 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/get-oauth-authentication.ts b/src/get-oauth-authentication.ts index 779e96a7e..6251de320 100644 --- a/src/get-oauth-authentication.ts +++ b/src/get-oauth-authentication.ts @@ -4,6 +4,7 @@ import { StrategyOptionsWithDefaults, OAuthAccesTokenAuthentication } from "./types"; +import { RequestError } from '@octokit/request-error'; export async function getOAuthAuthentication( state: StrategyOptionsWithDefaults, @@ -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", diff --git a/test/index.test.ts b/test/index.test.ts index 433f3af3e..86b02b96c 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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'); +});