Sin descripción
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.

defaultCredentials.js 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.clearCredentials = exports.getCredentialPathAsync = void 0;
  4. const fs = require("fs");
  5. const path = require("path");
  6. const api_1 = require("./api");
  7. const logger_1 = require("./logger");
  8. async function getCredentialPathAsync(account) {
  9. const filePath = credFilePath(account.user);
  10. if (!filePath) {
  11. logger_1.logger.debug("defaultcredentials: could not create path to default credentials file.");
  12. return undefined;
  13. }
  14. const cred = getCredential(account.tokens);
  15. if (!cred) {
  16. logger_1.logger.debug("defaultcredentials: no credential available.");
  17. return undefined;
  18. }
  19. logger_1.logger.debug(`defaultcredentials: writing to file ${filePath}`);
  20. return new Promise((res, rej) => {
  21. fs.writeFile(filePath, JSON.stringify(cred, undefined, 2), "utf8", (err) => {
  22. if (err) {
  23. rej(err);
  24. }
  25. else {
  26. res(filePath);
  27. }
  28. });
  29. });
  30. }
  31. exports.getCredentialPathAsync = getCredentialPathAsync;
  32. function clearCredentials(account) {
  33. const filePath = credFilePath(account.user);
  34. if (!filePath) {
  35. return;
  36. }
  37. if (!fs.existsSync(filePath)) {
  38. return;
  39. }
  40. fs.unlinkSync(filePath);
  41. }
  42. exports.clearCredentials = clearCredentials;
  43. function getCredential(tokens) {
  44. if (tokens.refresh_token) {
  45. return {
  46. client_id: api_1.clientId,
  47. client_secret: api_1.clientSecret,
  48. refresh_token: tokens.refresh_token,
  49. type: "authorized_user",
  50. };
  51. }
  52. }
  53. function credFilePath(user) {
  54. let configDir = undefined;
  55. if (process.platform.startsWith("win")) {
  56. configDir = process.env["APPDATA"];
  57. }
  58. else {
  59. const home = process.env["HOME"];
  60. if (home) {
  61. configDir = path.join(home, ".config");
  62. }
  63. }
  64. if (!configDir) {
  65. return undefined;
  66. }
  67. if (!fs.existsSync(configDir)) {
  68. fs.mkdirSync(configDir);
  69. }
  70. const fbtConfigDir = path.join(configDir, "firebase");
  71. if (!fs.existsSync(fbtConfigDir)) {
  72. fs.mkdirSync(fbtConfigDir);
  73. }
  74. return path.join(fbtConfigDir, `${userEmailSlug(user)}_application_default_credentials.json`);
  75. }
  76. function userEmailSlug(user) {
  77. const email = user.email || "unknown_user";
  78. const slug = email.replace("@", "_").replace(".", "_");
  79. return slug;
  80. }