authenticate.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. module.exports = authenticate;
  2. const { Deprecation } = require("deprecation");
  3. const once = require("once");
  4. const deprecateAuthenticate = once((log, deprecation) => log.warn(deprecation));
  5. function authenticate(state, options) {
  6. deprecateAuthenticate(
  7. state.octokit.log,
  8. new Deprecation(
  9. '[@octokit/rest] octokit.authenticate() is deprecated. Use "auth" constructor option instead.'
  10. )
  11. );
  12. if (!options) {
  13. state.auth = false;
  14. return;
  15. }
  16. switch (options.type) {
  17. case "basic":
  18. if (!options.username || !options.password) {
  19. throw new Error(
  20. "Basic authentication requires both a username and password to be set"
  21. );
  22. }
  23. break;
  24. case "oauth":
  25. if (!options.token && !(options.key && options.secret)) {
  26. throw new Error(
  27. "OAuth2 authentication requires a token or key & secret to be set"
  28. );
  29. }
  30. break;
  31. case "token":
  32. case "app":
  33. if (!options.token) {
  34. throw new Error("Token authentication requires a token to be set");
  35. }
  36. break;
  37. default:
  38. throw new Error(
  39. "Invalid authentication type, must be 'basic', 'oauth', 'token' or 'app'"
  40. );
  41. }
  42. state.auth = options;
  43. }