This commit is contained in:
eric sciple 2020-01-24 12:20:19 -05:00
parent beb1329f9f
commit 2b95e76931
7736 changed files with 1874747 additions and 51184 deletions

View file

@ -1,30 +1,22 @@
module.exports = authenticationBeforeRequest
module.exports = authenticationBeforeRequest;
const btoa = require('btoa-lite')
const btoa = require("btoa-lite");
const withAuthorizationPrefix = require('./with-authorization-prefix')
const withAuthorizationPrefix = require("./with-authorization-prefix");
function authenticationBeforeRequest (state, options) {
if (typeof state.auth === 'string') {
options.headers['authorization'] = withAuthorizationPrefix(state.auth)
// https://developer.github.com/v3/previews/#integrations
if (/^bearer /i.test(state.auth) && !/machine-man/.test(options.headers['accept'])) {
const acceptHeaders = options.headers['accept'].split(',')
.concat('application/vnd.github.machine-man-preview+json')
options.headers['accept'] = acceptHeaders.filter(Boolean).join(',')
}
return
function authenticationBeforeRequest(state, options) {
if (typeof state.auth === "string") {
options.headers.authorization = withAuthorizationPrefix(state.auth);
return;
}
if (state.auth.username) {
const hash = btoa(`${state.auth.username}:${state.auth.password}`)
options.headers['authorization'] = `Basic ${hash}`
const hash = btoa(`${state.auth.username}:${state.auth.password}`);
options.headers.authorization = `Basic ${hash}`;
if (state.otp) {
options.headers['x-github-otp'] = state.otp
options.headers["x-github-otp"] = state.otp;
}
return
return;
}
if (state.auth.clientId) {
@ -39,23 +31,23 @@ function authenticationBeforeRequest (state, options) {
// We identify by checking the URL. It must merge both "/applications/:client_id/tokens/:access_token"
// as well as "/applications/123/tokens/token456"
if (/\/applications\/:?[\w_]+\/tokens\/:?[\w_]+($|\?)/.test(options.url)) {
const hash = btoa(`${state.auth.clientId}:${state.auth.clientSecret}`)
options.headers['authorization'] = `Basic ${hash}`
return
const hash = btoa(`${state.auth.clientId}:${state.auth.clientSecret}`);
options.headers.authorization = `Basic ${hash}`;
return;
}
options.url += options.url.indexOf('?') === -1 ? '?' : '&'
options.url += `client_id=${state.auth.clientId}&client_secret=${state.auth.clientSecret}`
return
options.url += options.url.indexOf("?") === -1 ? "?" : "&";
options.url += `client_id=${state.auth.clientId}&client_secret=${state.auth.clientSecret}`;
return;
}
return Promise.resolve()
.then(() => {
return state.auth()
return state.auth();
})
.then((authorization) => {
options.headers['authorization'] = withAuthorizationPrefix(authorization)
})
.then(authorization => {
options.headers.authorization = withAuthorizationPrefix(authorization);
});
}

View file

@ -1,21 +1,76 @@
module.exports = authenticationPlugin
module.exports = authenticationPlugin;
const beforeRequest = require('./before-request')
const requestError = require('./request-error')
const validate = require('./validate')
const { createTokenAuth } = require("@octokit/auth-token");
const { Deprecation } = require("deprecation");
const once = require("once");
function authenticationPlugin (octokit, options) {
if (!options.auth) {
return
const beforeRequest = require("./before-request");
const requestError = require("./request-error");
const validate = require("./validate");
const withAuthorizationPrefix = require("./with-authorization-prefix");
const deprecateAuthBasic = once((log, deprecation) => log.warn(deprecation));
const deprecateAuthObject = once((log, deprecation) => log.warn(deprecation));
function authenticationPlugin(octokit, options) {
// If `options.authStrategy` is set then use it and pass in `options.auth`
if (options.authStrategy) {
const auth = options.authStrategy(options.auth);
octokit.hook.wrap("request", auth.hook);
octokit.auth = auth;
return;
}
validate(options.auth)
// If neither `options.authStrategy` nor `options.auth` are set, the `octokit` instance
// is unauthenticated. The `octokit.auth()` method is a no-op and no request hook is registred.
if (!options.auth) {
octokit.auth = () =>
Promise.resolve({
type: "unauthenticated"
});
return;
}
const isBasicAuthString =
typeof options.auth === "string" &&
/^basic/.test(withAuthorizationPrefix(options.auth));
// If only `options.auth` is set to a string, use the default token authentication strategy.
if (typeof options.auth === "string" && !isBasicAuthString) {
const auth = createTokenAuth(options.auth);
octokit.hook.wrap("request", auth.hook);
octokit.auth = auth;
return;
}
// Otherwise log a deprecation message
const [deprecationMethod, deprecationMessapge] = isBasicAuthString
? [
deprecateAuthBasic,
'Setting the "new Octokit({ auth })" option to a Basic Auth string is deprecated. Use https://github.com/octokit/auth-basic.js instead. See (https://octokit.github.io/rest.js/#authentication)'
]
: [
deprecateAuthObject,
'Setting the "new Octokit({ auth })" option to an object without also setting the "authStrategy" option is deprecated and will be removed in v17. See (https://octokit.github.io/rest.js/#authentication)'
];
deprecationMethod(
octokit.log,
new Deprecation("[@octokit/rest] " + deprecationMessapge)
);
octokit.auth = () =>
Promise.resolve({
type: "deprecated",
message: deprecationMessapge
});
validate(options.auth);
const state = {
octokit,
auth: options.auth
}
};
octokit.hook.before('request', beforeRequest.bind(null, state))
octokit.hook.error('request', requestError.bind(null, state))
octokit.hook.before("request", beforeRequest.bind(null, state));
octokit.hook.error("request", requestError.bind(null, state));
}

View file

@ -1,47 +1,61 @@
module.exports = authenticationRequestError
module.exports = authenticationRequestError;
const { RequestError } = require('@octokit/request-error')
const { RequestError } = require("@octokit/request-error");
function authenticationRequestError (state, error, options) {
if (!error.headers) throw error
function authenticationRequestError(state, error, options) {
if (!error.headers) throw error;
const otpRequired = /required/.test(error.headers['x-github-otp'] || '')
const otpRequired = /required/.test(error.headers["x-github-otp"] || "");
// handle "2FA required" error only
if (error.status !== 401 || !otpRequired) {
throw error
throw error;
}
if (error.status === 401 && otpRequired && error.request && error.request.headers['x-github-otp']) {
if (
error.status === 401 &&
otpRequired &&
error.request &&
error.request.headers["x-github-otp"]
) {
if (state.otp) {
delete state.otp // no longer valid, request again
delete state.otp; // no longer valid, request again
} else {
throw new RequestError('Invalid one-time password for two-factor authentication', 401, {
headers: error.headers,
request: options
})
throw new RequestError(
"Invalid one-time password for two-factor authentication",
401,
{
headers: error.headers,
request: options
}
);
}
}
if (typeof state.auth.on2fa !== 'function') {
throw new RequestError('2FA required, but options.on2fa is not a function. See https://github.com/octokit/rest.js#authentication', 401, {
headers: error.headers,
request: options
})
if (typeof state.auth.on2fa !== "function") {
throw new RequestError(
"2FA required, but options.on2fa is not a function. See https://github.com/octokit/rest.js#authentication",
401,
{
headers: error.headers,
request: options
}
);
}
return Promise.resolve()
.then(() => {
return state.auth.on2fa()
return state.auth.on2fa();
})
.then((oneTimePassword) => {
.then(oneTimePassword => {
const newOptions = Object.assign(options, {
headers: Object.assign(options.headers, { 'x-github-otp': oneTimePassword })
})
return state.octokit.request(newOptions)
.then(response => {
// If OTP still valid, then persist it for following requests
state.otp = oneTimePassword
return response
headers: Object.assign(options.headers, {
"x-github-otp": oneTimePassword
})
})
});
return state.octokit.request(newOptions).then(response => {
// If OTP still valid, then persist it for following requests
state.otp = oneTimePassword;
return response;
});
});
}

View file

@ -1,21 +1,21 @@
module.exports = validateAuth
module.exports = validateAuth;
function validateAuth (auth) {
if (typeof auth === 'string') {
return
function validateAuth(auth) {
if (typeof auth === "string") {
return;
}
if (typeof auth === 'function') {
return
if (typeof auth === "function") {
return;
}
if (auth.username && auth.password) {
return
return;
}
if (auth.clientId && auth.clientSecret) {
return
return;
}
throw new Error(`Invalid "auth" option: ${JSON.stringify(auth)}`)
throw new Error(`Invalid "auth" option: ${JSON.stringify(auth)}`);
}

View file

@ -1,23 +1,23 @@
module.exports = withAuthorizationPrefix
module.exports = withAuthorizationPrefix;
const atob = require('atob-lite')
const atob = require("atob-lite");
const REGEX_IS_BASIC_AUTH = /^[\w-]+:/
const REGEX_IS_BASIC_AUTH = /^[\w-]+:/;
function withAuthorizationPrefix (authorization) {
function withAuthorizationPrefix(authorization) {
if (/^(basic|bearer|token) /i.test(authorization)) {
return authorization
return authorization;
}
try {
if (REGEX_IS_BASIC_AUTH.test(atob(authorization))) {
return `basic ${authorization}`
return `basic ${authorization}`;
}
} catch (error) { }
} catch (error) {}
if (authorization.split(/\./).length === 3) {
return `bearer ${authorization}`
return `bearer ${authorization}`;
}
return `token ${authorization}`
return `token ${authorization}`;
}