mirror of
https://github.com/outline/outline.git
synced 2026-11-28 16:19:46 +03:00
* Run passkey login inside the desktop app when supported Desktop apps that configure a platform authenticator expose isPasskeySupported on the bridge. When available, the WebAuthn ceremony runs in the app instead of the system browser. If it fails, the user can still use a passkey from their browser. * Name passkeys registered in the desktop app The Chromium Touch ID authenticator reports the same AAGUID as any Chromium browser, so passkeys from the desktop app were named "Chromium Browser". * Allow more than one passkey from the Chromium authenticator The Chromium Touch ID authenticator reports the same AAGUID on every device, so users could not register the desktop app on a second Mac. * Disable adding passkeys in desktop apps without passkey support
211 lines
6.0 KiB
TypeScript
211 lines
6.0 KiB
TypeScript
import { startRegistration } from "@simplewebauthn/browser";
|
||
import { errToString } from "@shared/utils/error";
|
||
import type { JSONObject } from "@shared/types";
|
||
import { observer } from "mobx-react";
|
||
import { KeyIcon, PlusIcon } from "outline-icons";
|
||
import * as React from "react";
|
||
import { useTranslation, Trans } from "react-i18next";
|
||
import { toast } from "sonner";
|
||
import Button from "~/components/Button";
|
||
import ConfirmationDialog from "~/components/ConfirmationDialog";
|
||
import Heading from "~/components/Heading";
|
||
import Scene from "~/components/Scene";
|
||
import Text from "~/components/Text";
|
||
import PlaceholderList from "~/components/List/Placeholder";
|
||
import useStores from "~/hooks/useStores";
|
||
import { client } from "~/utils/ApiClient";
|
||
import PasskeyListItem from "./components/PasskeyListItem";
|
||
import RenamePasskeyDialog from "./components/RenamePasskeyDialog";
|
||
import { Action } from "~/components/Actions";
|
||
import usePolicy from "~/hooks/usePolicy";
|
||
import useCurrentTeam from "~/hooks/useCurrentTeam";
|
||
import Notice from "~/components/Notice";
|
||
import DelayedMount from "~/components/DelayedMount";
|
||
import Desktop from "~/utils/Desktop";
|
||
|
||
type Passkey = {
|
||
id: string;
|
||
name: string;
|
||
aaguid: string | null;
|
||
userAgent: string | null;
|
||
lastActiveAt: string | null;
|
||
createdAt: string;
|
||
updatedAt: string;
|
||
};
|
||
|
||
function PasskeysSettings() {
|
||
const { t } = useTranslation();
|
||
const { dialogs } = useStores();
|
||
const [passkeys, setPasskeys] = React.useState<Passkey[]>([]);
|
||
const [isLoading, setIsLoading] = React.useState(true);
|
||
const [isRegistering, setIsRegistering] = React.useState(false);
|
||
const [isSupported, setIsSupported] = React.useState<boolean | undefined>(
|
||
() => (Desktop.isElectron() ? undefined : true)
|
||
);
|
||
|
||
const team = useCurrentTeam();
|
||
const can = usePolicy(team);
|
||
|
||
const loadPasskeys = React.useCallback(async () => {
|
||
try {
|
||
const res = await client.post("/passkeys.list");
|
||
setPasskeys(res.data || []);
|
||
} catch (_err) {
|
||
toast.error(t("Failed to load passkeys"));
|
||
} finally {
|
||
setIsLoading(false);
|
||
}
|
||
}, [t]);
|
||
|
||
React.useEffect(() => {
|
||
void loadPasskeys();
|
||
}, [loadPasskeys]);
|
||
|
||
React.useEffect(() => {
|
||
if (Desktop.isElectron()) {
|
||
void Desktop.isPasskeySupported().then(setIsSupported);
|
||
}
|
||
}, []);
|
||
|
||
const handleRegister = async () => {
|
||
setIsRegistering(true);
|
||
try {
|
||
const resp = await client.post(
|
||
"/passkeys.generateRegistrationOptions",
|
||
undefined,
|
||
{
|
||
baseUrl: "/auth",
|
||
}
|
||
);
|
||
const attResp = await startRegistration(resp.data);
|
||
await client.post(
|
||
"/passkeys.verifyRegistration",
|
||
attResp as unknown as JSONObject,
|
||
{
|
||
baseUrl: "/auth",
|
||
}
|
||
);
|
||
toast.success(t("Passkey added successfully"));
|
||
await loadPasskeys();
|
||
} catch (err) {
|
||
toast.error(
|
||
errToString(err) || t("Failed to register passkey. Please try again.")
|
||
);
|
||
} finally {
|
||
setIsRegistering(false);
|
||
}
|
||
};
|
||
|
||
const handleRename = (passkeyId: string, currentName: string | null) => {
|
||
dialogs.openModal({
|
||
title: t("Rename passkey"),
|
||
content: (
|
||
<RenamePasskeyDialog
|
||
passkeyId={passkeyId}
|
||
currentName={currentName}
|
||
onSuccess={async () => {
|
||
await loadPasskeys();
|
||
dialogs.closeAllModals();
|
||
}}
|
||
/>
|
||
),
|
||
});
|
||
};
|
||
|
||
const handleDelete = (passkeyId: string) => {
|
||
dialogs.openModal({
|
||
title: t("Delete passkey"),
|
||
content: (
|
||
<ConfirmationDialog
|
||
onSubmit={async () => {
|
||
try {
|
||
await client.post("/passkeys.delete", { id: passkeyId });
|
||
toast.success(t("Passkey deleted successfully"));
|
||
await loadPasskeys();
|
||
} catch (err) {
|
||
toast.error(
|
||
errToString(err) ||
|
||
t("Failed to delete passkey. Please try again.")
|
||
);
|
||
}
|
||
}}
|
||
savingText={`${t("Deleting")}…`}
|
||
danger
|
||
>
|
||
<Trans>
|
||
Are you sure you want to delete this passkey? You will no longer be
|
||
able to use it to sign in.
|
||
</Trans>
|
||
</ConfirmationDialog>
|
||
),
|
||
});
|
||
};
|
||
|
||
return (
|
||
<Scene
|
||
title={t("Passkeys")}
|
||
icon={<KeyIcon />}
|
||
actions={
|
||
<Action>
|
||
<Button
|
||
onClick={handleRegister}
|
||
disabled={isRegistering || !isSupported || !can.createUserPasskey}
|
||
icon={<PlusIcon />}
|
||
>
|
||
{isRegistering ? `${t("Registering")}…` : `${t("Add passkey")}…`}
|
||
</Button>
|
||
</Action>
|
||
}
|
||
>
|
||
<Heading>{t("Passkeys")}</Heading>
|
||
<Text as="p" type="secondary">
|
||
<Trans>
|
||
Passkeys allow you to sign in safely without a password using your
|
||
device’s biometric authentication (Face ID, Touch ID, Windows Hello)
|
||
or security key.
|
||
</Trans>
|
||
</Text>
|
||
|
||
{team.passkeysEnabled === false && (
|
||
<Notice>
|
||
{t("Passkey sign-in is currently disabled for this workspace.")}{" "}
|
||
{can.update
|
||
? t("Enable for all users in Settings → Authentication.")
|
||
: t("Contact a workspace admin to enable it.")}
|
||
</Notice>
|
||
)}
|
||
|
||
{isSupported === false && (
|
||
<Notice>
|
||
{t(
|
||
"Passkeys cannot be added in the desktop app on this device. Use a web browser to add a passkey."
|
||
)}
|
||
</Notice>
|
||
)}
|
||
|
||
{isLoading ? (
|
||
<DelayedMount>
|
||
<PlaceholderList count={5} />
|
||
</DelayedMount>
|
||
) : passkeys.length > 0 ? (
|
||
<>
|
||
{passkeys.map((pk) => (
|
||
<PasskeyListItem
|
||
key={pk.id}
|
||
passkey={pk}
|
||
onRename={() => handleRename(pk.id, pk.name)}
|
||
onDelete={() => handleDelete(pk.id)}
|
||
/>
|
||
))}
|
||
</>
|
||
) : (
|
||
<Text as="p" type="secondary">
|
||
{t("You don’t have any passkeys yet.")}
|
||
</Text>
|
||
)}
|
||
</Scene>
|
||
);
|
||
}
|
||
|
||
export default observer(PasskeysSettings);
|