@intuned/runtime-dev 1.0.6-cli-auth.0.0.17-test → 1.0.6-cli-auth.0.0.19-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.
@@ -16,17 +16,30 @@ _commander.program.description("Create an auth session").option("-i, --input <in
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, options.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, options.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);
@@ -142,7 +161,7 @@ async function runCheckApiViaCLI(authSessionPath) {
142
161
  }
143
162
  return result;
144
163
  }
145
- async function storeAuthSessionInstance(authSessionInstance, customName, authSessionInput, shouldOverride = false) {
164
+ async function storeAuthSessionInstance(authSessionInstance, customName, authSessionInput) {
146
165
  try {
147
166
  const authSessionsDirectoryPath = _path.default.join(process.cwd(), _constants.AUTH_SESSIONS_INSTANCES_FOLDER_NAME);
148
167
  await fs.ensureDir(authSessionsDirectoryPath);
@@ -157,7 +176,7 @@ async function storeAuthSessionInstance(authSessionInstance, customName, authSes
157
176
  const projectAuthConfig = await (0, _utils.getSettingIntunedJSON)("authSessions");
158
177
  let existingMetadata = {};
159
178
  const authSessionInstanceMetadataPath = _path.default.join(authSessionInstancePath, `metadata.json`);
160
- if (authSessionExists && shouldOverride) {
179
+ if (authSessionExists) {
161
180
  try {
162
181
  existingMetadata = await fs.readJSON(authSessionInstanceMetadataPath);
163
182
  } catch (readError) {
@@ -206,4 +225,61 @@ async function retrieveAuthSessionInstance(authSessionId, pathsOnly = false) {
206
225
  } catch (error) {
207
226
  throw new Error(`Error retrieving auth session instance: ${error.message}`);
208
227
  }
228
+ }
229
+ async function getStorageState(context) {
230
+ const result = {};
231
+ const storageState = await context.storageState();
232
+ result.cookies = storageState.cookies;
233
+ result.origins = storageState.origins;
234
+ const sessionDataList = [];
235
+ const pages = context.pages();
236
+ for (const page of pages) {
237
+ if (page.isClosed()) continue;
238
+ try {
239
+ const sessionData = await page.evaluate(() => {
240
+ const items = {
241
+ ...window.sessionStorage
242
+ };
243
+ return {
244
+ origin: window.location.origin,
245
+ sessionStorage: Object.entries(items).map(([name, value]) => ({
246
+ name,
247
+ value
248
+ }))
249
+ };
250
+ });
251
+ sessionDataList.push(sessionData);
252
+ } catch (error) {
253
+ console.error("Error getting sessionStorage:", error);
254
+ }
255
+ }
256
+ result.sessionStorage = sessionDataList;
257
+ return result;
258
+ }
259
+ async function recordAuthSession(startUrl, endUrl) {
260
+ let browser;
261
+ let context;
262
+ try {
263
+ browser = await playwright.chromium.launch({
264
+ headless: false,
265
+ args: ["--start-maximized", "--no-sandbox"]
266
+ });
267
+ context = await browser.newContext({
268
+ viewport: null
269
+ });
270
+ const page = await context.newPage();
271
+ await page.goto(startUrl);
272
+ await page.waitForURL(url => url.toString().startsWith(endUrl), {
273
+ timeout: 300000
274
+ });
275
+ const storageState = await context.storageState();
276
+ return storageState;
277
+ } catch (error) {
278
+ console.error("Failed to record authentication session:", error);
279
+ throw new Error(`Authentication recording failed: ${error.message}`);
280
+ } finally {
281
+ if (browser) {
282
+ await browser.close();
283
+ }
284
+ }
209
285
  }
@@ -82,7 +82,7 @@ async function runApiViaCLI(apiName, inputData, options) {
82
82
  }
83
83
  const {
84
84
  metadata
85
- } = await (0, _utils.retrieveAuthSessionInstance)(options === null || options === void 0 ? void 0 : options.authSession, true);
85
+ } = await (0, _utils.retrieveAuthSessionInstance)(options === null || options === void 0 ? void 0 : options.authSession);
86
86
  if ((metadata === null || metadata === void 0 ? void 0 : metadata.authSessionType) === "MANUAL") {
87
87
  throw new Error("Expired Auth session is recorder-based, please provide a new one or refresh it manually");
88
88
  }
@@ -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.17-test",
3
+ "version": "1.0.6-cli-auth.0.0.19-test",
4
4
  "description": "Intuned runtime",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",