Skip to content

Commit

Permalink
feat: auth({ type: "oauth-user" }). Deprecates `auth({ type: "oauth…
Browse files Browse the repository at this point in the history
…" })` (#264)
  • Loading branch information
gr2m authored Mar 23, 2021
1 parent f0e1ddc commit 18de867
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 12 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -271,7 +271,7 @@ createAppAuth({
<code>string</code>
</th>
<td>
<strong>Required</strong>. Must be either <code>"app"</code>, <code>"installation"</code>, or <code>"oauth"</code>.
<strong>Required</strong>. Must be either <code>"app"</code>, <code>"installation"</code>, or <code>"oauth-user"</code>.
</td>
</tr>
<tr>
Expand Down Expand Up @@ -364,7 +364,7 @@ const installationAuth123 = await appAuth({
<code>string</code>
</th>
<td>
Only relevant if <code>type</code> is set to <code>"oauth"</code>.<br>
Only relevant if <code>type</code> is set to <code>"oauth-user"</code>.<br>
<br>
The authorization <code>code</code> which was passed as query parameter to the callback URL from the <a href="https://docs.github.com/en/developers/apps/authorizing-oauth-apps#2-users-are-redirected-back-to-your-site-by-github">OAuth web application flow</a>.
</td>
Expand All @@ -377,7 +377,7 @@ const installationAuth123 = await appAuth({
<code>string</code>
</th>
<td>
Only relevant if <code>type</code> is set to <code>"oauth"</code>.<br>
Only relevant if <code>type</code> is set to <code>"oauth-user"</code>.<br>
<br>
The URL in your application where users are sent after authorization. See <a href="https://docs.github.com/en/developers/apps/authorizing-oauth-apps#redirect-urls">redirect urls</a>.
</td>
Expand All @@ -390,7 +390,7 @@ const installationAuth123 = await appAuth({
<code>string</code>
</th>
<td>
Only relevant if <code>type</code> is set to <code>"oauth"</code>.<br>
Only relevant if <code>type</code> is set to <code>"oauth-user"</code>.<br>
<br>
The unguessable random string you provided in Step 1 of the <a href="https://docs.github.com/en/developers/apps/authorizing-oauth-apps#2-users-are-redirected-back-to-your-site-by-github">OAuth web application flow</a>.
</td>
Expand Down Expand Up @@ -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({
Expand Down
9 changes: 9 additions & 0 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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}`);
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
66 changes: 65 additions & 1 deletion test/deprecations.test.ts
Original file line number Diff line number Diff line change
@@ -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:
//
Expand Down
8 changes: 4 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ test("README example for oauth", async () => {
});

const authentication = await auth({
type: "oauth",
type: "oauth-user",
code: "123456",
});

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -1720,7 +1720,7 @@ test("oauth endpoint error", async () => {

await expect(
auth({
type: "oauth",
type: "oauth-user",
code: "12345678901234567890",
redirectUrl: "https://example.com/login",
})
Expand Down

0 comments on commit 18de867

Please sign in to comment.