@octokit/auth-oauth-device
Advanced tools
Comparing version
@@ -1,17 +0,36 @@ | ||
'use strict'; | ||
"use strict"; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
// pkg/dist-src/index.js | ||
var dist_src_exports = {}; | ||
__export(dist_src_exports, { | ||
createOAuthDeviceAuth: () => createOAuthDeviceAuth | ||
}); | ||
module.exports = __toCommonJS(dist_src_exports); | ||
var import_universal_user_agent = require("universal-user-agent"); | ||
var import_request = require("@octokit/request"); | ||
var universalUserAgent = require('universal-user-agent'); | ||
var request = require('@octokit/request'); | ||
var oauthMethods = require('@octokit/oauth-methods'); | ||
// pkg/dist-src/get-oauth-access-token.js | ||
var import_oauth_methods = require("@octokit/oauth-methods"); | ||
async function getOAuthAccessToken(state, options) { | ||
const cachedAuthentication = getCachedAuthentication(state, options.auth); | ||
if (cachedAuthentication) return cachedAuthentication; | ||
// Step 1: Request device and user codes | ||
// https://docs.github.com/en/developers/apps/authorizing-oauth-apps#step-1-app-requests-the-device-and-user-verification-codes-from-github | ||
const { | ||
data: verification | ||
} = await oauthMethods.createDeviceCode({ | ||
if (cachedAuthentication) | ||
return cachedAuthentication; | ||
const { data: verification } = await (0, import_oauth_methods.createDeviceCode)({ | ||
clientType: state.clientType, | ||
@@ -23,14 +42,17 @@ clientId: state.clientId, | ||
}); | ||
// Step 2: User must enter the user code on https://github.com/login/device | ||
// See https://docs.github.com/en/developers/apps/authorizing-oauth-apps#step-2-prompt-the-user-to-enter-the-user-code-in-a-browser | ||
await state.onVerification(verification); | ||
// Step 3: Exchange device code for access token | ||
// See https://docs.github.com/en/developers/apps/authorizing-oauth-apps#step-3-app-polls-github-to-check-if-the-user-authorized-the-device | ||
const authentication = await waitForAccessToken(options.request || state.request, state.clientId, state.clientType, verification); | ||
const authentication = await waitForAccessToken( | ||
options.request || state.request, | ||
state.clientId, | ||
state.clientType, | ||
verification | ||
); | ||
state.authentication = authentication; | ||
return authentication; | ||
} | ||
function getCachedAuthentication(state, auth) { | ||
if (auth.refresh === true) return false; | ||
if (!state.authentication) return false; | ||
function getCachedAuthentication(state, auth2) { | ||
if (auth2.refresh === true) | ||
return false; | ||
if (!state.authentication) | ||
return false; | ||
if (state.clientType === "github-app") { | ||
@@ -40,3 +62,5 @@ return state.authentication; | ||
const authentication = state.authentication; | ||
const newScope = ("scopes" in auth && auth.scopes || state.scopes).join(" "); | ||
const newScope = ("scopes" in auth2 && auth2.scopes || state.scopes).join( | ||
" " | ||
); | ||
const currentScope = authentication.scopes.join(" "); | ||
@@ -46,3 +70,3 @@ return newScope === currentScope ? authentication : false; | ||
async function wait(seconds) { | ||
await new Promise(resolve => setTimeout(resolve, seconds * 1000)); | ||
await new Promise((resolve) => setTimeout(resolve, seconds * 1e3)); | ||
} | ||
@@ -56,9 +80,6 @@ async function waitForAccessToken(request, clientId, clientType, verification) { | ||
}; | ||
// WHY TYPESCRIPT WHY ARE YOU DOING THIS TO ME | ||
const { | ||
authentication | ||
} = clientType === "oauth-app" ? await oauthMethods.exchangeDeviceCode({ | ||
const { authentication } = clientType === "oauth-app" ? await (0, import_oauth_methods.exchangeDeviceCode)({ | ||
...options, | ||
clientType: "oauth-app" | ||
}) : await oauthMethods.exchangeDeviceCode({ | ||
}) : await (0, import_oauth_methods.exchangeDeviceCode)({ | ||
...options, | ||
@@ -73,6 +94,4 @@ clientType: "github-app" | ||
} catch (error) { | ||
// istanbul ignore if | ||
// @ts-ignore | ||
if (!error.response) throw error; | ||
// @ts-ignore | ||
if (!error.response) | ||
throw error; | ||
const errorType = error.response.data.error; | ||
@@ -91,2 +110,3 @@ if (errorType === "authorization_pending") { | ||
// pkg/dist-src/auth.js | ||
async function auth(state, authOptions) { | ||
@@ -98,15 +118,14 @@ return getOAuthAccessToken(state, { | ||
// pkg/dist-src/hook.js | ||
async function hook(state, request, route, parameters) { | ||
let endpoint = request.endpoint.merge(route, parameters); | ||
// Do not intercept request to retrieve codes or token | ||
let endpoint = request.endpoint.merge( | ||
route, | ||
parameters | ||
); | ||
if (/\/login\/(oauth\/access_token|device\/code)$/.test(endpoint.url)) { | ||
return request(endpoint); | ||
} | ||
const { | ||
token | ||
} = await getOAuthAccessToken(state, { | ||
const { token } = await getOAuthAccessToken(state, { | ||
request, | ||
auth: { | ||
type: "oauth" | ||
} | ||
auth: { type: "oauth" } | ||
}); | ||
@@ -117,31 +136,33 @@ endpoint.headers.authorization = `token ${token}`; | ||
const VERSION = "4.0.4"; | ||
// pkg/dist-src/version.js | ||
var VERSION = "4.0.5"; | ||
// pkg/dist-src/index.js | ||
function createOAuthDeviceAuth(options) { | ||
const requestWithDefaults = options.request || request.request.defaults({ | ||
const requestWithDefaults = options.request || import_request.request.defaults({ | ||
headers: { | ||
"user-agent": `octokit-auth-oauth-device.js/${VERSION} ${universalUserAgent.getUserAgent()}` | ||
"user-agent": `octokit-auth-oauth-device.js/${VERSION} ${(0, import_universal_user_agent.getUserAgent)()}` | ||
} | ||
}); | ||
const { | ||
request: request$1 = requestWithDefaults, | ||
...otherOptions | ||
} = options; | ||
const { request = requestWithDefaults, ...otherOptions } = options; | ||
const state = options.clientType === "github-app" ? { | ||
...otherOptions, | ||
clientType: "github-app", | ||
request: request$1 | ||
request | ||
} : { | ||
...otherOptions, | ||
clientType: "oauth-app", | ||
request: request$1, | ||
request, | ||
scopes: options.scopes || [] | ||
}; | ||
if (!options.clientId) { | ||
throw new Error('[@octokit/auth-oauth-device] "clientId" option must be set (https://github.com/octokit/auth-oauth-device.js#usage)'); | ||
throw new Error( | ||
'[@octokit/auth-oauth-device] "clientId" option must be set (https://github.com/octokit/auth-oauth-device.js#usage)' | ||
); | ||
} | ||
if (!options.onVerification) { | ||
throw new Error('[@octokit/auth-oauth-device] "onVerification" option must be a function (https://github.com/octokit/auth-oauth-device.js#usage)'); | ||
throw new Error( | ||
'[@octokit/auth-oauth-device] "onVerification" option must be a function (https://github.com/octokit/auth-oauth-device.js#usage)' | ||
); | ||
} | ||
// @ts-ignore too much for tsc / ts-jest ¯\_(ツ)_/¯ | ||
return Object.assign(auth.bind(null, state), { | ||
@@ -151,4 +172,5 @@ hook: hook.bind(null, state) | ||
} | ||
exports.createOAuthDeviceAuth = createOAuthDeviceAuth; | ||
//# sourceMappingURL=index.js.map | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
createOAuthDeviceAuth | ||
}); |
import { getOAuthAccessToken } from "./get-oauth-access-token"; | ||
export async function auth(state, authOptions) { | ||
return getOAuthAccessToken(state, { | ||
auth: authOptions, | ||
}); | ||
async function auth(state, authOptions) { | ||
return getOAuthAccessToken(state, { | ||
auth: authOptions | ||
}); | ||
} | ||
export { | ||
auth | ||
}; |
import { createDeviceCode, exchangeDeviceCode } from "@octokit/oauth-methods"; | ||
export async function getOAuthAccessToken(state, options) { | ||
const cachedAuthentication = getCachedAuthentication(state, options.auth); | ||
if (cachedAuthentication) | ||
return cachedAuthentication; | ||
// Step 1: Request device and user codes | ||
// https://docs.github.com/en/developers/apps/authorizing-oauth-apps#step-1-app-requests-the-device-and-user-verification-codes-from-github | ||
const { data: verification } = await createDeviceCode({ | ||
clientType: state.clientType, | ||
clientId: state.clientId, | ||
request: options.request || state.request, | ||
// @ts-expect-error the extra code to make TS happy is not worth it | ||
scopes: options.auth.scopes || state.scopes, | ||
}); | ||
// Step 2: User must enter the user code on https://github.com/login/device | ||
// See https://docs.github.com/en/developers/apps/authorizing-oauth-apps#step-2-prompt-the-user-to-enter-the-user-code-in-a-browser | ||
await state.onVerification(verification); | ||
// Step 3: Exchange device code for access token | ||
// See https://docs.github.com/en/developers/apps/authorizing-oauth-apps#step-3-app-polls-github-to-check-if-the-user-authorized-the-device | ||
const authentication = await waitForAccessToken(options.request || state.request, state.clientId, state.clientType, verification); | ||
state.authentication = authentication; | ||
return authentication; | ||
async function getOAuthAccessToken(state, options) { | ||
const cachedAuthentication = getCachedAuthentication(state, options.auth); | ||
if (cachedAuthentication) | ||
return cachedAuthentication; | ||
const { data: verification } = await createDeviceCode({ | ||
clientType: state.clientType, | ||
clientId: state.clientId, | ||
request: options.request || state.request, | ||
// @ts-expect-error the extra code to make TS happy is not worth it | ||
scopes: options.auth.scopes || state.scopes | ||
}); | ||
await state.onVerification(verification); | ||
const authentication = await waitForAccessToken( | ||
options.request || state.request, | ||
state.clientId, | ||
state.clientType, | ||
verification | ||
); | ||
state.authentication = authentication; | ||
return authentication; | ||
} | ||
function getCachedAuthentication(state, auth) { | ||
if (auth.refresh === true) | ||
return false; | ||
if (!state.authentication) | ||
return false; | ||
if (state.clientType === "github-app") { | ||
return state.authentication; | ||
} | ||
const authentication = state.authentication; | ||
const newScope = (("scopes" in auth && auth.scopes) || state.scopes).join(" "); | ||
const currentScope = authentication.scopes.join(" "); | ||
return newScope === currentScope ? authentication : false; | ||
if (auth.refresh === true) | ||
return false; | ||
if (!state.authentication) | ||
return false; | ||
if (state.clientType === "github-app") { | ||
return state.authentication; | ||
} | ||
const authentication = state.authentication; | ||
const newScope = ("scopes" in auth && auth.scopes || state.scopes).join( | ||
" " | ||
); | ||
const currentScope = authentication.scopes.join(" "); | ||
return newScope === currentScope ? authentication : false; | ||
} | ||
async function wait(seconds) { | ||
await new Promise((resolve) => setTimeout(resolve, seconds * 1000)); | ||
await new Promise((resolve) => setTimeout(resolve, seconds * 1e3)); | ||
} | ||
async function waitForAccessToken(request, clientId, clientType, verification) { | ||
try { | ||
const options = { | ||
clientId, | ||
request, | ||
code: verification.device_code, | ||
}; | ||
// WHY TYPESCRIPT WHY ARE YOU DOING THIS TO ME | ||
const { authentication } = clientType === "oauth-app" | ||
? await exchangeDeviceCode({ | ||
...options, | ||
clientType: "oauth-app", | ||
}) | ||
: await exchangeDeviceCode({ | ||
...options, | ||
clientType: "github-app", | ||
}); | ||
return { | ||
type: "token", | ||
tokenType: "oauth", | ||
...authentication, | ||
}; | ||
try { | ||
const options = { | ||
clientId, | ||
request, | ||
code: verification.device_code | ||
}; | ||
const { authentication } = clientType === "oauth-app" ? await exchangeDeviceCode({ | ||
...options, | ||
clientType: "oauth-app" | ||
}) : await exchangeDeviceCode({ | ||
...options, | ||
clientType: "github-app" | ||
}); | ||
return { | ||
type: "token", | ||
tokenType: "oauth", | ||
...authentication | ||
}; | ||
} catch (error) { | ||
if (!error.response) | ||
throw error; | ||
const errorType = error.response.data.error; | ||
if (errorType === "authorization_pending") { | ||
await wait(verification.interval); | ||
return waitForAccessToken(request, clientId, clientType, verification); | ||
} | ||
catch (error) { | ||
// istanbul ignore if | ||
// @ts-ignore | ||
if (!error.response) | ||
throw error; | ||
// @ts-ignore | ||
const errorType = error.response.data.error; | ||
if (errorType === "authorization_pending") { | ||
await wait(verification.interval); | ||
return waitForAccessToken(request, clientId, clientType, verification); | ||
} | ||
if (errorType === "slow_down") { | ||
await wait(verification.interval + 5); | ||
return waitForAccessToken(request, clientId, clientType, verification); | ||
} | ||
throw error; | ||
if (errorType === "slow_down") { | ||
await wait(verification.interval + 5); | ||
return waitForAccessToken(request, clientId, clientType, verification); | ||
} | ||
throw error; | ||
} | ||
} | ||
export { | ||
getOAuthAccessToken | ||
}; |
import { getOAuthAccessToken } from "./get-oauth-access-token"; | ||
export async function hook(state, request, route, parameters) { | ||
let endpoint = request.endpoint.merge(route, parameters); | ||
// Do not intercept request to retrieve codes or token | ||
if (/\/login\/(oauth\/access_token|device\/code)$/.test(endpoint.url)) { | ||
return request(endpoint); | ||
} | ||
const { token } = await getOAuthAccessToken(state, { | ||
request, | ||
auth: { type: "oauth" }, | ||
}); | ||
endpoint.headers.authorization = `token ${token}`; | ||
async function hook(state, request, route, parameters) { | ||
let endpoint = request.endpoint.merge( | ||
route, | ||
parameters | ||
); | ||
if (/\/login\/(oauth\/access_token|device\/code)$/.test(endpoint.url)) { | ||
return request(endpoint); | ||
} | ||
const { token } = await getOAuthAccessToken(state, { | ||
request, | ||
auth: { type: "oauth" } | ||
}); | ||
endpoint.headers.authorization = `token ${token}`; | ||
return request(endpoint); | ||
} | ||
export { | ||
hook | ||
}; |
@@ -6,32 +6,35 @@ import { getUserAgent } from "universal-user-agent"; | ||
import { VERSION } from "./version"; | ||
export function createOAuthDeviceAuth(options) { | ||
const requestWithDefaults = options.request || | ||
octokitRequest.defaults({ | ||
headers: { | ||
"user-agent": `octokit-auth-oauth-device.js/${VERSION} ${getUserAgent()}`, | ||
}, | ||
}); | ||
const { request = requestWithDefaults, ...otherOptions } = options; | ||
const state = options.clientType === "github-app" | ||
? { | ||
...otherOptions, | ||
clientType: "github-app", | ||
request, | ||
} | ||
: { | ||
...otherOptions, | ||
clientType: "oauth-app", | ||
request, | ||
scopes: options.scopes || [], | ||
}; | ||
if (!options.clientId) { | ||
throw new Error('[@octokit/auth-oauth-device] "clientId" option must be set (https://github.com/octokit/auth-oauth-device.js#usage)'); | ||
function createOAuthDeviceAuth(options) { | ||
const requestWithDefaults = options.request || octokitRequest.defaults({ | ||
headers: { | ||
"user-agent": `octokit-auth-oauth-device.js/${VERSION} ${getUserAgent()}` | ||
} | ||
if (!options.onVerification) { | ||
throw new Error('[@octokit/auth-oauth-device] "onVerification" option must be a function (https://github.com/octokit/auth-oauth-device.js#usage)'); | ||
} | ||
// @ts-ignore too much for tsc / ts-jest ¯\_(ツ)_/¯ | ||
return Object.assign(auth.bind(null, state), { | ||
hook: hook.bind(null, state), | ||
}); | ||
}); | ||
const { request = requestWithDefaults, ...otherOptions } = options; | ||
const state = options.clientType === "github-app" ? { | ||
...otherOptions, | ||
clientType: "github-app", | ||
request | ||
} : { | ||
...otherOptions, | ||
clientType: "oauth-app", | ||
request, | ||
scopes: options.scopes || [] | ||
}; | ||
if (!options.clientId) { | ||
throw new Error( | ||
'[@octokit/auth-oauth-device] "clientId" option must be set (https://github.com/octokit/auth-oauth-device.js#usage)' | ||
); | ||
} | ||
if (!options.onVerification) { | ||
throw new Error( | ||
'[@octokit/auth-oauth-device] "onVerification" option must be a function (https://github.com/octokit/auth-oauth-device.js#usage)' | ||
); | ||
} | ||
return Object.assign(auth.bind(null, state), { | ||
hook: hook.bind(null, state) | ||
}); | ||
} | ||
export { | ||
createOAuthDeviceAuth | ||
}; |
@@ -1,1 +0,4 @@ | ||
export const VERSION = "4.0.4"; | ||
const VERSION = "4.0.5"; | ||
export { | ||
VERSION | ||
}; |
@@ -1,2 +0,2 @@ | ||
import { OAuthAppAuthOptions, GitHubAppAuthOptions, OAuthAppAuthentication, GitHubAppAuthentication, OAuthAppState, GitHubAppState } from "./types"; | ||
import type { OAuthAppAuthOptions, GitHubAppAuthOptions, OAuthAppAuthentication, GitHubAppAuthentication, OAuthAppState, GitHubAppState } from "./types"; | ||
export declare function auth(state: OAuthAppState | GitHubAppState, authOptions: OAuthAppAuthOptions | GitHubAppAuthOptions): Promise<OAuthAppAuthentication | GitHubAppAuthentication>; |
@@ -1,3 +0,3 @@ | ||
import { RequestInterface } from "@octokit/types"; | ||
import { OAuthAppState, GitHubAppState, OAuthAppAuthOptions, GitHubAppAuthOptions, OAuthAppAuthentication, GitHubAppAuthentication } from "./types"; | ||
import type { RequestInterface } from "@octokit/types"; | ||
import type { OAuthAppState, GitHubAppState, OAuthAppAuthOptions, GitHubAppAuthOptions, OAuthAppAuthentication, GitHubAppAuthentication } from "./types"; | ||
export declare function getOAuthAccessToken(state: OAuthAppState | GitHubAppState, options: { | ||
@@ -4,0 +4,0 @@ request?: RequestInterface; |
@@ -1,3 +0,3 @@ | ||
import { RequestInterface, OctokitResponse, EndpointOptions, RequestParameters, Route } from "@octokit/types"; | ||
import { OAuthAppState, GitHubAppState } from "./types"; | ||
import type { RequestInterface, OctokitResponse, EndpointOptions, RequestParameters, Route } from "@octokit/types"; | ||
import type { OAuthAppState, GitHubAppState } from "./types"; | ||
export declare function hook(state: OAuthAppState | GitHubAppState, request: RequestInterface, route: Route | EndpointOptions, parameters?: RequestParameters): Promise<OctokitResponse<any>>; |
@@ -1,4 +0,4 @@ | ||
import { GitHubAppAuthInterface, GitHubAppStrategyOptions, OAuthAppAuthInterface, OAuthAppStrategyOptions } from "./types"; | ||
export { OAuthAppStrategyOptions, OAuthAppAuthOptions, OAuthAppAuthentication, GitHubAppStrategyOptions, GitHubAppAuthOptions, GitHubAppAuthentication, GitHubAppAuthenticationWithExpiration, } from "./types"; | ||
import type { GitHubAppAuthInterface, GitHubAppStrategyOptions, OAuthAppAuthInterface, OAuthAppStrategyOptions } from "./types"; | ||
export type { OAuthAppStrategyOptions, OAuthAppAuthOptions, OAuthAppAuthentication, GitHubAppStrategyOptions, GitHubAppAuthOptions, GitHubAppAuthentication, GitHubAppAuthenticationWithExpiration, } from "./types"; | ||
export declare function createOAuthDeviceAuth(options: OAuthAppStrategyOptions): OAuthAppAuthInterface; | ||
export declare function createOAuthDeviceAuth(options: GitHubAppStrategyOptions): GitHubAppAuthInterface; |
@@ -1,2 +0,2 @@ | ||
import { RequestInterface, Route, EndpointOptions, RequestParameters, OctokitResponse } from "@octokit/types"; | ||
import type { RequestInterface, Route, EndpointOptions, RequestParameters, OctokitResponse } from "@octokit/types"; | ||
import * as OAuthMethodsTypes from "@octokit/oauth-methods"; | ||
@@ -3,0 +3,0 @@ export type ClientType = "oauth-app" | "github-app"; |
@@ -1,1 +0,1 @@ | ||
export declare const VERSION = "4.0.4"; | ||
export declare const VERSION = "4.0.5"; |
@@ -1,140 +0,142 @@ | ||
import { getUserAgent } from 'universal-user-agent'; | ||
import { request } from '@octokit/request'; | ||
import { createDeviceCode, exchangeDeviceCode } from '@octokit/oauth-methods'; | ||
// pkg/dist-src/index.js | ||
import { getUserAgent } from "universal-user-agent"; | ||
import { request as octokitRequest } from "@octokit/request"; | ||
// pkg/dist-src/get-oauth-access-token.js | ||
import { createDeviceCode, exchangeDeviceCode } from "@octokit/oauth-methods"; | ||
async function getOAuthAccessToken(state, options) { | ||
const cachedAuthentication = getCachedAuthentication(state, options.auth); | ||
if (cachedAuthentication) | ||
return cachedAuthentication; | ||
// Step 1: Request device and user codes | ||
// https://docs.github.com/en/developers/apps/authorizing-oauth-apps#step-1-app-requests-the-device-and-user-verification-codes-from-github | ||
const { data: verification } = await createDeviceCode({ | ||
clientType: state.clientType, | ||
clientId: state.clientId, | ||
request: options.request || state.request, | ||
// @ts-expect-error the extra code to make TS happy is not worth it | ||
scopes: options.auth.scopes || state.scopes, | ||
}); | ||
// Step 2: User must enter the user code on https://github.com/login/device | ||
// See https://docs.github.com/en/developers/apps/authorizing-oauth-apps#step-2-prompt-the-user-to-enter-the-user-code-in-a-browser | ||
await state.onVerification(verification); | ||
// Step 3: Exchange device code for access token | ||
// See https://docs.github.com/en/developers/apps/authorizing-oauth-apps#step-3-app-polls-github-to-check-if-the-user-authorized-the-device | ||
const authentication = await waitForAccessToken(options.request || state.request, state.clientId, state.clientType, verification); | ||
state.authentication = authentication; | ||
return authentication; | ||
const cachedAuthentication = getCachedAuthentication(state, options.auth); | ||
if (cachedAuthentication) | ||
return cachedAuthentication; | ||
const { data: verification } = await createDeviceCode({ | ||
clientType: state.clientType, | ||
clientId: state.clientId, | ||
request: options.request || state.request, | ||
// @ts-expect-error the extra code to make TS happy is not worth it | ||
scopes: options.auth.scopes || state.scopes | ||
}); | ||
await state.onVerification(verification); | ||
const authentication = await waitForAccessToken( | ||
options.request || state.request, | ||
state.clientId, | ||
state.clientType, | ||
verification | ||
); | ||
state.authentication = authentication; | ||
return authentication; | ||
} | ||
function getCachedAuthentication(state, auth) { | ||
if (auth.refresh === true) | ||
return false; | ||
if (!state.authentication) | ||
return false; | ||
if (state.clientType === "github-app") { | ||
return state.authentication; | ||
} | ||
const authentication = state.authentication; | ||
const newScope = (("scopes" in auth && auth.scopes) || state.scopes).join(" "); | ||
const currentScope = authentication.scopes.join(" "); | ||
return newScope === currentScope ? authentication : false; | ||
function getCachedAuthentication(state, auth2) { | ||
if (auth2.refresh === true) | ||
return false; | ||
if (!state.authentication) | ||
return false; | ||
if (state.clientType === "github-app") { | ||
return state.authentication; | ||
} | ||
const authentication = state.authentication; | ||
const newScope = ("scopes" in auth2 && auth2.scopes || state.scopes).join( | ||
" " | ||
); | ||
const currentScope = authentication.scopes.join(" "); | ||
return newScope === currentScope ? authentication : false; | ||
} | ||
async function wait(seconds) { | ||
await new Promise((resolve) => setTimeout(resolve, seconds * 1000)); | ||
await new Promise((resolve) => setTimeout(resolve, seconds * 1e3)); | ||
} | ||
async function waitForAccessToken(request, clientId, clientType, verification) { | ||
try { | ||
const options = { | ||
clientId, | ||
request, | ||
code: verification.device_code, | ||
}; | ||
// WHY TYPESCRIPT WHY ARE YOU DOING THIS TO ME | ||
const { authentication } = clientType === "oauth-app" | ||
? await exchangeDeviceCode({ | ||
...options, | ||
clientType: "oauth-app", | ||
}) | ||
: await exchangeDeviceCode({ | ||
...options, | ||
clientType: "github-app", | ||
}); | ||
return { | ||
type: "token", | ||
tokenType: "oauth", | ||
...authentication, | ||
}; | ||
try { | ||
const options = { | ||
clientId, | ||
request, | ||
code: verification.device_code | ||
}; | ||
const { authentication } = clientType === "oauth-app" ? await exchangeDeviceCode({ | ||
...options, | ||
clientType: "oauth-app" | ||
}) : await exchangeDeviceCode({ | ||
...options, | ||
clientType: "github-app" | ||
}); | ||
return { | ||
type: "token", | ||
tokenType: "oauth", | ||
...authentication | ||
}; | ||
} catch (error) { | ||
if (!error.response) | ||
throw error; | ||
const errorType = error.response.data.error; | ||
if (errorType === "authorization_pending") { | ||
await wait(verification.interval); | ||
return waitForAccessToken(request, clientId, clientType, verification); | ||
} | ||
catch (error) { | ||
// istanbul ignore if | ||
// @ts-ignore | ||
if (!error.response) | ||
throw error; | ||
// @ts-ignore | ||
const errorType = error.response.data.error; | ||
if (errorType === "authorization_pending") { | ||
await wait(verification.interval); | ||
return waitForAccessToken(request, clientId, clientType, verification); | ||
} | ||
if (errorType === "slow_down") { | ||
await wait(verification.interval + 5); | ||
return waitForAccessToken(request, clientId, clientType, verification); | ||
} | ||
throw error; | ||
if (errorType === "slow_down") { | ||
await wait(verification.interval + 5); | ||
return waitForAccessToken(request, clientId, clientType, verification); | ||
} | ||
throw error; | ||
} | ||
} | ||
// pkg/dist-src/auth.js | ||
async function auth(state, authOptions) { | ||
return getOAuthAccessToken(state, { | ||
auth: authOptions, | ||
}); | ||
return getOAuthAccessToken(state, { | ||
auth: authOptions | ||
}); | ||
} | ||
// pkg/dist-src/hook.js | ||
async function hook(state, request, route, parameters) { | ||
let endpoint = request.endpoint.merge(route, parameters); | ||
// Do not intercept request to retrieve codes or token | ||
if (/\/login\/(oauth\/access_token|device\/code)$/.test(endpoint.url)) { | ||
return request(endpoint); | ||
} | ||
const { token } = await getOAuthAccessToken(state, { | ||
request, | ||
auth: { type: "oauth" }, | ||
}); | ||
endpoint.headers.authorization = `token ${token}`; | ||
let endpoint = request.endpoint.merge( | ||
route, | ||
parameters | ||
); | ||
if (/\/login\/(oauth\/access_token|device\/code)$/.test(endpoint.url)) { | ||
return request(endpoint); | ||
} | ||
const { token } = await getOAuthAccessToken(state, { | ||
request, | ||
auth: { type: "oauth" } | ||
}); | ||
endpoint.headers.authorization = `token ${token}`; | ||
return request(endpoint); | ||
} | ||
const VERSION = "4.0.4"; | ||
// pkg/dist-src/version.js | ||
var VERSION = "4.0.5"; | ||
// pkg/dist-src/index.js | ||
function createOAuthDeviceAuth(options) { | ||
const requestWithDefaults = options.request || | ||
request.defaults({ | ||
headers: { | ||
"user-agent": `octokit-auth-oauth-device.js/${VERSION} ${getUserAgent()}`, | ||
}, | ||
}); | ||
const { request: request$1 = requestWithDefaults, ...otherOptions } = options; | ||
const state = options.clientType === "github-app" | ||
? { | ||
...otherOptions, | ||
clientType: "github-app", | ||
request: request$1, | ||
} | ||
: { | ||
...otherOptions, | ||
clientType: "oauth-app", | ||
request: request$1, | ||
scopes: options.scopes || [], | ||
}; | ||
if (!options.clientId) { | ||
throw new Error('[@octokit/auth-oauth-device] "clientId" option must be set (https://github.com/octokit/auth-oauth-device.js#usage)'); | ||
const requestWithDefaults = options.request || octokitRequest.defaults({ | ||
headers: { | ||
"user-agent": `octokit-auth-oauth-device.js/${VERSION} ${getUserAgent()}` | ||
} | ||
if (!options.onVerification) { | ||
throw new Error('[@octokit/auth-oauth-device] "onVerification" option must be a function (https://github.com/octokit/auth-oauth-device.js#usage)'); | ||
} | ||
// @ts-ignore too much for tsc / ts-jest ¯\_(ツ)_/¯ | ||
return Object.assign(auth.bind(null, state), { | ||
hook: hook.bind(null, state), | ||
}); | ||
}); | ||
const { request = requestWithDefaults, ...otherOptions } = options; | ||
const state = options.clientType === "github-app" ? { | ||
...otherOptions, | ||
clientType: "github-app", | ||
request | ||
} : { | ||
...otherOptions, | ||
clientType: "oauth-app", | ||
request, | ||
scopes: options.scopes || [] | ||
}; | ||
if (!options.clientId) { | ||
throw new Error( | ||
'[@octokit/auth-oauth-device] "clientId" option must be set (https://github.com/octokit/auth-oauth-device.js#usage)' | ||
); | ||
} | ||
if (!options.onVerification) { | ||
throw new Error( | ||
'[@octokit/auth-oauth-device] "onVerification" option must be a function (https://github.com/octokit/auth-oauth-device.js#usage)' | ||
); | ||
} | ||
return Object.assign(auth.bind(null, state), { | ||
hook: hook.bind(null, state) | ||
}); | ||
} | ||
export { createOAuthDeviceAuth }; | ||
//# sourceMappingURL=index.js.map | ||
export { | ||
createOAuthDeviceAuth | ||
}; |
{ | ||
"name": "@octokit/auth-oauth-device", | ||
"version": "4.0.5", | ||
"description": "GitHub OAuth Device authentication strategy for JavaScript", | ||
"version": "4.0.4", | ||
"license": "MIT", | ||
"files": [ | ||
"dist-*/", | ||
"bin/" | ||
], | ||
"pika": true, | ||
"sideEffects": false, | ||
"repository": "github:octokit/auth-oauth-device.js", | ||
"keywords": [ | ||
@@ -18,3 +12,4 @@ "github", | ||
], | ||
"repository": "github:octokit/auth-oauth-device.js", | ||
"author": "Gregor Martynus (https://dev.to/gr2m)", | ||
"license": "MIT", | ||
"dependencies": { | ||
@@ -27,27 +22,30 @@ "@octokit/oauth-methods": "^2.0.0", | ||
"devDependencies": { | ||
"@octokit/tsconfig": "^1.0.2", | ||
"@pika/pack": "^0.5.0", | ||
"@pika/plugin-build-node": "^0.9.2", | ||
"@pika/plugin-build-web": "^0.9.2", | ||
"@pika/plugin-ts-standard-pkg": "^0.9.2", | ||
"@octokit/tsconfig": "^2.0.0", | ||
"@types/jest": "^29.0.0", | ||
"@types/node": "^18.0.0", | ||
"esbuild": "^0.17.19", | ||
"fetch-mock": "^9.11.0", | ||
"glob": "^10.2.7", | ||
"jest": "^29.0.0", | ||
"prettier": "2.8.3", | ||
"semantic-release": "^20.0.0", | ||
"prettier": "2.8.8", | ||
"semantic-release": "^21.0.0", | ||
"semantic-release-plugin-update-version-in-files": "^1.1.0", | ||
"ts-jest": "^29.0.0", | ||
"typescript": "^4.2.2" | ||
"typescript": "^5.0.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"engines": { | ||
"node": ">= 14" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"source": "dist-src/index.js", | ||
"files": [ | ||
"dist-*/**", | ||
"bin/**" | ||
], | ||
"main": "dist-node/index.js", | ||
"browser": "dist-web/index.js", | ||
"types": "dist-types/index.d.ts", | ||
"main": "dist-node/index.js", | ||
"module": "dist-web/index.js" | ||
"module": "dist-src/index.js", | ||
"sideEffects": false | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
12
-14.29%530
7.51%1
-50%50816
-13.08%18
-5.26%