History

Move source auth handling into plugins

Rhys Sullivan committed 7 months ago

Commit db9b84d1 parent 02d549c15 files · +98 −125Browse files

Showing the changes to plugins/openapi/react (1 of 15 files). Show all

Unified · Split

plugins/openapi/react/index.tsx+36−14
@@ -38,7 +38,7 @@ import { · 37 unmodified lines
3838 type SourceToolExplorerSearch,
3939 useSourcePluginNavigation,
4040 useSourcePluginSearch,
41−} from "@executor/react/source-plugins";
41+} from "@executor/react/plugins";
4242 import {
4343 openApiHttpApiExtension,
4444 } from "@executor/plugin-openapi-http";
@@ -169,6 +169,31 @@ const inputFromConfig = ( · 124 unmodified lines
169169 auth: config.auth,
170170 });
171171
172+const secretValue = (input: OpenApiConnectInput["auth"]): string =>
173+ input.kind === "bearer"
174+ ? JSON.stringify(input.tokenSecretRef)
175+ : "";
176+
177+const authFromSecretValue = (
178+ authKind: OpenApiConnectInput["auth"]["kind"],
179+ value: string,
180+): OpenApiConnectInput["auth"] => {
181+ if (authKind === "none") {
182+ return {
183+ kind: "none",
184+ };
185+ }
186+
187+ if (!value) {
188+ throw new Error("Select a secret for bearer auth.");
189+ }
190+
191+ return {
192+ kind: "bearer",
193+ tokenSecretRef: JSON.parse(value) as OpenApiConnectInput["auth"] & { tokenSecretRef: never }["tokenSecretRef"],
194+ };
195+};
196+
172197 const useAvailableSecrets = (
173198 openApiHttpClient: ReturnType<typeof getOpenApiHttpClient>,
174199 ) => {
@@ -205,9 +230,7 @@ function OpenApiSourceForm(props: { · 30 unmodified lines
205230 props.initialValue.auth.kind,
206231 );
207232 const [tokenSecretRef, setTokenSecretRef] = useState(
208− props.initialValue.auth.kind === "bearer"
209− ? props.initialValue.auth.tokenSecretRef
210− : "",
233+ secretValue(props.initialValue.auth),
211234 );
212235 const [error, setError] = useState<string | null>(null);
213236 const [preview, setPreview] = useState<OpenApiPreviewResponse | null>(null);
@@ -309,19 +332,12 @@ function OpenApiSourceForm(props: { · 95 unmodified lines
309332 }
310333
311334 try {
335+ const auth = authFromSecretValue(authKind, tokenSecretRef);
312336 await submitMutation.mutateAsync({
313337 name: trimmedName,
314338 specUrl: trimmedSpecUrl,
315339 baseUrl: trimmedBaseUrl || null,
316− auth:
317− authKind === "bearer"
318− ? {
319− kind: "bearer",
320− tokenSecretRef: tokenSecretRef.trim(),
321− }
322− : {
323− kind: "none",
324− },
340+ auth,
325341 });
326342 } catch (cause) {
327343 setError(cause instanceof Error ? cause.message : "Failed saving source.");
@@ -398,7 +414,13 @@ function OpenApiSourceForm(props: { · 70 unmodified lines
398414 >
399415 <option value="">Select a secret</option>
400416 {availableSecrets.map((secret) => (
401− <option key={secret.id} value={secret.id}>
417+ <option
418+ key={`${secret.providerId}:${secret.id}`}
419+ value={JSON.stringify({
420+ providerId: secret.providerId,
421+ handle: secret.id,
422+ })}
423+ >
402424 {secret.name || secret.id}
403425 </option>
404426 ))}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t