@octokit/oauth-app
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -16,3 +16,3 @@ 'use strict'; | ||
const VERSION = "2.0.0"; | ||
const VERSION = "2.0.1"; | ||
@@ -408,6 +408,4 @@ function addEventHandler(state, eventName, eventHandler) { | ||
const { | ||
pathname, | ||
searchParams | ||
} = new URL(request.url, "http://localhost"); | ||
const route = [request.method, pathname].join(" "); | ||
const query = fromEntries(searchParams); | ||
@@ -418,3 +416,2 @@ const headers = request.headers; | ||
return { | ||
route, | ||
headers, | ||
@@ -430,3 +427,2 @@ query | ||
if (!bodyString) return resolve({ | ||
route, | ||
headers, | ||
@@ -438,3 +434,2 @@ query | ||
resolve({ | ||
route, | ||
headers, | ||
@@ -452,2 +447,23 @@ query, | ||
async function middleware(app, options, request, response) { | ||
// request.url mayb include ?query parameters which we don't want for `route` | ||
// hence the workaround using new URL() | ||
const { | ||
pathname | ||
} = new URL(request.url, "http://localhost"); | ||
const route = [request.method, pathname].join(" "); | ||
const routes = { | ||
getLogin: `GET ${options.pathPrefix}/login`, | ||
getCallback: `GET ${options.pathPrefix}/callback`, | ||
createToken: `POST ${options.pathPrefix}/token`, | ||
getToken: `GET ${options.pathPrefix}/token`, | ||
patchToken: `PATCH ${options.pathPrefix}/token`, | ||
deleteToken: `DELETE ${options.pathPrefix}/token`, | ||
deleteGrant: `DELETE ${options.pathPrefix}/grant` | ||
}; | ||
if (!Object.values(routes).includes(route)) { | ||
options.onUnhandledRequest(request, response); | ||
return; | ||
} | ||
let parsedRequest; | ||
@@ -467,3 +483,2 @@ | ||
const { | ||
route, | ||
headers, | ||
@@ -475,3 +490,5 @@ query, | ||
try { | ||
if (route === `GET ${options.pathPrefix}/login`) { | ||
var _headers$authorizatio4; | ||
if (route === routes.getLogin) { | ||
var _query$scopes; | ||
@@ -491,3 +508,3 @@ | ||
if (route === `GET ${options.pathPrefix}/callback`) { | ||
if (route === routes.getCallback) { | ||
if (query.error) { | ||
@@ -516,3 +533,3 @@ throw new Error(`[@octokit/oauth-app] ${query.error} ${query.error_description}`); | ||
if (route === `POST ${options.pathPrefix}/token`) { | ||
if (route === routes.createToken) { | ||
// @ts-ignore body is guaraenteed to exist | ||
@@ -545,3 +562,3 @@ const { | ||
if (route === `GET ${options.pathPrefix}/token`) { | ||
if (route === routes.getToken) { | ||
var _headers$authorizatio; | ||
@@ -564,3 +581,3 @@ | ||
if (route === `PATCH ${options.pathPrefix}/token`) { | ||
if (route === routes.patchToken) { | ||
var _headers$authorizatio2; | ||
@@ -583,3 +600,3 @@ | ||
if (route === `DELETE ${options.pathPrefix}/token`) { | ||
if (route === routes.deleteToken) { | ||
var _headers$authorizatio3; | ||
@@ -598,21 +615,16 @@ | ||
return response.end(); | ||
} | ||
} // route === routes.deleteGrant | ||
if (route === `DELETE ${options.pathPrefix}/grant`) { | ||
var _headers$authorizatio4; | ||
const token = (_headers$authorizatio4 = headers.authorization) === null || _headers$authorizatio4 === void 0 ? void 0 : _headers$authorizatio4.substr("token ".length); | ||
const token = (_headers$authorizatio4 = headers.authorization) === null || _headers$authorizatio4 === void 0 ? void 0 : _headers$authorizatio4.substr("token ".length); | ||
if (!token) { | ||
throw new Error('[@octokit/oauth-app] "Authorization" header is required'); | ||
} | ||
await app.deleteAuthorization({ | ||
token | ||
}); | ||
response.writeHead(204); | ||
return response.end(); | ||
if (!token) { | ||
throw new Error('[@octokit/oauth-app] "Authorization" header is required'); | ||
} | ||
options.onUnhandledRequest(request, response); | ||
await app.deleteAuthorization({ | ||
token | ||
}); | ||
response.writeHead(204); | ||
return response.end(); | ||
} catch (error) { | ||
@@ -619,0 +631,0 @@ response.writeHead(400, { |
import { parseRequest } from "./parse-request"; | ||
export async function middleware(app, options, request, response) { | ||
// request.url mayb include ?query parameters which we don't want for `route` | ||
// hence the workaround using new URL() | ||
const { pathname } = new URL(request.url, "http://localhost"); | ||
const route = [request.method, pathname].join(" "); | ||
const routes = { | ||
getLogin: `GET ${options.pathPrefix}/login`, | ||
getCallback: `GET ${options.pathPrefix}/callback`, | ||
createToken: `POST ${options.pathPrefix}/token`, | ||
getToken: `GET ${options.pathPrefix}/token`, | ||
patchToken: `PATCH ${options.pathPrefix}/token`, | ||
deleteToken: `DELETE ${options.pathPrefix}/token`, | ||
deleteGrant: `DELETE ${options.pathPrefix}/grant`, | ||
}; | ||
if (!Object.values(routes).includes(route)) { | ||
options.onUnhandledRequest(request, response); | ||
return; | ||
} | ||
let parsedRequest; | ||
@@ -15,5 +32,5 @@ try { | ||
} | ||
const { route, headers, query, body } = parsedRequest; | ||
const { headers, query, body } = parsedRequest; | ||
try { | ||
if (route === `GET ${options.pathPrefix}/login`) { | ||
if (route === routes.getLogin) { | ||
const url = app.getAuthorizationUrl({ | ||
@@ -28,3 +45,3 @@ state: query.state, | ||
} | ||
if (route === `GET ${options.pathPrefix}/callback`) { | ||
if (route === routes.getCallback) { | ||
if (query.error) { | ||
@@ -48,3 +65,3 @@ throw new Error(`[@octokit/oauth-app] ${query.error} ${query.error_description}`); | ||
} | ||
if (route === `POST ${options.pathPrefix}/token`) { | ||
if (route === routes.createToken) { | ||
// @ts-ignore body is guaraenteed to exist | ||
@@ -64,3 +81,3 @@ const { state: oauthState, code, redirectUrl } = body; | ||
} | ||
if (route === `GET ${options.pathPrefix}/token`) { | ||
if (route === routes.getToken) { | ||
const token = headers.authorization?.substr("token ".length); | ||
@@ -78,3 +95,3 @@ if (!token) { | ||
} | ||
if (route === `PATCH ${options.pathPrefix}/token`) { | ||
if (route === routes.patchToken) { | ||
const token = headers.authorization?.substr("token ".length); | ||
@@ -92,3 +109,3 @@ if (!token) { | ||
} | ||
if (route === `DELETE ${options.pathPrefix}/token`) { | ||
if (route === routes.deleteToken) { | ||
const token = headers.authorization?.substr("token ".length); | ||
@@ -104,14 +121,12 @@ if (!token) { | ||
} | ||
if (route === `DELETE ${options.pathPrefix}/grant`) { | ||
const token = headers.authorization?.substr("token ".length); | ||
if (!token) { | ||
throw new Error('[@octokit/oauth-app] "Authorization" header is required'); | ||
} | ||
await app.deleteAuthorization({ | ||
token, | ||
}); | ||
response.writeHead(204); | ||
return response.end(); | ||
// route === routes.deleteGrant | ||
const token = headers.authorization?.substr("token ".length); | ||
if (!token) { | ||
throw new Error('[@octokit/oauth-app] "Authorization" header is required'); | ||
} | ||
options.onUnhandledRequest(request, response); | ||
await app.deleteAuthorization({ | ||
token, | ||
}); | ||
response.writeHead(204); | ||
return response.end(); | ||
} | ||
@@ -118,0 +133,0 @@ catch (error) { |
// @ts-ignore remove once Node 10 is out maintenance. Replace with Object.fromEntries | ||
import fromEntries from "fromentries"; | ||
export async function parseRequest(request) { | ||
const { pathname, searchParams } = new URL(request.url, "http://localhost"); | ||
const route = [request.method, pathname].join(" "); | ||
const { searchParams } = new URL(request.url, "http://localhost"); | ||
const query = fromEntries(searchParams); | ||
const headers = request.headers; | ||
if (!["POST", "PATCH"].includes(request.method)) { | ||
return { route, headers, query }; | ||
return { headers, query }; | ||
} | ||
@@ -19,5 +18,5 @@ return new Promise((resolve, reject) => { | ||
if (!bodyString) | ||
return resolve({ route, headers, query }); | ||
return resolve({ headers, query }); | ||
try { | ||
resolve({ route, headers, query, body: JSON.parse(bodyString) }); | ||
resolve({ headers, query, body: JSON.parse(bodyString) }); | ||
} | ||
@@ -24,0 +23,0 @@ catch (error) { |
@@ -1,1 +0,1 @@ | ||
export const VERSION = "2.0.0"; | ||
export const VERSION = "2.0.1"; |
/// <reference types="node" /> | ||
import { IncomingMessage } from "http"; | ||
declare type ParsedRequest = { | ||
route: string; | ||
headers: { | ||
@@ -6,0 +5,0 @@ authorization?: string; |
@@ -1,1 +0,1 @@ | ||
export declare const VERSION = "2.0.0"; | ||
export declare const VERSION = "2.0.1"; |
{ | ||
"name": "@octokit/oauth-app", | ||
"description": "GitHub OAuth toolset for Node.js", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
113200
1280