-
Notifications
You must be signed in to change notification settings - Fork 894
feat: add OAuth2 user settings page #12237
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
678f900
Add user settings page for revoking OAuth2 apps
code-asher 8180890
Update revoke endpoint
code-asher fd1e443
Remove deleted app check
code-asher 12e1400
Update revoke endpoint again
code-asher 43e777a
Make error required
code-asher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add user settings page for revoking OAuth2 apps
- Loading branch information
commit 678f900d0cbd37a5f304aa46abffcfa2f9db04cb
There are no files selected for viewing
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
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
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 deletions
69
site/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 deletions
34
site/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 deletions
94
site/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.
Uh oh!
There was an error while loading. Please reload this page.