Skip to content

Commit

Permalink
Revert "feat(auth): Allow workspace to pre-populate URL for quick sig…
Browse files Browse the repository at this point in the history
…n-in (#6653)"

This reverts commit 9719dc4.
  • Loading branch information
umpox committed Jan 24, 2025
1 parent 95b0e79 commit a1f5eee
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 63 deletions.
2 changes: 1 addition & 1 deletion lib/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ export {
checkVersion,
} from './sourcegraph-api/siteVersion'
export { configOverwrites } from './models/configOverwrites'
export { isS2, isWorkspaceInstance } from './sourcegraph-api/environments'
export { isS2 } from './sourcegraph-api/environments'
export { createGitDiff } from './editor/create-git-diff'

export { serialize, deserialize } from './lexicalEditor/atMentionsSerializer'
22 changes: 0 additions & 22 deletions lib/shared/src/sourcegraph-api/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,3 @@ export function isS2(arg: Pick<AuthStatus, 'endpoint'> | undefined | string): bo
// TODO: Update to live link https://linear.app/sourcegraph/issue/CORE-535/cody-clients-migrate-ctas-to-live-links
export const DOTCOM_WORKSPACE_UPGRADE_URL = new URL('https://sourcegraph.com/cody/manage')
export const SG_WORKSPACES_URL = new URL('https://workspaces.sourcegraph.com')

export const Workspaces_Host_Prod = '.sourcegraph.app'
export const Workspaces_Host_Dev = '.sourcegraphdev.app'

// 🚨 SECURITY: This is used to validate a set of URLs we will allow to be passed in
// to the editor in the URL handler.
export function isWorkspaceInstance(authStatus: Pick<AuthStatus, 'endpoint'> | undefined): boolean
export function isWorkspaceInstance(url: string): boolean
export function isWorkspaceInstance(arg: Pick<AuthStatus, 'endpoint'> | undefined | string): boolean {
const url = typeof arg === 'string' ? arg : arg?.endpoint
if (url === undefined) {
return false
}
try {
return (
new URL(url).host.endsWith(Workspaces_Host_Prod) ||
new URL(url).host.endsWith(Workspaces_Host_Dev)
)
} catch {
return false
}
}
43 changes: 3 additions & 40 deletions vscode/src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
isDotCom,
isError,
isNetworkLikeError,
isWorkspaceInstance,
telemetryRecorder,
} from '@sourcegraph/cody-shared'
import { resolveAuth } from '@sourcegraph/cody-shared/src/configuration/auth-resolver'
Expand All @@ -41,28 +40,6 @@ interface LoginMenuItem {

type AuthMenuType = 'signin' | 'switch'

/**
* Handles trying to directly sign-in or add to an enterprise instance.
* First tries to sign in with the current token, if it's valid. Otherwise,
* opens the sign-in flow and has user confirm.
*/
async function showEnterpriseInstanceUrlFlow(endpoint: string): Promise<void> {
const { configuration } = await currentResolvedConfig()
const auth = await resolveAuth(endpoint, configuration, secretStorage)

const authStatus = await authProvider.validateAndStoreCredentials(auth, 'store-if-valid')

if (!authStatus?.authenticated) {
const instanceUrl = await showInstanceURLInputBox(endpoint)
if (!instanceUrl) {
return
}
authProvider.setAuthPendingToEndpoint(instanceUrl)
redirectToEndpointLogin(instanceUrl)
} else {
await showAuthResultMessage(endpoint, authStatus)
}
}
/**
* Show a quickpick to select the endpoint to sign into.
*/
Expand Down Expand Up @@ -160,12 +137,12 @@ async function showAuthMenu(type: AuthMenuType): Promise<LoginMenuItem | null> {
/**
* Show a VS Code input box to ask the user to enter a Sourcegraph instance URL.
*/
async function showInstanceURLInputBox(url?: string): Promise<string | undefined> {
async function showInstanceURLInputBox(title: string): Promise<string | undefined> {
const result = await vscode.window.showInputBox({
title: 'Connect to a Sourcegraph instance',
title,
prompt: 'Enter the URL of the Sourcegraph instance. For example, https://sourcegraph.example.com.',
placeHolder: 'https://sourcegraph.example.com',
value: url ?? 'https://',
value: 'https://',
password: false,
ignoreFocusOut: true,
// valide input to ensure the user is not entering a token as URL
Expand Down Expand Up @@ -323,22 +300,8 @@ export async function tokenCallbackHandler(uri: vscode.Uri): Promise<void> {
closeAuthProgressIndicator()

const params = new URLSearchParams(uri.query)

const token = params.get('code') || params.get('token')
const endpoint = currentAuthStatus().endpoint

// If we were provided an instance URL then it means we are
// request the user setup auth with a different sourcegraph instance
// We want to prompt them to switch to this instance and if needed
// start the auth flow
const instanceHost = params.get('instance')
const instanceUrl = instanceHost ? new URL(instanceHost).origin : undefined
if (instanceUrl && isWorkspaceInstance(instanceUrl)) {
// Prompt the user to switch/setup with the new instance
await showEnterpriseInstanceUrlFlow(instanceUrl)
return
}

if (!token || !endpoint) {
return
}
Expand Down

0 comments on commit a1f5eee

Please sign in to comment.