🚀 feat(_route): add dev to origin

This commit is contained in:
eshanized
2024-12-23 02:19:28 +05:30
parent 0bca0ca1d7
commit 937b1a56ab
16382 changed files with 2985935 additions and 1967 deletions

View File

@@ -0,0 +1 @@
../../../@octokit+auth-oauth-app@7.1.0/node_modules/@octokit/auth-oauth-app

View File

@@ -0,0 +1 @@
../../../@octokit+auth-oauth-user@4.1.0/node_modules/@octokit/auth-oauth-user

View File

@@ -0,0 +1 @@
../../../@octokit+auth-unauthenticated@5.0.1/node_modules/@octokit/auth-unauthenticated

View File

@@ -0,0 +1 @@
../../../@octokit+core@5.2.0/node_modules/@octokit/core

View File

@@ -0,0 +1,7 @@
MIT License Copyright (c) 2020 Octokit contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,806 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// pkg/dist-src/index.js
var dist_src_exports = {};
__export(dist_src_exports, {
OAuthApp: () => OAuthApp,
createAWSLambdaAPIGatewayV2Handler: () => createAWSLambdaAPIGatewayV2Handler,
createNodeMiddleware: () => createNodeMiddleware,
createWebWorkerHandler: () => createWebWorkerHandler,
handleRequest: () => handleRequest,
sendNodeResponse: () => sendResponse,
unknownRouteResponse: () => unknownRouteResponse
});
module.exports = __toCommonJS(dist_src_exports);
var import_auth_oauth_app = require("@octokit/auth-oauth-app");
// pkg/dist-src/version.js
var VERSION = "6.1.0";
// pkg/dist-src/add-event-handler.js
function addEventHandler(state, eventName, eventHandler) {
if (Array.isArray(eventName)) {
for (const singleEventName of eventName) {
addEventHandler(state, singleEventName, eventHandler);
}
return;
}
if (!state.eventHandlers[eventName]) {
state.eventHandlers[eventName] = [];
}
state.eventHandlers[eventName].push(eventHandler);
}
// pkg/dist-src/oauth-app-octokit.js
var import_core = require("@octokit/core");
var import_universal_user_agent = require("universal-user-agent");
var OAuthAppOctokit = import_core.Octokit.defaults({
userAgent: `octokit-oauth-app.js/${VERSION} ${(0, import_universal_user_agent.getUserAgent)()}`
});
// pkg/dist-src/methods/get-user-octokit.js
var import_auth_oauth_user = require("@octokit/auth-oauth-user");
// pkg/dist-src/emit-event.js
async function emitEvent(state, context) {
const { name, action } = context;
if (state.eventHandlers[`${name}.${action}`]) {
for (const eventHandler of state.eventHandlers[`${name}.${action}`]) {
await eventHandler(context);
}
}
if (state.eventHandlers[name]) {
for (const eventHandler of state.eventHandlers[name]) {
await eventHandler(context);
}
}
}
// pkg/dist-src/methods/get-user-octokit.js
async function getUserOctokitWithState(state, options) {
return state.octokit.auth({
type: "oauth-user",
...options,
async factory(options2) {
const octokit = new state.Octokit({
authStrategy: import_auth_oauth_user.createOAuthUserAuth,
auth: options2
});
const authentication = await octokit.auth({
type: "get"
});
await emitEvent(state, {
name: "token",
action: "created",
token: authentication.token,
scopes: authentication.scopes,
authentication,
octokit
});
return octokit;
}
});
}
// pkg/dist-src/methods/get-web-flow-authorization-url.js
var OAuthMethods = __toESM(require("@octokit/oauth-methods"));
function getWebFlowAuthorizationUrlWithState(state, options) {
const optionsWithDefaults = {
clientId: state.clientId,
request: state.octokit.request,
...options,
allowSignup: state.allowSignup ?? options.allowSignup,
redirectUrl: options.redirectUrl ?? state.redirectUrl,
scopes: options.scopes ?? state.defaultScopes
};
return OAuthMethods.getWebFlowAuthorizationUrl({
clientType: state.clientType,
...optionsWithDefaults
});
}
// pkg/dist-src/methods/create-token.js
var OAuthAppAuth = __toESM(require("@octokit/auth-oauth-app"));
async function createTokenWithState(state, options) {
const authentication = await state.octokit.auth({
type: "oauth-user",
...options
});
await emitEvent(state, {
name: "token",
action: "created",
token: authentication.token,
scopes: authentication.scopes,
authentication,
octokit: new state.Octokit({
authStrategy: OAuthAppAuth.createOAuthUserAuth,
auth: {
clientType: state.clientType,
clientId: state.clientId,
clientSecret: state.clientSecret,
token: authentication.token,
scopes: authentication.scopes,
refreshToken: authentication.refreshToken,
expiresAt: authentication.expiresAt,
refreshTokenExpiresAt: authentication.refreshTokenExpiresAt
}
})
});
return { authentication };
}
// pkg/dist-src/methods/check-token.js
var OAuthMethods2 = __toESM(require("@octokit/oauth-methods"));
async function checkTokenWithState(state, options) {
const result = await OAuthMethods2.checkToken({
// @ts-expect-error not worth the extra code to appease TS
clientType: state.clientType,
clientId: state.clientId,
clientSecret: state.clientSecret,
request: state.octokit.request,
...options
});
Object.assign(result.authentication, { type: "token", tokenType: "oauth" });
return result;
}
// pkg/dist-src/methods/reset-token.js
var OAuthMethods3 = __toESM(require("@octokit/oauth-methods"));
var import_auth_oauth_user2 = require("@octokit/auth-oauth-user");
async function resetTokenWithState(state, options) {
const optionsWithDefaults = {
clientId: state.clientId,
clientSecret: state.clientSecret,
request: state.octokit.request,
...options
};
if (state.clientType === "oauth-app") {
const response2 = await OAuthMethods3.resetToken({
clientType: "oauth-app",
...optionsWithDefaults
});
const authentication2 = Object.assign(response2.authentication, {
type: "token",
tokenType: "oauth"
});
await emitEvent(state, {
name: "token",
action: "reset",
token: response2.authentication.token,
scopes: response2.authentication.scopes || void 0,
authentication: authentication2,
octokit: new state.Octokit({
authStrategy: import_auth_oauth_user2.createOAuthUserAuth,
auth: {
clientType: state.clientType,
clientId: state.clientId,
clientSecret: state.clientSecret,
token: response2.authentication.token,
scopes: response2.authentication.scopes
}
})
});
return { ...response2, authentication: authentication2 };
}
const response = await OAuthMethods3.resetToken({
clientType: "github-app",
...optionsWithDefaults
});
const authentication = Object.assign(response.authentication, {
type: "token",
tokenType: "oauth"
});
await emitEvent(state, {
name: "token",
action: "reset",
token: response.authentication.token,
authentication,
octokit: new state.Octokit({
authStrategy: import_auth_oauth_user2.createOAuthUserAuth,
auth: {
clientType: state.clientType,
clientId: state.clientId,
clientSecret: state.clientSecret,
token: response.authentication.token
}
})
});
return { ...response, authentication };
}
// pkg/dist-src/methods/refresh-token.js
var OAuthMethods4 = __toESM(require("@octokit/oauth-methods"));
var import_auth_oauth_user3 = require("@octokit/auth-oauth-user");
async function refreshTokenWithState(state, options) {
if (state.clientType === "oauth-app") {
throw new Error(
"[@octokit/oauth-app] app.refreshToken() is not supported for OAuth Apps"
);
}
const response = await OAuthMethods4.refreshToken({
clientType: "github-app",
clientId: state.clientId,
clientSecret: state.clientSecret,
request: state.octokit.request,
refreshToken: options.refreshToken
});
const authentication = Object.assign(response.authentication, {
type: "token",
tokenType: "oauth"
});
await emitEvent(state, {
name: "token",
action: "refreshed",
token: response.authentication.token,
authentication,
octokit: new state.Octokit({
authStrategy: import_auth_oauth_user3.createOAuthUserAuth,
auth: {
clientType: state.clientType,
clientId: state.clientId,
clientSecret: state.clientSecret,
token: response.authentication.token
}
})
});
return { ...response, authentication };
}
// pkg/dist-src/methods/scope-token.js
var OAuthMethods5 = __toESM(require("@octokit/oauth-methods"));
var import_auth_oauth_user4 = require("@octokit/auth-oauth-user");
async function scopeTokenWithState(state, options) {
if (state.clientType === "oauth-app") {
throw new Error(
"[@octokit/oauth-app] app.scopeToken() is not supported for OAuth Apps"
);
}
const response = await OAuthMethods5.scopeToken({
clientType: "github-app",
clientId: state.clientId,
clientSecret: state.clientSecret,
request: state.octokit.request,
...options
});
const authentication = Object.assign(response.authentication, {
type: "token",
tokenType: "oauth"
});
await emitEvent(state, {
name: "token",
action: "scoped",
token: response.authentication.token,
authentication,
octokit: new state.Octokit({
authStrategy: import_auth_oauth_user4.createOAuthUserAuth,
auth: {
clientType: state.clientType,
clientId: state.clientId,
clientSecret: state.clientSecret,
token: response.authentication.token
}
})
});
return { ...response, authentication };
}
// pkg/dist-src/methods/delete-token.js
var OAuthMethods6 = __toESM(require("@octokit/oauth-methods"));
var import_auth_unauthenticated = require("@octokit/auth-unauthenticated");
async function deleteTokenWithState(state, options) {
const optionsWithDefaults = {
clientId: state.clientId,
clientSecret: state.clientSecret,
request: state.octokit.request,
...options
};
const response = state.clientType === "oauth-app" ? await OAuthMethods6.deleteToken({
clientType: "oauth-app",
...optionsWithDefaults
}) : (
// istanbul ignore next
await OAuthMethods6.deleteToken({
clientType: "github-app",
...optionsWithDefaults
})
);
await emitEvent(state, {
name: "token",
action: "deleted",
token: options.token,
octokit: new state.Octokit({
authStrategy: import_auth_unauthenticated.createUnauthenticatedAuth,
auth: {
reason: `Handling "token.deleted" event. The access for the token has been revoked.`
}
})
});
return response;
}
// pkg/dist-src/methods/delete-authorization.js
var OAuthMethods7 = __toESM(require("@octokit/oauth-methods"));
var import_auth_unauthenticated2 = require("@octokit/auth-unauthenticated");
async function deleteAuthorizationWithState(state, options) {
const optionsWithDefaults = {
clientId: state.clientId,
clientSecret: state.clientSecret,
request: state.octokit.request,
...options
};
const response = state.clientType === "oauth-app" ? await OAuthMethods7.deleteAuthorization({
clientType: "oauth-app",
...optionsWithDefaults
}) : (
// istanbul ignore next
await OAuthMethods7.deleteAuthorization({
clientType: "github-app",
...optionsWithDefaults
})
);
await emitEvent(state, {
name: "token",
action: "deleted",
token: options.token,
octokit: new state.Octokit({
authStrategy: import_auth_unauthenticated2.createUnauthenticatedAuth,
auth: {
reason: `Handling "token.deleted" event. The access for the token has been revoked.`
}
})
});
await emitEvent(state, {
name: "authorization",
action: "deleted",
token: options.token,
octokit: new state.Octokit({
authStrategy: import_auth_unauthenticated2.createUnauthenticatedAuth,
auth: {
reason: `Handling "authorization.deleted" event. The access for the app has been revoked.`
}
})
});
return response;
}
// pkg/dist-src/middleware/unknown-route-response.js
function unknownRouteResponse(request) {
return {
status: 404,
headers: { "content-type": "application/json" },
text: JSON.stringify({
error: `Unknown route: ${request.method} ${request.url}`
})
};
}
// pkg/dist-src/middleware/handle-request.js
async function handleRequest(app, { pathPrefix = "/api/github/oauth" }, request) {
if (request.method === "OPTIONS") {
return {
status: 200,
headers: {
"access-control-allow-origin": "*",
"access-control-allow-methods": "*",
"access-control-allow-headers": "Content-Type, User-Agent, Authorization"
}
};
}
let { pathname } = new URL(request.url, "http://localhost");
if (!pathname.startsWith(`${pathPrefix}/`)) {
return void 0;
}
pathname = pathname.slice(pathPrefix.length + 1);
const route = [request.method, pathname].join(" ");
const routes = {
getLogin: `GET login`,
getCallback: `GET callback`,
createToken: `POST token`,
getToken: `GET token`,
patchToken: `PATCH token`,
patchRefreshToken: `PATCH refresh-token`,
scopeToken: `POST token/scoped`,
deleteToken: `DELETE token`,
deleteGrant: `DELETE grant`
};
if (!Object.values(routes).includes(route)) {
return unknownRouteResponse(request);
}
let json;
try {
const text = await request.text();
json = text ? JSON.parse(text) : {};
} catch (error) {
return {
status: 400,
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*"
},
text: JSON.stringify({
error: "[@octokit/oauth-app] request error"
})
};
}
const { searchParams } = new URL(request.url, "http://localhost");
const query = Object.fromEntries(searchParams);
const headers = request.headers;
try {
if (route === routes.getLogin) {
const { url } = app.getWebFlowAuthorizationUrl({
state: query.state,
scopes: query.scopes ? query.scopes.split(",") : void 0,
allowSignup: query.allowSignup ? query.allowSignup === "true" : void 0,
redirectUrl: query.redirectUrl
});
return { status: 302, headers: { location: url } };
}
if (route === routes.getCallback) {
if (query.error) {
throw new Error(
`[@octokit/oauth-app] ${query.error} ${query.error_description}`
);
}
if (!query.code) {
throw new Error('[@octokit/oauth-app] "code" parameter is required');
}
const {
authentication: { token: token2 }
} = await app.createToken({
code: query.code
});
return {
status: 200,
headers: {
"content-type": "text/html"
},
text: `<h1>Token created successfully</h1>
<p>Your token is: <strong>${token2}</strong>. Copy it now as it cannot be shown again.</p>`
};
}
if (route === routes.createToken) {
const { code, redirectUrl } = json;
if (!code) {
throw new Error('[@octokit/oauth-app] "code" parameter is required');
}
const result = await app.createToken({
code,
redirectUrl
});
delete result.authentication.clientSecret;
return {
status: 201,
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*"
},
text: JSON.stringify(result)
};
}
if (route === routes.getToken) {
const token2 = headers.authorization?.substr("token ".length);
if (!token2) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required'
);
}
const result = await app.checkToken({
token: token2
});
delete result.authentication.clientSecret;
return {
status: 200,
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*"
},
text: JSON.stringify(result)
};
}
if (route === routes.patchToken) {
const token2 = headers.authorization?.substr("token ".length);
if (!token2) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required'
);
}
const result = await app.resetToken({ token: token2 });
delete result.authentication.clientSecret;
return {
status: 200,
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*"
},
text: JSON.stringify(result)
};
}
if (route === routes.patchRefreshToken) {
const token2 = headers.authorization?.substr("token ".length);
if (!token2) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required'
);
}
const { refreshToken: refreshToken2 } = json;
if (!refreshToken2) {
throw new Error(
"[@octokit/oauth-app] refreshToken must be sent in request body"
);
}
const result = await app.refreshToken({ refreshToken: refreshToken2 });
delete result.authentication.clientSecret;
return {
status: 200,
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*"
},
text: JSON.stringify(result)
};
}
if (route === routes.scopeToken) {
const token2 = headers.authorization?.substr("token ".length);
if (!token2) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required'
);
}
const result = await app.scopeToken({
token: token2,
...json
});
delete result.authentication.clientSecret;
return {
status: 200,
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*"
},
text: JSON.stringify(result)
};
}
if (route === routes.deleteToken) {
const token2 = headers.authorization?.substr("token ".length);
if (!token2) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required'
);
}
await app.deleteToken({
token: token2
});
return {
status: 204,
headers: { "access-control-allow-origin": "*" }
};
}
const token = headers.authorization?.substr("token ".length);
if (!token) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required'
);
}
await app.deleteAuthorization({
token
});
return {
status: 204,
headers: { "access-control-allow-origin": "*" }
};
} catch (error) {
return {
status: 400,
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*"
},
text: JSON.stringify({ error: error.message })
};
}
}
// pkg/dist-src/middleware/node/parse-request.js
function parseRequest(request) {
const { method, url, headers } = request;
async function text() {
const text2 = await new Promise((resolve, reject) => {
let bodyChunks = [];
request.on("error", reject).on("data", (chunk) => bodyChunks.push(chunk)).on("end", () => resolve(Buffer.concat(bodyChunks).toString()));
});
return text2;
}
return { method, url, headers, text };
}
// pkg/dist-src/middleware/node/send-response.js
function sendResponse(octokitResponse, response) {
response.writeHead(octokitResponse.status, octokitResponse.headers);
response.end(octokitResponse.text);
}
// pkg/dist-src/middleware/node/index.js
function createNodeMiddleware(app, options = {}) {
return async function(request, response, next) {
const octokitRequest = await parseRequest(request);
const octokitResponse = await handleRequest(app, options, octokitRequest);
if (octokitResponse) {
sendResponse(octokitResponse, response);
return true;
} else {
next?.();
return false;
}
};
}
// pkg/dist-src/middleware/web-worker/parse-request.js
function parseRequest2(request) {
const headers = Object.fromEntries(request.headers.entries());
return {
method: request.method,
url: request.url,
headers,
text: () => request.text()
};
}
// pkg/dist-src/middleware/web-worker/send-response.js
function sendResponse2(octokitResponse) {
return new Response(octokitResponse.text, {
status: octokitResponse.status,
headers: octokitResponse.headers
});
}
// pkg/dist-src/middleware/web-worker/index.js
function createWebWorkerHandler(app, options = {}) {
return async function(request) {
const octokitRequest = await parseRequest2(request);
const octokitResponse = await handleRequest(app, options, octokitRequest);
return octokitResponse ? sendResponse2(octokitResponse) : void 0;
};
}
// pkg/dist-src/middleware/aws-lambda/api-gateway-v2-parse-request.js
function parseRequest3(request) {
const { method } = request.requestContext.http;
let url = request.rawPath;
const { stage } = request.requestContext;
if (url.startsWith("/" + stage))
url = url.substring(stage.length + 1);
if (request.rawQueryString)
url += "?" + request.rawQueryString;
const headers = request.headers;
const text = async () => request.body || "";
return { method, url, headers, text };
}
// pkg/dist-src/middleware/aws-lambda/api-gateway-v2-send-response.js
function sendResponse3(octokitResponse) {
return {
statusCode: octokitResponse.status,
headers: octokitResponse.headers,
body: octokitResponse.text
};
}
// pkg/dist-src/middleware/aws-lambda/api-gateway-v2.js
function createAWSLambdaAPIGatewayV2Handler(app, options = {}) {
return async function(event) {
const request = parseRequest3(event);
const response = await handleRequest(app, options, request);
return response ? sendResponse3(response) : void 0;
};
}
// pkg/dist-src/index.js
var OAuthApp = class {
static {
this.VERSION = VERSION;
}
static defaults(defaults) {
const OAuthAppWithDefaults = class extends this {
constructor(...args) {
super({
...defaults,
...args[0]
});
}
};
return OAuthAppWithDefaults;
}
constructor(options) {
const Octokit2 = options.Octokit || OAuthAppOctokit;
this.type = options.clientType || "oauth-app";
const octokit = new Octokit2({
authStrategy: import_auth_oauth_app.createOAuthAppAuth,
auth: {
clientType: this.type,
clientId: options.clientId,
clientSecret: options.clientSecret
}
});
const state = {
clientType: this.type,
clientId: options.clientId,
clientSecret: options.clientSecret,
// @ts-expect-error defaultScopes not permitted for GitHub Apps
defaultScopes: options.defaultScopes || [],
allowSignup: options.allowSignup,
baseUrl: options.baseUrl,
redirectUrl: options.redirectUrl,
log: options.log,
Octokit: Octokit2,
octokit,
eventHandlers: {}
};
this.on = addEventHandler.bind(null, state);
this.octokit = octokit;
this.getUserOctokit = getUserOctokitWithState.bind(null, state);
this.getWebFlowAuthorizationUrl = getWebFlowAuthorizationUrlWithState.bind(
null,
state
);
this.createToken = createTokenWithState.bind(
null,
state
);
this.checkToken = checkTokenWithState.bind(
null,
state
);
this.resetToken = resetTokenWithState.bind(
null,
state
);
this.refreshToken = refreshTokenWithState.bind(
null,
state
);
this.scopeToken = scopeTokenWithState.bind(
null,
state
);
this.deleteToken = deleteTokenWithState.bind(null, state);
this.deleteAuthorization = deleteAuthorizationWithState.bind(null, state);
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
OAuthApp,
createAWSLambdaAPIGatewayV2Handler,
createNodeMiddleware,
createWebWorkerHandler,
handleRequest,
sendNodeResponse,
unknownRouteResponse
});

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,15 @@
function addEventHandler(state, eventName, eventHandler) {
if (Array.isArray(eventName)) {
for (const singleEventName of eventName) {
addEventHandler(state, singleEventName, eventHandler);
}
return;
}
if (!state.eventHandlers[eventName]) {
state.eventHandlers[eventName] = [];
}
state.eventHandlers[eventName].push(eventHandler);
}
export {
addEventHandler
};

View File

@@ -0,0 +1,16 @@
async function emitEvent(state, context) {
const { name, action } = context;
if (state.eventHandlers[`${name}.${action}`]) {
for (const eventHandler of state.eventHandlers[`${name}.${action}`]) {
await eventHandler(context);
}
}
if (state.eventHandlers[name]) {
for (const eventHandler of state.eventHandlers[name]) {
await eventHandler(context);
}
}
}
export {
emitEvent
};

View File

@@ -0,0 +1,117 @@
import { createOAuthAppAuth } from "@octokit/auth-oauth-app";
import { VERSION } from "./version";
import { addEventHandler } from "./add-event-handler";
import { OAuthAppOctokit } from "./oauth-app-octokit";
import {
getUserOctokitWithState
} from "./methods/get-user-octokit.js";
import {
getWebFlowAuthorizationUrlWithState
} from "./methods/get-web-flow-authorization-url.js";
import {
createTokenWithState
} from "./methods/create-token.js";
import {
checkTokenWithState
} from "./methods/check-token.js";
import {
resetTokenWithState
} from "./methods/reset-token.js";
import {
refreshTokenWithState
} from "./methods/refresh-token.js";
import {
scopeTokenWithState
} from "./methods/scope-token.js";
import {
deleteTokenWithState
} from "./methods/delete-token.js";
import {
deleteAuthorizationWithState
} from "./methods/delete-authorization.js";
import { handleRequest } from "./middleware/handle-request.js";
import { unknownRouteResponse } from "./middleware/unknown-route-response.js";
import { createNodeMiddleware } from "./middleware/node/index.js";
import { sendResponse } from "./middleware/node/send-response.js";
import { createWebWorkerHandler } from "./middleware/web-worker/index.js";
import { createAWSLambdaAPIGatewayV2Handler } from "./middleware/aws-lambda/api-gateway-v2.js";
class OAuthApp {
static {
this.VERSION = VERSION;
}
static defaults(defaults) {
const OAuthAppWithDefaults = class extends this {
constructor(...args) {
super({
...defaults,
...args[0]
});
}
};
return OAuthAppWithDefaults;
}
constructor(options) {
const Octokit = options.Octokit || OAuthAppOctokit;
this.type = options.clientType || "oauth-app";
const octokit = new Octokit({
authStrategy: createOAuthAppAuth,
auth: {
clientType: this.type,
clientId: options.clientId,
clientSecret: options.clientSecret
}
});
const state = {
clientType: this.type,
clientId: options.clientId,
clientSecret: options.clientSecret,
// @ts-expect-error defaultScopes not permitted for GitHub Apps
defaultScopes: options.defaultScopes || [],
allowSignup: options.allowSignup,
baseUrl: options.baseUrl,
redirectUrl: options.redirectUrl,
log: options.log,
Octokit,
octokit,
eventHandlers: {}
};
this.on = addEventHandler.bind(null, state);
this.octokit = octokit;
this.getUserOctokit = getUserOctokitWithState.bind(null, state);
this.getWebFlowAuthorizationUrl = getWebFlowAuthorizationUrlWithState.bind(
null,
state
);
this.createToken = createTokenWithState.bind(
null,
state
);
this.checkToken = checkTokenWithState.bind(
null,
state
);
this.resetToken = resetTokenWithState.bind(
null,
state
);
this.refreshToken = refreshTokenWithState.bind(
null,
state
);
this.scopeToken = scopeTokenWithState.bind(
null,
state
);
this.deleteToken = deleteTokenWithState.bind(null, state);
this.deleteAuthorization = deleteAuthorizationWithState.bind(null, state);
}
}
export {
OAuthApp,
createAWSLambdaAPIGatewayV2Handler,
createNodeMiddleware,
createWebWorkerHandler,
handleRequest,
sendResponse as sendNodeResponse,
unknownRouteResponse
};

View File

@@ -0,0 +1,16 @@
import * as OAuthMethods from "@octokit/oauth-methods";
async function checkTokenWithState(state, options) {
const result = await OAuthMethods.checkToken({
// @ts-expect-error not worth the extra code to appease TS
clientType: state.clientType,
clientId: state.clientId,
clientSecret: state.clientSecret,
request: state.octokit.request,
...options
});
Object.assign(result.authentication, { type: "token", tokenType: "oauth" });
return result;
}
export {
checkTokenWithState
};

View File

@@ -0,0 +1,32 @@
import * as OAuthAppAuth from "@octokit/auth-oauth-app";
import { emitEvent } from "../emit-event.js";
async function createTokenWithState(state, options) {
const authentication = await state.octokit.auth({
type: "oauth-user",
...options
});
await emitEvent(state, {
name: "token",
action: "created",
token: authentication.token,
scopes: authentication.scopes,
authentication,
octokit: new state.Octokit({
authStrategy: OAuthAppAuth.createOAuthUserAuth,
auth: {
clientType: state.clientType,
clientId: state.clientId,
clientSecret: state.clientSecret,
token: authentication.token,
scopes: authentication.scopes,
refreshToken: authentication.refreshToken,
expiresAt: authentication.expiresAt,
refreshTokenExpiresAt: authentication.refreshTokenExpiresAt
}
})
});
return { authentication };
}
export {
createTokenWithState
};

View File

@@ -0,0 +1,47 @@
import * as OAuthMethods from "@octokit/oauth-methods";
import { createUnauthenticatedAuth } from "@octokit/auth-unauthenticated";
import { emitEvent } from "../emit-event.js";
async function deleteAuthorizationWithState(state, options) {
const optionsWithDefaults = {
clientId: state.clientId,
clientSecret: state.clientSecret,
request: state.octokit.request,
...options
};
const response = state.clientType === "oauth-app" ? await OAuthMethods.deleteAuthorization({
clientType: "oauth-app",
...optionsWithDefaults
}) : (
// istanbul ignore next
await OAuthMethods.deleteAuthorization({
clientType: "github-app",
...optionsWithDefaults
})
);
await emitEvent(state, {
name: "token",
action: "deleted",
token: options.token,
octokit: new state.Octokit({
authStrategy: createUnauthenticatedAuth,
auth: {
reason: `Handling "token.deleted" event. The access for the token has been revoked.`
}
})
});
await emitEvent(state, {
name: "authorization",
action: "deleted",
token: options.token,
octokit: new state.Octokit({
authStrategy: createUnauthenticatedAuth,
auth: {
reason: `Handling "authorization.deleted" event. The access for the app has been revoked.`
}
})
});
return response;
}
export {
deleteAuthorizationWithState
};

View File

@@ -0,0 +1,36 @@
import * as OAuthMethods from "@octokit/oauth-methods";
import { createUnauthenticatedAuth } from "@octokit/auth-unauthenticated";
import { emitEvent } from "../emit-event.js";
async function deleteTokenWithState(state, options) {
const optionsWithDefaults = {
clientId: state.clientId,
clientSecret: state.clientSecret,
request: state.octokit.request,
...options
};
const response = state.clientType === "oauth-app" ? await OAuthMethods.deleteToken({
clientType: "oauth-app",
...optionsWithDefaults
}) : (
// istanbul ignore next
await OAuthMethods.deleteToken({
clientType: "github-app",
...optionsWithDefaults
})
);
await emitEvent(state, {
name: "token",
action: "deleted",
token: options.token,
octokit: new state.Octokit({
authStrategy: createUnauthenticatedAuth,
auth: {
reason: `Handling "token.deleted" event. The access for the token has been revoked.`
}
})
});
return response;
}
export {
deleteTokenWithState
};

View File

@@ -0,0 +1,10 @@
function getOAuthClientCode() {
return `import { Octokit: Core } from "https://esm.sh/@octokit/core";
export const Octokit = Core.defaults({
oauth: {}
})`;
}
export {
getOAuthClientCode
};

View File

@@ -0,0 +1,29 @@
import { createOAuthUserAuth } from "@octokit/auth-oauth-user";
import { emitEvent } from "../emit-event.js";
async function getUserOctokitWithState(state, options) {
return state.octokit.auth({
type: "oauth-user",
...options,
async factory(options2) {
const octokit = new state.Octokit({
authStrategy: createOAuthUserAuth,
auth: options2
});
const authentication = await octokit.auth({
type: "get"
});
await emitEvent(state, {
name: "token",
action: "created",
token: authentication.token,
scopes: authentication.scopes,
authentication,
octokit
});
return octokit;
}
});
}
export {
getUserOctokitWithState
};

View File

@@ -0,0 +1,18 @@
import * as OAuthMethods from "@octokit/oauth-methods";
function getWebFlowAuthorizationUrlWithState(state, options) {
const optionsWithDefaults = {
clientId: state.clientId,
request: state.octokit.request,
...options,
allowSignup: state.allowSignup ?? options.allowSignup,
redirectUrl: options.redirectUrl ?? state.redirectUrl,
scopes: options.scopes ?? state.defaultScopes
};
return OAuthMethods.getWebFlowAuthorizationUrl({
clientType: state.clientType,
...optionsWithDefaults
});
}
export {
getWebFlowAuthorizationUrlWithState
};

View File

@@ -0,0 +1,40 @@
import * as OAuthMethods from "@octokit/oauth-methods";
import { emitEvent } from "../emit-event.js";
import { createOAuthUserAuth } from "@octokit/auth-oauth-user";
async function refreshTokenWithState(state, options) {
if (state.clientType === "oauth-app") {
throw new Error(
"[@octokit/oauth-app] app.refreshToken() is not supported for OAuth Apps"
);
}
const response = await OAuthMethods.refreshToken({
clientType: "github-app",
clientId: state.clientId,
clientSecret: state.clientSecret,
request: state.octokit.request,
refreshToken: options.refreshToken
});
const authentication = Object.assign(response.authentication, {
type: "token",
tokenType: "oauth"
});
await emitEvent(state, {
name: "token",
action: "refreshed",
token: response.authentication.token,
authentication,
octokit: new state.Octokit({
authStrategy: createOAuthUserAuth,
auth: {
clientType: state.clientType,
clientId: state.clientId,
clientSecret: state.clientSecret,
token: response.authentication.token
}
})
});
return { ...response, authentication };
}
export {
refreshTokenWithState
};

View File

@@ -0,0 +1,66 @@
import * as OAuthMethods from "@octokit/oauth-methods";
import { emitEvent } from "../emit-event.js";
import { createOAuthUserAuth } from "@octokit/auth-oauth-user";
async function resetTokenWithState(state, options) {
const optionsWithDefaults = {
clientId: state.clientId,
clientSecret: state.clientSecret,
request: state.octokit.request,
...options
};
if (state.clientType === "oauth-app") {
const response2 = await OAuthMethods.resetToken({
clientType: "oauth-app",
...optionsWithDefaults
});
const authentication2 = Object.assign(response2.authentication, {
type: "token",
tokenType: "oauth"
});
await emitEvent(state, {
name: "token",
action: "reset",
token: response2.authentication.token,
scopes: response2.authentication.scopes || void 0,
authentication: authentication2,
octokit: new state.Octokit({
authStrategy: createOAuthUserAuth,
auth: {
clientType: state.clientType,
clientId: state.clientId,
clientSecret: state.clientSecret,
token: response2.authentication.token,
scopes: response2.authentication.scopes
}
})
});
return { ...response2, authentication: authentication2 };
}
const response = await OAuthMethods.resetToken({
clientType: "github-app",
...optionsWithDefaults
});
const authentication = Object.assign(response.authentication, {
type: "token",
tokenType: "oauth"
});
await emitEvent(state, {
name: "token",
action: "reset",
token: response.authentication.token,
authentication,
octokit: new state.Octokit({
authStrategy: createOAuthUserAuth,
auth: {
clientType: state.clientType,
clientId: state.clientId,
clientSecret: state.clientSecret,
token: response.authentication.token
}
})
});
return { ...response, authentication };
}
export {
resetTokenWithState
};

View File

@@ -0,0 +1,40 @@
import * as OAuthMethods from "@octokit/oauth-methods";
import { createOAuthUserAuth } from "@octokit/auth-oauth-user";
import { emitEvent } from "../emit-event.js";
async function scopeTokenWithState(state, options) {
if (state.clientType === "oauth-app") {
throw new Error(
"[@octokit/oauth-app] app.scopeToken() is not supported for OAuth Apps"
);
}
const response = await OAuthMethods.scopeToken({
clientType: "github-app",
clientId: state.clientId,
clientSecret: state.clientSecret,
request: state.octokit.request,
...options
});
const authentication = Object.assign(response.authentication, {
type: "token",
tokenType: "oauth"
});
await emitEvent(state, {
name: "token",
action: "scoped",
token: response.authentication.token,
authentication,
octokit: new state.Octokit({
authStrategy: createOAuthUserAuth,
auth: {
clientType: state.clientType,
clientId: state.clientId,
clientSecret: state.clientSecret,
token: response.authentication.token
}
})
});
return { ...response, authentication };
}
export {
scopeTokenWithState
};

View File

@@ -0,0 +1,15 @@
function parseRequest(request) {
const { method } = request.requestContext.http;
let url = request.rawPath;
const { stage } = request.requestContext;
if (url.startsWith("/" + stage))
url = url.substring(stage.length + 1);
if (request.rawQueryString)
url += "?" + request.rawQueryString;
const headers = request.headers;
const text = async () => request.body || "";
return { method, url, headers, text };
}
export {
parseRequest
};

View File

@@ -0,0 +1,10 @@
function sendResponse(octokitResponse) {
return {
statusCode: octokitResponse.status,
headers: octokitResponse.headers,
body: octokitResponse.text
};
}
export {
sendResponse
};

View File

@@ -0,0 +1,13 @@
import { parseRequest } from "./api-gateway-v2-parse-request";
import { sendResponse } from "./api-gateway-v2-send-response.js";
import { handleRequest } from "../handle-request.js";
function createAWSLambdaAPIGatewayV2Handler(app, options = {}) {
return async function(event) {
const request = parseRequest(event);
const response = await handleRequest(app, options, request);
return response ? sendResponse(response) : void 0;
};
}
export {
createAWSLambdaAPIGatewayV2Handler
};

View File

@@ -0,0 +1,230 @@
import { OAuthApp } from "../index.js";
import { unknownRouteResponse } from "./unknown-route-response.js";
async function handleRequest(app, { pathPrefix = "/api/github/oauth" }, request) {
if (request.method === "OPTIONS") {
return {
status: 200,
headers: {
"access-control-allow-origin": "*",
"access-control-allow-methods": "*",
"access-control-allow-headers": "Content-Type, User-Agent, Authorization"
}
};
}
let { pathname } = new URL(request.url, "http://localhost");
if (!pathname.startsWith(`${pathPrefix}/`)) {
return void 0;
}
pathname = pathname.slice(pathPrefix.length + 1);
const route = [request.method, pathname].join(" ");
const routes = {
getLogin: `GET login`,
getCallback: `GET callback`,
createToken: `POST token`,
getToken: `GET token`,
patchToken: `PATCH token`,
patchRefreshToken: `PATCH refresh-token`,
scopeToken: `POST token/scoped`,
deleteToken: `DELETE token`,
deleteGrant: `DELETE grant`
};
if (!Object.values(routes).includes(route)) {
return unknownRouteResponse(request);
}
let json;
try {
const text = await request.text();
json = text ? JSON.parse(text) : {};
} catch (error) {
return {
status: 400,
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*"
},
text: JSON.stringify({
error: "[@octokit/oauth-app] request error"
})
};
}
const { searchParams } = new URL(request.url, "http://localhost");
const query = Object.fromEntries(searchParams);
const headers = request.headers;
try {
if (route === routes.getLogin) {
const { url } = app.getWebFlowAuthorizationUrl({
state: query.state,
scopes: query.scopes ? query.scopes.split(",") : void 0,
allowSignup: query.allowSignup ? query.allowSignup === "true" : void 0,
redirectUrl: query.redirectUrl
});
return { status: 302, headers: { location: url } };
}
if (route === routes.getCallback) {
if (query.error) {
throw new Error(
`[@octokit/oauth-app] ${query.error} ${query.error_description}`
);
}
if (!query.code) {
throw new Error('[@octokit/oauth-app] "code" parameter is required');
}
const {
authentication: { token: token2 }
} = await app.createToken({
code: query.code
});
return {
status: 200,
headers: {
"content-type": "text/html"
},
text: `<h1>Token created successfully</h1>
<p>Your token is: <strong>${token2}</strong>. Copy it now as it cannot be shown again.</p>`
};
}
if (route === routes.createToken) {
const { code, redirectUrl } = json;
if (!code) {
throw new Error('[@octokit/oauth-app] "code" parameter is required');
}
const result = await app.createToken({
code,
redirectUrl
});
delete result.authentication.clientSecret;
return {
status: 201,
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*"
},
text: JSON.stringify(result)
};
}
if (route === routes.getToken) {
const token2 = headers.authorization?.substr("token ".length);
if (!token2) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required'
);
}
const result = await app.checkToken({
token: token2
});
delete result.authentication.clientSecret;
return {
status: 200,
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*"
},
text: JSON.stringify(result)
};
}
if (route === routes.patchToken) {
const token2 = headers.authorization?.substr("token ".length);
if (!token2) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required'
);
}
const result = await app.resetToken({ token: token2 });
delete result.authentication.clientSecret;
return {
status: 200,
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*"
},
text: JSON.stringify(result)
};
}
if (route === routes.patchRefreshToken) {
const token2 = headers.authorization?.substr("token ".length);
if (!token2) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required'
);
}
const { refreshToken } = json;
if (!refreshToken) {
throw new Error(
"[@octokit/oauth-app] refreshToken must be sent in request body"
);
}
const result = await app.refreshToken({ refreshToken });
delete result.authentication.clientSecret;
return {
status: 200,
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*"
},
text: JSON.stringify(result)
};
}
if (route === routes.scopeToken) {
const token2 = headers.authorization?.substr("token ".length);
if (!token2) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required'
);
}
const result = await app.scopeToken({
token: token2,
...json
});
delete result.authentication.clientSecret;
return {
status: 200,
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*"
},
text: JSON.stringify(result)
};
}
if (route === routes.deleteToken) {
const token2 = headers.authorization?.substr("token ".length);
if (!token2) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required'
);
}
await app.deleteToken({
token: token2
});
return {
status: 204,
headers: { "access-control-allow-origin": "*" }
};
}
const token = headers.authorization?.substr("token ".length);
if (!token) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required'
);
}
await app.deleteAuthorization({
token
});
return {
status: 204,
headers: { "access-control-allow-origin": "*" }
};
} catch (error) {
return {
status: 400,
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*"
},
text: JSON.stringify({ error: error.message })
};
}
}
export {
handleRequest
};

View File

@@ -0,0 +1,19 @@
import { parseRequest } from "./parse-request.js";
import { sendResponse } from "./send-response.js";
import { handleRequest } from "../handle-request.js";
function createNodeMiddleware(app, options = {}) {
return async function(request, response, next) {
const octokitRequest = await parseRequest(request);
const octokitResponse = await handleRequest(app, options, octokitRequest);
if (octokitResponse) {
sendResponse(octokitResponse, response);
return true;
} else {
next?.();
return false;
}
};
}
export {
createNodeMiddleware
};

View File

@@ -0,0 +1,14 @@
function parseRequest(request) {
const { method, url, headers } = request;
async function text() {
const text2 = await new Promise((resolve, reject) => {
let bodyChunks = [];
request.on("error", reject).on("data", (chunk) => bodyChunks.push(chunk)).on("end", () => resolve(Buffer.concat(bodyChunks).toString()));
});
return text2;
}
return { method, url, headers, text };
}
export {
parseRequest
};

View File

@@ -0,0 +1,7 @@
function sendResponse(octokitResponse, response) {
response.writeHead(octokitResponse.status, octokitResponse.headers);
response.end(octokitResponse.text);
}
export {
sendResponse
};

View File

@@ -0,0 +1,12 @@
function unknownRouteResponse(request) {
return {
status: 404,
headers: { "content-type": "application/json" },
text: JSON.stringify({
error: `Unknown route: ${request.method} ${request.url}`
})
};
}
export {
unknownRouteResponse
};

View File

@@ -0,0 +1,13 @@
import { parseRequest } from "./parse-request.js";
import { sendResponse } from "./send-response.js";
import { handleRequest } from "../handle-request.js";
function createWebWorkerHandler(app, options = {}) {
return async function(request) {
const octokitRequest = await parseRequest(request);
const octokitResponse = await handleRequest(app, options, octokitRequest);
return octokitResponse ? sendResponse(octokitResponse) : void 0;
};
}
export {
createWebWorkerHandler
};

View File

@@ -0,0 +1,12 @@
function parseRequest(request) {
const headers = Object.fromEntries(request.headers.entries());
return {
method: request.method,
url: request.url,
headers,
text: () => request.text()
};
}
export {
parseRequest
};

View File

@@ -0,0 +1,9 @@
function sendResponse(octokitResponse) {
return new Response(octokitResponse.text, {
status: octokitResponse.status,
headers: octokitResponse.headers
});
}
export {
sendResponse
};

View File

@@ -0,0 +1,9 @@
import { Octokit } from "@octokit/core";
import { getUserAgent } from "universal-user-agent";
import { VERSION } from "./version.js";
const OAuthAppOctokit = Octokit.defaults({
userAgent: `octokit-oauth-app.js/${VERSION} ${getUserAgent()}`
});
export {
OAuthAppOctokit
};

View File

@@ -0,0 +1,4 @@
const VERSION = "6.1.0";
export {
VERSION
};

View File

@@ -0,0 +1,2 @@
import type { EventHandler, EventAndActionName, State, ClientType, Options } from "./types.js";
export declare function addEventHandler(state: State, eventName: EventAndActionName | EventAndActionName[], eventHandler: EventHandler<Options<ClientType>>): void;

View File

@@ -0,0 +1,2 @@
import type { State, EventHandlerContext, ClientType, Options } from "./types.js";
export declare function emitEvent(state: State, context: EventHandlerContext<Options<ClientType>>): Promise<void>;

View File

@@ -0,0 +1,50 @@
import { type GetUserOctokitWithStateInterface } from "./methods/get-user-octokit.js";
import { type GetWebFlowAuthorizationUrlInterface } from "./methods/get-web-flow-authorization-url.js";
import { type CreateTokenInterface } from "./methods/create-token.js";
import { type CheckTokenInterface } from "./methods/check-token.js";
import { type ResetTokenInterface } from "./methods/reset-token.js";
import { type RefreshTokenInterface } from "./methods/refresh-token.js";
import { type ScopeTokenInterface } from "./methods/scope-token.js";
import { type DeleteTokenInterface } from "./methods/delete-token.js";
import { type DeleteAuthorizationInterface } from "./methods/delete-authorization.js";
import type { AddEventHandler, ClientType, ClientTypeFromOptions, ConstructorOptions, OctokitTypeFromOptions, Options } from "./types.js";
export type { HandlerOptions, OctokitRequest, OctokitResponse, } from "./middleware/types.js";
export { handleRequest } from "./middleware/handle-request.js";
export { unknownRouteResponse } from "./middleware/unknown-route-response.js";
export { createNodeMiddleware } from "./middleware/node/index.js";
export { sendResponse as sendNodeResponse } from "./middleware/node/send-response.js";
export { createWebWorkerHandler } from "./middleware/web-worker/index.js";
export { createAWSLambdaAPIGatewayV2Handler } from "./middleware/aws-lambda/api-gateway-v2.js";
type Constructor<T> = new (...args: any[]) => T;
export declare class OAuthApp<TOptions extends Options<ClientType> = Options<"oauth-app">> {
static VERSION: string;
static defaults<TDefaults extends Options<ClientType>, S extends Constructor<OAuthApp<TDefaults>>>(this: S, defaults: TDefaults): {
new (...args: any[]): {
type: ClientTypeFromOptions<TDefaults>;
on: AddEventHandler<TDefaults>;
octokit: OctokitTypeFromOptions<TDefaults>;
getUserOctokit: GetUserOctokitWithStateInterface<ClientTypeFromOptions<TDefaults>>;
getWebFlowAuthorizationUrl: GetWebFlowAuthorizationUrlInterface<ClientTypeFromOptions<TDefaults>>;
createToken: CreateTokenInterface<ClientTypeFromOptions<TDefaults>>;
checkToken: CheckTokenInterface<ClientTypeFromOptions<TDefaults>>;
resetToken: ResetTokenInterface<ClientTypeFromOptions<TDefaults>>;
refreshToken: RefreshTokenInterface;
scopeToken: ScopeTokenInterface;
deleteToken: DeleteTokenInterface;
deleteAuthorization: DeleteAuthorizationInterface;
};
} & S;
constructor(options: ConstructorOptions<TOptions>);
type: ClientTypeFromOptions<TOptions>;
on: AddEventHandler<TOptions>;
octokit: OctokitTypeFromOptions<TOptions>;
getUserOctokit: GetUserOctokitWithStateInterface<ClientTypeFromOptions<TOptions>>;
getWebFlowAuthorizationUrl: GetWebFlowAuthorizationUrlInterface<ClientTypeFromOptions<TOptions>>;
createToken: CreateTokenInterface<ClientTypeFromOptions<TOptions>>;
checkToken: CheckTokenInterface<ClientTypeFromOptions<TOptions>>;
resetToken: ResetTokenInterface<ClientTypeFromOptions<TOptions>>;
refreshToken: RefreshTokenInterface;
scopeToken: ScopeTokenInterface;
deleteToken: DeleteTokenInterface;
deleteAuthorization: DeleteAuthorizationInterface;
}

View File

@@ -0,0 +1,14 @@
import * as OAuthMethods from "@octokit/oauth-methods";
import type { ClientType, State } from "../types.js";
export type CheckTokenOptions = {
token: string;
};
export declare function checkTokenWithState(state: State, options: CheckTokenOptions): Promise<any>;
export interface CheckTokenInterface<TClientType extends ClientType> {
(options: CheckTokenOptions): (TClientType extends "oauth-app" ? Promise<OAuthMethods.CheckTokenOAuthAppResponse> : Promise<OAuthMethods.CheckTokenGitHubAppResponse>) & {
authentication: {
type: "token";
tokenType: "oauth";
};
};
}

View File

@@ -0,0 +1,20 @@
import * as OAuthAppAuth from "@octokit/auth-oauth-app";
import type { ClientType, GithubAppUserAuthenticationWithOptionalExpiration, State } from "../types.js";
export type CreateTokenWebFlowOptions = Omit<OAuthAppAuth.WebFlowAuthOptions, "type">;
export type CreateTokenOAuthAppDeviceFlowOptions = Omit<OAuthAppAuth.OAuthAppDeviceFlowAuthOptions, "type">;
export type CreateTokenGitHubAppDeviceFlowOptions = Omit<OAuthAppAuth.GitHubAppDeviceFlowAuthOptions, "type">;
export declare function createTokenWithState(state: State, options: CreateTokenWebFlowOptions | CreateTokenOAuthAppDeviceFlowOptions | CreateTokenGitHubAppDeviceFlowOptions): Promise<{
authentication: OAuthAppAuth.OAuthAppUserAuthentication | OAuthAppAuth.GitHubAppUserAuthentication | OAuthAppAuth.GitHubAppUserAuthenticationWithExpiration;
}>;
export interface CreateTokenInterface<TClientType extends ClientType> {
(options: CreateTokenWebFlowOptions): TClientType extends "oauth-app" ? Promise<{
authentication: OAuthAppAuth.OAuthAppUserAuthentication;
}> : Promise<{
authentication: GithubAppUserAuthenticationWithOptionalExpiration;
}>;
(options: TClientType extends "oauth-app" ? CreateTokenOAuthAppDeviceFlowOptions : CreateTokenGitHubAppDeviceFlowOptions): TClientType extends "oauth-app" ? Promise<{
authentication: OAuthAppAuth.OAuthAppUserAuthentication;
}> : Promise<{
authentication: GithubAppUserAuthenticationWithOptionalExpiration;
}>;
}

View File

@@ -0,0 +1,9 @@
import * as OAuthMethods from "@octokit/oauth-methods";
import type { State } from "../types.js";
export type DeleteAuthorizationOptions = {
token: string;
};
export declare function deleteAuthorizationWithState(state: State, options: DeleteAuthorizationOptions): Promise<OAuthMethods.DeleteAuthorizationResponse>;
export interface DeleteAuthorizationInterface {
(options: DeleteAuthorizationOptions): Promise<OAuthMethods.DeleteAuthorizationResponse>;
}

View File

@@ -0,0 +1,9 @@
import * as OAuthMethods from "@octokit/oauth-methods";
import type { State } from "../types.js";
export type DeleteTokenOptions = {
token: string;
};
export declare function deleteTokenWithState(state: State, options: DeleteTokenOptions): Promise<OAuthMethods.DeleteTokenResponse>;
export interface DeleteTokenInterface {
(options: DeleteTokenOptions): Promise<OAuthMethods.DeleteTokenResponse>;
}

View File

@@ -0,0 +1 @@
export declare function getOAuthClientCode(): string;

View File

@@ -0,0 +1,10 @@
import type { OAuthAppStrategyOptionsWebFlow, OAuthAppStrategyOptionsDeviceFlow, OAuthAppStrategyOptionsExistingAuthentication, GitHubAppStrategyOptionsWebFlow, GitHubAppStrategyOptionsDeviceFlow, GitHubAppStrategyOptionsExistingAuthentication, GitHubAppStrategyOptionsExistingAuthenticationWithExpiration } from "@octokit/auth-oauth-user";
import type { State, OctokitInstance, ClientType } from "../types.js";
type StateOptions = "clientType" | "clientId" | "clientSecret" | "request";
export type GetUserOctokitOAuthAppOptions = Omit<OAuthAppStrategyOptionsWebFlow, StateOptions> | Omit<OAuthAppStrategyOptionsDeviceFlow, StateOptions> | Omit<OAuthAppStrategyOptionsExistingAuthentication, StateOptions>;
export type GetUserOctokitGitHubAppOptions = Omit<GitHubAppStrategyOptionsWebFlow, StateOptions> | Omit<GitHubAppStrategyOptionsDeviceFlow, StateOptions> | Omit<GitHubAppStrategyOptionsExistingAuthentication, StateOptions> | Omit<GitHubAppStrategyOptionsExistingAuthenticationWithExpiration, StateOptions>;
export declare function getUserOctokitWithState(state: State, options: GetUserOctokitOAuthAppOptions | GetUserOctokitGitHubAppOptions): Promise<import("@octokit/core").Octokit>;
export interface GetUserOctokitWithStateInterface<TClientType extends ClientType> {
(options: TClientType extends "oauth-app" ? GetUserOctokitOAuthAppOptions : GetUserOctokitGitHubAppOptions): Promise<OctokitInstance>;
}
export {};

View File

@@ -0,0 +1,10 @@
import * as OAuthMethods from "@octokit/oauth-methods";
import type { ClientType, State } from "../types.js";
type StateOptions = "clientType" | "clientId" | "clientSecret" | "request";
export type GetWebFlowAuthorizationUrlOAuthAppOptions = Omit<OAuthMethods.GetWebFlowAuthorizationUrlOAuthAppOptions, StateOptions>;
export type GetWebFlowAuthorizationUrlGitHubAppOptions = Omit<OAuthMethods.GetWebFlowAuthorizationUrlGitHubAppOptions, StateOptions>;
export declare function getWebFlowAuthorizationUrlWithState(state: State, options: any): any;
export interface GetWebFlowAuthorizationUrlInterface<TClientType extends ClientType> {
(options: TClientType extends "oauth-app" ? GetWebFlowAuthorizationUrlOAuthAppOptions : GetWebFlowAuthorizationUrlGitHubAppOptions): TClientType extends "oauth-app" ? OAuthMethods.GetWebFlowAuthorizationUrlOAuthAppResult : OAuthMethods.GetWebFlowAuthorizationUrlGitHubAppResult;
}
export {};

View File

@@ -0,0 +1,19 @@
import * as OAuthMethods from "@octokit/oauth-methods";
import type { State } from "../types.js";
export type RefreshTokenOptions = {
refreshToken: string;
};
export declare function refreshTokenWithState(state: State, options: RefreshTokenOptions): Promise<OAuthMethods.RefreshTokenResponse & {
authentication: {
type: "token";
tokenType: "oauth";
};
}>;
export interface RefreshTokenInterface {
(options: RefreshTokenOptions): Promise<OAuthMethods.RefreshTokenResponse & {
authentication: {
type: "token";
tokenType: "oauth";
};
}>;
}

View File

@@ -0,0 +1,24 @@
import * as OAuthMethods from "@octokit/oauth-methods";
import type { ClientType, State } from "../types.js";
export type ResetTokenOptions = {
token: string;
};
export declare function resetTokenWithState(state: State, options: ResetTokenOptions): Promise<(OAuthMethods.ResetTokenOAuthAppResponse | OAuthMethods.ResetTokenGitHubAppResponse) & {
authentication: {
type: "token";
tokenType: "oauth";
};
}>;
export interface ResetTokenInterface<TClientType extends ClientType> {
(options: ResetTokenOptions): TClientType extends "oauth-app" ? Promise<OAuthMethods.ResetTokenOAuthAppResponse & {
authentication: {
type: "token";
tokenType: "oauth";
};
}> : Promise<OAuthMethods.ResetTokenGitHubAppResponse & {
authentication: {
type: "token";
tokenType: "oauth";
};
}>;
}

View File

@@ -0,0 +1,19 @@
import * as OAuthMethods from "@octokit/oauth-methods";
import type { State } from "../types.js";
type StateOptions = "clientType" | "clientId" | "clientSecret" | "request";
export type ScopeTokenOptions = Omit<OAuthMethods.ScopeTokenOptions, StateOptions>;
export declare function scopeTokenWithState(state: State, options: ScopeTokenOptions): Promise<OAuthMethods.ScopeTokenResponse & {
authentication: {
type: "token";
tokenType: "oauth";
};
}>;
export interface ScopeTokenInterface {
(options: ScopeTokenOptions): Promise<OAuthMethods.ScopeTokenResponse & {
authentication: {
type: "token";
tokenType: "oauth";
};
}>;
}
export {};

View File

@@ -0,0 +1,3 @@
import type { OctokitRequest } from "../types.js";
import type { APIGatewayProxyEventV2 } from "aws-lambda";
export declare function parseRequest(request: APIGatewayProxyEventV2): OctokitRequest;

View File

@@ -0,0 +1,3 @@
import type { OctokitResponse } from "../types.js";
import type { APIGatewayProxyStructuredResultV2 } from "aws-lambda";
export declare function sendResponse(octokitResponse: OctokitResponse): APIGatewayProxyStructuredResultV2;

View File

@@ -0,0 +1,5 @@
import type { HandlerOptions } from "../types.js";
import type { OAuthApp } from "../../index.js";
import type { ClientType, Options } from "../../types.js";
import type { APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2 } from "aws-lambda";
export declare function createAWSLambdaAPIGatewayV2Handler(app: OAuthApp<Options<ClientType>>, options?: HandlerOptions): (event: APIGatewayProxyEventV2) => Promise<APIGatewayProxyStructuredResultV2 | undefined>;

View File

@@ -0,0 +1,4 @@
import { OAuthApp } from "../index.js";
import type { HandlerOptions, OctokitRequest, OctokitResponse } from "./types.js";
import type { ClientType, Options } from "../types.js";
export declare function handleRequest(app: OAuthApp<Options<ClientType>>, { pathPrefix }: HandlerOptions, request: OctokitRequest): Promise<OctokitResponse | undefined>;

View File

@@ -0,0 +1,7 @@
type IncomingMessage = any;
type ServerResponse = any;
import type { OAuthApp } from "../../index.js";
import type { HandlerOptions } from "../types.js";
import type { ClientType, Options } from "../../types.js";
export declare function createNodeMiddleware(app: OAuthApp<Options<ClientType>>, options?: HandlerOptions): (request: IncomingMessage, response: ServerResponse, next?: Function) => Promise<boolean>;
export {};

View File

@@ -0,0 +1,4 @@
type IncomingMessage = any;
import type { OctokitRequest } from "../types.js";
export declare function parseRequest(request: IncomingMessage): OctokitRequest;
export {};

View File

@@ -0,0 +1,4 @@
type ServerResponse = any;
import type { OctokitResponse } from "../types.js";
export declare function sendResponse(octokitResponse: OctokitResponse, response: ServerResponse): void;
export {};

View File

@@ -0,0 +1,14 @@
export type OctokitRequest = {
method: string;
url: string;
headers: Record<string, string>;
text: () => Promise<string>;
};
export type OctokitResponse = {
status: number;
headers?: Record<string, string>;
text?: string;
};
export type HandlerOptions = {
pathPrefix?: string;
};

View File

@@ -0,0 +1,8 @@
import type { OctokitRequest } from "./types.js";
export declare function unknownRouteResponse(request: OctokitRequest): {
status: number;
headers: {
"content-type": string;
};
text: string;
};

View File

@@ -0,0 +1,4 @@
import type { OAuthApp } from "../../index.js";
import type { HandlerOptions } from "../types.js";
import type { ClientType, Options } from "../../types.js";
export declare function createWebWorkerHandler<T extends Options<ClientType>>(app: OAuthApp<T>, options?: HandlerOptions): (request: Request) => Promise<Response | undefined>;

View File

@@ -0,0 +1,2 @@
import type { OctokitRequest } from "../types.js";
export declare function parseRequest(request: Request): OctokitRequest;

View File

@@ -0,0 +1,2 @@
import type { OctokitResponse } from "../types.js";
export declare function sendResponse(octokitResponse: OctokitResponse): Response;

View File

@@ -0,0 +1,2 @@
import { Octokit } from "@octokit/core";
export declare const OAuthAppOctokit: typeof Octokit;

View File

@@ -0,0 +1,68 @@
import { Octokit } from "@octokit/core";
import type { OAuthAppUserAuthentication, GitHubAppUserAuthentication, GitHubAppUserAuthenticationWithExpiration } from "@octokit/auth-oauth-app";
import { OAuthAppOctokit } from "./oauth-app-octokit";
export type ClientType = "oauth-app" | "github-app";
export type OAuthAppOctokitClassType = typeof OAuthAppOctokit;
export type GithubAppUserAuthenticationWithOptionalExpiration = GitHubAppUserAuthentication & Partial<GitHubAppUserAuthenticationWithExpiration>;
export type Scope = string;
export type ClientId = string;
export type ClientSecret = string;
export type Token = string;
export type EventName = "token" | "authorization";
export type ActionName = "created" | "reset" | "deleted" | "refreshed" | "scoped";
export type EventAndActionName = "token" | "token.created" | "token.reset" | "token.refreshed" | "token.scoped" | "token.deleted" | "authorization" | "authorization.deleted";
type CommonOptions<TOctokit extends OAuthAppOctokitClassType> = {
clientId?: ClientId;
clientSecret?: ClientSecret;
allowSignup?: boolean;
baseUrl?: string;
redirectUrl?: string;
log?: typeof console;
Octokit?: TOctokit;
};
export type Options<TClientType extends ClientType, TOctokit extends OAuthAppOctokitClassType = OAuthAppOctokitClassType> = TClientType extends "oauth-app" ? CommonOptions<TOctokit> & {
clientType?: TClientType;
defaultScopes?: Scope[];
} : CommonOptions<TOctokit> & {
clientType?: TClientType;
};
export type ConstructorOptions<TOptions extends Options<ClientType>> = TOptions & {
clientId: ClientId;
clientSecret: ClientSecret;
};
export type OctokitTypeFromOptions<TOptions extends Options<ClientType>> = TOptions["Octokit"] extends typeof Octokit ? InstanceType<TOptions["Octokit"]> : Octokit;
export type OctokitClassTypeFromOptions<TOptions extends Options<ClientType>> = TOptions["Octokit"] extends typeof Octokit ? TOptions["Octokit"] : typeof Octokit;
export type ClientTypeFromOptions<TOptions extends Options<ClientType>> = TOptions["clientType"] extends "github-app" ? "github-app" : "oauth-app";
export type OctokitInstance = InstanceType<OAuthAppOctokitClassType>;
export type State = {
clientType: ClientType;
clientId: ClientId;
clientSecret: ClientSecret;
defaultScopes: Scope[];
allowSignup?: boolean;
baseUrl?: string;
redirectUrl?: string;
log?: typeof console;
Octokit: OAuthAppOctokitClassType;
octokit: OctokitInstance;
eventHandlers: {
[key: string]: EventHandler<Options<ClientType>>[];
};
};
export type EventHandlerContext<TOptions extends Options<ClientType>> = ClientTypeFromOptions<TOptions> extends "oauth-app" ? {
name: EventName;
action: ActionName;
token: Token;
scopes?: Scope[];
octokit: OctokitTypeFromOptions<TOptions>;
authentication?: OAuthAppUserAuthentication | GitHubAppUserAuthentication | GitHubAppUserAuthenticationWithExpiration;
} : {
name: EventName;
action: ActionName;
token: Token;
octokit: OctokitTypeFromOptions<TOptions>;
authentication?: GithubAppUserAuthenticationWithOptionalExpiration;
};
export type EventHandler<TOptions extends Options<ClientType>> = (context: EventHandlerContext<TOptions>) => void;
export type AddEventHandler<TOptions extends Options<ClientType>> = (eventName: EventAndActionName | EventAndActionName[], eventHandler: EventHandler<TOptions>) => void;
export {};

View File

@@ -0,0 +1 @@
export declare const VERSION = "6.1.0";

View File

@@ -0,0 +1,53 @@
{
"name": "@octokit/oauth-app",
"version": "6.1.0",
"description": "GitHub OAuth toolset for Node.js",
"repository": "github:octokit/oauth-app.js",
"keywords": [
"github",
"api",
"sdk",
"toolkit"
],
"author": "Gregor Martynus (https://twitter.com/gr2m)",
"license": "MIT",
"dependencies": {
"@octokit/auth-oauth-app": "^7.0.0",
"@octokit/auth-oauth-user": "^4.0.0",
"@octokit/auth-unauthenticated": "^5.0.0",
"@octokit/core": "^5.0.0",
"@octokit/oauth-authorization-url": "^6.0.2",
"@octokit/oauth-methods": "^4.0.0",
"@types/aws-lambda": "^8.10.83",
"universal-user-agent": "^6.0.0"
},
"devDependencies": {
"@octokit/tsconfig": "^2.0.0",
"@types/jest": "^29.0.0",
"@types/node": "^20.0.0",
"esbuild": "^0.19.0",
"express": "^4.17.1",
"fetch-mock": "npm:@gr2m/fetch-mock@9.11.0-pull-request-644.1",
"glob": "^10.2.5",
"jest": "^29.0.0",
"nock": "^13.0.0",
"prettier": "3.2.4",
"semantic-release-plugin-update-version-in-files": "^1.0.0",
"ts-jest": "^29.0.0",
"typescript": "^5.0.0"
},
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">= 18"
},
"files": [
"dist-*/**",
"bin/**"
],
"main": "dist-node/index.js",
"types": "dist-types/index.d.ts",
"source": "dist-src/index.js",
"sideEffects": false
}

View File

@@ -0,0 +1 @@
../../../@octokit+oauth-authorization-url@6.0.2/node_modules/@octokit/oauth-authorization-url

View File

@@ -0,0 +1 @@
../../../@octokit+oauth-methods@4.1.0/node_modules/@octokit/oauth-methods

View File

@@ -0,0 +1 @@
../../../@types+aws-lambda@8.10.146/node_modules/@types/aws-lambda

View File

@@ -0,0 +1 @@
../../universal-user-agent@6.0.1/node_modules/universal-user-agent