setup-node/node_modules/@octokit/rest/plugins/authentication-deprecated/authenticate.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-01-24 12:20:19 -05:00
module.exports = authenticate;
2020-01-24 12:20:19 -05:00
const { Deprecation } = require("deprecation");
const once = require("once");
2020-01-24 12:20:19 -05:00
const deprecateAuthenticate = once((log, deprecation) => log.warn(deprecation));
2020-01-24 12:20:19 -05:00
function authenticate(state, options) {
deprecateAuthenticate(
state.octokit.log,
new Deprecation(
'[@octokit/rest] octokit.authenticate() is deprecated. Use "auth" constructor option instead.'
)
);
if (!options) {
2020-01-24 12:20:19 -05:00
state.auth = false;
return;
}
switch (options.type) {
2020-01-24 12:20:19 -05:00
case "basic":
if (!options.username || !options.password) {
2020-01-24 12:20:19 -05:00
throw new Error(
"Basic authentication requires both a username and password to be set"
);
}
2020-01-24 12:20:19 -05:00
break;
2020-01-24 12:20:19 -05:00
case "oauth":
if (!options.token && !(options.key && options.secret)) {
2020-01-24 12:20:19 -05:00
throw new Error(
"OAuth2 authentication requires a token or key & secret to be set"
);
}
2020-01-24 12:20:19 -05:00
break;
2020-01-24 12:20:19 -05:00
case "token":
case "app":
if (!options.token) {
2020-01-24 12:20:19 -05:00
throw new Error("Token authentication requires a token to be set");
}
2020-01-24 12:20:19 -05:00
break;
default:
2020-01-24 12:20:19 -05:00
throw new Error(
"Invalid authentication type, must be 'basic', 'oauth', 'token' or 'app'"
);
}
2020-01-24 12:20:19 -05:00
state.auth = options;
}