From 18de867f61a094059ffe40e0dac29c6996996f16 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Tue, 23 Mar 2021 13:45:03 -0700 Subject: [PATCH] feat: `auth({ type: "oauth-user" })`. Deprecates `auth({ type: "oauth" })` (#264) --- README.md | 12 +++---- src/auth.ts | 9 ++++++ src/types.ts | 2 +- test/deprecations.test.ts | 66 ++++++++++++++++++++++++++++++++++++++- test/index.test.ts | 8 ++--- 5 files changed, 85 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index afca83493..00fbe9b9a 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ const installationAuthentication = await auth({ type: "installation" }); // } // Retrieve an oauth-access token -const oauthAuthentication = await auth({ type: "oauth", code: "123456" }); +const oauthAuthentication = await auth({ type: "oauth-user", code: "123456" }); // resolves with // { // type: 'token', @@ -271,7 +271,7 @@ createAppAuth({ string - Required. Must be either "app", "installation", or "oauth". + Required. Must be either "app", "installation", or "oauth-user". @@ -364,7 +364,7 @@ const installationAuth123 = await appAuth({ string - Only relevant if type is set to "oauth".
+ Only relevant if type is set to "oauth-user".

The authorization code which was passed as query parameter to the callback URL from the OAuth web application flow. @@ -377,7 +377,7 @@ const installationAuth123 = await appAuth({ string - Only relevant if type is set to "oauth".
+ Only relevant if type is set to "oauth-user".

The URL in your application where users are sent after authorization. See redirect urls. @@ -390,7 +390,7 @@ const installationAuth123 = await appAuth({ string - Only relevant if type is set to "oauth".
+ Only relevant if type is set to "oauth-user".

The unguessable random string you provided in Step 1 of the OAuth web application flow. @@ -673,7 +673,7 @@ Note that `auth.hook()` does not create and set an OAuth authentication token. B ```js const { token } = await auth({ - type: "oauth", + type: "oauth-user", code: "123456", }); const requestWithAuth = request.defaults({ diff --git a/src/auth.ts b/src/auth.ts index 5c57e4a4d..be96510fb 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,3 +1,5 @@ +import { Deprecation } from "deprecation"; + import { AuthOptions, Authentication, State } from "./types"; import { getAppAuthentication } from "./get-app-authentication"; import { getInstallationAuthentication } from "./get-installation-authentication"; @@ -15,6 +17,13 @@ export async function auth( case "installation": return getInstallationAuthentication(state, options); case "oauth": + state.log.warn( + // @ts-expect-error + new Deprecation( + `[@octokit/auth-app] {type: "oauth"} is deprecated. Use {type: "oauth-app"} instead` + ) + ); + case "oauth-user": return getOAuthAuthentication(state, options); default: throw new Error(`Invalid auth type: ${type}`); diff --git a/src/types.ts b/src/types.ts index de5365495..5f95c985c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -118,7 +118,7 @@ export type OAuthOptions = { export type AuthOptions = InstallationAuthOptions & OAuthOptions & { - type: "app" | "installation" | "oauth"; + type: "app" | "installation" | "oauth" | "oauth-user"; }; export type WithInstallationId = { diff --git a/test/deprecations.test.ts b/test/deprecations.test.ts index 936b86840..8cb5ec7c5 100644 --- a/test/deprecations.test.ts +++ b/test/deprecations.test.ts @@ -1,5 +1,69 @@ +import { request } from "@octokit/request"; +import fetchMock, { MockMatcherFunction } from "fetch-mock"; +import { Deprecation } from "deprecation"; + +import { createAppAuth } from "../src"; + describe("deprecations", () => { - test("There are currently no deprecations", () => {}); + test("auth({ type: 'oauth' }) - #263", async () => { + const matchCreateOAuthAccessToken: MockMatcherFunction = ( + url, + { body, headers } + ) => { + expect(url).toEqual("https://github.com/login/oauth/access_token"); + expect(headers).toStrictEqual({ + accept: "application/json", + "user-agent": "test", + "content-type": "application/json; charset=utf-8", + }); + expect(JSON.parse(String(body))).toStrictEqual({ + client_id: "12345678901234567890", + client_secret: "1234567890123456789012345678901234567890", + code: "123456", + }); + return true; + }; + + const createOAuthAccessTokenResponseData = { + access_token: "secret123", + scope: "", + token_type: "bearer", + }; + + const warn = jest.fn(); + const auth = createAppAuth({ + appId: "1", + privateKey: "", + clientId: "12345678901234567890", + clientSecret: "1234567890123456789012345678901234567890", + log: { warn }, + request: request.defaults({ + headers: { + "user-agent": "test", + }, + request: { + fetch: fetchMock + .sandbox() + .postOnce( + matchCreateOAuthAccessToken, + createOAuthAccessTokenResponseData + ), + }, + }), + }); + + await auth({ + type: "oauth", + code: "123456", + }); + + expect(warn).toHaveBeenCalledTimes(1); + expect(warn).toHaveBeenCalledWith( + new Deprecation( + '[@octokit/auth-app] {type: "oauth"} is deprecated. Use {type: "oauth-app"} instead' + ) + ); + }); // example: // diff --git a/test/index.test.ts b/test/index.test.ts index 1dabf6a7f..8a9b0dac2 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -183,7 +183,7 @@ test("README example for oauth", async () => { }); const authentication = await auth({ - type: "oauth", + type: "oauth-user", code: "123456", }); @@ -757,7 +757,7 @@ test("oauth with `code`, `redirectUrl` and `state`", async () => { }); const authentication = await auth({ - type: "oauth", + type: "oauth-user", code: "123456", state: "mystate123", redirectUrl: "https://example.com/login", @@ -799,7 +799,7 @@ test("oauth with custom baseUrl (GHE)", async () => { }); const authentication = await auth({ - type: "oauth", + type: "oauth-user", code: "123456", state: "mystate123", redirectUrl: "https://example.com/login", @@ -1720,7 +1720,7 @@ test("oauth endpoint error", async () => { await expect( auth({ - type: "oauth", + type: "oauth-user", code: "12345678901234567890", redirectUrl: "https://example.com/login", })