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

Commitdd339ea

Browse files
committed
Add user settings page for revoking OAuth2 apps
1 parenta6a0769 commitdd339ea

File tree

6 files changed

+234
-9
lines changed

6 files changed

+234
-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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
import{DeleteDialog}from"components/Dialogs/DeleteDialog/DeleteDialog";
6+
import{displayError,displaySuccess}from"components/GlobalSnackbar/utils";
7+
import{useMe}from"hooks";
8+
import{Section}from"../Section";
9+
importOAuth2ProviderPageViewfrom"./OAuth2ProviderPageView";
10+
11+
constOAuth2ProviderPage:FC=()=>{
12+
constme=useMe();
13+
constqueryClient=useQueryClient();
14+
constuserOAuth2AppsQuery=useQuery(getApps(me.id));
15+
constrevokeAppMutation=useMutation(revokeApp(queryClient,me.id));
16+
const[appIdToRevoke,setAppIdToRevoke]=useState<string>();
17+
constappToRevoke=userOAuth2AppsQuery.data?.find(
18+
(app)=>app.id===appIdToRevoke,
19+
);
20+
21+
// This can happen if the app disappears from the query data but a user has
22+
// already started the revoke flow. It is safe to place this directly in the
23+
// render, because it does not run every single time.
24+
if(appToRevoke===undefined&&typeofappIdToRevoke==="string"){
25+
setAppIdToRevoke(undefined);
26+
displayError("Application no longer exists.");
27+
}
28+
29+
return(
30+
<Sectiontitle="OAuth2 Applications"layout="fluid">
31+
<OAuth2ProviderPageView
32+
isLoading={userOAuth2AppsQuery.isLoading}
33+
error={userOAuth2AppsQuery.error}
34+
apps={userOAuth2AppsQuery.data}
35+
revoke={(app)=>{
36+
setAppIdToRevoke(app.id);
37+
}}
38+
/>
39+
{appToRevoke!==undefined&&(
40+
<DeleteDialog
41+
title="Revoke Application"
42+
verb="Revoking"
43+
info={`This will invalidate any tokens created by the OAuth2 application "${appToRevoke.name}".`}
44+
label="Name of the application to revoke"
45+
isOpen
46+
confirmLoading={revokeAppMutation.isLoading}
47+
name={appToRevoke.name}
48+
entity="application"
49+
onCancel={()=>setAppIdToRevoke(undefined)}
50+
onConfirm={async()=>{
51+
try{
52+
awaitrevokeAppMutation.mutateAsync(appToRevoke.id);
53+
displaySuccess(
54+
`You have successfully revoked the OAuth2 application "${appToRevoke.name}"`,
55+
);
56+
setAppIdToRevoke(undefined);
57+
}catch(error){
58+
displayError(
59+
getErrorMessage(error,"Failed to revoke application."),
60+
);
61+
}
62+
}}
63+
/>
64+
)}
65+
</Section>
66+
);
67+
};
68+
69+
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;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp