Files

53 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

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