Skip to content

Commit

Permalink
Fix Per-session MFA for desktops (#50793)
Browse files Browse the repository at this point in the history
* Add sendChallengeResponse implementation for desktop sessions.

* Rename useMfaTty to useMfaEmitter.
  • Loading branch information
Joerger authored Jan 13, 2025
1 parent 646329d commit b6e2bad
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions web/packages/teleport/src/Console/DocumentDb/DocumentDb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import AuthnDialog from 'teleport/components/AuthnDialog';
import Document from 'teleport/Console/Document';
import { Terminal, TerminalRef } from 'teleport/Console/DocumentSsh/Terminal';
import * as stores from 'teleport/Console/stores/types';
import { useMfaTty } from 'teleport/lib/useMfa';
import { useMfaEmitter } from 'teleport/lib/useMfa';

import { ConnectDialog } from './ConnectDialog';
import { useDbSession } from './useDbSession';
Expand All @@ -37,7 +37,7 @@ type Props = {
export function DocumentDb({ doc, visible }: Props) {
const terminalRef = useRef<TerminalRef>();
const { tty, status, closeDocument, sendDbConnectData } = useDbSession(doc);
const mfa = useMfaTty(tty);
const mfa = useMfaEmitter(tty);
useEffect(() => {
// when switching tabs or closing tabs, focus on visible terminal
terminalRef.current?.focus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Document from 'teleport/Console/Document';
import useKubeExecSession from 'teleport/Console/DocumentKubeExec/useKubeExecSession';
import { Terminal, TerminalRef } from 'teleport/Console/DocumentSsh/Terminal';
import * as stores from 'teleport/Console/stores/types';
import { useMfaTty } from 'teleport/lib/useMfa';
import { useMfaEmitter } from 'teleport/lib/useMfa';

import KubeExecData from './KubeExecDataDialog';

Expand All @@ -38,7 +38,7 @@ export default function DocumentKubeExec({ doc, visible }: Props) {
const terminalRef = useRef<TerminalRef>();
const { tty, status, closeDocument, sendKubeExecData } =
useKubeExecSession(doc);
const mfa = useMfaTty(tty);
const mfa = useMfaEmitter(tty);
useEffect(() => {
// when switching tabs or closing tabs, focus on visible terminal
terminalRef.current?.focus();
Expand Down
4 changes: 2 additions & 2 deletions web/packages/teleport/src/Console/DocumentSsh/DocumentSsh.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { TerminalSearch } from 'shared/components/TerminalSearch';

import AuthnDialog from 'teleport/components/AuthnDialog';
import * as stores from 'teleport/Console/stores';
import { useMfa, useMfaTty } from 'teleport/lib/useMfa';
import { useMfa, useMfaEmitter } from 'teleport/lib/useMfa';
import { MfaChallengeScope } from 'teleport/services/auth/auth';

import { useConsoleContext } from '../consoleContextProvider';
Expand All @@ -54,7 +54,7 @@ function DocumentSsh({ doc, visible }: PropTypes) {
const { tty, status, closeDocument, session } = useSshSession(doc);
const [showSearch, setShowSearch] = useState(false);

const ttyMfa = useMfaTty(tty);
const ttyMfa = useMfaEmitter(tty);
const ftMfa = useMfa({
isMfaRequired: ttyMfa.required,
req: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import useAttempt from 'shared/hooks/useAttemptNext';

import type { UrlDesktopParams } from 'teleport/config';
import { ButtonState } from 'teleport/lib/tdp';
import { useMfaTty } from 'teleport/lib/useMfa';
import { useMfaEmitter } from 'teleport/lib/useMfa';
import desktopService from 'teleport/services/desktops';
import userService from 'teleport/services/user';

Expand Down Expand Up @@ -129,7 +129,7 @@ export default function useDesktopSession() {
});
const tdpClient = clientCanvasProps.tdpClient;

const mfa = useMfaTty(tdpClient);
const mfa = useMfaEmitter(tdpClient);

const onShareDirectory = () => {
try {
Expand Down
9 changes: 9 additions & 0 deletions web/packages/teleport/src/lib/tdp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import init, {
import { AuthenticatedWebSocket } from 'teleport/lib/AuthenticatedWebSocket';
import { EventEmitterMfaSender } from 'teleport/lib/EventEmitterMfaSender';
import { TermEvent, WebsocketCloseCode } from 'teleport/lib/term/enums';
import { MfaChallengeResponse } from 'teleport/services/mfa';

import Codec, {
FileType,
Expand Down Expand Up @@ -619,6 +620,14 @@ export default class Client extends EventEmitterMfaSender {
this.send(this.codec.encodeClipboardData(clipboardData));
}

sendChallengeResponse(data: MfaChallengeResponse) {
const msg = this.codec.encodeMfaJson({
mfaType: 'n',
jsonString: JSON.stringify(data),
});
this.send(msg);
}

addSharedDirectory(sharedDirectory: FileSystemDirectoryHandle) {
try {
this.sdManager.add(sharedDirectory);
Expand Down
2 changes: 1 addition & 1 deletion web/packages/teleport/src/lib/useMfa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export function useMfa({ req, isMfaRequired }: MfaProps): MfaState {
};
}

export function useMfaTty(emitterSender: EventEmitterMfaSender): MfaState {
export function useMfaEmitter(emitterSender: EventEmitterMfaSender): MfaState {
const [mfaRequired, setMfaRequired] = useState(false);

const mfa = useMfa({ isMfaRequired: mfaRequired });
Expand Down

0 comments on commit b6e2bad

Please sign in to comment.