Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not require state parameter to create a token #288

Merged
merged 1 commit into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,6 @@ For the web flow, you have to pass the `code` from URL redirect described in [st

```js
const { token } = await app.createToken({
state: "state123",
code: "code123",
});
```
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/cloudflare/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ async function onUnhandledRequestDefaultCloudflare(
return sendResponse(octokitResponse);
}

export function createCloudflareHandler(
app: OAuthApp,
export function createCloudflareHandler<T>(
app: OAuthApp<T>,
{
pathPrefix,
onUnhandledRequest = onUnhandledRequestDefaultCloudflare,
Expand Down
18 changes: 6 additions & 12 deletions src/middleware/handle-request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OAuthApp } from "../index";
import { OctokitRequest, OctokitResponse, HandlerOptions } from "./types";
import { HandlerOptions, OctokitRequest, OctokitResponse } from "./types";
import { ClientType, Options } from "../types";
// @ts-ignore - requires esModuleInterop flag
import fromEntries from "fromentries";
Expand Down Expand Up @@ -89,16 +89,13 @@ export async function handleRequest(
`[@octokit/oauth-app] ${query.error} ${query.error_description}`
);
}
if (!query.state || !query.code) {
throw new Error(
'[@octokit/oauth-app] Both "code" & "state" parameters are required'
);
if (!query.code) {
throw new Error('[@octokit/oauth-app] "code" parameter is required');
}

const {
authentication: { token },
} = await app.createToken({
state: query.state,
code: query.code,
});

Expand All @@ -114,16 +111,13 @@ export async function handleRequest(
}

if (route === routes.createToken) {
const { state: oauthState, code, redirectUrl } = json;
const { code, redirectUrl } = json;

if (!oauthState || !code) {
throw new Error(
'[@octokit/oauth-app] Both "code" & "state" parameters are required'
);
if (!code) {
throw new Error('[@octokit/oauth-app] "code" parameter is required');
}

const result = await app.createToken({
state: oauthState,
code,
redirectUrl,
});
Expand Down
13 changes: 2 additions & 11 deletions test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ describe("app", () => {
clientId: "0123",
clientSecret: "0123secret",
});
const { url } = app.getWebFlowAuthorizationUrl({
state: "state123",
});
const { url } = app.getWebFlowAuthorizationUrl({ state: "state123" });
expect(url).toStrictEqual(
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=0123&state=state123"
);
Expand All @@ -105,9 +103,7 @@ describe("app", () => {
clientId: "lv1.0123",
clientSecret: "0123secret",
});
const { url } = app.getWebFlowAuthorizationUrl({
state: "state123",
});
const { url } = app.getWebFlowAuthorizationUrl({ state: "state123" });
expect(url).toStrictEqual(
"https://github.com/login/oauth/authorize?allow_signup=true&client_id=lv1.0123&state=state123"
);
Expand All @@ -128,7 +124,6 @@ describe("app", () => {
client_id: "0123",
client_secret: "0123secret",
code: "code123",
state: "state123",
},
}
)
Expand Down Expand Up @@ -158,7 +153,6 @@ describe("app", () => {
app.on("token.created", onTokenCallback);

const result = await app.createToken({
state: "state123",
code: "code123",
});

Expand Down Expand Up @@ -195,7 +189,6 @@ describe("app", () => {
it("app.createToken(options) for device flow", async () => {
const mock = fetchMock
.sandbox()

.postOnce(
"https://github.com/login/device/code",
{
Expand Down Expand Up @@ -944,7 +937,6 @@ describe("app", () => {
client_id: "0123",
client_secret: "0123secret",
code: "code123",
state: "state123",
},
}
)
Expand Down Expand Up @@ -985,7 +977,6 @@ describe("app", () => {
app.on("token.created", onTokenCallback2);

await app.createToken({
state: "state123",
code: "code123",
});

Expand Down
27 changes: 19 additions & 8 deletions test/cloudflare-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { URL } from "url";
import * as nodeFetch from "node-fetch";
import fromEntries from "fromentries";
import { OAuthApp, createCloudflareHandler } from "../src/";
import { createCloudflareHandler, OAuthApp } from "../src/";

describe("createCloudflareHandler(app)", () => {
beforeAll(() => {
Expand All @@ -15,6 +15,22 @@ describe("createCloudflareHandler(app)", () => {
delete (global as any).Response;
});

it("support both oauth-app and github-app", () => {
const oauthApp = new OAuthApp({
clientType: "oauth-app",
clientId: "0123",
clientSecret: "0123secret",
});
createCloudflareHandler(oauthApp);

const githubApp = new OAuthApp({
clientType: "github-app",
clientId: "0123",
clientSecret: "0123secret",
});
createCloudflareHandler(githubApp);
});

it("allow pre-flight requests", async () => {
const app = new OAuthApp({
clientId: "0123",
Expand Down Expand Up @@ -118,7 +134,6 @@ describe("createCloudflareHandler(app)", () => {

expect(appMock.createToken.mock.calls.length).toEqual(1);
expect(appMock.createToken.mock.calls[0][0]).toStrictEqual({
state: "state123",
code: "012345",
});
});
Expand All @@ -141,7 +156,6 @@ describe("createCloudflareHandler(app)", () => {
method: "POST",
body: JSON.stringify({
code: "012345",
state: "state123",
redirectUrl: "http://example.com",
}),
});
Expand All @@ -154,7 +168,6 @@ describe("createCloudflareHandler(app)", () => {

expect(appMock.createToken.mock.calls.length).toEqual(1);
expect(appMock.createToken.mock.calls[0][0]).toStrictEqual({
state: "state123",
code: "012345",
redirectUrl: "http://example.com",
});
Expand Down Expand Up @@ -446,8 +459,7 @@ describe("createCloudflareHandler(app)", () => {

expect(response.status).toEqual(400);
expect(await response.json()).toStrictEqual({
error:
'[@octokit/oauth-app] Both "code" & "state" parameters are required',
error: '[@octokit/oauth-app] "code" parameter is required',
});
});

Expand Down Expand Up @@ -483,8 +495,7 @@ describe("createCloudflareHandler(app)", () => {

expect(response.status).toEqual(400);
expect(await response.json()).toStrictEqual({
error:
'[@octokit/oauth-app] Both "code" & "state" parameters are required',
error: '[@octokit/oauth-app] "code" parameter is required',
});
});

Expand Down
11 changes: 3 additions & 8 deletions test/node-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createServer } from "http";
import { URL } from "url";

import fetch from "node-fetch";
import { OAuthApp, createNodeMiddleware } from "../src/";
import { createNodeMiddleware, OAuthApp } from "../src/";

// import without types
const express = require("express");
Expand Down Expand Up @@ -152,7 +152,6 @@ describe("createNodeMiddleware(app)", () => {

expect(appMock.createToken.mock.calls.length).toEqual(1);
expect(appMock.createToken.mock.calls[0][0]).toStrictEqual({
state: "state123",
code: "012345",
});
});
Expand Down Expand Up @@ -180,7 +179,6 @@ describe("createNodeMiddleware(app)", () => {
method: "POST",
body: JSON.stringify({
code: "012345",
state: "state123",
redirectUrl: "http://example.com",
}),
}
Expand All @@ -195,7 +193,6 @@ describe("createNodeMiddleware(app)", () => {

expect(appMock.createToken.mock.calls.length).toEqual(1);
expect(appMock.createToken.mock.calls[0][0]).toStrictEqual({
state: "state123",
code: "012345",
redirectUrl: "http://example.com",
});
Expand Down Expand Up @@ -571,8 +568,7 @@ describe("createNodeMiddleware(app)", () => {

expect(response.status).toEqual(400);
expect(await response.json()).toStrictEqual({
error:
'[@octokit/oauth-app] Both "code" & "state" parameters are required',
error: '[@octokit/oauth-app] "code" parameter is required',
});
});

Expand Down Expand Up @@ -619,8 +615,7 @@ describe("createNodeMiddleware(app)", () => {

expect(response.status).toEqual(400);
expect(await response.json()).toStrictEqual({
error:
'[@octokit/oauth-app] Both "code" & "state" parameters are required',
error: '[@octokit/oauth-app] "code" parameter is required',
});
});

Expand Down