Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite3beb7d

Browse files
committed
Add user settings page for revoking OAuth2 apps
1 parent32afbd6 commite3beb7d

File tree

7 files changed

+242
-9
lines changed

7 files changed

+242
-9
lines changed

‎site/src/AppRouter.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ const ExternalAuthPage = lazy(
156156
constUserExternalAuthSettingsPage=lazy(
157157
()=>import("./pages/UserSettingsPage/ExternalAuthPage/ExternalAuthPage"),
158158
);
159+
constUserOAuth2ProviderSettingsPage=lazy(
160+
()=>
161+
import("./pages/UserSettingsPage/OAuth2ProviderPage/OAuth2ProviderPage"),
162+
);
159163
constTemplateVersionPage=lazy(
160164
()=>import("./pages/TemplateVersionPage/TemplateVersionPage"),
161165
);
@@ -362,6 +366,10 @@ export const AppRouter: FC = () => {
362366
path="external-auth"
363367
element={<UserExternalAuthSettingsPage/>}
364368
/>
369+
<Route
370+
path="oauth2-provider"
371+
element={<UserOAuth2ProviderSettingsPage/>}
372+
/>
365373
<Routepath="tokens">
366374
<Routeindexelement={<TokensPage/>}/>
367375
<Routepath="new"element={<CreateTokenPage/>}/>

‎site/src/api/api.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -967,10 +967,13 @@ export const unlinkExternalAuthProvider = async (
967967
returnresp.data;
968968
};
969969

970-
exportconstgetOAuth2ProviderApps=async():Promise<
971-
TypesGen.OAuth2ProviderApp[]
972-
>=>{
973-
constresp=awaitaxios.get(`/api/v2/oauth2-provider/apps`);
970+
exportconstgetOAuth2ProviderApps=async(
971+
filter?:TypesGen.OAuth2ProviderAppFilter,
972+
):Promise<TypesGen.OAuth2ProviderApp[]>=>{
973+
constparams=filter?.user_id
974+
?newURLSearchParams({user_id:filter.user_id})
975+
:"";
976+
constresp=awaitaxios.get(`/api/v2/oauth2-provider/apps?${params}`);
974977
returnresp.data;
975978
};
976979

@@ -995,6 +998,7 @@ export const putOAuth2ProviderApp = async (
995998
constresponse=awaitaxios.put(`/api/v2/oauth2-provider/apps/${id}`,data);
996999
returnresponse.data;
9971000
};
1001+
9981002
exportconstdeleteOAuth2ProviderApp=async(id:string):Promise<void>=>{
9991003
awaitaxios.delete(`/api/v2/oauth2-provider/apps/${id}`);
10001004
};
@@ -1022,6 +1026,10 @@ export const deleteOAuth2ProviderAppSecret = async (
10221026
);
10231027
};
10241028

1029+
exportconstrevokeOAuth2ProviderApp=async(appId:string):Promise<void>=>{
1030+
awaitaxios.delete(`/api/v2/oauth2-provider/apps/${appId}/tokens`);
1031+
};
1032+
10251033
exportconstgetAuditLogs=async(
10261034
options:TypesGen.AuditLogsRequest,
10271035
):Promise<TypesGen.AuditLogResponse>=>{

‎site/src/api/queries/oauth2.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import * as API from "api/api";
33
importtype*asTypesGenfrom"api/typesGenerated";
44

55
constappsKey=["oauth2-provider","apps"];
6-
constappKey=(id:string)=>appsKey.concat(id);
7-
constappSecretsKey=(id:string)=>appKey(id).concat("secrets");
6+
constuserAppsKey=(userId:string)=>appsKey.concat(userId);
7+
constappKey=(appId:string)=>appsKey.concat(appId);
8+
constappSecretsKey=(appId:string)=>appKey(appId).concat("secrets");
89

9-
exportconstgetApps=()=>{
10+
exportconstgetApps=(userId?:string)=>{
1011
return{
11-
queryKey:appsKey,
12-
queryFn:()=>API.getOAuth2ProviderApps(),
12+
queryKey:userId ?appsKey.concat(userId) :appsKey,
13+
queryFn:()=>API.getOAuth2ProviderApps({user_id:userId}),
1314
};
1415
};
1516

@@ -91,3 +92,14 @@ export const deleteAppSecret = (queryClient: QueryClient) => {
9192
},
9293
};
9394
};
95+
96+
exportconstrevokeApp=(queryClient:QueryClient,userId:string)=>{
97+
return{
98+
mutationFn:API.revokeOAuth2ProviderApp,
99+
onSuccess:async()=>{
100+
awaitqueryClient.invalidateQueries({
101+
queryKey:userAppsKey(userId),
102+
});
103+
},
104+
};
105+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import{typeFC,useState}from"react";
2+
import{useMutation,useQuery,useQueryClient}from"react-query";
3+
import{getErrorMessage}from"api/errors";
4+
import{getApps,revokeApp}from"api/queries/oauth2";
5+
importtype*asTypesGenfrom"api/typesGenerated";
6+
import{DeleteDialog}from"components/Dialogs/DeleteDialog/DeleteDialog";
7+
import{displayError,displaySuccess}from"components/GlobalSnackbar/utils";
8+
import{useMe}from"hooks";
9+
import{Section}from"../Section";
10+
importOAuth2ProviderPageViewfrom"./OAuth2ProviderPageView";
11+
12+
constOAuth2ProviderPage:FC=()=>{
13+
constme=useMe();
14+
constqueryClient=useQueryClient();
15+
constuserOAuth2AppsQuery=useQuery(getApps(me.id));
16+
constrevokeAppMutation=useMutation(revokeApp(queryClient,me.id));
17+
const[appIdToRevoke,setAppIdToRevoke]=useState<string>();
18+
constappToRevoke=userOAuth2AppsQuery.data?.find(
19+
(app)=>app.id===appIdToRevoke,
20+
);
21+
22+
// This can happen if the app disappears from the query data but a user has
23+
// already started the revoke flow. It is safe to place this directly in the
24+
// render, because it does not run every single time.
25+
if(appToRevoke===undefined&&typeofappIdToRevoke==="string"){
26+
setAppIdToRevoke(undefined);
27+
displayError("Application no longer exists.");
28+
}
29+
30+
return(
31+
<Sectiontitle="OAuth2 Applications"layout="fluid">
32+
<OAuth2ProviderPageView
33+
isLoading={userOAuth2AppsQuery.isLoading}
34+
error={userOAuth2AppsQuery.error}
35+
apps={userOAuth2AppsQuery.data}
36+
revoke={(app)=>{
37+
setAppIdToRevoke(app.id);
38+
}}
39+
/>
40+
{appToRevoke!==undefined&&(
41+
<DeleteDialog
42+
title="Revoke Application"
43+
verb="Revoking"
44+
info={`This will invalidate any tokens created by the OAuth2 application "${appToRevoke.name}".`}
45+
label="Name of the application to revoke"
46+
isOpen
47+
confirmLoading={revokeAppMutation.isLoading}
48+
name={appToRevoke.name}
49+
entity="application"
50+
onCancel={()=>setAppIdToRevoke(undefined)}
51+
onConfirm={async()=>{
52+
try{
53+
awaitrevokeAppMutation.mutateAsync(appToRevoke.id);
54+
displaySuccess(
55+
`You have successfully revoked the OAuth2 application "${appToRevoke.name}"`,
56+
);
57+
setAppIdToRevoke(undefined);
58+
}catch(error){
59+
displayError(
60+
getErrorMessage(error,"Failed to revoke application."),
61+
);
62+
}
63+
}}
64+
/>
65+
)}
66+
</Section>
67+
);
68+
};
69+
70+
exportdefaultOAuth2ProviderPage;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
importtype{Meta,StoryObj}from"@storybook/react";
2+
import{MockOAuth2ProviderApps}from"testHelpers/entities";
3+
importOAuth2ProviderPageViewfrom"./OAuth2ProviderPageView";
4+
5+
constmeta:Meta<typeofOAuth2ProviderPageView>={
6+
title:"pages/UserSettingsPage/OAuth2ProviderPageView",
7+
component:OAuth2ProviderPageView,
8+
};
9+
10+
exportdefaultmeta;
11+
typeStory=StoryObj<typeofOAuth2ProviderPageView>;
12+
13+
exportconstLoading:Story={
14+
args:{
15+
isLoading:true,
16+
revoke:()=>undefined,
17+
},
18+
};
19+
20+
exportconstError:Story={
21+
args:{
22+
isLoading:false,
23+
error:"some error",
24+
revoke:()=>undefined,
25+
},
26+
};
27+
28+
exportconstApps:Story={
29+
args:{
30+
isLoading:false,
31+
apps:MockOAuth2ProviderApps,
32+
revoke:()=>undefined,
33+
},
34+
};
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
importButtonfrom"@mui/material/Button";
2+
importTablefrom"@mui/material/Table";
3+
importTableBodyfrom"@mui/material/TableBody";
4+
importTableCellfrom"@mui/material/TableCell";
5+
importTableContainerfrom"@mui/material/TableContainer";
6+
importTableHeadfrom"@mui/material/TableHead";
7+
importTableRowfrom"@mui/material/TableRow";
8+
import{typeFC}from"react";
9+
importtype*asTypesGenfrom"api/typesGenerated";
10+
import{AvatarData}from"components/AvatarData/AvatarData";
11+
import{Avatar}from"components/Avatar/Avatar";
12+
import{ErrorAlert}from"components/Alert/ErrorAlert";
13+
import{TableLoader}from"components/TableLoader/TableLoader";
14+
15+
exporttypeOAuth2ProviderPageViewProps={
16+
isLoading:boolean;
17+
error?:unknown;
18+
apps?:TypesGen.OAuth2ProviderApp[];
19+
revoke:(app:TypesGen.OAuth2ProviderApp)=>void;
20+
};
21+
22+
constOAuth2ProviderPageView:FC<OAuth2ProviderPageViewProps>=({
23+
isLoading,
24+
error,
25+
apps,
26+
revoke,
27+
})=>{
28+
return(
29+
<>
30+
{error&&<ErrorAlerterror={error}/>}
31+
32+
<TableContainer>
33+
<Table>
34+
<TableHead>
35+
<TableRow>
36+
<TableCellwidth="100%">Name</TableCell>
37+
<TableCellwidth="1%"/>
38+
</TableRow>
39+
</TableHead>
40+
<TableBody>
41+
{isLoading&&<TableLoader/>}
42+
{apps?.map((app)=>(
43+
<OAuth2AppRowkey={app.id}app={app}revoke={revoke}/>
44+
))}
45+
{apps?.length===0&&(
46+
<TableRow>
47+
<TableCellcolSpan={999}>
48+
<divcss={{textAlign:"center"}}>
49+
No OAuth2 applications have been authorized.
50+
</div>
51+
</TableCell>
52+
</TableRow>
53+
)}
54+
</TableBody>
55+
</Table>
56+
</TableContainer>
57+
</>
58+
);
59+
};
60+
61+
typeOAuth2AppRowProps={
62+
app:TypesGen.OAuth2ProviderApp;
63+
revoke:(app:TypesGen.OAuth2ProviderApp)=>void;
64+
};
65+
66+
constOAuth2AppRow:FC<OAuth2AppRowProps>=({ app, revoke})=>{
67+
return(
68+
<TableRowkey={app.id}data-testid={`app-${app.id}`}>
69+
<TableCell>
70+
<AvatarData
71+
title={app.name}
72+
avatar={
73+
Boolean(app.icon)&&(
74+
<Avatarsrc={app.icon}variant="square"fitImage/>
75+
)
76+
}
77+
/>
78+
</TableCell>
79+
80+
<TableCell>
81+
<Button
82+
variant="contained"
83+
size="small"
84+
color="error"
85+
onClick={()=>revoke(app)}
86+
>
87+
Revoke&hellip;
88+
</Button>
89+
</TableCell>
90+
</TableRow>
91+
);
92+
};
93+
94+
exportdefaultOAuth2ProviderPageView;

‎site/src/pages/UserSettingsPage/Sidebar.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import AccountIcon from "@mui/icons-material/Person";
55
importAppearanceIconfrom"@mui/icons-material/Brush";
66
importScheduleIconfrom"@mui/icons-material/EditCalendarOutlined";
77
importSecurityIconfrom"@mui/icons-material/LockOutlined";
8+
importTokenfrom"@mui/icons-material/Token";
89
importtype{User}from"api/typesGenerated";
910
import{UserAvatar}from"components/UserAvatar/UserAvatar";
1011
import{
@@ -23,6 +24,7 @@ export const Sidebar: FC<SidebarProps> = ({ user }) => {
2324
const{ entitlements}=useDashboard();
2425
constshowSchedulePage=
2526
entitlements.features.advanced_template_scheduling.enabled;
27+
constshowOAuth2Page=entitlements.features.oauth2_provider.enabled;
2628

2729
return(
2830
<BaseSidebar>
@@ -53,6 +55,11 @@ export const Sidebar: FC<SidebarProps> = ({ user }) => {
5355
<SidebarNavItemhref="external-auth"icon={GitIcon}>
5456
External Authentication
5557
</SidebarNavItem>
58+
{showOAuth2Page&&(
59+
<SidebarNavItemhref="oauth2-provider"icon={Token}>
60+
OAuth2 Applications
61+
</SidebarNavItem>
62+
)}
5663
<SidebarNavItemhref="tokens"icon={VpnKeyOutlined}>
5764
Tokens
5865
</SidebarNavItem>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp