Socket
Socket
Sign inDemoInstall

@octokit/auth-oauth-device

Package Overview
Dependencies
Maintainers
2
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@octokit/auth-oauth-device - npm Package Compare versions

Comparing version 3.1.0 to 3.1.1

146

dist-node/index.js

@@ -7,3 +7,3 @@ 'use strict';

var request = require('@octokit/request');
var requestError = require('@octokit/request-error');
var oauthMethods = require('@octokit/oauth-methods');

@@ -96,35 +96,15 @@ function _defineProperty(obj, key, value) {

async function getOAuthAccessToken(state, options) {
// @ts-expect-error looks like TypeScript cannot handle the different OAuth App/GitHub App paths here
const cachedAuthentication = getCachedAuthentication(state, options.auth);
if (cachedAuthentication) return cachedAuthentication; // The "/login/device/code" is not part of the REST API hosted on api.github.com,
// instead it’s using the github.com domain.
const request = options.request || state.request;
const baseUrl = /^https:\/\/(api\.)?github\.com$/.test(request.endpoint.DEFAULTS.baseUrl) ? "https://github.com" : request.endpoint.DEFAULTS.baseUrl.replace("/api/v3", ""); // Step 1: Request device and user codes
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 scope = "scopes" in state ? {
scope: ("scopes" in options.auth && options.auth.scopes || state.scopes).join(" ")
} : {};
const parameters = _objectSpread2({
baseUrl,
method: "POST",
url: "/login/device/code",
headers: {
accept: "application/json"
},
client_id: state.clientId
}, scope);
const requestCodesResponse = await request(parameters);
if ("error" in requestCodesResponse.data) {
throw new requestError.RequestError(`${requestCodesResponse.data.error_description} (${requestCodesResponse.data.error})`, requestCodesResponse.status, {
headers: requestCodesResponse.headers,
request: request.endpoint(parameters)
});
}
const verification = requestCodesResponse.data; // Step 2: User must enter the user code on https://github.com/login/device
const {
data: verification
} = await oauthMethods.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

@@ -135,3 +115,3 @@

const authentication = await waitForAccessToken(request, baseUrl, state.clientId, state.clientType, verification);
const authentication = await waitForAccessToken(options.request || state.request, state.clientId, state.clientType, verification);
state.authentication = authentication;

@@ -159,76 +139,41 @@ return authentication;

async function waitForAccessToken(request, baseUrl, clientId, clientType, verification) {
const requestOptions = {
baseUrl,
method: "POST",
url: "/login/oauth/access_token",
headers: {
accept: "application/json"
},
client_id: clientId,
device_code: verification.device_code,
grant_type: "urn:ietf:params:oauth:grant-type:device_code"
};
const {
data,
headers
} = await request(requestOptions);
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
if ("access_token" in data) {
if (clientType === "oauth-app") {
return {
type: "token",
tokenType: "oauth",
clientType: "oauth-app",
clientId: clientId,
token: data.access_token,
scopes: data.scope.split(/,\s*/).filter(Boolean)
};
const {
authentication
} = clientType === "oauth-app" ? await oauthMethods.exchangeDeviceCode(_objectSpread2(_objectSpread2({}, options), {}, {
clientType: "oauth-app"
})) : await oauthMethods.exchangeDeviceCode(_objectSpread2(_objectSpread2({}, options), {}, {
clientType: "github-app"
}));
return _objectSpread2({
type: "token",
tokenType: "oauth"
}, authentication);
} catch (error) {
// istanbul ignore if
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);
}
if ("refresh_token" in data) {
const apiTimeInMs = new Date(headers.date).getTime();
return {
type: "token",
tokenType: "oauth",
clientType: "github-app",
clientId: clientId,
token: data.access_token,
refreshToken: data.refresh_token,
expiresAt: toTimestamp(apiTimeInMs, data.expires_in),
refreshTokenExpiresAt: toTimestamp(apiTimeInMs, data.refresh_token_expires_in)
};
if (errorType === "slow_down") {
await wait(verification.interval + 5);
return waitForAccessToken(request, clientId, clientType, verification);
}
return {
type: "token",
tokenType: "oauth",
clientType: "github-app",
clientId: clientId,
token: data.access_token
};
throw error;
}
if (data.error === "authorization_pending") {
await wait(verification.interval);
return waitForAccessToken(request, baseUrl, clientId, clientType, verification);
}
if (data.error === "slow_down") {
await wait(verification.interval + 5);
return waitForAccessToken(request, baseUrl, clientId, clientType, verification);
}
throw new requestError.RequestError(`${data.error_description} (${data.error}, ${data.error_url})`, 400, {
request: request.endpoint.merge(requestOptions),
headers: headers
});
}
function toTimestamp(apiTimeInMs, expirationInSeconds) {
return new Date(apiTimeInMs + expirationInSeconds * 1000).toISOString();
}
async function auth(state, authOptions) {
// @ts-expect-error looks like TypeScript cannot handle the different OAuth App/GitHub App paths here
return getOAuthAccessToken(state, {

@@ -244,5 +189,4 @@ auth: authOptions

return request(endpoint);
} // @ts-expect-error looks like TypeScript cannot handle the different OAuth App/GitHub App paths here
}
const {

@@ -260,3 +204,3 @@ token

const VERSION = "3.1.0";
const VERSION = "3.1.1";

@@ -290,3 +234,3 @@ function createOAuthDeviceAuth(options) {

throw new Error('[@octokit/auth-oauth-device] "onVerification" option must be a function (https://github.com/octokit/auth-oauth-device.js#usage)');
} // @ts-expect-error looks like TypeScript cannot handle the different OAuth App/GitHub App paths here
} // @ts-ignore too much for tsc / ts-jest ¯\_(ツ)_/¯

@@ -293,0 +237,0 @@

import { getOAuthAccessToken } from "./get-oauth-access-token";
export async function auth(state, authOptions) {
// @ts-expect-error looks like TypeScript cannot handle the different OAuth App/GitHub App paths here
return getOAuthAccessToken(state, {

@@ -5,0 +4,0 @@ auth: authOptions,

@@ -1,39 +0,15 @@

import { RequestError } from "@octokit/request-error";
import { createDeviceCode, exchangeDeviceCode } from "@octokit/oauth-methods";
export async function getOAuthAccessToken(state, options) {
// @ts-expect-error looks like TypeScript cannot handle the different OAuth App/GitHub App paths here
const cachedAuthentication = getCachedAuthentication(state, options.auth);
if (cachedAuthentication)
return cachedAuthentication;
// The "/login/device/code" is not part of the REST API hosted on api.github.com,
// instead it’s using the github.com domain.
const request = options.request || state.request;
const baseUrl = /^https:\/\/(api\.)?github\.com$/.test(request.endpoint.DEFAULTS.baseUrl)
? "https://github.com"
: request.endpoint.DEFAULTS.baseUrl.replace("/api/v3", "");
// 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 scope = "scopes" in state
? {
scope: (("scopes" in options.auth && options.auth.scopes) ||
state.scopes).join(" "),
}
: {};
const parameters = {
baseUrl,
method: "POST",
url: "/login/device/code",
headers: {
accept: "application/json",
},
client_id: state.clientId,
...scope,
};
const requestCodesResponse = await request(parameters);
if ("error" in requestCodesResponse.data) {
throw new RequestError(`${requestCodesResponse.data.error_description} (${requestCodesResponse.data.error})`, requestCodesResponse.status, {
headers: requestCodesResponse.headers,
request: request.endpoint(parameters),
});
}
const verification = requestCodesResponse.data;
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

@@ -44,3 +20,3 @@ // See https://docs.github.com/en/developers/apps/authorizing-oauth-apps#step-2-prompt-the-user-to-enter-the-user-code-in-a-browser

// 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(request, baseUrl, state.clientId, state.clientType, verification);
const authentication = await waitForAccessToken(options.request || state.request, state.clientId, state.clientType, verification);
state.authentication = authentication;

@@ -65,62 +41,40 @@ return authentication;

}
async function waitForAccessToken(request, baseUrl, clientId, clientType, verification) {
const requestOptions = {
baseUrl,
method: "POST",
url: "/login/oauth/access_token",
headers: {
accept: "application/json",
},
client_id: clientId,
device_code: verification.device_code,
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
};
const { data, headers } = await request(requestOptions);
if ("access_token" in data) {
if (clientType === "oauth-app") {
return {
type: "token",
tokenType: "oauth",
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",
clientId: clientId,
token: data.access_token,
scopes: data.scope.split(/,\s*/).filter(Boolean),
};
}
if ("refresh_token" in data) {
const apiTimeInMs = new Date(headers.date).getTime();
return {
type: "token",
tokenType: "oauth",
})
: await exchangeDeviceCode({
...options,
clientType: "github-app",
clientId: clientId,
token: data.access_token,
refreshToken: data.refresh_token,
expiresAt: toTimestamp(apiTimeInMs, data.expires_in),
refreshTokenExpiresAt: toTimestamp(apiTimeInMs, data.refresh_token_expires_in),
};
}
});
return {
type: "token",
tokenType: "oauth",
clientType: "github-app",
clientId: clientId,
token: data.access_token,
...authentication,
};
}
if (data.error === "authorization_pending") {
await wait(verification.interval);
return waitForAccessToken(request, baseUrl, clientId, clientType, verification);
catch (error) {
// istanbul ignore if
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);
}
if (errorType === "slow_down") {
await wait(verification.interval + 5);
return waitForAccessToken(request, clientId, clientType, verification);
}
throw error;
}
if (data.error === "slow_down") {
await wait(verification.interval + 5);
return waitForAccessToken(request, baseUrl, clientId, clientType, verification);
}
throw new RequestError(`${data.error_description} (${data.error}, ${data.error_url})`, 400, {
request: request.endpoint.merge(requestOptions),
headers: headers,
});
}
function toTimestamp(apiTimeInMs, expirationInSeconds) {
return new Date(apiTimeInMs + expirationInSeconds * 1000).toISOString();
}

@@ -8,3 +8,2 @@ import { getOAuthAccessToken } from "./get-oauth-access-token";

}
// @ts-expect-error looks like TypeScript cannot handle the different OAuth App/GitHub App paths here
const { token } = await getOAuthAccessToken(state, {

@@ -11,0 +10,0 @@ request,

@@ -32,3 +32,3 @@ import { getUserAgent } from "universal-user-agent";

}
// @ts-expect-error looks like TypeScript cannot handle the different OAuth App/GitHub App paths here
// @ts-ignore too much for tsc / ts-jest ¯\_(ツ)_/¯
return Object.assign(auth.bind(null, state), {

@@ -35,0 +35,0 @@ hook: hook.bind(null, state),

@@ -1,1 +0,1 @@

export const VERSION = "3.1.0";
export const VERSION = "3.1.1";
import { OAuthAppAuthOptions, GitHubAppAuthOptions, OAuthAppAuthentication, GitHubAppAuthentication, OAuthAppState, GitHubAppState } from "./types";
export declare function auth(state: OAuthAppState, authOptions: OAuthAppAuthOptions): Promise<OAuthAppAuthentication>;
export declare function auth(state: GitHubAppState, authOptions: GitHubAppAuthOptions): Promise<GitHubAppAuthentication>;
export declare function auth(state: OAuthAppState | GitHubAppState, authOptions: OAuthAppAuthOptions | GitHubAppAuthOptions): Promise<OAuthAppAuthentication | GitHubAppAuthentication>;
import { RequestInterface } from "@octokit/types";
import { OAuthAppState, GitHubAppState, OAuthAppAuthOptions, GitHubAppAuthOptions, OAuthAppAuthentication, GitHubAppAuthentication } from "./types";
export declare function getOAuthAccessToken(state: OAuthAppState, options: {
export declare function getOAuthAccessToken(state: OAuthAppState | GitHubAppState, options: {
request?: RequestInterface;
auth: OAuthAppAuthOptions;
}): Promise<OAuthAppAuthentication>;
export declare function getOAuthAccessToken(state: GitHubAppState, options: {
request?: RequestInterface;
auth: GitHubAppAuthOptions;
}): Promise<GitHubAppAuthentication>;
auth: OAuthAppAuthOptions | GitHubAppAuthOptions;
}): Promise<OAuthAppAuthentication | GitHubAppAuthentication>;
import { RequestInterface, Route, EndpointOptions, RequestParameters, OctokitResponse } from "@octokit/types";
import * as OAuthMethodsTypes from "@octokit/oauth-methods";
export declare type ClientType = "oauth-app" | "github-app";

@@ -36,24 +37,11 @@ export declare type OAuthAppStrategyOptions = {

tokenType: "oauth";
clientType: "oauth-app";
clientId: string;
token: string;
scopes: string[];
};
} & Omit<OAuthMethodsTypes.OAuthAppAuthentication, "clientSecret">;
export declare type GitHubAppAuthentication = {
type: "token";
tokenType: "oauth";
clientType: "github-app";
clientId: string;
token: string;
};
} & Omit<OAuthMethodsTypes.GitHubAppAuthentication, "clientSecret">;
export declare type GitHubAppAuthenticationWithExpiration = {
type: "token";
tokenType: "oauth";
clientType: "github-app";
clientId: string;
token: string;
refreshToken: string;
expiresAt: string;
refreshTokenExpiresAt: string;
};
} & Omit<OAuthMethodsTypes.GitHubAppAuthentication, "clientSecret">;
export declare type Verification = {

@@ -82,2 +70,1 @@ device_code: string;

};
export declare type CodeExchangeResponseError = "authorization_pending" | "slow_down" | "expired_token" | "unsupported_grant_type" | "incorrect_client_credentials" | "incorrect_device_code" | "access_denied";

@@ -1,1 +0,1 @@

export declare const VERSION = "3.1.0";
export declare const VERSION = "3.1.1";
import { getUserAgent } from 'universal-user-agent';
import { request } from '@octokit/request';
import { RequestError } from '@octokit/request-error';
import { createDeviceCode, exchangeDeviceCode } from '@octokit/oauth-methods';
async function getOAuthAccessToken(state, options) {
// @ts-expect-error looks like TypeScript cannot handle the different OAuth App/GitHub App paths here
const cachedAuthentication = getCachedAuthentication(state, options.auth);
if (cachedAuthentication)
return cachedAuthentication;
// The "/login/device/code" is not part of the REST API hosted on api.github.com,
// instead it’s using the github.com domain.
const request = options.request || state.request;
const baseUrl = /^https:\/\/(api\.)?github\.com$/.test(request.endpoint.DEFAULTS.baseUrl)
? "https://github.com"
: request.endpoint.DEFAULTS.baseUrl.replace("/api/v3", "");
// 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 scope = "scopes" in state
? {
scope: (("scopes" in options.auth && options.auth.scopes) ||
state.scopes).join(" "),
}
: {};
const parameters = {
baseUrl,
method: "POST",
url: "/login/device/code",
headers: {
accept: "application/json",
},
client_id: state.clientId,
...scope,
};
const requestCodesResponse = await request(parameters);
if ("error" in requestCodesResponse.data) {
throw new RequestError(`${requestCodesResponse.data.error_description} (${requestCodesResponse.data.error})`, requestCodesResponse.status, {
headers: requestCodesResponse.headers,
request: request.endpoint(parameters),
});
}
const verification = requestCodesResponse.data;
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

@@ -47,3 +23,3 @@ // See https://docs.github.com/en/developers/apps/authorizing-oauth-apps#step-2-prompt-the-user-to-enter-the-user-code-in-a-browser

// 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(request, baseUrl, state.clientId, state.clientType, verification);
const authentication = await waitForAccessToken(options.request || state.request, state.clientId, state.clientType, verification);
state.authentication = authentication;

@@ -68,66 +44,43 @@ return authentication;

}
async function waitForAccessToken(request, baseUrl, clientId, clientType, verification) {
const requestOptions = {
baseUrl,
method: "POST",
url: "/login/oauth/access_token",
headers: {
accept: "application/json",
},
client_id: clientId,
device_code: verification.device_code,
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
};
const { data, headers } = await request(requestOptions);
if ("access_token" in data) {
if (clientType === "oauth-app") {
return {
type: "token",
tokenType: "oauth",
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",
clientId: clientId,
token: data.access_token,
scopes: data.scope.split(/,\s*/).filter(Boolean),
};
}
if ("refresh_token" in data) {
const apiTimeInMs = new Date(headers.date).getTime();
return {
type: "token",
tokenType: "oauth",
})
: await exchangeDeviceCode({
...options,
clientType: "github-app",
clientId: clientId,
token: data.access_token,
refreshToken: data.refresh_token,
expiresAt: toTimestamp(apiTimeInMs, data.expires_in),
refreshTokenExpiresAt: toTimestamp(apiTimeInMs, data.refresh_token_expires_in),
};
}
});
return {
type: "token",
tokenType: "oauth",
clientType: "github-app",
clientId: clientId,
token: data.access_token,
...authentication,
};
}
if (data.error === "authorization_pending") {
await wait(verification.interval);
return waitForAccessToken(request, baseUrl, clientId, clientType, verification);
catch (error) {
// istanbul ignore if
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);
}
if (errorType === "slow_down") {
await wait(verification.interval + 5);
return waitForAccessToken(request, clientId, clientType, verification);
}
throw error;
}
if (data.error === "slow_down") {
await wait(verification.interval + 5);
return waitForAccessToken(request, baseUrl, clientId, clientType, verification);
}
throw new RequestError(`${data.error_description} (${data.error}, ${data.error_url})`, 400, {
request: request.endpoint.merge(requestOptions),
headers: headers,
});
}
function toTimestamp(apiTimeInMs, expirationInSeconds) {
return new Date(apiTimeInMs + expirationInSeconds * 1000).toISOString();
}
async function auth(state, authOptions) {
// @ts-expect-error looks like TypeScript cannot handle the different OAuth App/GitHub App paths here
return getOAuthAccessToken(state, {

@@ -144,3 +97,2 @@ auth: authOptions,

}
// @ts-expect-error looks like TypeScript cannot handle the different OAuth App/GitHub App paths here
const { token } = await getOAuthAccessToken(state, {

@@ -154,3 +106,3 @@ request,

const VERSION = "3.1.0";
const VERSION = "3.1.1";

@@ -183,3 +135,3 @@ function createOAuthDeviceAuth(options) {

}
// @ts-expect-error looks like TypeScript cannot handle the different OAuth App/GitHub App paths here
// @ts-ignore too much for tsc / ts-jest ¯\_(ツ)_/¯
return Object.assign(auth.bind(null, state), {

@@ -186,0 +138,0 @@ hook: hook.bind(null, state),

{
"name": "@octokit/auth-oauth-device",
"description": "GitHub OAuth Device authentication strategy for JavaScript",
"version": "3.1.0",
"version": "3.1.1",
"license": "MIT",

@@ -22,3 +22,2 @@ "files": [

"@octokit/request": "^5.4.14",
"@octokit/request-error": "^2.0.5",
"@octokit/types": "^6.10.0",

@@ -25,0 +24,0 @@ "universal-user-agent": "^6.0.0"

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc