-
Notifications
You must be signed in to change notification settings - Fork 43
Add alternativeWebUrl param
#956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import { buildOAuthTokenData } from "../oauth/utils"; | |
| import { withOptionalProgress } from "../progress"; | ||
| import { maybeAskAuthMethod, maybeAskUrl } from "../promptUtils"; | ||
| import { isKeyringEnabled } from "../settings/cli"; | ||
| import { resolveBrowserUrl } from "../util"; | ||
| import { vscodeProposed } from "../vscodeProposed"; | ||
|
|
||
| import type { User } from "coder/site/src/api/typesGenerated"; | ||
|
|
@@ -361,7 +362,9 @@ export class LoginCoordinator implements vscode.Disposable { | |
| } | ||
| // This prompt is for convenience; do not error if they close it since | ||
| // they may already have a token or already have the page opened. | ||
| await vscode.env.openExternal(vscode.Uri.parse(`${url}/cli-auth`)); | ||
| await vscode.env.openExternal( | ||
| vscode.Uri.parse(`${resolveBrowserUrl(url)}/cli-auth`), | ||
| ); | ||
|
Comment on lines
+365
to
+367
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This only switches the legacy token page to Useful? React with 👍 / 👎.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We didn't touch the OAuth path deliberately, but I think the case highlighted here is worth addressing so we have consistency. Will submit a fix shortly.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed! |
||
|
|
||
| // For token auth, start with the existing token in the prompt or the last | ||
| // used token. Once submitted, if there is a failure we will keep asking | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import * as vscode from "vscode"; | ||
|
|
||
| import { CoderApi } from "../api/coderApi"; | ||
| import { resolveBrowserUrl } from "../util"; | ||
|
|
||
| import { | ||
| AUTH_GRANT_TYPE, | ||
|
|
@@ -201,7 +202,13 @@ export class OAuthAuthorizer implements vscode.Disposable { | |
| code_challenge_method: PKCE_CHALLENGE_METHOD, | ||
| }); | ||
|
|
||
| const url = `${metadata.authorization_endpoint}?${params.toString()}`; | ||
| // The server-advertised endpoint is authoritative for the path, but the | ||
| // origin may be unreachable from a browser (e.g. blocked port). When | ||
| // `coder.alternativeWebUrl` is set, swap the origin so the user lands on | ||
| // a reachable host while preserving the path the server told us to use. | ||
|
Comment on lines
+205
to
+208
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agents tend to over explain, I think this can be trimmed or removed entirely |
||
| const endpoint = new URL(metadata.authorization_endpoint); | ||
| const browserBase = resolveBrowserUrl(endpoint.origin); | ||
| const url = `${browserBase}${endpoint.pathname}?${params.toString()}`; | ||
|
|
||
|
Comment on lines
+209
to
212
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old code was: const url = `${metadata.authorization_endpoint}?${params.toString()}`;The new code drops const endpoint = new URL(metadata.authorization_endpoint);
const browserOrigin = new URL(resolveUiUrl(coderApi.getHost())).origin;
endpoint.protocol = new URL(browserOrigin).protocol;
endpoint.host = new URL(browserOrigin).host;
for (const [key, value] of Object.entries(params)) {
endpoint.searchParams.set(key, value);
}
const url = endpoint.toString();Or, if the helper is as per the suggestion above, just call the open helper with the endpoint pathname and the params as the query. Either way, query strings already on |
||
| this.logger.debug("Built OAuth authorization URL:", { | ||
| client_id: clientId, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import * as os from "node:os"; | ||
| import url from "node:url"; | ||
| import * as vscode from "vscode"; | ||
|
|
||
| export interface AuthorityParts { | ||
| agent: string | undefined; | ||
|
|
@@ -202,6 +203,20 @@ export async function renameWithRetry( | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Return the URL for opening Coder pages in the browser. Uses the | ||
| * `coder.alternativeWebUrl` setting when configured, otherwise returns | ||
| * the connection URL unchanged. | ||
| */ | ||
| export function resolveBrowserUrl(connectionUrl: string): string { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| const alt = vscode.workspace | ||
| .getConfiguration("coder") | ||
| .get<string>("alternativeWebUrl") | ||
| ?.trim() | ||
| .replace(/\/+$/, ""); | ||
| return alt || connectionUrl; | ||
| } | ||
|
|
||
| export function escapeCommandArg(arg: string): string { | ||
| const escapedString = arg.replaceAll('"', String.raw`\"`); | ||
| return `"${escapedString}"`; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import { | |
|
|
||
| import { type CoderApi } from "../../api/coderApi"; | ||
| import { type Logger } from "../../logging/logger"; | ||
| import { resolveBrowserUrl } from "../../util"; | ||
| import { | ||
| dispatchCommand, | ||
| dispatchRequest, | ||
|
|
@@ -154,7 +155,12 @@ export class ChatPanelProvider | |
| const resolved = new URL(url, coderUrl); | ||
| const expected = new URL(coderUrl); | ||
| if (resolved.origin === expected.origin) { | ||
| void vscode.env.openExternal(vscode.Uri.parse(resolved.toString())); | ||
| const browserBase = resolveBrowserUrl(coderUrl); | ||
| // Concatenate rather than `new URL(path, base)` so a path prefix on | ||
| // the alternative URL (e.g. a reverse proxy at https://host/coder) | ||
| // is preserved. | ||
|
Comment on lines
+159
to
+161
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // Preserve the server-advertised path; only the origin/prefix may change. |
||
| const browserUrl = `${browserBase}${resolved.pathname}${resolved.search}${resolved.hash}`; | ||
| void vscode.env.openExternal(vscode.Uri.parse(browserUrl)); | ||
| } | ||
| } catch { | ||
| this.logger.warn(`Chat: invalid navigate URL: ${url}`); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ import { | |
| streamBuildLogs, | ||
| } from "../../api/workspace"; | ||
| import { type Logger } from "../../logging/logger"; | ||
| import { resolveBrowserUrl } from "../../util"; | ||
| import { vscodeProposed } from "../../vscodeProposed"; | ||
| import { | ||
| dispatchCommand, | ||
|
|
@@ -308,19 +309,21 @@ export class TasksPanelProvider | |
| } | ||
|
|
||
| private async handleViewInCoder(taskId: string): Promise<void> { | ||
| const baseUrl = this.client.getHost(); | ||
| if (!baseUrl) return; | ||
| const connUrl = this.client.getHost(); | ||
| if (!connUrl) return; | ||
|
|
||
| const baseUrl = resolveBrowserUrl(connUrl); | ||
| const task = await this.client.getTask("me", taskId); | ||
| vscode.env.openExternal( | ||
| vscode.Uri.parse(`${baseUrl}/tasks/${task.owner_name}/${task.id}`), | ||
| ); | ||
| } | ||
|
|
||
| private async handleViewLogs(taskId: string): Promise<void> { | ||
| const baseUrl = this.client.getHost(); | ||
| if (!baseUrl) return; | ||
| const connUrl = this.client.getHost(); | ||
| if (!connUrl) return; | ||
|
|
||
| const baseUrl = resolveBrowserUrl(connUrl); | ||
| const task = await this.client.getTask("me", taskId); | ||
| vscode.env.openExternal(vscode.Uri.parse(getTaskBuildUrl(baseUrl, task))); | ||
|
Comment on lines
+326
to
328
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of always doing
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could even do it in a safer way like |
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,13 @@ describe("ChatPanelProvider", () => { | |
| beforeEach(() => { | ||
| vi.resetAllMocks(); | ||
| windowMock.__setActiveColorThemeKind(vscode.ColorThemeKind.Dark); | ||
|
|
||
| vi.mocked(vscode.workspace.getConfiguration).mockReturnValue({ | ||
| get: vi.fn(), | ||
|
Comment on lines
+91
to
+92
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do use |
||
| has: vi.fn().mockReturnValue(false), | ||
| inspect: vi.fn(), | ||
| update: vi.fn().mockResolvedValue(undefined), | ||
| } as unknown as vscode.WorkspaceConfiguration); | ||
| }); | ||
|
|
||
| describe("theme sync", () => { | ||
|
|
@@ -171,6 +178,40 @@ describe("ChatPanelProvider", () => { | |
| ); | ||
| }); | ||
|
|
||
| it("uses alternative web URL for navigation when configured", () => { | ||
| vi.mocked(vscode.workspace.getConfiguration).mockReturnValue({ | ||
| get: vi.fn().mockReturnValue("https://web.example.com"), | ||
| } as unknown as vscode.WorkspaceConfiguration); | ||
|
|
||
| const { sendFromWebview } = createHarness(); | ||
|
|
||
| sendFromWebview({ | ||
| method: "coder:navigate", | ||
| params: { url: "/templates" }, | ||
| }); | ||
|
|
||
| expect(vscode.env.openExternal).toHaveBeenCalledWith( | ||
| vscode.Uri.parse("https://web.example.com/templates"), | ||
| ); | ||
| }); | ||
|
|
||
| it("preserves path prefix in alternative web URL", () => { | ||
| vi.mocked(vscode.workspace.getConfiguration).mockReturnValue({ | ||
| get: vi.fn().mockReturnValue("https://proxy.example.com/coder"), | ||
| } as unknown as vscode.WorkspaceConfiguration); | ||
|
|
||
| const { sendFromWebview } = createHarness(); | ||
|
|
||
| sendFromWebview({ | ||
| method: "coder:navigate", | ||
| params: { url: "/templates" }, | ||
| }); | ||
|
|
||
| expect(vscode.env.openExternal).toHaveBeenCalledWith( | ||
| vscode.Uri.parse("https://proxy.example.com/coder/templates"), | ||
| ); | ||
| }); | ||
|
|
||
| it("ignores navigate without url payload", () => { | ||
| const { sendFromWebview } = createHarness(); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do add something like "when empty, the connection URL is used for UI". I do think we need to be clear that this only affects calls like
vscode.env.openExternal(without mentioning this here of course)