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

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.f45f126 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,10 +66,13 @@ 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.';
@@ -127,7 +130,7 @@ function buildOpenBrowserCommand(url, platform) {
127
130
  return ['open', [url]];
128
131
  }
129
132
  if (platform === 'win32') {
130
- return ['cmd', ['/c', 'start', '', url]];
133
+ return ['rundll32', ['url.dll,FileProtocolHandler', url]];
131
134
  }
132
135
  return ['xdg-open', [url]];
133
136
  }
@@ -157,8 +160,9 @@ async function postToTokenEndpoint(client, form) {
157
160
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
158
161
  body: new URLSearchParams(form),
159
162
  });
163
+ const body = await response.json();
160
164
  if (!response.ok) {
161
- throw new Error(`SSO token request failed (${response.status}).`);
165
+ throw new Error(`SSO token request failed (${response.status} ${body.error}).`);
162
166
  }
163
- return response.json();
167
+ return body;
164
168
  }
@@ -62,11 +62,11 @@ describe('fetchSsoDomainCheck', () => {
62
62
  afterEach(() => {
63
63
  global.fetch = originalFetch;
64
64
  });
65
- function respondWith(body, status = 200) {
65
+ function stubDomainCheckFetch(body, status = 200) {
66
66
  global.fetch = jest.fn().mockResolvedValue(new Response(body, { status }));
67
67
  }
68
68
  it('should name the Cognito identity provider for an SSO domain', async () => {
69
- respondWith(JSON.stringify({ identityProvider: 'sso-vibeiq-com' }));
69
+ stubDomainCheckFetch(JSON.stringify({ identityProvider: 'sso-vibeiq-com' }));
70
70
  await expect((0, sso_1.fetchSsoDomainCheck)('Dev@VibeIQ.com', 'https://api')).resolves.toEqual({
71
71
  kind: 'cognito',
72
72
  identityProvider: 'sso-vibeiq-com',
@@ -74,19 +74,19 @@ describe('fetchSsoDomainCheck', () => {
74
74
  expect(global.fetch).toHaveBeenCalledWith('https://api/sso-login/domain-check/vibeiq.com');
75
75
  });
76
76
  it('should report a legacy SSO domain', async () => {
77
- respondWith(JSON.stringify({ identityProvider: '', url: 'https://api/sso-login/login/acme.com' }));
77
+ stubDomainCheckFetch(JSON.stringify({ identityProvider: '', url: 'https://api/sso-login/login/acme.com' }));
78
78
  await expect((0, sso_1.fetchSsoDomainCheck)('dev@acme.com', 'https://api')).resolves.toEqual({
79
79
  kind: 'legacy',
80
80
  });
81
81
  });
82
82
  it('should report no SSO for an empty response', async () => {
83
- respondWith('');
83
+ stubDomainCheckFetch('');
84
84
  await expect((0, sso_1.fetchSsoDomainCheck)('dev@example.com', 'https://api')).resolves.toEqual({
85
85
  kind: 'none',
86
86
  });
87
87
  });
88
88
  it('should fail when the lookup fails', async () => {
89
- respondWith('{}', 502);
89
+ stubDomainCheckFetch('{}', 502);
90
90
  await expect((0, sso_1.fetchSsoDomainCheck)('dev@example.com', 'https://api')).rejects.toThrow('SSO lookup for example.com failed (502).');
91
91
  });
92
92
  });
@@ -97,17 +97,24 @@ describe('listenForLoopbackCode', () => {
97
97
  expect(response.status).toBe(200);
98
98
  await expect(listener.code).resolves.toBe('the-code');
99
99
  });
100
- it('should reject a redirect carrying another attempt state', async () => {
100
+ it('should keep waiting for this attempt after a redirect from another attempt', async () => {
101
+ const listener = await (0, sso_1.listenForLoopbackCode)('the-state');
102
+ const staleResponse = await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?code=old-code&state=old-state`);
103
+ await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?code=the-code&state=the-state`);
104
+ expect(staleResponse.status).toBe(400);
105
+ await expect(listener.code).resolves.toBe('the-code');
106
+ });
107
+ it('should reject a redirect for this attempt that carries no code', async () => {
101
108
  const listener = await (0, sso_1.listenForLoopbackCode)('the-state');
102
109
  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`);
110
+ const response = await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?state=the-state`);
104
111
  expect(response.status).toBe(400);
105
112
  await rejection;
106
113
  });
107
114
  it('should reject when the identity provider reports an error', async () => {
108
115
  const listener = await (0, sso_1.listenForLoopbackCode)('the-state');
109
116
  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`);
117
+ await fetch(`${sso_1.LOOPBACK_CALLBACK_URL}?error=access_denied&error_description=User%20cancelled&state=the-state`);
111
118
  await rejection;
112
119
  });
113
120
  it('should tell the user to paste a code instead when the port is taken', async () => {
@@ -117,3 +124,15 @@ describe('listenForLoopbackCode', () => {
117
124
  await listener.code;
118
125
  });
119
126
  });
127
+ describe('exchangeCodeForTokens', () => {
128
+ const originalFetch = global.fetch;
129
+ afterEach(() => {
130
+ global.fetch = originalFetch;
131
+ });
132
+ it('should name the Cognito error when the exchange is refused', async () => {
133
+ global.fetch = jest
134
+ .fn()
135
+ .mockResolvedValue(new Response(JSON.stringify({ error: 'invalid_grant' }), { status: 400 }));
136
+ await expect((0, sso_1.exchangeCodeForTokens)(ssoClient, 'the-code', sso_1.LOOPBACK_CALLBACK_URL, 'verifier')).rejects.toThrow('SSO token request failed (400 invalid_grant).');
137
+ });
138
+ });
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "3.6.0-alpha.aae3e70",
2
+ "version": "3.6.0-alpha.f45f126",
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.f45f126",
4
4
  "description": "VibeIQ's Contrail Platform CLI.",
5
5
  "bin": {
6
6
  "contrail-cli": "./bin/run",