UNPKG

aws-delivlib

Version:

A fabulous library for defining continuous pipelines for building, testing and releasing code libraries.

244 lines (235 loc) • 8.43 kB
"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, { createOAuthUserAuth: () => createOAuthUserAuth, requiresBasicAuth: () => requiresBasicAuth }); module.exports = __toCommonJS(dist_src_exports); var import_universal_user_agent = require("universal-user-agent"); var import_request = require("@octokit/request"); // pkg/dist-src/version.js var VERSION = "2.1.2"; // pkg/dist-src/get-authentication.js var import_auth_oauth_device = require("@octokit/auth-oauth-device"); var import_oauth_methods = require("@octokit/oauth-methods"); async function getAuthentication(state) { if ("code" in state.strategyOptions) { const { authentication } = await (0, import_oauth_methods.exchangeWebFlowCode)({ clientId: state.clientId, clientSecret: state.clientSecret, clientType: state.clientType, onTokenCreated: state.onTokenCreated, ...state.strategyOptions, request: state.request }); return { type: "token", tokenType: "oauth", ...authentication }; } if ("onVerification" in state.strategyOptions) { const deviceAuth = (0, import_auth_oauth_device.createOAuthDeviceAuth)({ clientType: state.clientType, clientId: state.clientId, onTokenCreated: state.onTokenCreated, ...state.strategyOptions, request: state.request }); const authentication = await deviceAuth({ type: "oauth" }); return { clientSecret: state.clientSecret, ...authentication }; } if ("token" in state.strategyOptions) { return { type: "token", tokenType: "oauth", clientId: state.clientId, clientSecret: state.clientSecret, clientType: state.clientType, onTokenCreated: state.onTokenCreated, ...state.strategyOptions }; } throw new Error("[@octokit/auth-oauth-user] Invalid strategy options"); } // pkg/dist-src/auth.js var import_oauth_methods2 = require("@octokit/oauth-methods"); async function auth(state, options = {}) { var _a, _b; if (!state.authentication) { state.authentication = state.clientType === "oauth-app" ? await getAuthentication(state) : await getAuthentication(state); } if (state.authentication.invalid) { throw new Error("[@octokit/auth-oauth-user] Token is invalid"); } const currentAuthentication = state.authentication; if ("expiresAt" in currentAuthentication) { if (options.type === "refresh" || new Date(currentAuthentication.expiresAt) < /* @__PURE__ */ new Date()) { const { authentication } = await (0, import_oauth_methods2.refreshToken)({ clientType: "github-app", clientId: state.clientId, clientSecret: state.clientSecret, refreshToken: currentAuthentication.refreshToken, request: state.request }); state.authentication = { tokenType: "oauth", type: "token", ...authentication }; } } if (options.type === "refresh") { if (state.clientType === "oauth-app") { throw new Error( "[@octokit/auth-oauth-user] OAuth Apps do not support expiring tokens" ); } if (!currentAuthentication.hasOwnProperty("expiresAt")) { throw new Error("[@octokit/auth-oauth-user] Refresh token missing"); } await ((_a = state.onTokenCreated) == null ? void 0 : _a.call(state, state.authentication, { type: options.type })); } if (options.type === "check" || options.type === "reset") { const method = options.type === "check" ? import_oauth_methods2.checkToken : import_oauth_methods2.resetToken; try { const { authentication } = await method({ // @ts-expect-error making TS happy would require unnecessary code so no clientType: state.clientType, clientId: state.clientId, clientSecret: state.clientSecret, token: state.authentication.token, request: state.request }); state.authentication = { tokenType: "oauth", type: "token", // @ts-expect-error TBD ...authentication }; if (options.type === "reset") { await ((_b = state.onTokenCreated) == null ? void 0 : _b.call(state, state.authentication, { type: options.type })); } return state.authentication; } catch (error) { if (error.status === 404) { error.message = "[@octokit/auth-oauth-user] Token is invalid"; state.authentication.invalid = true; } throw error; } } if (options.type === "delete" || options.type === "deleteAuthorization") { const method = options.type === "delete" ? import_oauth_methods2.deleteToken : import_oauth_methods2.deleteAuthorization; try { await method({ // @ts-expect-error making TS happy would require unnecessary code so no clientType: state.clientType, clientId: state.clientId, clientSecret: state.clientSecret, token: state.authentication.token, request: state.request }); } catch (error) { if (error.status !== 404) throw error; } state.authentication.invalid = true; return state.authentication; } return state.authentication; } // pkg/dist-src/hook.js var import_btoa_lite = __toESM(require("btoa-lite")); // pkg/dist-src/requires-basic-auth.js var ROUTES_REQUIRING_BASIC_AUTH = /\/applications\/[^/]+\/(token|grant)s?/; function requiresBasicAuth(url) { return url && ROUTES_REQUIRING_BASIC_AUTH.test(url); } // pkg/dist-src/hook.js async function hook(state, request, route, parameters = {}) { const endpoint = request.endpoint.merge( route, parameters ); if (/\/login\/(oauth\/access_token|device\/code)$/.test(endpoint.url)) { return request(endpoint); } if (requiresBasicAuth(endpoint.url)) { const credentials = (0, import_btoa_lite.default)(`${state.clientId}:${state.clientSecret}`); endpoint.headers.authorization = `basic ${credentials}`; return request(endpoint); } const { token } = state.clientType === "oauth-app" ? await auth({ ...state, request }) : await auth({ ...state, request }); endpoint.headers.authorization = "token " + token; return request(endpoint); } // pkg/dist-src/index.js function createOAuthUserAuth({ clientId, clientSecret, clientType = "oauth-app", request = import_request.request.defaults({ headers: { "user-agent": `octokit-auth-oauth-app.js/${VERSION} ${(0, import_universal_user_agent.getUserAgent)()}` } }), onTokenCreated, ...strategyOptions }) { const state = Object.assign({ clientType, clientId, clientSecret, onTokenCreated, strategyOptions, request }); return Object.assign(auth.bind(null, state), { // @ts-expect-error not worth the extra code needed to appease TS hook: hook.bind(null, state) }); } createOAuthUserAuth.VERSION = VERSION; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createOAuthUserAuth, requiresBasicAuth });