Skip to content

Commit 9b05a3f

Browse files
authored
fix(codecatalyst): default to builder ID sign in if URI contains no IdC params (#4174)
Problem: If a user opens a vscode URI to open a dev environment and there are no IdC params, then the user is pointed to the login page instead of being routed to builder ID sign in. This means the user has to click the link again after signing in. Solution: Always default to builder ID if there are no IdC params in the URI, rather than prompt for sign in.
1 parent b17938d commit 9b05a3f

File tree

2 files changed

+59
-27
lines changed

2 files changed

+59
-27
lines changed

src/codecatalyst/uriHandlers.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import * as vscode from 'vscode'
77
import { SearchParams, UriHandler } from '../shared/vscode/uriHandler'
88
import { getCodeCatalystConfig } from '../shared/clients/codecatalystClient'
99
import { CodeCatalystCommands } from './commands'
10+
import { builderIdStartUrl } from '../auth/sso/model'
11+
import { defaultSsoRegion } from '../auth/connection'
12+
import { getLogger } from '../shared/logger'
1013

1114
type ConnectParams = {
1215
devEnvironmentId: string
@@ -54,18 +57,14 @@ function parseCloneParams(query: SearchParams) {
5457
}
5558

5659
export function parseConnectParams(query: SearchParams): ConnectParams {
60+
const params = query.getFromKeysOrThrow('devEnvironmentId', 'spaceName', 'projectName')
61+
5762
try {
58-
// eslint-disable-next-line @typescript-eslint/naming-convention
59-
const { sso_start_url, sso_region, ...rest } = query.getFromKeysOrThrow(
60-
'devEnvironmentId',
61-
'spaceName',
62-
'projectName',
63-
'sso_start_url',
64-
'sso_region'
65-
)
66-
return { ...rest, sso: { startUrl: sso_start_url, region: sso_region } }
63+
const ssoParams = query.getFromKeysOrThrow('sso_start_url', 'sso_region')
64+
return { ...params, sso: { startUrl: ssoParams.sso_start_url, region: ssoParams.sso_region } }
6765
} catch {
68-
return query.getFromKeysOrThrow('devEnvironmentId', 'spaceName', 'projectName')
66+
getLogger().debug(`No IdC SSO params provided in CodeCatalyst URI, defaulting to Builder ID.`)
67+
return { ...params, sso: { startUrl: builderIdStartUrl, region: defaultSsoRegion } }
6968
}
7069
}
7170

src/test/codecatalyst/uriHandlers.test.ts

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,17 @@ import { VSCODE_EXTENSION_ID } from '../../shared/extensions'
1212
import { CodeCatalystClient } from '../../shared/clients/codecatalystClient'
1313
import { anything, mock, reset, when } from 'ts-mockito'
1414
import { SeverityLevel } from '../shared/vscode/message'
15-
import { DevEnvironmentId } from '../../codecatalyst/model'
1615
import { getTestWindow } from '../shared/vscode/window'
16+
import { builderIdStartUrl } from '../../auth/sso/model'
17+
import { defaultSsoRegion } from '../../auth/connection'
1718

1819
type Stub<T extends (...args: any[]) => any> = sinon.SinonStub<Parameters<T>, ReturnType<T>>
1920

2021
function createCloneUri(target: string): vscode.Uri {
2122
return vscode.Uri.parse(`vscode://${VSCODE_EXTENSION_ID.awstoolkit}/clone?url=${encodeURIComponent(target)}`)
2223
}
2324

24-
function createConnectUri(env: DevEnvironmentId): vscode.Uri {
25-
const params = {
26-
devEnvironmentId: env.id,
27-
spaceName: env.org.name,
28-
projectName: env.project.name,
29-
}
25+
function createConnectUri(params: { [key: string]: any }): vscode.Uri {
3026
const encoded = Object.entries(params)
3127
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
3228
.join('&')
@@ -37,20 +33,10 @@ function createConnectUri(env: DevEnvironmentId): vscode.Uri {
3733
// The path is apart of our public API! They should not be easy to change.
3834

3935
describe('CodeCatalyst handlers', function () {
40-
let handler: UriHandler
4136
let commandStub: Stub<typeof vscode.commands.executeCommand>
4237
const client = mock<CodeCatalystClient>()
4338

4439
beforeEach(function () {
45-
handler = new UriHandler()
46-
register(handler, {
47-
openDevEnv: {
48-
execute: async () => undefined,
49-
} as any,
50-
cloneRepo: {
51-
execute: async () => undefined,
52-
} as any,
53-
})
5440
commandStub = sinon.stub(vscode.commands, 'executeCommand')
5541
})
5642

@@ -60,6 +46,17 @@ describe('CodeCatalyst handlers', function () {
6046
})
6147

6248
describe('clone', function () {
49+
let handler: UriHandler
50+
51+
beforeEach(function () {
52+
handler = new UriHandler()
53+
register(handler, {
54+
cloneRepo: {
55+
execute: async () => undefined,
56+
} as any,
57+
} as any)
58+
})
59+
6360
it('registers for "/clone"', function () {
6461
assert.throws(() => handler.onPath('/clone', () => {}))
6562
})
@@ -83,6 +80,42 @@ describe('CodeCatalyst handlers', function () {
8380
project: { name: 'project' },
8481
}
8582

83+
const params = {
84+
devEnvironmentId: devenvId.id,
85+
spaceName: devenvId.org.name,
86+
projectName: devenvId.project.name,
87+
}
88+
89+
let openDevEnvMock: sinon.SinonExpectation
90+
let handler: UriHandler
91+
92+
beforeEach(function () {
93+
handler = new UriHandler()
94+
openDevEnvMock = sinon.mock()
95+
register(handler, {
96+
openDevEnv: {
97+
execute: openDevEnvMock,
98+
} as any,
99+
} as any)
100+
})
101+
102+
it('returns builder ID SSO if IdC params are not present', async function () {
103+
await handler.handleUri(createConnectUri(params))
104+
assert.ok(
105+
openDevEnvMock.calledWith(devenvId, undefined, {
106+
startUrl: builderIdStartUrl,
107+
region: defaultSsoRegion,
108+
})
109+
)
110+
})
111+
112+
it('returns provided IdC params', async function () {
113+
const ssoStartUrl = 'https://my-url'
114+
const ssoRegion = 'us-west-2'
115+
await handler.handleUri(createConnectUri({ ...params, sso_start_url: ssoStartUrl, sso_region: ssoRegion }))
116+
assert.ok(openDevEnvMock.calledWith(devenvId, undefined, { startUrl: ssoStartUrl, region: ssoRegion }))
117+
})
118+
86119
it('checks that the environment exists', async function () {
87120
// This test is not accurate anymore because dependencies are checked prior to API calls
88121
// Unit tests are ran without other extensions activated, so this fails on the SSH extension check

0 commit comments

Comments
 (0)