@intuned/runtime-dev 1.0.6-cli-auth.0.0.18-test → 1.0.6-cli-auth.0.0.20-test

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.
@@ -9,9 +9,9 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
9
9
  _dotenv.default.config({
10
10
  path: `.env`
11
11
  });
12
- _commander.program.description("Check an auth session, if it is still valid or not").option("-a, --auth-session <session>", "Name/id of the auth session instance to use").action(async options => {
12
+ _commander.program.description("Check an auth session, if it is still valid or not").argument("<auth-session>", "Name/id of the auth session instance to use").action(async authSession => {
13
13
  try {
14
- if (!options.authSession) {
14
+ if (!authSession) {
15
15
  throw new Error("Auth session instance is required, provide an ID/name for it");
16
16
  }
17
17
  const _isAuthEnabled = await (0, _utils.isAuthEnabled)();
@@ -24,7 +24,7 @@ _commander.program.description("Check an auth session, if it is still valid or n
24
24
  }
25
25
  const {
26
26
  authSessionInstanceStoragePath
27
- } = await (0, _utils.retrieveAuthSessionInstance)(options.authSession, true);
27
+ } = await (0, _utils.retrieveAuthSessionInstance)(authSession, true);
28
28
  const checkResult = await (0, _utils.runCheckApi)(authSessionInstanceStoragePath);
29
29
  if (checkResult) {
30
30
  console.log(_chalk.default.green("✓ Auth session checked successfully"));
@@ -10,23 +10,36 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
10
10
  _dotenv.default.config({
11
11
  path: `.env`
12
12
  });
13
- _commander.program.description("Create an auth session").option("-i, --input <input>", "Auth session input parameters file").option("-n, --auth-session-name <output>", "Custom name for the auth session instance stored").action(async options => {
13
+ _commander.program.description("Create an auth session").argument("[auth-session-name]", "Name/id of the auth session instance to use (optional)").option("-i, --input <input>", "Auth session input parameters file").action(async (authSessionName, options) => {
14
14
  try {
15
15
  const _isAuthEnabled = await (0, _utils.isAuthEnabled)();
16
16
  if (!_isAuthEnabled) {
17
17
  throw new Error("Auth session is not enabled, enable it in Intuned.json to be able to use it");
18
18
  }
19
- const createApiExists = await (0, _utils.ensureAuthApi)("create");
20
- if (!createApiExists) {
21
- throw new Error("Auth session creation API not implemented, please create it in the auth sessions specified directory");
19
+ const authType = await (0, _utils.getAuthType)();
20
+ let authSessionInstancePath;
21
+ if (authType === "MANUAL") {
22
+ const recorderConfig = await (0, _utils.ensureRecorderURLs)();
23
+ const {
24
+ startUrl,
25
+ endUrl
26
+ } = recorderConfig;
27
+ console.log(_chalk.default.blue("Starting auth session recorder..."));
28
+ const session = await (0, _utils.recordAuthSession)(startUrl, endUrl);
29
+ authSessionInstancePath = await (0, _utils.storeAuthSessionInstance)(session, authSessionName, {});
30
+ } else {
31
+ const createApiExists = await (0, _utils.ensureAuthApi)("create");
32
+ if (!createApiExists) {
33
+ throw new Error("Auth session creation API not implemented, please create it in the auth sessions specified directory");
34
+ }
35
+ const authSessionInput = (await (0, _utils2.loadParameters)(options === null || options === void 0 ? void 0 : options.input)) ?? {};
36
+ const session = await (0, _utils.runCreateApi)(authSessionInput);
37
+ if (!session) {
38
+ console.error(_chalk.default.red("Failed to create auth session."));
39
+ process.exit(1);
40
+ }
41
+ authSessionInstancePath = await (0, _utils.storeAuthSessionInstance)(session, authSessionName, authSessionInput);
22
42
  }
23
- const authSessionInput = (await (0, _utils2.loadParameters)(options.input)) ?? {};
24
- const session = await (0, _utils.runCreateApi)(authSessionInput);
25
- if (!session) {
26
- console.error(_chalk.default.red("Failed to create auth session."));
27
- process.exit(1);
28
- }
29
- const authSessionInstancePath = await (0, _utils.storeAuthSessionInstance)(session, options.authSessionName, authSessionInput);
30
43
  console.log(_chalk.default.green("✓ Auth session created successfully!"));
31
44
  if (authSessionInstancePath) {
32
45
  console.log(_chalk.default.underline.green.white(`🔒 Auth session instance and metadata stored at ${authSessionInstancePath}`));
@@ -1,12 +1,18 @@
1
1
  import { StorageState } from "../../common/contextStorageStateHelpers";
2
- import { AuthSessionMetadata } from "../../common/cli/types";
2
+ import { AuthSessionMetadata, AuthSessionType } from "../../common/cli/types";
3
+ import * as playwright from "playwright-core";
3
4
  export declare function isAuthEnabled(): Promise<boolean>;
5
+ export declare function getAuthType(): Promise<AuthSessionType>;
6
+ export declare function ensureRecorderURLs(): Promise<{
7
+ startUrl: string;
8
+ endUrl: string;
9
+ }>;
4
10
  export declare function ensureAuthApi(operation: "create" | "check"): Promise<boolean>;
5
11
  export declare function runCreateApi(authSessionInput: Record<string, any>): Promise<StorageState>;
6
12
  export declare function runCheckApi(authSessionPath: string): Promise<boolean>;
7
13
  export declare function runCreateApiViaCLI(authSessionInput: Record<string, any>): Promise<StorageState>;
8
14
  export declare function runCheckApiViaCLI(authSessionPath: string): Promise<boolean>;
9
- export declare function storeAuthSessionInstance(authSessionInstance: StorageState, customName?: string, authSessionInput?: Record<string, any>, shouldOverride?: boolean): Promise<string>;
15
+ export declare function storeAuthSessionInstance(authSessionInstance: StorageState, customName?: string, authSessionInput?: Record<string, any>): Promise<string>;
10
16
  export declare function retrieveAuthSessionInstance(authSessionId: string, pathsOnly?: boolean): Promise<{
11
17
  authSessionInstanceStoragePath: string;
12
18
  authSessionInstanceMetadataPath: string;
@@ -18,3 +24,5 @@ export declare function retrieveAuthSessionInstance(authSessionId: string, paths
18
24
  authSessionInstanceStoragePath?: undefined;
19
25
  authSessionInstanceMetadataPath?: undefined;
20
26
  }>;
27
+ export declare function getStorageState(context: playwright.BrowserContext): Promise<StorageState>;
28
+ export declare function recordAuthSession(startUrl: string, endUrl: string): Promise<StorageState>;
@@ -4,7 +4,11 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.ensureAuthApi = ensureAuthApi;
7
+ exports.ensureRecorderURLs = ensureRecorderURLs;
8
+ exports.getAuthType = getAuthType;
9
+ exports.getStorageState = getStorageState;
7
10
  exports.isAuthEnabled = isAuthEnabled;
11
+ exports.recordAuthSession = recordAuthSession;
8
12
  exports.retrieveAuthSessionInstance = retrieveAuthSessionInstance;
9
13
  exports.runCheckApi = runCheckApi;
10
14
  exports.runCheckApiViaCLI = runCheckApiViaCLI;
@@ -22,6 +26,7 @@ var _tsNodeImport = require("../common/tsNodeImport");
22
26
  var _promptly = require("promptly");
23
27
  var _utils = require("../../common/cli/utils");
24
28
  var _types = require("../../common/cli/types");
29
+ var playwright = _interopRequireWildcard(require("playwright-core"));
25
30
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
26
31
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
27
32
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -34,6 +39,20 @@ async function isAuthEnabled() {
34
39
  return false;
35
40
  }
36
41
  }
42
+ async function getAuthType() {
43
+ const projectAuthConfig = await (0, _utils.getSettingIntunedJSON)("authSessions");
44
+ return projectAuthConfig.type ?? "API";
45
+ }
46
+ async function ensureRecorderURLs() {
47
+ const projectAuthConfig = await (0, _utils.getSettingIntunedJSON)("authSessions");
48
+ if (projectAuthConfig.type === "MANUAL" && projectAuthConfig.startUrl && projectAuthConfig.endUrl) {
49
+ return {
50
+ startUrl: projectAuthConfig.startUrl,
51
+ endUrl: projectAuthConfig.endUrl
52
+ };
53
+ }
54
+ throw new Error("Auth session recorder URLs are not configured, please configure them in Intuned.json");
55
+ }
37
56
  async function ensureAuthApi(operation) {
38
57
  const createApiFile = `${_constants.AUTH_SESSIONS_FOLDER_NAME}/${operation}.ts`;
39
58
  const createApiFilePath = _path.default.join(process.cwd(), createApiFile);
@@ -134,6 +153,9 @@ async function runCheckApiViaCLI(authSessionPath) {
134
153
  importFunction: _tsNodeImport.tsNodeImport
135
154
  });
136
155
  if (runApiResult.isErr()) {
156
+ if (runApiResult.error instanceof _runApi.AutomationError) {
157
+ throw runApiResult.error.error;
158
+ }
137
159
  return false;
138
160
  }
139
161
  const result = runApiResult.value.result;
@@ -142,7 +164,7 @@ async function runCheckApiViaCLI(authSessionPath) {
142
164
  }
143
165
  return result;
144
166
  }
145
- async function storeAuthSessionInstance(authSessionInstance, customName, authSessionInput, shouldOverride = false) {
167
+ async function storeAuthSessionInstance(authSessionInstance, customName, authSessionInput) {
146
168
  try {
147
169
  const authSessionsDirectoryPath = _path.default.join(process.cwd(), _constants.AUTH_SESSIONS_INSTANCES_FOLDER_NAME);
148
170
  await fs.ensureDir(authSessionsDirectoryPath);
@@ -157,7 +179,7 @@ async function storeAuthSessionInstance(authSessionInstance, customName, authSes
157
179
  const projectAuthConfig = await (0, _utils.getSettingIntunedJSON)("authSessions");
158
180
  let existingMetadata = {};
159
181
  const authSessionInstanceMetadataPath = _path.default.join(authSessionInstancePath, `metadata.json`);
160
- if (authSessionExists && shouldOverride) {
182
+ if (authSessionExists) {
161
183
  try {
162
184
  existingMetadata = await fs.readJSON(authSessionInstanceMetadataPath);
163
185
  } catch (readError) {
@@ -206,4 +228,59 @@ async function retrieveAuthSessionInstance(authSessionId, pathsOnly = false) {
206
228
  } catch (error) {
207
229
  throw new Error(`Error retrieving auth session instance: ${error.message}`);
208
230
  }
231
+ }
232
+ async function getStorageState(context) {
233
+ const result = {};
234
+ const storageState = await context.storageState();
235
+ result.cookies = storageState.cookies;
236
+ result.origins = storageState.origins;
237
+ const sessionDataList = [];
238
+ const pages = context.pages();
239
+ for (const page of pages) {
240
+ if (page.isClosed()) continue;
241
+ try {
242
+ const sessionData = await page.evaluate(() => {
243
+ const items = {
244
+ ...window.sessionStorage
245
+ };
246
+ return {
247
+ origin: window.location.origin,
248
+ sessionStorage: Object.entries(items).map(([name, value]) => ({
249
+ name,
250
+ value
251
+ }))
252
+ };
253
+ });
254
+ sessionDataList.push(sessionData);
255
+ } catch (error) {
256
+ console.error("Error getting sessionStorage:", error);
257
+ }
258
+ }
259
+ result.sessionStorage = sessionDataList;
260
+ return result;
261
+ }
262
+ async function recordAuthSession(startUrl, endUrl) {
263
+ let browser;
264
+ try {
265
+ browser = await playwright.chromium.launch({
266
+ headless: false,
267
+ args: ["--start-maximized", "--no-sandbox"]
268
+ });
269
+ const context = await browser.newContext({
270
+ viewport: null
271
+ });
272
+ const page = await context.newPage();
273
+ await page.goto(startUrl);
274
+ await page.waitForURL(url => url.toString().startsWith(endUrl), {
275
+ timeout: 300000
276
+ });
277
+ const storageState = await getStorageState(context);
278
+ return storageState;
279
+ } catch (error) {
280
+ throw new Error(`Authentication recording failed: ${error.message}`);
281
+ } finally {
282
+ if (browser) {
283
+ await browser.close();
284
+ }
285
+ }
209
286
  }
@@ -92,7 +92,7 @@ async function runApiViaCLI(apiName, inputData, options) {
92
92
  if (!refresehAuthSessionInstance) {
93
93
  throw new Error("Failed to refresh auth session");
94
94
  }
95
- await (0, _utils.storeAuthSessionInstance)(refresehAuthSessionInstance, options === null || options === void 0 ? void 0 : options.authSession, authSessionInput, true);
95
+ await (0, _utils.storeAuthSessionInstance)(refresehAuthSessionInstance, options === null || options === void 0 ? void 0 : options.authSession, authSessionInput);
96
96
  const checkResult = await (0, _utils.runCheckApiViaCLI)(authSessionPathToUse);
97
97
  if (!checkResult) {
98
98
  throw new Error("Failed to refresh auth session");
@@ -65,3 +65,10 @@ export type AuthSessionMetadata = {
65
65
  recorderStartUrl?: string;
66
66
  recorderEndUrl?: string;
67
67
  };
68
+ export interface SessionStorageState {
69
+ origin: string;
70
+ sessionStorage: {
71
+ name: string;
72
+ value: string;
73
+ }[];
74
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intuned/runtime-dev",
3
- "version": "1.0.6-cli-auth.0.0.18-test",
3
+ "version": "1.0.6-cli-auth.0.0.20-test",
4
4
  "description": "Intuned runtime",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",