Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import './style.scss';
import { Enter } from '@fluentui/keyboard-keys';
import { useMutation } from '@tanstack/react-query';
import { listen } from '@tauri-apps/api/event';
import { hostname } from '@tauri-apps/plugin-os';
import { Fragment, useCallback, useEffect, useState } from 'react';
import { Fragment, useCallback, useState } from 'react';
import { Button } from '../../../../../shared/components/Button/Button';
import { ButtonVariant } from '../../../../../shared/components/Button/types';
import { Controls } from '../../../../../shared/components/Controls/Controls';
Expand All @@ -22,15 +21,6 @@ import {
} from '../../hooks/useConfigureMfaStore';
import { useMfaConfigErrorHandler } from '../../hooks/useMfaConfigErrorHandler';

const DEFAULT_KEY_NAME = 'Security key';

/** A second key registered from the same machine would otherwise propose the name the first one
* took, leaving two entries the user cannot tell apart in Defguard. */
const defaultKeyName = (host: string | null | undefined, hasKey: boolean): string => {
const base = isPresent(host) ? `${host} key` : DEFAULT_KEY_NAME;
return hasKey ? `${base} (${new Date().toISOString().slice(0, 10)})` : base;
};

interface Props {
onCancel: () => void;
/** The session outlived its deadline, so the whole flow has to start over. */
Expand All @@ -40,25 +30,13 @@ interface Props {
/** Registers a security key, offered however many the account already holds. */
export const ConfigureFido2Step = ({ onCancel, onSessionExpired }: Props) => {
const sessionId = useConfigureMfaStore((s) => s.sessionId);
// The snapshot is frozen at the start of the session, so this still reads false on the first
// key even after it is registered.
const hasKeyAlready = useConfigureMfaStore((s) =>
s.configuredMethods.includes(MfaMethod.Fido2),
);

const [name, setName] = useState<string | null>(null);
const [pin, setPin] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const [isAwaitingTouch, setIsAwaitingTouch] = useState(false);
const collectsPin = fido2CollectsPinInApp();

// The machine's own name is what the user will recognize the key by in Defguard.
useEffect(() => {
void hostname().then((host) => {
setName((current) => current ?? defaultKeyName(host, hasKeyAlready));
});
}, [hasKeyAlready]);

const handleApiError = useMfaConfigErrorHandler({
context: 'Security key registration failed',
setError,
Expand Down
Loading