@contrail/cli 3.6.0-alpha.aae3e70 → 3.6.0-alpha.bd1031b

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.
package/README.md CHANGED
@@ -17,7 +17,7 @@ $ npm install -g @contrail/cli
17
17
  $ contrail COMMAND
18
18
  running command...
19
19
  $ contrail (--version)
20
- @contrail/cli/3.6.0-alpha.aae3e70 linux-x64 node-v24.22.1
20
+ @contrail/cli/3.6.0-alpha.bd1031b linux-x64 node-v24.22.1
21
21
  $ contrail --help [COMMAND]
22
22
  USAGE
23
23
  $ contrail COMMAND
@@ -17,7 +17,7 @@ const COGNITO_CONFIGS = {
17
17
  clientId: '209s9c62o165cqab0fu5ihjtu9',
18
18
  sso: {
19
19
  domain: 'https://dev-vibeiq.auth.us-east-1.amazoncognito.com',
20
- clientId: '4nrd626t15ukbertqmcngfnom3',
20
+ clientId: '37m7g73hfetr5ab4o5b2ocv1j9',
21
21
  pastedCodeCallbackUrl: 'https://login.dev.vibeiq.com/cli',
22
22
  },
23
23
  },
@@ -240,11 +240,11 @@ async function refreshCurrentUserOrg() {
240
240
  return returnAndStoreConfig(sdk_1.Auth.config, 'password');
241
241
  }
242
242
  async function refreshAccessToken(signInMethod) {
243
- const newAccessToken = await requestAccessTokenForSignInMethod(signInMethod);
243
+ const newAccessToken = await fetchRefreshedAccessTokenForSignInMethod(signInMethod);
244
244
  sdk_1.Auth.config = { ...sdk_1.Auth.config, apiUserToken: newAccessToken };
245
245
  (0, sdk_1.setConfig)(sdk_1.Auth.config);
246
246
  }
247
- async function requestAccessTokenForSignInMethod(signInMethod) {
247
+ async function fetchRefreshedAccessTokenForSignInMethod(signInMethod) {
248
248
  const { apiRefreshToken, apiGateway } = sdk_1.Auth.config;
249
249
  if (signInMethod === 'sso') {
250
250
  const ssoClient = COGNITO_CONFIGS[(0, config_1.findEnvironmentName)(apiGateway)].sso;
@@ -232,6 +232,7 @@ describe('signIn with SSO', () => {
232
232
  delete process.env.CONTRAIL_CLI_PASSWORD;
233
233
  config_1.getConfig.mockResolvedValue({ apiGateway: DEV_GATEWAY, orgSlug: 'adam-zone-1' });
234
234
  sdk_1.getCurrentUser.mockResolvedValue({ id: 'user-1' });
235
+ mockAuthenticateUser.mockImplementation((_details, callbacks) => callbacks.onSuccess({ idToken: { jwtToken: 'cognito-id-token' } }));
235
236
  promptSpy = jest.spyOn(inquirer_1.default, 'prompt');
236
237
  pasteCodeFromPrintedSignInUrl();
237
238
  fetchMock = jest.fn();
package/dist/auth/sso.js CHANGED
@@ -66,16 +66,19 @@ function listenForLoopbackCode(expectedState) {
66
66
  response.writeHead(404).end();
67
67
  return;
68
68
  }
69
+ if (url.searchParams.get('state') !== expectedState) {
70
+ response.writeHead(400, { 'Content-Type': 'text/plain' }).end('Unknown sign-in attempt.');
71
+ return;
72
+ }
69
73
  const error = url.searchParams.get('error_description') ?? url.searchParams.get('error');
70
74
  const receivedCode = url.searchParams.get('code');
71
- const isExpectedState = url.searchParams.get('state') === expectedState;
72
- const signedIn = !error && receivedCode && isExpectedState;
75
+ const signedIn = !error && Boolean(receivedCode);
73
76
  const page = signedIn
74
77
  ? 'Sign-in received. Return to your terminal.'
75
78
  : 'Sign-in failed. Return to your terminal.';
76
79
  response
77
80
  .writeHead(signedIn ? 200 : 400, { 'Content-Type': 'text/plain', Connection: 'close' })
78
- .end(page, () => stopListening(() => {
81
+ .end(page, () => stopListening(request.socket, () => {
79
82
  if (error) {
80
83
  rejectCode(new Error(`SSO sign-in failed: ${error}`));
81
84
  }
@@ -87,10 +90,16 @@ function listenForLoopbackCode(expectedState) {
87
90
  }
88
91
  }));
89
92
  });
90
- const timeout = setTimeout(() => stopListening(() => rejectCode(new Error('Timed out waiting for the browser sign-in.'))), BROWSER_SIGN_IN_TIMEOUT_MS);
91
- function stopListening(settle) {
93
+ const openSockets = new Set();
94
+ server.on('connection', (socket) => {
95
+ openSockets.add(socket);
96
+ socket.once('close', () => openSockets.delete(socket));
97
+ });
98
+ const timeout = setTimeout(() => stopListening(undefined, () => rejectCode(new Error('Timed out waiting for the browser sign-in.'))), BROWSER_SIGN_IN_TIMEOUT_MS);
99
+ function stopListening(respondingSocket, settle) {
92
100
  clearTimeout(timeout);
93
101
  server.close();
102
+ openSockets.forEach((socket) => socket !== respondingSocket && socket.destroy());
94
103
  settle();
95
104
  }
96
105
  return new Promise((resolve, reject) => {
@@ -127,7 +136,7 @@ function buildOpenBrowserCommand(url, platform) {
127
136
  return ['open', [url]];
128
137
  }
129
138
  if (platform === 'win32') {
130
- return ['cmd', ['/c', 'start', '', url]];
139
+ return ['rundll32', ['url.dll,FileProtocolHandler', url]];
131
140
  }
132
141
  return ['xdg-open', [url]];
133
142
  }
@@ -157,8 +166,9 @@ async function postToTokenEndpoint(client, form) {
157
166
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
158
167
  body: new URLSearchParams(form),
159
168
  });
169
+ const body = await response.json();
160
170
  if (!response.ok) {
161
- throw new Error(`SSO token request failed (${response.status}).`);
171
+ throw new Error(`SSO token request failed (${response.status} ${body.error}).`);
162
172
  }
163
- return response.json();
173
+ return body;
164
174
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const node_crypto_1 = require("node:crypto");
4
+ const node_net_1 = require("node:net");
4
5
  const sso_1 = require("./sso");
5
6
  const ssoClient = {
6
7
  domain: 'https://dev-vibeiq.auth.us-east-1.amazoncognito.com',
@@ -62,11 +63,11 @@ describe('fetchSsoDomainCheck', () => {
62
63
  afterEach(() => {
63
64
  global.fetch = originalFetch;
64
65
  });
65
- function respondWith(body, status = 200) {
66
+ function stubDomainCheckFetch(body, status = 200) {
66
67
  global.fetch = jest.fn().mockResolvedValue(new Response(body, { status }));
67
68
  }
68
69
  it('should name the Cognito identity provider for an SSO domain', async () => {
69
- respondWith(JSON.stringify({ identityProvider: 'sso-vibeiq-com' }));
70
+ stubDomainCheckFetch(JSON.stringify({ identityProvider: 'sso-vibeiq-com' }));
70
71
  await expect((0, sso_1.fetchSsoDomainCheck)('Dev@VibeIQ.com', 'https://api')).resolves.toEqual({
71
72
  kind: 'cognito',
72
73
  identityProvider: 'sso-vibeiq-com',
@@ -74,19 +75,19 @@ describe('fetchSsoDomainCheck', () => {
74
75
  expect(global.fetch).toHaveBeenCalledWith('https://api/sso-login/domain-check/vibeiq.com');
75
76
  });
76
77
  it('should report a legacy SSO domain', async () => {
77
- respondWith(JSON.stringify({ identityProvider: '', url: 'https://api/sso-login/login/acme.com' }));
78
+ stubDomainCheckFetch(JSON.stringify({ identityProvider: '', url: 'https://api/sso-login/login/acme.com' }));
78
79
  await expect((0, sso_1.fetchSsoDomainCheck)('dev@acme.com', 'https://api')).resolves.toEqual({
79
80
  kind: 'legacy',
80
81
  });
81
82
  });
82
83
  it('should report no SSO for an empty response', async () => {
83
- respondWith('');
84
+ stubDomainCheckFetch('');
84
85
  await expect((0, sso_1.fetchSsoDomainCheck)('dev@example.com', 'https://api')).resolves.toEqual({
85
86
  kind: 'none',
86
87
  });
87
88
  });
88
89
  it('should fail when the lookup fails', async () => {
89
- respondWith('{}', 502);
90
+ stubDomainCheckFetch('{}', 502);
90
91
  await expect((0, sso_1.fetchSsoDomainCheck)('dev@example.com', 'https://api')).rejects.toThrow('SSO lookup for example.com failed (502).');
91
92
  });
92
93
  });
@@ -97,19 +98,35 @@ describe('listenForLoopbackCode', () => {
97
98
  expect(response.status).toBe(200);
98
99
  await expect(listener.code).resolves.toBe('the-code');
99
100
  });
100
- it('should reject a redirect carrying another attempt state', async () => {
101
+ it('should keep waiting for this attempt after a redirect from another attempt', async () => {
102
+ const listener = await (0, sso_1.listenForLoopbackCode)('the-state');
103
+ const staleResponse = await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?code=old-code&state=old-state`);
104
+ await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?code=the-code&state=the-state`);
105
+ expect(staleResponse.status).toBe(400);
106
+ await expect(listener.code).resolves.toBe('the-code');
107
+ });
108
+ it('should reject a redirect for this attempt that carries no code', async () => {
101
109
  const listener = await (0, sso_1.listenForLoopbackCode)('the-state');
102
110
  const rejection = expect(listener.code).rejects.toThrow('SSO sign-in returned an unexpected response.');
103
- const response = await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?code=the-code&state=other-state`);
111
+ const response = await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?state=the-state`);
104
112
  expect(response.status).toBe(400);
105
113
  await rejection;
106
114
  });
107
115
  it('should reject when the identity provider reports an error', async () => {
108
116
  const listener = await (0, sso_1.listenForLoopbackCode)('the-state');
109
117
  const rejection = expect(listener.code).rejects.toThrow('SSO sign-in failed: User cancelled');
110
- await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?error=access_denied&error_description=User%20cancelled`);
118
+ await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?error=access_denied&error_description=User%20cancelled&state=the-state`);
111
119
  await rejection;
112
120
  });
121
+ it('should close idle browser connections once the code arrives', async () => {
122
+ const listener = await (0, sso_1.listenForLoopbackCode)('the-state');
123
+ const idleConnection = (0, node_net_1.connect)(7731, 'localhost');
124
+ await new Promise((resolve) => idleConnection.once('connect', resolve));
125
+ const idleConnectionClosed = new Promise((resolve) => idleConnection.once('close', resolve));
126
+ await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?code=the-code&state=the-state`);
127
+ await expect(listener.code).resolves.toBe('the-code');
128
+ await expect(idleConnectionClosed).resolves.toBe(false);
129
+ });
113
130
  it('should tell the user to paste a code instead when the port is taken', async () => {
114
131
  const listener = await (0, sso_1.listenForLoopbackCode)('the-state');
115
132
  await expect((0, sso_1.listenForLoopbackCode)('another-state')).rejects.toThrow('Port 7731 is in use. Run contrail login --no-browser instead.');
@@ -117,3 +134,15 @@ describe('listenForLoopbackCode', () => {
117
134
  await listener.code;
118
135
  });
119
136
  });
137
+ describe('exchangeCodeForTokens', () => {
138
+ const originalFetch = global.fetch;
139
+ afterEach(() => {
140
+ global.fetch = originalFetch;
141
+ });
142
+ it('should name the Cognito error when the exchange is refused', async () => {
143
+ global.fetch = jest
144
+ .fn()
145
+ .mockResolvedValue(new Response(JSON.stringify({ error: 'invalid_grant' }), { status: 400 }));
146
+ await expect((0, sso_1.exchangeCodeForTokens)(ssoClient, 'the-code', sso_1.LOOPBACK_CALLBACK_URL, 'verifier')).rejects.toThrow('SSO token request failed (400 invalid_grant).');
147
+ });
148
+ });
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "3.6.0-alpha.aae3e70",
2
+ "version": "3.6.0-alpha.bd1031b",
3
3
  "commands": {
4
4
  "login": {
5
5
  "id": "login",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/cli",
3
- "version": "3.6.0-alpha.aae3e70",
3
+ "version": "3.6.0-alpha.bd1031b",
4
4
  "description": "VibeIQ's Contrail Platform CLI.",
5
5
  "bin": {
6
6
  "contrail-cli": "./bin/run",