Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Allow magic string to be replaced with a session token for external apps
  • Loading branch information
kylecarbs committed Sep 26, 2023
commit d0710a703da581f20bb5f768a508e25d3b6df573
33 changes: 31 additions & 2 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 Down Expand Up @@ -103,7 +108,31 @@ export const AppLink: FC<AppLinkProps> = ({ app, workspace, agent }) => {
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.
window.location.href = href;

// 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);
if (hasMagicToken !== -1) {
setFetchingSessionToken(true);
getApiKey()
.then((key) => {
const url = href.replaceAll(
magicTokenString,
key.key,
);
window.location.href = url;
setFetchingSessionToken(false);
})
.catch((ex) => {
console.error(ex);
setFetchingSessionToken(false);
});
Copy link
Member

Choose a reason for hiding this comment

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

you could just make this event handler async and then await these

Copy link
Member Author

Choose a reason for hiding this comment

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

I suppose that's the same as console.error really, will change

} else {
window.location.href = href;
}
} else {
window.open(
href,
Expand Down