- Notifications
You must be signed in to change notification settings - Fork928
feat: add user settings page for revoking OAuth2 apps#11780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
8 changes: 8 additions & 0 deletionssite/src/AppRouter.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
16 changes: 12 additions & 4 deletionssite/src/api/api.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
22 changes: 17 additions & 5 deletionssite/src/api/queries/oauth2.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletionssite/src/pages/UserSettingsPage/OAuth2ProviderPage/OAuth2ProviderPage.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { type FC, useState } from "react"; | ||
import { useMutation, useQuery, useQueryClient } from "react-query"; | ||
import { getErrorMessage } from "api/errors"; | ||
import { getApps, revokeApp } from "api/queries/oauth2"; | ||
import { DeleteDialog } from "components/Dialogs/DeleteDialog/DeleteDialog"; | ||
import { displayError, displaySuccess } from "components/GlobalSnackbar/utils"; | ||
import { useMe } from "contexts/auth/useMe"; | ||
import { Section } from "../Section"; | ||
import OAuth2ProviderPageView from "./OAuth2ProviderPageView"; | ||
const OAuth2ProviderPage: FC = () => { | ||
const me = useMe(); | ||
const queryClient = useQueryClient(); | ||
const userOAuth2AppsQuery = useQuery(getApps(me.id)); | ||
const revokeAppMutation = useMutation(revokeApp(queryClient, me.id)); | ||
const [appIdToRevoke, setAppIdToRevoke] = useState<string>(); | ||
const appToRevoke = userOAuth2AppsQuery.data?.find( | ||
(app) => app.id === appIdToRevoke, | ||
); | ||
// This can happen if the app disappears from the query data but a user has | ||
// already started the revoke flow. It is safe to place this directly in the | ||
// render, because it does not run every single time. | ||
if (appToRevoke === undefined && typeof appIdToRevoke === "string") { | ||
setAppIdToRevoke(undefined); | ||
displayError("Application no longer exists."); | ||
} | ||
return ( | ||
<Section title="OAuth2 Applications" layout="fluid"> | ||
<OAuth2ProviderPageView | ||
isLoading={userOAuth2AppsQuery.isLoading} | ||
error={userOAuth2AppsQuery.error} | ||
apps={userOAuth2AppsQuery.data} | ||
revoke={(app) => { | ||
setAppIdToRevoke(app.id); | ||
}} | ||
/> | ||
{appToRevoke !== undefined && ( | ||
<DeleteDialog | ||
title="Revoke Application" | ||
verb="Revoking" | ||
info={`This will invalidate any tokens created by the OAuth2 application "${appToRevoke.name}".`} | ||
label="Name of the application to revoke" | ||
isOpen | ||
confirmLoading={revokeAppMutation.isLoading} | ||
name={appToRevoke.name} | ||
entity="application" | ||
onCancel={() => setAppIdToRevoke(undefined)} | ||
onConfirm={async () => { | ||
try { | ||
await revokeAppMutation.mutateAsync(appToRevoke.id); | ||
displaySuccess( | ||
`You have successfully revoked the OAuth2 application "${appToRevoke.name}"`, | ||
); | ||
setAppIdToRevoke(undefined); | ||
} catch (error) { | ||
displayError( | ||
getErrorMessage(error, "Failed to revoke application."), | ||
); | ||
} | ||
}} | ||
/> | ||
)} | ||
</Section> | ||
); | ||
}; | ||
export default OAuth2ProviderPage; |
34 changes: 34 additions & 0 deletionssite/src/pages/UserSettingsPage/OAuth2ProviderPage/OAuth2ProviderPageView.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { MockOAuth2ProviderApps } from "testHelpers/entities"; | ||
import OAuth2ProviderPageView from "./OAuth2ProviderPageView"; | ||
const meta: Meta<typeof OAuth2ProviderPageView> = { | ||
title: "pages/UserSettingsPage/OAuth2ProviderPageView", | ||
component: OAuth2ProviderPageView, | ||
}; | ||
export default meta; | ||
type Story = StoryObj<typeof OAuth2ProviderPageView>; | ||
export const Loading: Story = { | ||
args: { | ||
isLoading: true, | ||
revoke: () => undefined, | ||
}, | ||
}; | ||
export const Error: Story = { | ||
args: { | ||
isLoading: false, | ||
error: "some error", | ||
revoke: () => undefined, | ||
}, | ||
}; | ||
export const Apps: Story = { | ||
args: { | ||
isLoading: false, | ||
apps: MockOAuth2ProviderApps, | ||
revoke: () => undefined, | ||
}, | ||
}; |
94 changes: 94 additions & 0 deletionssite/src/pages/UserSettingsPage/OAuth2ProviderPage/OAuth2ProviderPageView.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import Button from "@mui/material/Button"; | ||
import Table from "@mui/material/Table"; | ||
import TableBody from "@mui/material/TableBody"; | ||
import TableCell from "@mui/material/TableCell"; | ||
import TableContainer from "@mui/material/TableContainer"; | ||
import TableHead from "@mui/material/TableHead"; | ||
import TableRow from "@mui/material/TableRow"; | ||
import { type FC } from "react"; | ||
import type * as TypesGen from "api/typesGenerated"; | ||
import { AvatarData } from "components/AvatarData/AvatarData"; | ||
import { Avatar } from "components/Avatar/Avatar"; | ||
import { ErrorAlert } from "components/Alert/ErrorAlert"; | ||
import { TableLoader } from "components/TableLoader/TableLoader"; | ||
export type OAuth2ProviderPageViewProps = { | ||
isLoading: boolean; | ||
error?: unknown; | ||
apps?: TypesGen.OAuth2ProviderApp[]; | ||
revoke: (app: TypesGen.OAuth2ProviderApp) => void; | ||
}; | ||
const OAuth2ProviderPageView: FC<OAuth2ProviderPageViewProps> = ({ | ||
isLoading, | ||
error, | ||
apps, | ||
revoke, | ||
}) => { | ||
return ( | ||
<> | ||
{error && <ErrorAlert error={error} />} | ||
<TableContainer> | ||
<Table> | ||
<TableHead> | ||
<TableRow> | ||
<TableCell width="100%">Name</TableCell> | ||
<TableCell width="1%" /> | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{isLoading && <TableLoader />} | ||
{apps?.map((app) => ( | ||
<OAuth2AppRow key={app.id} app={app} revoke={revoke} /> | ||
))} | ||
{apps?.length === 0 && ( | ||
<TableRow> | ||
<TableCell colSpan={999}> | ||
<div css={{ textAlign: "center" }}> | ||
No OAuth2 applications have been authorized. | ||
</div> | ||
</TableCell> | ||
</TableRow> | ||
)} | ||
</TableBody> | ||
</Table> | ||
</TableContainer> | ||
</> | ||
); | ||
}; | ||
type OAuth2AppRowProps = { | ||
app: TypesGen.OAuth2ProviderApp; | ||
revoke: (app: TypesGen.OAuth2ProviderApp) => void; | ||
}; | ||
const OAuth2AppRow: FC<OAuth2AppRowProps> = ({ app, revoke }) => { | ||
return ( | ||
<TableRow key={app.id} data-testid={`app-${app.id}`}> | ||
<TableCell> | ||
<AvatarData | ||
title={app.name} | ||
avatar={ | ||
Boolean(app.icon) && ( | ||
<Avatar src={app.icon} variant="square" fitImage /> | ||
) | ||
} | ||
/> | ||
</TableCell> | ||
<TableCell> | ||
<Button | ||
variant="contained" | ||
size="small" | ||
color="error" | ||
onClick={() => revoke(app)} | ||
> | ||
Revoke… | ||
</Button> | ||
</TableCell> | ||
</TableRow> | ||
); | ||
}; | ||
export default OAuth2ProviderPageView; |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.