Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

requireAuth.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.requireAuth = void 0;
  4. const google_auth_library_1 = require("google-auth-library");
  5. const clc = require("colorette");
  6. const api = require("./api");
  7. const apiv2 = require("./apiv2");
  8. const error_1 = require("./error");
  9. const logger_1 = require("./logger");
  10. const utils = require("./utils");
  11. const scopes = require("./scopes");
  12. const auth_1 = require("./auth");
  13. const AUTH_ERROR_MESSAGE = `Command requires authentication, please run ${clc.bold("firebase login")}`;
  14. let authClient;
  15. function getAuthClient(config) {
  16. if (authClient) {
  17. return authClient;
  18. }
  19. authClient = new google_auth_library_1.GoogleAuth(config);
  20. return authClient;
  21. }
  22. async function autoAuth(options, authScopes) {
  23. const client = getAuthClient({ scopes: authScopes, projectId: options.project });
  24. const token = await client.getAccessToken();
  25. token !== null ? apiv2.setAccessToken(token) : false;
  26. }
  27. async function requireAuth(options) {
  28. api.setScopes([scopes.CLOUD_PLATFORM, scopes.FIREBASE_PLATFORM]);
  29. options.authScopes = api.getScopes();
  30. const tokens = options.tokens;
  31. const user = options.user;
  32. let tokenOpt = utils.getInheritedOption(options, "token");
  33. if (tokenOpt) {
  34. logger_1.logger.debug("> authorizing via --token option");
  35. utils.logWarning("Authenticating with `--token` is deprecated and will be removed in a future major version of `firebase-tools`. " +
  36. "Instead, use a service account key with `GOOGLE_APPLICATION_CREDENTIALS`: https://cloud.google.com/docs/authentication/getting-started");
  37. }
  38. else if (process.env.FIREBASE_TOKEN) {
  39. logger_1.logger.debug("> authorizing via FIREBASE_TOKEN environment variable");
  40. utils.logWarning("Authenticating with `FIREBASE_TOKEN` is deprecated and will be removed in a future major version of `firebase-tools`. " +
  41. "Instead, use a service account key with `GOOGLE_APPLICATION_CREDENTIALS`: https://cloud.google.com/docs/authentication/getting-started");
  42. }
  43. else if (user) {
  44. logger_1.logger.debug(`> authorizing via signed-in user (${user.email})`);
  45. }
  46. else {
  47. try {
  48. return await autoAuth(options, options.authScopes);
  49. }
  50. catch (e) {
  51. throw new error_1.FirebaseError(`Failed to authenticate, have you run ${clc.bold("firebase login")}?`, { original: e });
  52. }
  53. }
  54. tokenOpt = tokenOpt || process.env.FIREBASE_TOKEN;
  55. if (tokenOpt) {
  56. (0, auth_1.setRefreshToken)(tokenOpt);
  57. return;
  58. }
  59. if (!user || !tokens) {
  60. throw new error_1.FirebaseError(AUTH_ERROR_MESSAGE);
  61. }
  62. (0, auth_1.setActiveAccount)(options, { user, tokens });
  63. }
  64. exports.requireAuth = requireAuth;