@sap/cli-core 2023.14.0 → 2023.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/CHANGELOG.md +179 -2
  2. package/README.md +1 -1
  3. package/cache/secrets/SecretsStorageImpl.d.ts +22 -0
  4. package/cache/secrets/SecretsStorageImpl.js +147 -0
  5. package/cache/secrets/SecretsStorageSingleton.d.ts +4 -0
  6. package/cache/secrets/SecretsStorageSingleton.js +7 -0
  7. package/cache/secrets/types.d.ts +11 -0
  8. package/cache/secrets/types.js +2 -0
  9. package/cache/secrets/utils.d.ts +2 -0
  10. package/cache/secrets/utils.js +18 -0
  11. package/commands/{cache.command → config.command/cache.command}/clean.command.d.ts +1 -1
  12. package/commands/{cache.command → config.command/cache.command}/clean.command.js +3 -3
  13. package/commands/{cache.command → config.command/cache.command}/index.d.ts +1 -1
  14. package/commands/config.command/cache.command/init.command.d.ts +4 -0
  15. package/commands/{cache.command → config.command/cache.command}/init.command.js +15 -8
  16. package/commands/{cache.command → config.command/cache.command}/show.command.d.ts +1 -1
  17. package/commands/{cache.command → config.command/cache.command}/show.command.js +4 -4
  18. package/commands/{host.command.d.ts → config.command/host.command.d.ts} +1 -1
  19. package/commands/{host.command.js → config.command/host.command.js} +12 -12
  20. package/commands/config.command/index.d.ts +2 -0
  21. package/commands/config.command/index.js +19 -0
  22. package/commands/{passcode.command.d.ts → config.command/passcode.command.d.ts} +1 -1
  23. package/commands/{passcode.command.js → config.command/passcode.command.js} +8 -8
  24. package/commands/{secrets.command → config.command/secrets.command}/index.d.ts +1 -1
  25. package/commands/{secrets.command → config.command/secrets.command}/index.js +2 -1
  26. package/commands/config.command/secrets.command/reset.command.d.ts +3 -0
  27. package/commands/config.command/secrets.command/reset.command.js +17 -0
  28. package/commands/{secrets.command → config.command/secrets.command}/show.command.d.ts +1 -1
  29. package/commands/{secrets.command → config.command/secrets.command}/show.command.js +14 -7
  30. package/commands/handler/authentication/oauth/secretsProvider/cache.js +4 -5
  31. package/commands/handler/authentication/oauth/secretsProvider/file.js +3 -3
  32. package/commands/handler/authentication/oauth/secretsProvider/options.js +12 -6
  33. package/commands/handler/authentication/oauth/tokenProvider/getToken.js +5 -61
  34. package/commands/handler/authentication/oauth/tokenProvider/refreshToken.js +1 -1
  35. package/commands/handler/authentication/oauth/tokenProvider/setAuthorization.js +4 -4
  36. package/commands/handler/authentication/oauth/tokenProvider/utils.d.ts +2 -0
  37. package/commands/handler/authentication/oauth/tokenProvider/utils.js +77 -4
  38. package/commands/handler/authentication/oauth/utils.d.ts +0 -4
  39. package/commands/handler/authentication/oauth/utils.js +13 -27
  40. package/commands/handler/authentication/passcode/function.js +1 -1
  41. package/commands/handler/authentication/passcode/input.js +2 -2
  42. package/commands/handler/authentication/passcode/setPasscode.js +1 -1
  43. package/commands/handler/authentication/technicalJWT/index.js +2 -2
  44. package/commands/handler/checkOptionsExistence.js +1 -1
  45. package/commands/handler/error.js +2 -2
  46. package/commands/handler/fail.js +1 -1
  47. package/commands/handler/fetch/fetch.js +1 -1
  48. package/commands/handler/fetch/utils.js +1 -1
  49. package/commands/handler/input/file.js +1 -1
  50. package/commands/handler/input/input.js +2 -2
  51. package/commands/handler/mandatoryOptions.js +1 -1
  52. package/commands/handler/next.js +3 -2
  53. package/commands/handler/options/env.js +1 -2
  54. package/commands/handler/options/file.js +1 -1
  55. package/commands/handler/options/index.js +5 -4
  56. package/commands/handler/options/option.js +1 -1
  57. package/commands/handler/options/pipe.js +1 -1
  58. package/commands/handler/options/utils.d.ts +1 -1
  59. package/commands/handler/options/utils.js +11 -4
  60. package/commands/handler/or.js +3 -2
  61. package/commands/handler/parseArguments.js +1 -1
  62. package/commands/handler/resilient.js +2 -2
  63. package/commands/handler/root/index.js +2 -2
  64. package/commands/handler/stackTrace.js +2 -2
  65. package/commands/handler/succeed.js +1 -1
  66. package/commands/login.command.js +34 -4
  67. package/commands/logout.command.js +16 -6
  68. package/commands/openAPI.command/index.js +3 -8
  69. package/commands/openAPI.command/utils.js +4 -13
  70. package/commands/utils.d.ts +2 -0
  71. package/commands/utils.js +8 -0
  72. package/config/core.d.ts +1 -0
  73. package/config/core.js +5 -1
  74. package/config/index.js +1 -0
  75. package/constants.d.ts +3 -1
  76. package/constants.js +34 -5
  77. package/discovery/index.js +0 -4
  78. package/dwc/dwc.js +15 -26
  79. package/dwc/run.js +2 -0
  80. package/dwc/utils.d.ts +2 -0
  81. package/dwc/utils.js +34 -2
  82. package/index.d.ts +2 -1
  83. package/index.js +4 -1
  84. package/logger/index.d.ts +1 -10
  85. package/logger/types.d.ts +10 -0
  86. package/logger/types.js +2 -0
  87. package/logger/utils.d.ts +2 -0
  88. package/logger/utils.js +12 -0
  89. package/package.json +7 -6
  90. package/result/ResultHandlerFactory.js +1 -0
  91. package/result/ResultHandlerImpl.js +1 -0
  92. package/types.d.ts +9 -3
  93. package/utils/commands.d.ts +2 -2
  94. package/utils/commands.js +23 -11
  95. package/utils/http/index.js +0 -1
  96. package/utils/utils.d.ts +5 -1
  97. package/utils/utils.js +15 -4
  98. package/commands/cache.command/init.command.d.ts +0 -3
  99. /package/commands/{cache.command → config.command/cache.command}/index.js +0 -0
@@ -3,11 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const reset_command_1 = __importDefault(require("./reset.command"));
6
7
  const show_command_1 = __importDefault(require("./show.command"));
7
8
  const secretsCommand = {
8
9
  type: "topCommand",
9
10
  command: "secrets",
10
11
  description: "work with the locally stored secrets",
11
- subCommands: [show_command_1.default],
12
+ subCommands: [show_command_1.default, reset_command_1.default],
12
13
  };
13
14
  exports.default = secretsCommand;
@@ -0,0 +1,3 @@
1
+ import { Command } from "../../../types";
2
+ declare const resetCommand: Command;
3
+ export default resetCommand;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /* jscpd:ignore-start */
4
+ const SecretsStorageSingleton_1 = require("../../../cache/secrets/SecretsStorageSingleton");
5
+ const handler_1 = require("../../handler");
6
+ const cache_1 = require("../../handler/authentication/oauth/secretsProvider/cache");
7
+ /* jscpd:ignore-end */
8
+ const removeSecrets = async () => async () => {
9
+ await SecretsStorageSingleton_1.SecretsStorageSingleton.SINGLETON.deleteAllSecrets();
10
+ };
11
+ const resetCommand = {
12
+ type: "command",
13
+ command: "reset",
14
+ description: "remove all locally stored secrets for interactive OAuth authentication",
15
+ handler: (0, handler_1.createNextHandler)((0, handler_1.createParseArgumentsHandler)(), (0, handler_1.createResilientHandler)((0, cache_1.create)()), removeSecrets),
16
+ };
17
+ exports.default = resetCommand;
@@ -1,3 +1,3 @@
1
- import { Command } from "../../types";
1
+ import { Command } from "../../../types";
2
2
  declare const showCommand: Command;
3
3
  export default showCommand;
@@ -1,14 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const logger_1 = require("../../logger");
4
- const handler_1 = require("../handler");
5
- const cache_1 = require("../handler/authentication/oauth/secretsProvider/cache");
6
- const utils_1 = require("../handler/authentication/oauth/utils");
7
- const showSecrets = () => async () => {
3
+ /* jscpd:ignore-start */
4
+ const SecretsStorageSingleton_1 = require("../../../cache/secrets/SecretsStorageSingleton");
5
+ const logger_1 = require("../../../logger");
6
+ const handler_1 = require("../../handler");
7
+ const cache_1 = require("../../handler/authentication/oauth/secretsProvider/cache");
8
+ /* jscpd:ignore-end */
9
+ const showSecrets = async () => async () => {
8
10
  const { output, error } = (0, logger_1.get)("commands.secrets-show");
9
11
  try {
10
- const secrets = (0, utils_1.getSecrets)();
11
- output(JSON.stringify(secrets, null, 2));
12
+ const secrets = await SecretsStorageSingleton_1.SecretsStorageSingleton.SINGLETON.getAllSecrets();
13
+ if (secrets.length === 0) {
14
+ output("No secrets exist");
15
+ }
16
+ else {
17
+ output(JSON.stringify(secrets, null, 2));
18
+ }
12
19
  }
13
20
  catch (err) {
14
21
  error("failed to read secrets", err);
@@ -1,15 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.create = void 0;
4
- const constants_1 = require("../../../../../constants");
5
4
  const logger_1 = require("../../../../../logger");
6
- const utils_1 = require("../utils");
7
- const cache_1 = require("../../../../../cache");
8
- const create = () => () => async () => {
5
+ const SecretsStorageSingleton_1 = require("../../../../../cache/secrets/SecretsStorageSingleton");
6
+ const create = () => async () => async () => {
9
7
  const { error, info } = (0, logger_1.get)("commands.handler.authentication.oauth.secretsProvider.cache");
10
8
  info("reading secrets from cache");
11
9
  try {
12
- (0, utils_1.setSecrets)(JSON.parse(await (0, cache_1.readFile)(constants_1.CACHE_SECRETS_FILE)));
10
+ // verify whether secret is available by retrieving default secret for current host
11
+ await SecretsStorageSingleton_1.SecretsStorageSingleton.SINGLETON.getDefaultSecret();
13
12
  }
14
13
  catch (err) {
15
14
  error("failed to read secrets from cache", err);
@@ -7,13 +7,13 @@ const constants_1 = require("../../../../../constants");
7
7
  const logger_1 = require("../../../../../logger");
8
8
  const options_1 = require("../../../../../utils/options");
9
9
  const options_2 = require("../../../options");
10
- const utils_1 = require("../utils");
11
- const handler = () => async () => {
10
+ const SecretsStorageSingleton_1 = require("../../../../../cache/secrets/SecretsStorageSingleton");
11
+ const handler = async () => async () => {
12
12
  const { error, debug, info } = (0, logger_1.get)("commands.handler.authentication.oauth.secretsProvider.file");
13
13
  info("reading secrets from file");
14
14
  try {
15
15
  const secretsFile = (0, options_1.getOptionValueFromConfig)(constants_1.OPTION_SECRETS_FILE);
16
- (0, utils_1.setSecrets)(JSON.parse(await (0, fs_extra_1.readFile)(secretsFile, "utf8")));
16
+ await SecretsStorageSingleton_1.SecretsStorageSingleton.SINGLETON.storeSecret(JSON.parse(await (0, fs_extra_1.readFile)(secretsFile, "utf8")));
17
17
  debug("secrets found from file");
18
18
  }
19
19
  catch (err) {
@@ -6,13 +6,14 @@ const or_1 = require("../../../or");
6
6
  const options_1 = require("../../../options");
7
7
  const checkOptionsExistence_1 = require("../../../checkOptionsExistence");
8
8
  const constants_1 = require("../../../../../constants");
9
- const utils_1 = require("../utils");
10
9
  const logger_1 = require("../../../../../logger");
11
10
  const options_2 = require("../../../../../utils/options");
11
+ const SecretsStorageSingleton_1 = require("../../../../../cache/secrets/SecretsStorageSingleton");
12
12
  const createOptHandler = (option) => (0, options_1.create)([option], undefined, true, true);
13
- const setOAuthFromOptions = () => async () => {
13
+ const getLogger = () => (0, logger_1.get)("commands.handler.authentication.oauth.secretsProvider.options");
14
+ const setOAuthFromOptions = async () => async () => {
14
15
  const expiresIn = (0, options_2.getOptionValueFromConfigGracefully)(constants_1.OPTION_EXPIRES_IN);
15
- (0, utils_1.setSecrets)({
16
+ await SecretsStorageSingleton_1.SecretsStorageSingleton.SINGLETON.storeSecret({
16
17
  client_id: (0, options_2.getOptionValueFromConfigGracefully)(constants_1.OPTION_CLIENT_ID),
17
18
  client_secret: (0, options_2.getOptionValueFromConfigGracefully)(constants_1.OPTION_CLIENT_SECRET),
18
19
  authorization_url: (0, options_2.getOptionValueFromConfigGracefully)(constants_1.OPTION_AUTHORIZATION_URL),
@@ -22,9 +23,14 @@ const setOAuthFromOptions = () => async () => {
22
23
  expires_in: expiresIn ? parseInt(expiresIn, 10) : undefined,
23
24
  });
24
25
  };
25
- const pre = () => async () => {
26
- const { info } = (0, logger_1.get)("commands.handler.authentication.oauth.secretsProvider.options");
26
+ const pre = async () => async () => {
27
+ const { info } = getLogger();
27
28
  info("reading secrets from options");
28
29
  };
29
- const create = () => (0, next_1.create)(pre, (0, or_1.create)((0, checkOptionsExistence_1.create)(constants_1.OPTION_ACCESS_TOKEN, false), (0, next_1.create)(createOptHandler(constants_1.OPTION_CLIENT_ID), createOptHandler(constants_1.OPTION_CLIENT_SECRET), createOptHandler(constants_1.OPTION_AUTHORIZATION_URL), createOptHandler(constants_1.OPTION_TOKEN_URL), (0, options_1.create)([constants_1.OPTION_REFRESH_TOKEN, constants_1.OPTION_EXPIRES_IN]))), setOAuthFromOptions);
30
+ const create = () => (0, next_1.create)(pre, (0, or_1.create)((0, checkOptionsExistence_1.create)(constants_1.OPTION_ACCESS_TOKEN, false), (0, next_1.create)(createOptHandler(constants_1.OPTION_CLIENT_ID), createOptHandler(constants_1.OPTION_CLIENT_SECRET), (0, options_1.create)([
31
+ constants_1.OPTION_AUTHORIZATION_URL,
32
+ constants_1.OPTION_TOKEN_URL,
33
+ constants_1.OPTION_REFRESH_TOKEN,
34
+ constants_1.OPTION_EXPIRES_IN,
35
+ ]))), setOAuthFromOptions);
30
36
  exports.create = create;
@@ -1,81 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.create = void 0;
4
- const fs_extra_1 = require("fs-extra");
5
- const http_1 = require("http");
6
4
  const next_1 = require("../../../next");
7
5
  const options_1 = require("../../../options");
8
6
  const constants_1 = require("../../../../../constants");
9
7
  const logger_1 = require("../../../../../logger");
10
8
  const utils_1 = require("../utils");
11
- const options_2 = require("../../../../../utils/options");
12
- const utils_2 = require("../../../../../utils/utils");
9
+ const utils_2 = require("./utils");
10
+ const SecretsStorageSingleton_1 = require("../../../../../cache/secrets/SecretsStorageSingleton");
13
11
  const getLogger = () => (0, logger_1.get)("commands.handler.authentication.oauth.tokenProvider.getToken");
14
- const retrieveCode = async () => new Promise((resolve, reject) => {
15
- const PORT = parseInt(process.env.CLI_HTTP_PORT || "8080", 10);
16
- const { debug } = getLogger();
17
- let timeout;
18
- const server = (0, http_1.createServer)(async (req, res) => {
19
- clearTimeout(timeout);
20
- const code = new URL(req.url ? `http://localhost:${PORT}${req.url}` : "http://no-code.damn").searchParams.get("code");
21
- let file;
22
- try {
23
- file = await (0, fs_extra_1.readFile)(code ? constants_1.PATH_TO_SUCCESS_HTML : constants_1.PATH_TO_ERROR_HTML, "utf8");
24
- }
25
- catch (err) {
26
- debug("failed to read file for code", code, err);
27
- file =
28
- "<html><body>Ops, something went wrong! Please try again.</body></html>";
29
- }
30
- res.writeHead(200, {
31
- "Content-Type": "text/html",
32
- "Content-Security-Policy": "frame-ancestors 'none'",
33
- "X-Frame-Options": "DENY",
34
- });
35
- res.end(file, "utf8");
36
- server.close();
37
- if (code) {
38
- resolve(code);
39
- }
40
- else {
41
- reject(new Error("no code found in callback URI"));
42
- }
43
- });
44
- timeout = setTimeout(() => {
45
- server.close();
46
- reject(new Error("Did not receive a code within 30 seconds"));
47
- }, 30 * 1000);
48
- server.listen(PORT);
49
- debug(`started http server at localhost:${PORT}`);
50
- });
51
- const getCode = async (authorizeUrl, clientId) => {
52
- const { debug } = getLogger();
53
- try {
54
- return (0, options_2.getOptionValueFromConfig)(constants_1.OPTION_CODE);
55
- }
56
- catch (err) {
57
- debug("failed to retrieve code from options", err);
58
- return (await Promise.all([
59
- retrieveCode(),
60
- (0, utils_2.openUrlInBrowser)(authorizeUrl, {
61
- response_type: "code",
62
- client_id: clientId,
63
- }),
64
- ]))[0];
65
- }
66
- };
67
- const handler = () => async () => {
12
+ const handler = async () => async () => {
68
13
  const { info: logInfo, debug } = getLogger();
69
14
  logInfo("checking token existence");
70
- const secrets = (0, utils_1.getSecrets)();
15
+ const secrets = await SecretsStorageSingleton_1.SecretsStorageSingleton.SINGLETON.getDefaultSecret();
71
16
  if (!secrets.access_token && !secrets.refresh_token) {
72
17
  debug("access token not available, retrieving token from server");
73
- const code = await getCode(secrets.authorization_url, secrets.client_id);
18
+ const code = await (0, utils_2.getCode)(secrets.authorization_url, secrets.client_id);
74
19
  await (0, utils_1.readToken)({
75
20
  code,
76
21
  grant_type: "authorization_code",
77
22
  });
78
- await (0, utils_1.storeSecrets)();
79
23
  }
80
24
  else if (secrets.access_token) {
81
25
  debug("token available");
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.create = void 0;
4
4
  const logger_1 = require("../../../../../logger");
5
5
  const utils_1 = require("./utils");
6
- const create = (forceRefresh = false) => () => async () => {
6
+ const create = (forceRefresh = false) => async () => async () => {
7
7
  try {
8
8
  await (0, utils_1.refreshToken)(forceRefresh);
9
9
  }
@@ -8,12 +8,12 @@ const options_1 = require("../../../../../utils/options");
8
8
  const utils_1 = require("../../../options/utils");
9
9
  const utils_2 = require("../../../utils");
10
10
  const utils_3 = require("../../utils");
11
- const utils_4 = require("../utils");
11
+ const SecretsStorageSingleton_1 = require("../../../../../cache/secrets/SecretsStorageSingleton");
12
12
  const updateAuthorization = async () => {
13
13
  const { info: logInfo, debug } = (0, logger_1.get)("commands.handler.authentication.oauth.tokenProvider.setAuthorization");
14
14
  logInfo("setting authorization using access token");
15
15
  const config = (0, config_1.get)();
16
- const secrets = (0, utils_4.getSecrets)();
16
+ const secrets = await SecretsStorageSingleton_1.SecretsStorageSingleton.SINGLETON.getDefaultSecret();
17
17
  const accessToken = secrets.access_token ||
18
18
  (0, options_1.getOptionValueFromConfigGracefully)(constants_1.OPTION_ACCESS_TOKEN);
19
19
  if (accessToken) {
@@ -28,8 +28,8 @@ const updateAuthorization = async () => {
28
28
  }
29
29
  };
30
30
  exports.updateAuthorization = updateAuthorization;
31
- const create = () => (command) => {
32
- (0, utils_1.checkOptions)(constants_1.OPTION_ACCESS_TOKEN, command);
31
+ const create = () => async (command) => {
32
+ await (0, utils_1.checkOptions)(constants_1.OPTION_ACCESS_TOKEN, command);
33
33
  return exports.updateAuthorization;
34
34
  };
35
35
  exports.create = create;
@@ -1 +1,3 @@
1
1
  export declare const refreshToken: (forceRefresh?: boolean) => Promise<void>;
2
+ export declare const retrieveCode: () => Promise<string>;
3
+ export declare const getCode: (authorizeUrl: string, clientId: string) => Promise<string>;
@@ -1,12 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.refreshToken = void 0;
3
+ exports.getCode = exports.retrieveCode = exports.refreshToken = void 0;
4
+ const http_1 = require("http");
5
+ const fs_extra_1 = require("fs-extra");
4
6
  const logger_1 = require("../../../../../logger");
5
7
  const utils_1 = require("../utils");
6
8
  const setAuthorization_1 = require("./setAuthorization");
9
+ const constants_1 = require("../../../../../constants");
10
+ const options_1 = require("../../../../../utils/options");
11
+ const utils_2 = require("../../../../../utils/utils");
12
+ const SecretsStorageSingleton_1 = require("../../../../../cache/secrets/SecretsStorageSingleton");
13
+ const getLogger = () => (0, logger_1.get)("commands.handler.authentication.oauth.tokenProvider.utils.refreshToken");
7
14
  const refreshToken = async (forceRefresh = false) => {
8
- const { info: logInfo, debug } = (0, logger_1.get)("commands.handler.authentication.oauth.tokenProvider.utils.refreshToken");
9
- const secrets = (0, utils_1.getSecrets)();
15
+ const { info: logInfo, debug } = getLogger();
16
+ const secrets = await SecretsStorageSingleton_1.SecretsStorageSingleton.SINGLETON.getDefaultSecret();
10
17
  if ((!forceRefresh && !secrets.expires_after) || !secrets.refresh_token) {
11
18
  throw new Error("invalid secrets information");
12
19
  }
@@ -17,7 +24,6 @@ const refreshToken = async (forceRefresh = false) => {
17
24
  refresh_token: secrets.refresh_token,
18
25
  grant_type: "refresh_token",
19
26
  });
20
- await (0, utils_1.storeSecrets)();
21
27
  await (0, setAuthorization_1.updateAuthorization)();
22
28
  }
23
29
  else {
@@ -25,3 +31,70 @@ const refreshToken = async (forceRefresh = false) => {
25
31
  }
26
32
  };
27
33
  exports.refreshToken = refreshToken;
34
+ const retrieveCode = async () =>
35
+ // eslint-disable-next-line no-async-promise-executor
36
+ new Promise(async (resolve, reject) => {
37
+ const { debug, error } = getLogger();
38
+ try {
39
+ const secrets = await SecretsStorageSingleton_1.SecretsStorageSingleton.SINGLETON.getDefaultSecret();
40
+ const defaultPort = secrets.customClient ? "8080" : "65000";
41
+ const PORT = parseInt(process.env.CLI_HTTP_PORT || defaultPort, 10);
42
+ let timeout;
43
+ const server = (0, http_1.createServer)(async (req, res) => {
44
+ clearTimeout(timeout);
45
+ const code = new URL(req.url
46
+ ? `http://localhost:${PORT}${req.url}`
47
+ : "http://no-code.damn").searchParams.get("code");
48
+ let file;
49
+ try {
50
+ file = await (0, fs_extra_1.readFile)(code ? constants_1.PATH_TO_SUCCESS_HTML : constants_1.PATH_TO_ERROR_HTML, "utf8");
51
+ }
52
+ catch (err) {
53
+ debug("failed to read file for code", code, err);
54
+ file =
55
+ "<html><body>Ops, something went wrong! Please try again.</body></html>";
56
+ }
57
+ res.writeHead(200, {
58
+ "Content-Type": "text/html",
59
+ "Content-Security-Policy": "frame-ancestors 'none'",
60
+ "X-Frame-Options": "DENY",
61
+ });
62
+ res.end(file, "utf8");
63
+ server.close();
64
+ if (code) {
65
+ resolve(code);
66
+ }
67
+ else {
68
+ reject(new Error("no code found in callback URI"));
69
+ }
70
+ });
71
+ timeout = setTimeout(() => {
72
+ server.close();
73
+ reject(new Error("Did not receive a code within 30 seconds"));
74
+ }, 30 * 1000);
75
+ server.listen(PORT);
76
+ debug(`started http server at localhost:${PORT}`);
77
+ }
78
+ catch (err) {
79
+ error("failed to instantiate server", err);
80
+ reject(new Error("failed to instantiate server", { cause: err }));
81
+ }
82
+ });
83
+ exports.retrieveCode = retrieveCode;
84
+ const getCode = async (authorizeUrl, clientId) => {
85
+ const { debug } = getLogger();
86
+ try {
87
+ return (0, options_1.getOptionValueFromConfig)(constants_1.OPTION_CODE);
88
+ }
89
+ catch (err) {
90
+ debug("failed to retrieve code from options", err);
91
+ return (await Promise.all([
92
+ (0, exports.retrieveCode)(),
93
+ (0, utils_2.openUrlInBrowser)(authorizeUrl, {
94
+ response_type: "code",
95
+ client_id: clientId,
96
+ }),
97
+ ]))[0];
98
+ }
99
+ };
100
+ exports.getCode = getCode;
@@ -1,4 +1,3 @@
1
- import { Secrets } from "../../../../types";
2
1
  type Data = {
3
2
  grant_type: "refresh_token";
4
3
  refresh_token: string;
@@ -6,9 +5,6 @@ type Data = {
6
5
  grant_type: "authorization_code";
7
6
  code: string;
8
7
  };
9
- export declare const setSecrets: (info: Partial<Secrets>) => void;
10
- export declare const getSecrets: () => Secrets;
11
- export declare const storeSecrets: () => Promise<void>;
12
8
  export declare const calculateExpiresAfter: (expires_in: number) => number;
13
9
  export declare const isExpired: (expires_after: number) => boolean;
14
10
  export declare const readToken: (data: Data) => Promise<void>;
@@ -3,37 +3,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.readToken = exports.isExpired = exports.calculateExpiresAfter = exports.storeSecrets = exports.getSecrets = exports.setSecrets = void 0;
6
+ exports.readToken = exports.isExpired = exports.calculateExpiresAfter = void 0;
7
7
  const qs_1 = __importDefault(require("qs"));
8
- const cache_1 = require("../../../../cache");
9
- const config_1 = require("../../../../config");
10
- const constants_1 = require("../../../../constants");
11
8
  const http_1 = require("../../../../utils/http");
12
- const setSecrets = (info) => {
13
- (0, config_1.set)({ oauth: info });
14
- };
15
- exports.setSecrets = setSecrets;
16
- const getSecrets = () => {
17
- const { oauth } = (0, config_1.get)();
18
- if (oauth) {
19
- return oauth;
20
- }
21
- throw new Error("no secrets exist");
22
- };
23
- exports.getSecrets = getSecrets;
24
- const storeSecrets = async () => {
25
- await (0, cache_1.writeFile)(constants_1.CACHE_SECRETS_FILE, JSON.stringify((0, exports.getSecrets)()));
26
- };
27
- exports.storeSecrets = storeSecrets;
9
+ const SecretsStorageSingleton_1 = require("../../../../cache/secrets/SecretsStorageSingleton");
28
10
  const calculateExpiresAfter = (expires_in) => Math.floor(Date.now() / 1000) + expires_in - 10;
29
11
  exports.calculateExpiresAfter = calculateExpiresAfter;
30
12
  const isExpired = (expires_after) => expires_after <= (0, exports.calculateExpiresAfter)(10);
31
13
  exports.isExpired = isExpired;
32
14
  const readToken = async (data) => {
33
- const secrets = (0, exports.getSecrets)();
15
+ const secrets = await SecretsStorageSingleton_1.SecretsStorageSingleton.SINGLETON.getDefaultSecret();
34
16
  if (!secrets.token_url || !secrets.client_id || !secrets.client_secret) {
35
17
  throw new Error("invalid secrets information");
36
18
  }
19
+ const body = {
20
+ ...data,
21
+ response_type: "token",
22
+ };
23
+ if (!secrets.customClient) {
24
+ body.client_id = secrets.client_id;
25
+ }
37
26
  const info = (await (0, http_1.fetch)({
38
27
  method: "POST",
39
28
  url: secrets.token_url,
@@ -42,15 +31,12 @@ const readToken = async (data) => {
42
31
  password: secrets.client_secret,
43
32
  },
44
33
  headers: {
45
- "x-sap-sac-custom-auth": true,
34
+ "x-sap-sac-custom-auth": !!secrets.customClient,
46
35
  "content-type": "application/x-www-form-urlencoded",
47
36
  },
48
- data: qs_1.default.stringify({
49
- ...data,
50
- response_type: "token",
51
- }),
37
+ data: qs_1.default.stringify(body),
52
38
  })).data;
53
- (0, exports.setSecrets)({
39
+ await SecretsStorageSingleton_1.SecretsStorageSingleton.SINGLETON.storeSecret({
54
40
  ...info,
55
41
  expires_after: (0, exports.calculateExpiresAfter)(info.expires_in),
56
42
  });
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.create = void 0;
4
4
  const config_1 = require("../../../../config");
5
5
  const constants_1 = require("../../../../constants");
6
- const create = () => () => async () => {
6
+ const create = () => async () => async () => {
7
7
  const config = (0, config_1.get)();
8
8
  /* jscpd:ignore-end */
9
9
  if (!config[constants_1.CONFIG_PASSCODE_FUNCTION] ||
@@ -9,8 +9,8 @@ const prompts_1 = __importDefault(require("prompts"));
9
9
  const config_1 = require("../../../../config");
10
10
  const constants_1 = require("../../../../constants");
11
11
  const utils_1 = require("../../options/utils");
12
- const create = () => (command) => {
13
- (0, utils_1.checkOptions)(constants_1.OPTION_PASSCODE, command);
12
+ const create = () => async (command) => {
13
+ await (0, utils_1.checkOptions)(constants_1.OPTION_PASSCODE, command);
14
14
  return async () => {
15
15
  const config = (0, config_1.get)();
16
16
  let passcode = config.options[constants_1.OPTION_PASSCODE.longName];
@@ -6,7 +6,7 @@ const utils_1 = require("../utils");
6
6
  const utils_2 = require("../../utils");
7
7
  /* jscpd:ignore-start */
8
8
  const constants_1 = require("../../../../constants");
9
- const create = () => () => async () => {
9
+ const create = () => async () => async () => {
10
10
  const config = (0, config_1.get)();
11
11
  /* jscpd:ignore-end */
12
12
  (0, utils_2.setTargetHost)(config.host);
@@ -5,8 +5,8 @@ const config_1 = require("../../../../config");
5
5
  const commands_1 = require("../../../../utils/commands");
6
6
  const types_1 = require("./types");
7
7
  const utils_1 = require("./utils");
8
- const create = () => (command) => {
9
- command.addOption((0, commands_1.buildOption)(types_1.OPTION_SECRET));
8
+ const create = () => async (command) => {
9
+ command.addOption(await (0, commands_1.buildOption)(types_1.OPTION_SECRET));
10
10
  return async () => {
11
11
  const token = await (0, utils_1.getTechnicalJwt)();
12
12
  (0, config_1.set)({ authorization: { Authorization: `Bearer ${token}` } });
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.create = void 0;
4
4
  const config_1 = require("../../config");
5
5
  const logger_1 = require("../../logger");
6
- const create = (option, throwIfExists = true) => () => async () => {
6
+ const create = (option, throwIfExists = true) => async () => async () => {
7
7
  const config = (0, config_1.get)();
8
8
  const { trace } = (0, logger_1.get)("commands.handler.checkOptionsExistence");
9
9
  trace(`checking options existence ${option.longName} in config, value in config is ${config.options[option.longName]}, throw error if option exists ${throwIfExists}`);
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.create = void 0;
4
4
  const logger_1 = require("../../logger");
5
5
  const stackTrace_1 = require("./stackTrace");
6
- const errorHandler = (errorMessage, handler) => (command) => {
7
- const commandHandler = handler(command);
6
+ const errorHandler = (errorMessage, handler) => async (command) => {
7
+ const commandHandler = await handler(command);
8
8
  return async (...args) => {
9
9
  try {
10
10
  await commandHandler(...args);
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.create = void 0;
4
4
  const logger_1 = require("../../logger");
5
- const create = () => () => async () => {
5
+ const create = () => async () => async () => {
6
6
  const { debug } = (0, logger_1.get)("commands.handler.fail");
7
7
  debug(`running fail handler`);
8
8
  throw new Error("Fail I must");
@@ -78,6 +78,6 @@ const runRequest = (method, path, parameterMappings, responsePostProcessor) => a
78
78
  /* jscpd:ignore-start */
79
79
  const create = (method, path, parameterMappings, responsePostProcessor) => {
80
80
  /* jscpd:ignore-end */
81
- return () => runRequest(method, path, parameterMappings, responsePostProcessor);
81
+ return async () => runRequest(method, path, parameterMappings, responsePostProcessor);
82
82
  };
83
83
  exports.create = create;
@@ -109,7 +109,7 @@ const handleResponseData = async (data) => {
109
109
  ResultHandlerFactory_1.ResultHandlerFactory.get().setResult(data);
110
110
  }
111
111
  const { output } = (0, logger_1.get)("handler.fetch");
112
- const formatted = config.options[constants_1.OPTION_PRETTY.longName]
112
+ const formatted = config.options.pretty
113
113
  ? JSON.stringify(data, null, 2)
114
114
  : JSON.stringify(data);
115
115
  if (config.options[constants_1.OPTION_OUTPUT.longName]) {
@@ -11,7 +11,7 @@ const constants_1 = require("../../../constants");
11
11
  const checkOptionsExistence_1 = require("../checkOptionsExistence");
12
12
  const next_1 = require("../next");
13
13
  const options_1 = require("../options");
14
- const readBodyFromFile = () => async () => {
14
+ const readBodyFromFile = async () => async () => {
15
15
  const config = (0, config_1.get)();
16
16
  const filePath = config.options[constants_1.OPTION_FILE_PATH.longName];
17
17
  const { debug, trace } = (0, logger_1.get)("commands.handler.input.file");
@@ -9,8 +9,8 @@ const config_1 = require("../../../config");
9
9
  const constants_1 = require("../../../constants");
10
10
  const logger_1 = require("../../../logger");
11
11
  const commands_1 = require("../../../utils/commands");
12
- const create = () => (command) => {
13
- command.addOption((0, commands_1.buildOption)(constants_1.OPTION_INPUT));
12
+ const create = () => async (command) => {
13
+ command.addOption(await (0, commands_1.buildOption)(constants_1.OPTION_INPUT));
14
14
  return async () => {
15
15
  const config = (0, config_1.get)();
16
16
  const { debug } = (0, logger_1.get)("handler.input.input");
@@ -11,7 +11,7 @@ const MANDATORY_OPTIONS = [
11
11
  constants_1.OPTION_OPTIONS_FILE,
12
12
  { ...constants_1.OPTION_HOST, required: true },
13
13
  ];
14
- const post = () => async () => {
14
+ const post = async () => async () => {
15
15
  const config = (0, config_1.get)();
16
16
  const tenant = (0, utils_1.parseTenant)(config.options[constants_1.OPTION_HOST.longName]);
17
17
  const info = (0, utils_1.getInfoFromTenant)(tenant, config.verbose);
@@ -2,10 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.create = void 0;
4
4
  const stackTrace_1 = require("./stackTrace");
5
- const nextHandler = (...handlers) => (command) => {
5
+ const nextHandler = (...handlers) => async (command) => {
6
6
  const commandHandlers = [];
7
7
  for (const handler of handlers) {
8
- commandHandlers.push(handler(command));
8
+ // eslint-disable-next-line no-await-in-loop
9
+ commandHandlers.push(await handler(command));
9
10
  }
10
11
  return async (...args) => {
11
12
  for (const handler of commandHandlers) {
@@ -5,14 +5,13 @@ const logger_1 = require("../../../logger");
5
5
  const utils_1 = require("../../../utils/utils");
6
6
  const utils_2 = require("./utils");
7
7
  /* jscpd:ignore-start */
8
- const create = (option) => () => async () => {
8
+ const create = (option) => async () => async () => {
9
9
  const { debug } = (0, logger_1.get)("commands.handler.options.env");
10
10
  /* jscpd:ignore-end */
11
11
  debug(`reading option ${option.longName} from env`);
12
12
  if (!process.env[(0, utils_1.toConstantCase)(option.longName)]) {
13
13
  throw new Error(`env variable process.env.${(0, utils_1.toConstantCase)(option.longName)} is not set`);
14
14
  }
15
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
16
15
  (0, utils_2.setOption)(option, process.env[(0, utils_1.toConstantCase)(option.longName)]);
17
16
  };
18
17
  exports.create = create;
@@ -6,7 +6,7 @@ const logger_1 = require("../../../logger");
6
6
  const options_1 = require("../../../utils/options");
7
7
  const utils_1 = require("./utils");
8
8
  /* jscpd:ignore-start */
9
- const create = (option) => () => async () => {
9
+ const create = (option) => async () => async () => {
10
10
  const { debug, trace } = (0, logger_1.get)("commands.handler.options.file");
11
11
  /* jscpd:ignore-end */
12
12
  debug(`reading option ${option.longName} from file`);