Skip to content

Commit bc3ded9

Browse files
committed
mask the password in the api-workbench logs
Also have a shortcut button for login/logout
1 parent 59fcf04 commit bc3ded9

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

ts/api-workbench/src/pages/api-workbench.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { AdlForm, useAdlFormState } from "@/components/forms/mui/form";
88
import { Modal } from "@/components/forms/mui/modal";
99
import { VEditor } from "@/components/forms/mui/veditor";
1010
import { createUiFactory } from "@/components/forms/factory";
11-
import { AppState, useAppState } from "@/hooks/use-app-state";
11+
import { AppState, AuthState, useAppState } from "@/hooks/use-app-state";
1212
import { AdlRequestError, encodeQueryString, ServiceBase } from "@/service/service-base";
1313
import * as ADL from "@adllang/adl-runtime";
1414
import { Json, JsonBinding, createJsonBinding, scopedNamesEqual } from "@adllang/adl-runtime";
@@ -118,14 +118,25 @@ export function ApiWorkbench() {
118118
}
119119
}
120120

121+
const loginEndpoint = endpoints.find((ep) => ep.name === "login");
122+
const logoutEndpoint = endpoints.find((ep) => ep.name === "logout");
123+
121124
return (
122125
<Container fixed>
123126
<Box>
124127
<Box sx={{ display: "flex", flexDirection: "row", justifyContent: "space-between", alignItems: "center" }}>
125128
<Typography variant="h4" component="h1" sx={{ mb: 2, marginTop: "20px" }}>
126129
API Workbench
127130
</Typography>
128-
<Button onClick={() => appState.logout()}>Logout</Button>
131+
<LoginLogoutButton
132+
authState={appState.authState}
133+
login={() =>
134+
loginEndpoint && setModal({ state: "create-request", endpoint: loginEndpoint, initial: undefined })
135+
}
136+
logout={() =>
137+
logoutEndpoint && setModal({ state: "create-request", endpoint: logoutEndpoint, initial: undefined })
138+
}
139+
/>
129140
</Box>
130141
{prevRequests.map((value, i) => (
131142
<CompletedRequestView
@@ -161,6 +172,16 @@ export function ApiWorkbench() {
161172
);
162173
}
163174

175+
function LoginLogoutButton(props: { authState: AuthState; login: () => void; logout: () => void }) {
176+
switch (props.authState.kind) {
177+
case "noauth":
178+
case "authfailed":
179+
return <Button onClick={props.login}>Login</Button>;
180+
case "auth":
181+
return <Button onClick={props.logout}>Logout</Button>;
182+
}
183+
}
184+
164185
function ModalChooseEndpoint(props: { endpoints: Endpoint[]; choose: (e: Endpoint) => void; cancel: () => void }) {
165186
return (
166187
<Modal onClickBackground={() => props.cancel()}>
@@ -353,11 +374,13 @@ async function executeRequest<I, O>(
353374
};
354375
}
355376

356-
function updateAppState<I, O>(appState: AppState, endpoint: HttpEndpoint<I, O>, _req: I, resp: O) {
377+
function updateAppState<I, O>(appState: AppState, endpoint: HttpEndpoint<I, O>, req: I, resp: O) {
357378
// All the endpoint handling is generic except for here, where we update the auth state when the
358379
// login or logout endpoints are called.
359380
switch (endpoint.name) {
360381
case "login":
382+
// Clear the password to avoid showing it on screen in the request history
383+
(req as API.LoginReq).password = "";
361384
appState.setAuthStateFromLogin(resp as API.LoginResp);
362385
break;
363386
case "logout":

0 commit comments

Comments
 (0)