diff --git a/src/lib/api/gatekeeper.ts b/src/lib/api/gatekeeper.ts index 7eee2ed54..b0406bf72 100644 --- a/src/lib/api/gatekeeper.ts +++ b/src/lib/api/gatekeeper.ts @@ -95,3 +95,49 @@ export async function gateDeactivateAccount(params: { password: params.password, }) } + +export async function gateListAppPasswords(params: { + serviceUrl: string + did: string + password: string +}): Promise<{ + passwords: {name: string; createdAt: string; privileged?: boolean}[] +}> { + return await gatekeeperPost(params.serviceUrl, '/gate/list-app-passwords', { + did: params.did, + password: params.password, + }) +} + +export async function gateCreateAppPassword(params: { + serviceUrl: string + did: string + password: string + name: string + privileged?: boolean +}): Promise<{ + name: string + password: string + createdAt: string + privileged?: boolean +}> { + return await gatekeeperPost(params.serviceUrl, '/gate/create-app-password', { + did: params.did, + password: params.password, + name: params.name, + privileged: params.privileged, + }) +} + +export async function gateRevokeAppPassword(params: { + serviceUrl: string + did: string + password: string + name: string +}): Promise { + await gatekeeperPost(params.serviceUrl, '/gate/revoke-app-password', { + did: params.did, + password: params.password, + name: params.name, + }) +} diff --git a/src/screens/Settings/AppPasswords.tsx b/src/screens/Settings/AppPasswords.tsx index 89eb9b949..2c83f85e7 100644 --- a/src/screens/Settings/AppPasswords.tsx +++ b/src/screens/Settings/AppPasswords.tsx @@ -1,4 +1,4 @@ -import {useCallback} from 'react' +import React, {useCallback, useState} from 'react' import {View} from 'react-native' import Animated, { FadeIn, @@ -11,12 +11,15 @@ import {msg, Trans} from '@lingui/macro' import {useLingui} from '@lingui/react' import {type NativeStackScreenProps} from '@react-navigation/native-stack' +import {useIsBlackskyPds} from '#/lib/hooks/useIsBlackskyPds' import {type CommonNavigatorParams} from '#/lib/routes/types' import {cleanError} from '#/lib/strings/errors' import { + type GatekeeperConfig, useAppPasswordDeleteMutation, useAppPasswordsQuery, } from '#/state/queries/app-passwords' +import {useSession} from '#/state/session' import {EmptyState} from '#/view/com/util/EmptyState' import {ErrorScreen} from '#/view/com/util/error/ErrorScreen' import * as Toast from '#/view/com/util/Toast' @@ -24,7 +27,9 @@ import {atoms as a, useTheme} from '#/alf' import {Admonition, colors} from '#/components/Admonition' import {Button, ButtonIcon, ButtonText} from '#/components/Button' import {useDialogControl} from '#/components/Dialog' +import * as TextField from '#/components/forms/TextField' import {Growth_Stroke2_Corner0_Rounded as Growth} from '#/components/icons/Growth' +import {Lock_Stroke2_Corner2_Rounded as Lock} from '#/components/icons/Lock' import {PlusLarge_Stroke2_Corner0_Rounded as PlusIcon} from '#/components/icons/Plus' import {Trash_Stroke2_Corner0_Rounded as TrashIcon} from '#/components/icons/Trash' import {Warning_Stroke2_Corner0_Rounded as WarningIcon} from '#/components/icons/Warning' @@ -38,9 +43,94 @@ import * as SettingsList from './components/SettingsList' type Props = NativeStackScreenProps export function AppPasswordsScreen({}: Props) { const {_} = useLingui() - const {data: appPasswords, error} = useAppPasswordsQuery() + const {currentAccount} = useSession() + const isOauth = currentAccount?.isOauthSession === true + const isBskyPds = useIsBlackskyPds() + const useGatekeeper = isOauth && isBskyPds + + const [gatekeeperPassword, setGatekeeperPassword] = useState('') + const [isAuthenticated, setIsAuthenticated] = useState(false) + const [authError, setAuthError] = useState() + + const gatekeeper: GatekeeperConfig | undefined = + useGatekeeper && isAuthenticated && currentAccount + ? { + serviceUrl: currentAccount.service, + did: currentAccount.did, + password: gatekeeperPassword, + } + : undefined + + const {data: appPasswords, error} = useAppPasswordsQuery( + useGatekeeper ? gatekeeper : undefined, + ) + const createAppPasswordControl = useDialogControl() + const handleAuthenticate = useCallback(() => { + if (!gatekeeperPassword) { + setAuthError(_(msg`Please enter your password.`)) + return + } + setAuthError(undefined) + setIsAuthenticated(true) + }, [gatekeeperPassword, _]) + + if (useGatekeeper && !isAuthenticated) { + return ( + + + + + + App Passwords + + + + + + + + + + + Please enter your account password to manage app passwords. + + + + + + + {authError && {authError}} + + + + + + + ) + } + return ( @@ -96,7 +186,10 @@ export function AppPasswordsScreen({}: Props) { exiting={FadeOut} layout={LinearTransition.delay(150)}> - + ))} @@ -126,6 +219,7 @@ export function AppPasswordsScreen({}: Props) { p.name) || []} + gatekeeper={gatekeeper} /> ) @@ -133,13 +227,15 @@ export function AppPasswordsScreen({}: Props) { function AppPasswordCard({ appPassword, + gatekeeper, }: { appPassword: ComAtprotoServerListAppPasswords.AppPassword + gatekeeper?: GatekeeperConfig }) { const t = useTheme() const {i18n, _} = useLingui() const deleteControl = Prompt.usePromptControl() - const {mutateAsync: deleteMutation} = useAppPasswordDeleteMutation() + const {mutateAsync: deleteMutation} = useAppPasswordDeleteMutation(gatekeeper) const onDelete = useCallback(async () => { await deleteMutation({name: appPassword.name}) @@ -208,7 +304,7 @@ function AppPasswordCard({ description={_( msg`Are you sure you want to delete the app password "${appPassword.name}"?`, )} - onConfirm={onDelete} + onConfirm={() => void onDelete()} confirmButtonCta={_(msg`Delete`)} confirmButtonColor="negative" /> diff --git a/src/screens/Settings/components/AddAppPasswordDialog.tsx b/src/screens/Settings/components/AddAppPasswordDialog.tsx index 64aa8bafc..5df4890f3 100644 --- a/src/screens/Settings/components/AddAppPasswordDialog.tsx +++ b/src/screens/Settings/components/AddAppPasswordDialog.tsx @@ -13,7 +13,10 @@ import {msg, Trans} from '@lingui/macro' import {useLingui} from '@lingui/react' import {useMutation} from '@tanstack/react-query' -import {useAppPasswordCreateMutation} from '#/state/queries/app-passwords' +import { + type GatekeeperConfig, + useAppPasswordCreateMutation, +} from '#/state/queries/app-passwords' import {atoms as a, native, useTheme} from '#/alf' import {Admonition} from '#/components/Admonition' import {Button, ButtonIcon, ButtonText} from '#/components/Button' @@ -29,20 +32,28 @@ import {CopyButton} from './CopyButton' export function AddAppPasswordDialog({ control, passwords, + gatekeeper, }: { control: Dialog.DialogControlProps passwords: string[] + gatekeeper?: GatekeeperConfig }) { const {height} = useWindowDimensions() return ( - + ) } -function CreateDialogInner({passwords}: {passwords: string[]}) { +function CreateDialogInner({ + passwords, + gatekeeper, +}: { + passwords: string[] + gatekeeper?: GatekeeperConfig +}) { const control = Dialog.useDialogContext() const t = useTheme() const {_} = useLingui() @@ -53,7 +64,7 @@ function CreateDialogInner({passwords}: {passwords: string[]}) { mutateAsync: actuallyCreateAppPassword, error: apiError, data, - } = useAppPasswordCreateMutation() + } = useAppPasswordCreateMutation(gatekeeper) const regexFailError = useMemo( () => diff --git a/src/state/queries/app-passwords.ts b/src/state/queries/app-passwords.ts index 12d66dd2c..e04b8fb89 100644 --- a/src/state/queries/app-passwords.ts +++ b/src/state/queries/app-passwords.ts @@ -1,25 +1,45 @@ import {type ComAtprotoServerCreateAppPassword} from '@atproto/api' import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query' +import { + gateCreateAppPassword, + gateListAppPasswords, + gateRevokeAppPassword, +} from '#/lib/api/gatekeeper' import {STALE} from '#/state/queries' import {useAgent} from '../session' const RQKEY_ROOT = 'app-passwords' export const RQKEY = () => [RQKEY_ROOT] -export function useAppPasswordsQuery() { +export type GatekeeperConfig = { + serviceUrl: string + did: string + password: string +} + +export function useAppPasswordsQuery(gatekeeper?: GatekeeperConfig) { const agent = useAgent() return useQuery({ staleTime: STALE.MINUTES.FIVE, - queryKey: RQKEY(), + queryKey: [...RQKEY(), gatekeeper ? 'gate' : 'direct'], queryFn: async () => { + if (gatekeeper) { + const res = await gateListAppPasswords({ + serviceUrl: gatekeeper.serviceUrl, + did: gatekeeper.did, + password: gatekeeper.password, + }) + return res.passwords + } const res = await agent.com.atproto.server.listAppPasswords({}) return res.data.passwords }, + enabled: gatekeeper ? !!gatekeeper.password : true, }) } -export function useAppPasswordCreateMutation() { +export function useAppPasswordCreateMutation(gatekeeper?: GatekeeperConfig) { const queryClient = useQueryClient() const agent = useAgent() return useMutation< @@ -28,6 +48,15 @@ export function useAppPasswordCreateMutation() { {name: string; privileged: boolean} >({ mutationFn: async ({name, privileged}) => { + if (gatekeeper) { + return await gateCreateAppPassword({ + serviceUrl: gatekeeper.serviceUrl, + did: gatekeeper.did, + password: gatekeeper.password, + name, + privileged, + }) + } return ( await agent.com.atproto.server.createAppPassword({ name, @@ -36,24 +65,33 @@ export function useAppPasswordCreateMutation() { ).data }, onSuccess() { - queryClient.invalidateQueries({ + void queryClient.invalidateQueries({ queryKey: RQKEY(), }) }, }) } -export function useAppPasswordDeleteMutation() { +export function useAppPasswordDeleteMutation(gatekeeper?: GatekeeperConfig) { const queryClient = useQueryClient() const agent = useAgent() return useMutation({ mutationFn: async ({name}) => { + if (gatekeeper) { + await gateRevokeAppPassword({ + serviceUrl: gatekeeper.serviceUrl, + did: gatekeeper.did, + password: gatekeeper.password, + name, + }) + return + } await agent.com.atproto.server.revokeAppPassword({ name, }) }, onSuccess() { - queryClient.invalidateQueries({ + void queryClient.invalidateQueries({ queryKey: RQKEY(), }) },