Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@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 2.0.0 to 3.0.0

155

dist-node/index.js

@@ -9,4 +9,88 @@ 'use strict';

function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
function _objectWithoutPropertiesLoose(source, excluded) {
if (source == null) return {};
var target = {};
var sourceKeys = Object.keys(source);
var key, i;
for (i = 0; i < sourceKeys.length; i++) {
key = sourceKeys[i];
if (excluded.indexOf(key) >= 0) continue;
target[key] = source[key];
}
return target;
}
function _objectWithoutProperties(source, excluded) {
if (source == null) return {};
var target = _objectWithoutPropertiesLoose(source, excluded);
var key, i;
if (Object.getOwnPropertySymbols) {
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
for (i = 0; i < sourceSymbolKeys.length; i++) {
key = sourceSymbolKeys[i];
if (excluded.indexOf(key) >= 0) continue;
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
target[key] = source[key];
}
}
return target;
}
async function getOAuthAccessToken(state, options) {
const scope = (options.auth.scopes || state.scopes).join(" ");
const cachedAuthentication = getCachedAuthentication(state, options.auth);

@@ -20,3 +104,7 @@ if (cachedAuthentication) return cachedAuthentication; // The "/login/device/code" is not part of the REST API hosted on api.github.com,

const parameters = {
const scope = "scopes" in state ? {
scope: (options.auth.scopes || state.scopes).join(" ")
} : {};
const parameters = _objectSpread2({
baseUrl,

@@ -28,5 +116,5 @@ method: "POST",

},
client_id: state.clientId,
scope
};
client_id: state.clientId
}, scope);
const requestCodesResponse = await request(parameters);

@@ -47,3 +135,3 @@

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

@@ -57,12 +145,10 @@ return authentication;

if (!("scopes" in state.authentication)) {
if (state.clientType === "github-app") {
return state.authentication;
}
const authentication = state.authentication;
const newScope = (auth.scopes || state.scopes).join(" ");
const currentScope = state.authentication.scopes.join(" ");
if (newScope === currentScope) {
return state.authentication;
}
const currentScope = authentication.scopes.join(" ");
return newScope === currentScope ? authentication : false;
}

@@ -74,3 +160,3 @@

async function waitForAccessToken(request, baseUrl, clientId, verification) {
async function waitForAccessToken(request, baseUrl, clientId, clientType, verification) {
const requestOptions = {

@@ -93,6 +179,2 @@ baseUrl,

if ("access_token" in data) {
// Only Client IDs belonging to GitHub Apps have a "lv1." prefix
// To be more future proof, we only check for the existense of the "."
const clientType = /\./.test(clientId) ? "github-app" : "oauth-app";
if (clientType === "oauth-app") {

@@ -134,3 +216,3 @@ return {

await wait(verification.interval);
return waitForAccessToken(request, baseUrl, clientId, verification);
return waitForAccessToken(request, baseUrl, clientId, clientType, verification);
}

@@ -140,3 +222,3 @@

await wait(verification.interval + 5);
return waitForAccessToken(request, baseUrl, clientId, verification);
return waitForAccessToken(request, baseUrl, clientId, clientType, verification);
}

@@ -179,14 +261,27 @@

const VERSION = "2.0.0";
const VERSION = "3.0.0";
function createOAuthDeviceAuth(options) {
const state = Object.assign({
request: request.request.defaults({
headers: {
"user-agent": `octokit-auth-oauth-device.js/${VERSION} ${universalUserAgent.getUserAgent()}`
}
}),
scopes: []
}, options);
const requestWithDefaults = options.request || request.request.defaults({
headers: {
"user-agent": `octokit-auth-oauth-device.js/${VERSION} ${universalUserAgent.getUserAgent()}`
}
});
const {
request: request$1 = requestWithDefaults,
clientType = "oauth-app",
scopes = []
} = options,
otherOptions = _objectWithoutProperties(options, ["request", "clientType", "scopes"]);
const state = clientType === "github-app" ? _objectSpread2({
clientType,
request: request$1
}, otherOptions) : _objectSpread2({
clientType,
request: request$1,
scopes
}, otherOptions);
if (!options.clientId) {

@@ -200,4 +295,4 @@ throw new Error('[@octokit/auth-oauth-device] "clientId" option must be set (https://github.com/octokit/auth-oauth-device.js#usage)');

return Object.assign(auth.bind(null, state), {
hook: hook.bind(null, state)
return Object.assign(options => auth(state, options), {
hook: (request, route, parameters) => hook(state, request, route, parameters)
});

@@ -204,0 +299,0 @@ }

28

dist-src/get-oauth-access-token.js
import { RequestError } from "@octokit/request-error";
export async function getOAuthAccessToken(state, options) {
const scope = (options.auth.scopes || state.scopes).join(" ");
const cachedAuthentication = getCachedAuthentication(state, options.auth);

@@ -15,2 +14,5 @@ if (cachedAuthentication)

// 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: (options.auth.scopes || state.scopes).join(" ") }
: {};
const parameters = {

@@ -24,3 +26,3 @@ baseUrl,

client_id: state.clientId,
scope,
...scope,
};

@@ -40,3 +42,3 @@ const requestCodesResponse = await request(parameters);

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

@@ -50,10 +52,11 @@ return authentication;

return false;
if (!("scopes" in state.authentication)) {
if (state.clientType === "github-app") {
return state.authentication;
}
const authentication = state.authentication;
const newScope = (auth.scopes || state.scopes).join(" ");
const currentScope = state.authentication.scopes.join(" ");
if (newScope === currentScope) {
return state.authentication;
}
const currentScope = authentication.scopes.join(" ");
return newScope === currentScope
? authentication
: false;
}

@@ -63,3 +66,3 @@ async function wait(seconds) {

}
async function waitForAccessToken(request, baseUrl, clientId, verification) {
async function waitForAccessToken(request, baseUrl, clientId, clientType, verification) {
const requestOptions = {

@@ -78,5 +81,2 @@ baseUrl,

if ("access_token" in data) {
// Only Client IDs belonging to GitHub Apps have a "lv1." prefix
// To be more future proof, we only check for the existense of the "."
const clientType = /\./.test(clientId) ? "github-app" : "oauth-app";
if (clientType === "oauth-app") {

@@ -115,7 +115,7 @@ return {

await wait(verification.interval);
return waitForAccessToken(request, baseUrl, clientId, verification);
return waitForAccessToken(request, baseUrl, clientId, clientType, verification);
}
if (data.error === "slow_down") {
await wait(verification.interval + 5);
return waitForAccessToken(request, baseUrl, clientId, verification);
return waitForAccessToken(request, baseUrl, clientId, clientType, verification);
}

@@ -122,0 +122,0 @@ throw new RequestError(`${data.error_description} (${data.error}, ${data.error_url})`, 400, {

import { getUserAgent } from "universal-user-agent";
import { request } from "@octokit/request";
import { request as octokitRequest } from "@octokit/request";
import { auth } from "./auth";

@@ -7,10 +7,21 @@ import { hook } from "./hook";

export function createOAuthDeviceAuth(options) {
const state = Object.assign({
request: request.defaults({
const requestWithDefaults = options.request ||
octokitRequest.defaults({
headers: {
"user-agent": `octokit-auth-oauth-device.js/${VERSION} ${getUserAgent()}`,
},
}),
scopes: [],
}, options);
});
const { request = requestWithDefaults, clientType = "oauth-app", scopes = [], ...otherOptions } = options;
const state = clientType === "github-app"
? {
clientType,
request,
...otherOptions,
}
: {
clientType,
request,
scopes,
...otherOptions,
};
if (!options.clientId) {

@@ -22,5 +33,5 @@ throw new Error('[@octokit/auth-oauth-device] "clientId" option must be set (https://github.com/octokit/auth-oauth-device.js#usage)');

}
return Object.assign(auth.bind(null, state), {
hook: hook.bind(null, state),
return Object.assign((options) => auth(state, options), {
hook: (request, route, parameters) => hook(state, request, route, parameters),
});
}

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

export const VERSION = "2.0.0";
export const VERSION = "3.0.0";

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

import { State, AuthOptions, Authentication } from "./types";
export declare function auth(state: State, authOptions: AuthOptions): Promise<Authentication>;
import { State, AuthOptions, Authentication, ClientType } from "./types";
export declare function auth<TClientType extends ClientType>(state: State, authOptions: AuthOptions): Promise<Authentication<TClientType>>;
import { RequestInterface } from "@octokit/types";
import { AuthOptions, State, Authentication } from "./types";
export declare function getOAuthAccessToken(state: State, options: {
import { AuthOptions, ClientType, State, Authentication } from "./types";
export declare function getOAuthAccessToken<TClientType extends ClientType>(state: State, options: {
request?: RequestInterface;
auth: AuthOptions;
}): Promise<Authentication>;
}): Promise<Authentication<TClientType>>;
import { RequestInterface, OctokitResponse, EndpointOptions, RequestParameters, Route } from "@octokit/types";
import { State } from "./types";
export declare function hook(state: State, request: RequestInterface, route: Route | EndpointOptions, parameters?: RequestParameters): Promise<OctokitResponse<any>>;
import { ClientType, State } from "./types";
export declare function hook<TClientType extends ClientType>(state: State, request: RequestInterface, route: Route | EndpointOptions, parameters?: RequestParameters): Promise<OctokitResponse<any>>;
import * as Types from "./types";
export declare type StrategyOptions = Types.StrategyOptions;
export declare type AuthOptions = Types.AuthOptions;
export declare type Authentication = Types.Authentication;
export declare type OAuthAppAuthentication = Types.OAuthAppAuthentication;
export declare type GitHubAppAuthentication = Types.GitHubAppAuthentication;
export declare type GitHubAppAuthenticationWithExpiration = Types.GitHubAppAuthenticationWithExpiration;
export declare function createOAuthDeviceAuth(options: Types.StrategyOptions): Types.AuthInterface;
export { StrategyOptions, AuthOptions, Authentication, OAuthAppAuthentication, GitHubAppAuthentication, GitHubAppAuthenticationWithExpiration, } from "./types";
export declare function createOAuthDeviceAuth<TClientType extends Types.ClientType = "oauth-app">(options: Types.StrategyOptions<TClientType>): Types.AuthInterface<TClientType>;
import { RequestInterface, Route, EndpointOptions, RequestParameters, OctokitResponse } from "@octokit/types";
export declare type StrategyOptions = {
export declare type ClientType = "oauth-app" | "github-app";
export declare type OAuthAppStrategyOptions<TClientType extends ClientType> = {
clientId: string;
clientType?: TClientType;
onVerification: OnVerificationCallback;

@@ -8,4 +10,13 @@ scopes?: string[];

};
export interface AuthInterface {
(options: AuthOptions): Promise<Authentication>;
export declare type GitHubAppStrategyOptions<TClientType extends ClientType> = {
clientId: string;
clientType: TClientType;
onVerification: OnVerificationCallback;
/** `scopes` are not permitted for GitHub Apps */
scopes?: never;
request?: RequestInterface;
};
export declare type StrategyOptions<TClientType extends ClientType = "oauth-app"> = TClientType extends "oauth-app" ? OAuthAppStrategyOptions<TClientType> : TClientType extends "github-app" ? GitHubAppStrategyOptions<TClientType> : never;
export interface AuthInterface<TClientType extends ClientType> {
(options: AuthOptions): Promise<Authentication<TClientType>>;
hook(request: RequestInterface, route: Route | EndpointOptions, parameters?: RequestParameters): Promise<OctokitResponse<any>>;

@@ -43,3 +54,3 @@ }

};
export declare type Authentication = OAuthAppAuthentication | GitHubAppAuthentication | GitHubAppAuthenticationWithExpiration;
export declare type Authentication<TClientType extends ClientType = "oauth-app"> = TClientType extends "oauth-app" ? OAuthAppAuthentication : TClientType extends "github-app" ? GitHubAppAuthentication | GitHubAppAuthenticationWithExpiration : never;
export declare type Verification = {

@@ -53,9 +64,18 @@ device_code: string;

export declare type OnVerificationCallback = (verification: Verification) => any | Promise<any>;
export declare type State = {
export declare type OAuthAppState = {
clientId: string;
clientType: "oauth-app";
onVerification: OnVerificationCallback;
scopes: string[];
request: RequestInterface;
authentication?: Authentication;
authentication?: Authentication<"oauth-app">;
};
export declare type GitHubAppState = {
clientId: string;
clientType: "github-app";
onVerification: OnVerificationCallback;
request: RequestInterface;
authentication?: Authentication<"github-app">;
};
export declare type State = OAuthAppState | GitHubAppState;
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 = "2.0.0";
export declare const VERSION = "3.0.0";

@@ -6,3 +6,2 @@ import { getUserAgent } from 'universal-user-agent';

async function getOAuthAccessToken(state, options) {
const scope = (options.auth.scopes || state.scopes).join(" ");
const cachedAuthentication = getCachedAuthentication(state, options.auth);

@@ -19,2 +18,5 @@ if (cachedAuthentication)

// 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: (options.auth.scopes || state.scopes).join(" ") }
: {};
const parameters = {

@@ -28,3 +30,3 @@ baseUrl,

client_id: state.clientId,
scope,
...scope,
};

@@ -44,3 +46,3 @@ const requestCodesResponse = await request(parameters);

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

@@ -54,10 +56,11 @@ return authentication;

return false;
if (!("scopes" in state.authentication)) {
if (state.clientType === "github-app") {
return state.authentication;
}
const authentication = state.authentication;
const newScope = (auth.scopes || state.scopes).join(" ");
const currentScope = state.authentication.scopes.join(" ");
if (newScope === currentScope) {
return state.authentication;
}
const currentScope = authentication.scopes.join(" ");
return newScope === currentScope
? authentication
: false;
}

@@ -67,3 +70,3 @@ async function wait(seconds) {

}
async function waitForAccessToken(request, baseUrl, clientId, verification) {
async function waitForAccessToken(request, baseUrl, clientId, clientType, verification) {
const requestOptions = {

@@ -82,5 +85,2 @@ baseUrl,

if ("access_token" in data) {
// Only Client IDs belonging to GitHub Apps have a "lv1." prefix
// To be more future proof, we only check for the existense of the "."
const clientType = /\./.test(clientId) ? "github-app" : "oauth-app";
if (clientType === "oauth-app") {

@@ -119,7 +119,7 @@ return {

await wait(verification.interval);
return waitForAccessToken(request, baseUrl, clientId, verification);
return waitForAccessToken(request, baseUrl, clientId, clientType, verification);
}
if (data.error === "slow_down") {
await wait(verification.interval + 5);
return waitForAccessToken(request, baseUrl, clientId, verification);
return waitForAccessToken(request, baseUrl, clientId, clientType, verification);
}

@@ -155,13 +155,24 @@ throw new RequestError(`${data.error_description} (${data.error}, ${data.error_url})`, 400, {

const VERSION = "2.0.0";
const VERSION = "3.0.0";
function createOAuthDeviceAuth(options) {
const state = Object.assign({
request: request.defaults({
const requestWithDefaults = options.request ||
request.defaults({
headers: {
"user-agent": `octokit-auth-oauth-device.js/${VERSION} ${getUserAgent()}`,
},
}),
scopes: [],
}, options);
});
const { request: request$1 = requestWithDefaults, clientType = "oauth-app", scopes = [], ...otherOptions } = options;
const state = clientType === "github-app"
? {
clientType,
request: request$1,
...otherOptions,
}
: {
clientType,
request: request$1,
scopes,
...otherOptions,
};
if (!options.clientId) {

@@ -173,4 +184,4 @@ throw new Error('[@octokit/auth-oauth-device] "clientId" option must be set (https://github.com/octokit/auth-oauth-device.js#usage)');

}
return Object.assign(auth.bind(null, state), {
hook: hook.bind(null, state),
return Object.assign((options) => auth(state, options), {
hook: (request, route, parameters) => hook(state, request, route, parameters),
});

@@ -177,0 +188,0 @@ }

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

@@ -6,0 +6,0 @@ "files": [

@@ -7,3 +7,2 @@ # auth-oauth-device.js

[![Build Status](https://github.com/octokit/auth-oauth-device.js/workflows/Test/badge.svg)](https://github.com/octokit/auth-oauth-device.js/actions?query=workflow%3ATest+branch%3Amain)
[![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=octokit/auth-oauth-device.js)](https://dependabot.com/)

@@ -15,2 +14,4 @@ `@octokit/auth-oauth-device` is implementing one of [GitHub’s OAuth Device Flow](https://docs.github.com/en/developers/apps/authorizing-oauth-apps#device-flow).

- [Usage](#usage)
- [For OAuth Apps](#for-oauth-apps)
- [For GitHub Apps](#for-github-apps)
- [`createOAuthDeviceAuth(options)`](#createoauthdeviceauthoptions)

@@ -65,5 +66,8 @@ - [`auth(options)`](#authoptions)

### For OAuth Apps
```js
const auth = createOAuthDeviceAuth({
clientId: "123",
clientType: "oauth-app",
clientId: "1234567890abcdef1234",
onVerification(verification) {

@@ -91,2 +95,4 @@ // verification example

// tokenType: 'oauth',
// clientType: 'oauth-app',
// clientId: '1234567890abcdef1234',
// token: '...', /* the created oauth token */

@@ -97,2 +103,49 @@ // scopes: [] /* depend on request scopes by OAuth app */

### For GitHub Apps
GitHub Apps do not support `scopes`. Client IDs of GitHub Apps have a `lv1.` prefix. If the GitHub App has expiring user tokens enabled, the resulting `authentication` object has extra properties related to expiration and refreshing the token.
```js
const auth = createOAuthDeviceAuth({
clientType: "github-app",
clientId: "lv1.1234567890abcdef",
onVerification(verification) {
// verification example
// {
// device_code: "3584d83530557fdd1f46af8289938c8ef79f9dc5",
// user_code: "WDJB-MJHT",
// verification_uri: "https://github.com/login/device",
// expires_in: 900,
// interval: 5,
// };
console.log("Open %s", verification.verification_uri);
console.log("Enter code: %s", verification.user_code);
},
});
const tokenAuthentication = await auth({
type: "oauth",
});
// resolves with
// {
// type: 'token',
// tokenType: 'oauth',
// clientType: 'github-app',
// clientId: 'lv1.1234567890abcdef',
// token: '...', /* the created oauth token */
// }
// or if expiring user tokens are enabled
// {
// type: 'token',
// tokenType: 'oauth',
// clientType: 'github-app',
// clientId: 'lv1.1234567890abcdef',
// token: '...', /* the created oauth token */
// refreshToken: "r1.c1b4a2e77838347a7e420ce178f2e7c6912e169246c34e1ccbf66c46812d16d5b1a9dc86a149873c",
// expiresAt: "2022-01-01T08:00:0.000Z",
// refreshTokenExpiresAt: "2021-07-01T00:00:0.000Z",
// }
```
## `createOAuthDeviceAuth(options)`

@@ -119,3 +172,3 @@

<th>
<code>options.clientId</code>
<code>clientId</code>
</th>

@@ -131,3 +184,3 @@ <th>

<th>
<code>options.onVerification</code>
<code>onVerification</code>
</th>

@@ -144,3 +197,3 @@ <th>

const auth = createOAuthDeviceAuth({
clientId: "123",
clientId: "1234567890abcdef1234",
onVerification(verification) {

@@ -159,5 +212,18 @@ console.log("Open %s", verification.verification_uri);

<th>
<code>options.request</code>
<code>clientType</code>
</th>
<th>
<code>string</code>
</th>
<td>
Must be either `oauth-app` or `github-app`. Defaults to `oauth-app`.
</td>
</tr>
<tr>
<th>
<code>request</code>
</th>
<th>
<code>function</code>

@@ -171,3 +237,3 @@ </th>

createOAuthDeviceAuth({
clientId: 123,
clientId: "1234567890abcdef1234",
clientSecret: "secret",

@@ -189,4 +255,8 @@ request: request.defaults({

<td>
Array of scope names enabled for the token. Defaults to <code>[]</code>. See <a href="https://docs.github.com/en/developers/apps/scopes-for-oauth-apps#available-scopes">available scopes</a>
</td>
Only relavant if `clientType` is set to `"oauth-app"`.
Array of scope names enabled for the token. Defaults to `[]`. See [available scopes](https://docs.github.com/en/developers/apps/scopes-for-oauth-apps#available-scopes).
</td>
</tr>

@@ -217,3 +287,3 @@ </tbody>

<th>
<code>options.type</code>
<code>type</code>
</th>

@@ -220,0 +290,0 @@ <th>

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