@sasjs/cli 4.18.6 → 4.19.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.
@@ -200,27 +200,6 @@ describe('AddCommand - cred', function () {
200
200
  expect(command.subCommand).toEqual('cred');
201
201
  expect(command.insecure).toEqual(true);
202
202
  });
203
- it('should parse a sasjs add cred command invoked with the alias sasjs auth', function () {
204
- var args = __spreadArray(__spreadArray([], __read(defaultArgs), false), ['auth', '-i'], false);
205
- var command = new addCommand_1.AddCommand(args);
206
- expect(command.name).toEqual('add');
207
- expect(command.insecure).toEqual(true);
208
- });
209
- it('should call the addCredential handler when executed with the alias sasjs auth', function () { return __awaiter(void 0, void 0, void 0, function () {
210
- var args, command;
211
- return __generator(this, function (_a) {
212
- switch (_a.label) {
213
- case 0:
214
- args = __spreadArray(__spreadArray([], __read(defaultArgs), false), ['auth', '-t', 'test'], false);
215
- command = new addCommand_1.AddCommand(args);
216
- return [4 /*yield*/, command.execute()];
217
- case 1:
218
- _a.sent();
219
- expect(addCredentialModule.addCredential).toHaveBeenCalledWith(target, false, types_1.TargetScope.Local);
220
- return [2 /*return*/];
221
- }
222
- });
223
- }); });
224
203
  it('should call the addCredential handler when executed without the insecure option', function () { return __awaiter(void 0, void 0, void 0, function () {
225
204
  var args, command;
226
205
  return __generator(this, function (_a) {
@@ -0,0 +1,9 @@
1
+ import { ReturnCode } from '../../types/command';
2
+ import { TargetCommand } from '../../types/command/targetCommand';
3
+ export declare class AuthCommand extends TargetCommand {
4
+ constructor(args: string[]);
5
+ get insecure(): boolean;
6
+ execute(): Promise<ReturnCode.Success | ReturnCode.InternalError>;
7
+ executeLogin(): Promise<ReturnCode.Success | ReturnCode.InternalError>;
8
+ executeCred(): Promise<ReturnCode.Success | ReturnCode.InternalError>;
9
+ }
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ Object.defineProperty(exports, "__esModule", { value: true });
54
+ exports.AuthCommand = void 0;
55
+ var types_1 = require("../../types");
56
+ var command_1 = require("../../types/command");
57
+ var targetCommand_1 = require("../../types/command/targetCommand");
58
+ var addCredential_1 = require("../add/addCredential");
59
+ var login_1 = require("./login");
60
+ var AuthSubCommand;
61
+ (function (AuthSubCommand) {
62
+ AuthSubCommand["Login"] = "login";
63
+ })(AuthSubCommand || (AuthSubCommand = {}));
64
+ // The syntax here uses square brackets for the subCommand (i.e. makes it
65
+ // optional) since `sasjs auth` without a subCommand retains its historical
66
+ // behaviour (it used to be an alias of `sasjs add cred`).
67
+ var syntax = 'auth [subCommand] [options]';
68
+ var usage = 'sasjs auth login --target <target-name> | sasjs auth [options]';
69
+ var description = "Authenticates against the specified target.\n" +
70
+ "With the 'login' subCommand, authentication is performed with a SAS username and password " +
71
+ "(no OAuth client/secret required, SASVIYA targets only) and the resulting tokens are saved.\n" +
72
+ "Without a subCommand, behaves like 'sasjs add cred' (client/secret based authentication).";
73
+ var examples = [
74
+ {
75
+ command: 'sasjs auth login --target <target-name>',
76
+ description: 'Logs in with SAS username/password (no client/secret required).'
77
+ },
78
+ {
79
+ command: 'sasjs auth login -t <target-name>',
80
+ description: ''
81
+ },
82
+ {
83
+ command: 'sasjs auth --target <target-name>',
84
+ description: 'Legacy behaviour, equivalent to `sasjs add cred`.'
85
+ }
86
+ ];
87
+ var AuthCommand = /** @class */ (function (_super) {
88
+ __extends(AuthCommand, _super);
89
+ function AuthCommand(args) {
90
+ var parseOptions = {
91
+ insecure: {
92
+ type: 'boolean',
93
+ alias: 'i',
94
+ default: false,
95
+ description: 'Allows the command to bypass the HTTPs requirement. Not recommended.'
96
+ }
97
+ };
98
+ return _super.call(this, args, { parseOptions: parseOptions, usage: usage, description: description, examples: examples, syntax: syntax }) || this;
99
+ }
100
+ Object.defineProperty(AuthCommand.prototype, "insecure", {
101
+ get: function () {
102
+ return !!this.parsed.insecure;
103
+ },
104
+ enumerable: false,
105
+ configurable: true
106
+ });
107
+ AuthCommand.prototype.execute = function () {
108
+ return __awaiter(this, void 0, void 0, function () {
109
+ var _a;
110
+ return __generator(this, function (_b) {
111
+ switch (_b.label) {
112
+ case 0:
113
+ if (!(this.subCommand === AuthSubCommand.Login)) return [3 /*break*/, 2];
114
+ return [4 /*yield*/, this.executeLogin()];
115
+ case 1:
116
+ _a = _b.sent();
117
+ return [3 /*break*/, 4];
118
+ case 2: return [4 /*yield*/, this.executeCred()];
119
+ case 3:
120
+ _a = _b.sent();
121
+ _b.label = 4;
122
+ case 4: return [2 /*return*/, _a];
123
+ }
124
+ });
125
+ });
126
+ };
127
+ AuthCommand.prototype.executeLogin = function () {
128
+ var _a;
129
+ return __awaiter(this, void 0, void 0, function () {
130
+ var target, err_1;
131
+ return __generator(this, function (_b) {
132
+ switch (_b.label) {
133
+ case 0: return [4 /*yield*/, this.getTargetInfo()];
134
+ case 1:
135
+ target = (_b.sent()).target;
136
+ _b.label = 2;
137
+ case 2:
138
+ _b.trys.push([2, 4, , 5]);
139
+ return [4 /*yield*/, (0, login_1.authLogin)(target, this.insecure)];
140
+ case 3:
141
+ _b.sent();
142
+ return [2 /*return*/, command_1.ReturnCode.Success];
143
+ case 4:
144
+ err_1 = _b.sent();
145
+ (_a = process.logger) === null || _a === void 0 ? void 0 : _a.error('Error logging in.', (err_1 === null || err_1 === void 0 ? void 0 : err_1.message) || err_1);
146
+ return [2 /*return*/, command_1.ReturnCode.InternalError];
147
+ case 5: return [2 /*return*/];
148
+ }
149
+ });
150
+ });
151
+ };
152
+ AuthCommand.prototype.executeCred = function () {
153
+ var _a, _b;
154
+ return __awaiter(this, void 0, void 0, function () {
155
+ var _c, target, isLocal, scope, err_2;
156
+ return __generator(this, function (_d) {
157
+ switch (_d.label) {
158
+ case 0: return [4 /*yield*/, this.getTargetInfo()];
159
+ case 1:
160
+ _c = _d.sent(), target = _c.target, isLocal = _c.isLocal;
161
+ scope = isLocal ? types_1.TargetScope.Local : types_1.TargetScope.Global;
162
+ _d.label = 2;
163
+ case 2:
164
+ _d.trys.push([2, 4, , 5]);
165
+ return [4 /*yield*/, (0, addCredential_1.addCredential)(target, this.insecure, scope)];
166
+ case 3:
167
+ _d.sent();
168
+ (_a = process.logger) === null || _a === void 0 ? void 0 : _a.success('Credentials successfully added!');
169
+ return [2 /*return*/, command_1.ReturnCode.Success];
170
+ case 4:
171
+ err_2 = _d.sent();
172
+ (_b = process.logger) === null || _b === void 0 ? void 0 : _b.error('Error adding credentials.', err_2.toString());
173
+ return [2 /*return*/, command_1.ReturnCode.InternalError];
174
+ case 5: return [2 /*return*/];
175
+ }
176
+ });
177
+ });
178
+ };
179
+ return AuthCommand;
180
+ }(targetCommand_1.TargetCommand));
181
+ exports.AuthCommand = AuthCommand;
@@ -0,0 +1,16 @@
1
+ import { Target } from '@sasjs/utils';
2
+ /**
3
+ * Authenticates against a SASVIYA target using a SAS username and password
4
+ * (OAuth2 resource owner password grant against the built-in, secret-less
5
+ * `sas.cli` client) and persists the resulting token pair. This enables all
6
+ * authenticated commands (`sasjs run`, `sasjs deploy`, etc.) on estates where
7
+ * no administrator-registered OAuth client/secret is available.
8
+ *
9
+ * The password is used only to mint the tokens and is never stored. When the
10
+ * access token expires, re-run `sasjs auth login -t <target>`.
11
+ * @param {Target} target - the target to authenticate against.
12
+ * @param {boolean} insecure - when true, bypasses TLS certificate validation
13
+ * (for self-signed cert Viya servers). Mirrors the `--insecure` flag on
14
+ * `sasjs add cred`.
15
+ */
16
+ export declare const authLogin: (target: Target, insecure?: boolean) => Promise<void>;
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ exports.authLogin = void 0;
54
+ var prompts_1 = __importDefault(require("prompts"));
55
+ var utils_1 = require("@sasjs/utils");
56
+ var utils_2 = require("../../utils");
57
+ /**
58
+ * Authenticates against a SASVIYA target using a SAS username and password
59
+ * (OAuth2 resource owner password grant against the built-in, secret-less
60
+ * `sas.cli` client) and persists the resulting token pair. This enables all
61
+ * authenticated commands (`sasjs run`, `sasjs deploy`, etc.) on estates where
62
+ * no administrator-registered OAuth client/secret is available.
63
+ *
64
+ * The password is used only to mint the tokens and is never stored. When the
65
+ * access token expires, re-run `sasjs auth login -t <target>`.
66
+ * @param {Target} target - the target to authenticate against.
67
+ * @param {boolean} insecure - when true, bypasses TLS certificate validation
68
+ * (for self-signed cert Viya servers). Mirrors the `--insecure` flag on
69
+ * `sasjs add cred`.
70
+ */
71
+ var authLogin = function (target, insecure) {
72
+ if (insecure === void 0) { insecure = false; }
73
+ return __awaiter(void 0, void 0, void 0, function () {
74
+ var user, pass, _a, access_token, refresh_token, _b, id, name;
75
+ var _c, _d, _e;
76
+ return __generator(this, function (_f) {
77
+ switch (_f.label) {
78
+ case 0:
79
+ if (target.serverType !== utils_1.ServerType.SasViya) {
80
+ throw new Error("'sasjs auth login' is only supported for SASVIYA targets. " +
81
+ "Target '".concat(target.name, "' is of type '").concat(target.serverType, "'. ") +
82
+ "Use 'sasjs add cred -t ".concat(target.name, "' instead."));
83
+ }
84
+ if (!target.serverUrl) {
85
+ throw new Error("Target '".concat(target.name, "' does not have a serverUrl configured."));
86
+ }
87
+ if (insecure) {
88
+ target = new utils_1.Target(__assign(__assign({}, target.toJson(false)), { httpsAgentOptions: __assign(__assign({}, target.httpsAgentOptions), { allowInsecureRequests: true, rejectUnauthorized: false }) }));
89
+ (_c = process.logger) === null || _c === void 0 ? void 0 : _c.warn('Executing with insecure connection.');
90
+ }
91
+ return [4 /*yield*/, (0, utils_1.getString)('Please enter your SAS username', function (v) { return !!v || 'Username is required.'; })
92
+ // Password is prompted with a masked input (prompts 'password' type) rather
93
+ // than getString from @sasjs/utils, because getString has no mask/hide mode
94
+ // and would echo the password to the terminal.
95
+ // The password stays in memory for the remainder of the process. This is an
96
+ // accepted trade-off for a short-lived CLI: the process exits within seconds
97
+ // and Node's V8 heap is not accessible to other users. Clearing the variable
98
+ // would not reliably zero the underlying V8 string storage anyway.
99
+ ];
100
+ case 1:
101
+ user = _f.sent();
102
+ return [4 /*yield*/, (0, prompts_1.default)({
103
+ type: 'password',
104
+ name: 'pass',
105
+ message: 'Please enter your SAS password'
106
+ }, {
107
+ onCancel: function () {
108
+ throw new Error('Input cancelled.');
109
+ }
110
+ })];
111
+ case 2:
112
+ pass = (_f.sent()).pass;
113
+ return [4 /*yield*/, (0, utils_2.getTokensWithPasswordGrant)(target, user, pass)];
114
+ case 3:
115
+ _a = _f.sent(), access_token = _a.access_token, refresh_token = _a.refresh_token;
116
+ return [4 /*yield*/, (0, utils_2.fetchLoggedInUser)(target, access_token)];
117
+ case 4:
118
+ _b = _f.sent(), id = _b.id, name = _b.name;
119
+ return [4 /*yield*/, (0, utils_2.saveTokens)(target.name, access_token, refresh_token || '')];
120
+ case 5:
121
+ _f.sent();
122
+ (_d = process.logger) === null || _d === void 0 ? void 0 : _d.success("Logged in as ".concat(id || 'unknown user').concat(name ? " (".concat(name, ")") : '', " on ").concat(target.serverUrl, "."));
123
+ (_e = process.logger) === null || _e === void 0 ? void 0 : _e.info("The access token is valid for the duration configured on the server (12 hours by default). " +
124
+ "When it expires, re-run 'sasjs auth login -t ".concat(target.name, "'."));
125
+ return [2 /*return*/];
126
+ }
127
+ });
128
+ });
129
+ };
130
+ exports.authLogin = authLogin;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,207 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __generator = (this && this.__generator) || function (thisArg, body) {
35
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
36
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
37
+ function verb(n) { return function (v) { return step([n, v]); }; }
38
+ function step(op) {
39
+ if (f) throw new TypeError("Generator is already executing.");
40
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
41
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
42
+ if (y = 0, t) op = [op[0] & 2, t.value];
43
+ switch (op[0]) {
44
+ case 0: case 1: t = op; break;
45
+ case 4: _.label++; return { value: op[1], done: false };
46
+ case 5: _.label++; y = op[1]; op = [0]; continue;
47
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
48
+ default:
49
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
50
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
51
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
52
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
53
+ if (t[2]) _.ops.pop();
54
+ _.trys.pop(); continue;
55
+ }
56
+ op = body.call(thisArg, _);
57
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
58
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
59
+ }
60
+ };
61
+ var __read = (this && this.__read) || function (o, n) {
62
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
63
+ if (!m) return o;
64
+ var i = m.call(o), r, ar = [], e;
65
+ try {
66
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
67
+ }
68
+ catch (error) { e = { error: error }; }
69
+ finally {
70
+ try {
71
+ if (r && !r.done && (m = i["return"])) m.call(i);
72
+ }
73
+ finally { if (e) throw e.error; }
74
+ }
75
+ return ar;
76
+ };
77
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
78
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
79
+ if (ar || !(i in from)) {
80
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
81
+ ar[i] = from[i];
82
+ }
83
+ }
84
+ return to.concat(ar || Array.prototype.slice.call(from));
85
+ };
86
+ Object.defineProperty(exports, "__esModule", { value: true });
87
+ var authCommand_1 = require("../authCommand");
88
+ var addCredentialModule = __importStar(require("../../add/addCredential"));
89
+ var authLoginModule = __importStar(require("../login"));
90
+ var configUtils = __importStar(require("../../../utils/config"));
91
+ var setConstantsUtils = __importStar(require("../../../utils/setConstants"));
92
+ var utils_1 = require("@sasjs/utils");
93
+ var types_1 = require("../../../types");
94
+ var command_1 = require("../../../types/command");
95
+ describe('AuthCommand', function () {
96
+ var defaultArgs = ['node', 'sasjs'];
97
+ var target = new utils_1.Target({
98
+ name: 'test',
99
+ appLoc: '/Public/test/',
100
+ serverType: utils_1.ServerType.SasViya,
101
+ contextName: 'test context'
102
+ });
103
+ beforeEach(function () {
104
+ process.logger = new utils_1.Logger(utils_1.LogLevel.Off);
105
+ jest.resetAllMocks();
106
+ jest.mock('../../add/addCredential');
107
+ jest.mock('../../../utils/config');
108
+ jest
109
+ .spyOn(addCredentialModule, 'addCredential')
110
+ .mockImplementation(function () { return Promise.resolve(target); });
111
+ jest
112
+ .spyOn(configUtils, 'findTargetInConfiguration')
113
+ .mockImplementation(function () { return Promise.resolve({ target: target, isLocal: true }); });
114
+ jest
115
+ .spyOn(configUtils, 'getLocalConfig')
116
+ .mockImplementation(function () { return Promise.resolve({}); });
117
+ jest
118
+ .spyOn(setConstantsUtils, 'setConstants')
119
+ .mockImplementation(function () { return Promise.resolve(); });
120
+ jest.spyOn(process.logger, 'success');
121
+ jest.spyOn(process.logger, 'error');
122
+ });
123
+ it('should parse a sasjs auth login command', function () {
124
+ var args = __spreadArray(__spreadArray([], __read(defaultArgs), false), ['auth', 'login', '-t', 'test'], false);
125
+ var command = new authCommand_1.AuthCommand(args);
126
+ expect(command.name).toEqual('auth');
127
+ expect(command.subCommand).toEqual('login');
128
+ });
129
+ it('should call the authLogin handler when executed with the login sub command', function () { return __awaiter(void 0, void 0, void 0, function () {
130
+ var args, command, returnCode;
131
+ return __generator(this, function (_a) {
132
+ switch (_a.label) {
133
+ case 0:
134
+ jest
135
+ .spyOn(authLoginModule, 'authLogin')
136
+ .mockImplementation(function () { return Promise.resolve(); });
137
+ args = __spreadArray(__spreadArray([], __read(defaultArgs), false), ['auth', 'login', '-t', 'test'], false);
138
+ command = new authCommand_1.AuthCommand(args);
139
+ return [4 /*yield*/, command.execute()];
140
+ case 1:
141
+ returnCode = _a.sent();
142
+ expect(authLoginModule.authLogin).toHaveBeenCalledWith(target, false);
143
+ expect(returnCode).toEqual(command_1.ReturnCode.Success);
144
+ return [2 /*return*/];
145
+ }
146
+ });
147
+ }); });
148
+ it('should pass the insecure flag to authLogin when --insecure is provided', function () { return __awaiter(void 0, void 0, void 0, function () {
149
+ var args, command, returnCode;
150
+ return __generator(this, function (_a) {
151
+ switch (_a.label) {
152
+ case 0:
153
+ jest
154
+ .spyOn(authLoginModule, 'authLogin')
155
+ .mockImplementation(function () { return Promise.resolve(); });
156
+ args = __spreadArray(__spreadArray([], __read(defaultArgs), false), ['auth', 'login', '-t', 'test', '--insecure'], false);
157
+ command = new authCommand_1.AuthCommand(args);
158
+ return [4 /*yield*/, command.execute()];
159
+ case 1:
160
+ returnCode = _a.sent();
161
+ expect(authLoginModule.authLogin).toHaveBeenCalledWith(target, true);
162
+ expect(returnCode).toEqual(command_1.ReturnCode.Success);
163
+ return [2 /*return*/];
164
+ }
165
+ });
166
+ }); });
167
+ it('should return an error code when login fails', function () { return __awaiter(void 0, void 0, void 0, function () {
168
+ var args, command, returnCode;
169
+ return __generator(this, function (_a) {
170
+ switch (_a.label) {
171
+ case 0:
172
+ jest
173
+ .spyOn(authLoginModule, 'authLogin')
174
+ .mockImplementation(function () { return Promise.reject(new Error('Login failed')); });
175
+ args = __spreadArray(__spreadArray([], __read(defaultArgs), false), ['auth', 'login', '-t', 'test'], false);
176
+ command = new authCommand_1.AuthCommand(args);
177
+ return [4 /*yield*/, command.execute()];
178
+ case 1:
179
+ returnCode = _a.sent();
180
+ expect(returnCode).toEqual(command_1.ReturnCode.InternalError);
181
+ expect(process.logger.error).toHaveBeenCalled();
182
+ return [2 /*return*/];
183
+ }
184
+ });
185
+ }); });
186
+ it('should parse a bare sasjs auth command (legacy alias of sasjs add cred)', function () {
187
+ var args = __spreadArray(__spreadArray([], __read(defaultArgs), false), ['auth', '-i'], false);
188
+ var command = new authCommand_1.AuthCommand(args);
189
+ expect(command.name).toEqual('auth');
190
+ expect(command.insecure).toEqual(true);
191
+ });
192
+ it('should call the addCredential handler when executed without a sub command', function () { return __awaiter(void 0, void 0, void 0, function () {
193
+ var args, command;
194
+ return __generator(this, function (_a) {
195
+ switch (_a.label) {
196
+ case 0:
197
+ args = __spreadArray(__spreadArray([], __read(defaultArgs), false), ['auth', '-t', 'test'], false);
198
+ command = new authCommand_1.AuthCommand(args);
199
+ return [4 /*yield*/, command.execute()];
200
+ case 1:
201
+ _a.sent();
202
+ expect(addCredentialModule.addCredential).toHaveBeenCalledWith(target, false, types_1.TargetScope.Local);
203
+ return [2 /*return*/];
204
+ }
205
+ });
206
+ }); });
207
+ });
@@ -0,0 +1 @@
1
+ export {};