Skip to content

feat: allow magic string to generate session token for external apps #9878

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

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions site/src/components/Resources/AppLink/AppLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { makeStyles } from "@mui/styles";
import Tooltip from "@mui/material/Tooltip";
import ErrorOutlineIcon from "@mui/icons-material/ErrorOutline";
import { PrimaryAgentButton } from "components/Resources/AgentButton";
import { FC } from "react";
import { FC, useState } from "react";
import { combineClasses } from "utils/combineClasses";
import * as TypesGen from "../../../api/typesGenerated";
import { generateRandomString } from "../../../utils/random";
import { BaseIcon } from "./BaseIcon";
import { ShareIcon } from "./ShareIcon";
import { useProxy } from "contexts/ProxyContext";
import { createAppLinkHref } from "utils/apps";
import { getApiKey } from "api/api";

const Language = {
appTitle: (appName: string, identifier: string): string =>
Expand All @@ -28,6 +29,7 @@ export const AppLink: FC<AppLinkProps> = ({ app, workspace, agent }) => {
const { proxy } = useProxy();
const preferredPathBase = proxy.preferredPathAppURL;
const appsHost = proxy.preferredWildcardHostname;
const [fetchingSessionToken, setFetchingSessionToken] = useState(false);

const styles = useStyles();
const username = workspace.owner_name;
Expand Down Expand Up @@ -72,6 +74,9 @@ export const AppLink: FC<AppLinkProps> = ({ app, workspace, agent }) => {
primaryTooltip =
"Your admin has not configured subdomain application access";
}
if (fetchingSessionToken) {
canClick = false;
}

const isPrivateApp = app.sharing_level === "owner";

Expand All @@ -96,13 +101,38 @@ export const AppLink: FC<AppLinkProps> = ({ app, workspace, agent }) => {
className={canClick ? styles.link : styles.disabledLink}
onClick={
canClick
? (event) => {
? async (event) => {
event.preventDefault();
window.open(
href,
Language.appTitle(appDisplayName, generateRandomString(12)),
"width=900,height=600",
);
// This is an external URI like "vscode://", so
// it needs to be opened with the browser protocol handler.
if (app.external && !app.url.startsWith("http")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still pretty convinced this should be an ||

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If app.external and the URL is https://my-internal-jira.com we wouldn't want to replace window.location.href, otherwise it would redirect the user from the dashboard.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aslilac lmk async if I'm thinking of this wrong, going to merge since it's blocking some registry work, but I'm happy to change it!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OH. I think I misunderstood what external meant here. It's probably fine.

// If the protocol is external the browser does not
// redirect the user from the page.

// This is a magic undocumented string that is replaced
// with a brand-new session token from the backend.
// This only exists for external URLs, and should only
// be used internally, and is highly subject to break.
const magicTokenString = "$SESSION_TOKEN";
const hasMagicToken = href.indexOf(magicTokenString);
let url = href;
if (hasMagicToken !== -1) {
setFetchingSessionToken(true);
const key = await getApiKey();
url = href.replaceAll(magicTokenString, key.key);
setFetchingSessionToken(false);
}
window.location.href = url;
} else {
window.open(
href,
Language.appTitle(
appDisplayName,
generateRandomString(12),
),
"width=900,height=600",
);
}
}
: undefined
}
Expand Down