Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions packages/teleport/src/AppLauncher/AppLauncher.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

import React from 'react';

import { AppLauncher } from './AppLauncher';
import { AppLauncherAccessDenied, AppLauncherProcessing } from './AppLauncher';

export default {
title: 'Teleport/AppLauncher',
};

export const Processing = () => {
return <AppLauncher status="processing" statusText="" />;
return <AppLauncherProcessing />;
};

export const Failed = () => {
return <AppLauncher status="failed" statusText="" />;
return <AppLauncherAccessDenied statusText="" />;
};
86 changes: 77 additions & 9 deletions packages/teleport/src/AppLauncher/AppLauncher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,93 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import React from 'react';
import React, { useCallback, useEffect } from 'react';

import { useLocation, useParams } from 'react-router';

import { Flex, Indicator } from 'design';

import { AccessDenied } from 'design/CardError';

import useAppLauncher from './useAppLauncher';
import useAttempt from 'shared/hooks/useAttemptNext';

export default function Container() {
const state = useAppLauncher();
return <AppLauncher {...state} />;
}
import { UrlLauncherParams } from 'teleport/config';
import service from 'teleport/services/apps';

export function AppLauncher() {
const { attempt, setAttempt } = useAttempt('processing');

const params = useParams<UrlLauncherParams>();
const { search } = useLocation();
const queryParams = new URLSearchParams(search);

const createAppSession = useCallback(async (params: UrlLauncherParams) => {
try {
let fqdn = params.fqdn;
if (!fqdn) {
const app = await service.getAppFqdn(params);

fqdn = app.fqdn;
}

const port = location.port ? `:${location.port}` : '';
const session = await service.createAppSession(params);

await fetch(`https://${fqdn}${port}/x-teleport-auth`, {
method: 'POST',
credentials: 'include',
headers: {
'X-Cookie-Value': session.cookieValue,
'X-Subject-Cookie-Value': session.subjectCookieValue,
},
});

let path = '';
if (queryParams.has('path')) {
path = decodeURIComponent(queryParams.get('path'));

export function AppLauncher(props: ReturnType<typeof useAppLauncher>) {
if (props.status === 'failed') {
return <AccessDenied message={props.statusText} />;
if (!path.startsWith('/')) {
path = `/${path}`;
}
}

window.location.replace(`https://${fqdn}${port}${path}`);
} catch (err) {
let statusText = 'Something went wrong';
if (err instanceof Error) {
statusText = err.message;
}

setAttempt({
status: 'failed',
statusText,
});
}
}, []);

useEffect(() => {
createAppSession(params);
}, [params]);

if (attempt.status === 'failed') {
return <AppLauncherAccessDenied statusText={attempt.statusText} />;
}

return <AppLauncherProcessing />;
}

export function AppLauncherProcessing() {
return (
<Flex height="180px" justifyContent="center" alignItems="center" flex="1">
<Indicator />
</Flex>
);
}

interface AppLauncherAccessDeniedProps {
statusText: string;
}

export function AppLauncherAccessDenied(props: AppLauncherAccessDeniedProps) {
return <AccessDenied message={props.statusText} />;
}
3 changes: 1 addition & 2 deletions packages/teleport/src/AppLauncher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import AppLauncher from './AppLauncher';
export default AppLauncher;
export { AppLauncher as default } from './AppLauncher';
90 changes: 0 additions & 90 deletions packages/teleport/src/AppLauncher/useAppLauncher.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/teleport/src/services/apps/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const service = {
fqdn: json.fqdn as string,
value: json.value as string,
subject: json.subject as string,
cookieValue: json.cookie_value as string,
subjectCookieValue: json.subject_cookie_value as string,
}));
},

Expand Down