Skip to content

Commit cc77b84

Browse files
committed
cleanup types and validation
1 parent 454a609 commit cc77b84

File tree

2 files changed

+58
-67
lines changed

2 files changed

+58
-67
lines changed

client/src/App.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -729,10 +729,6 @@ const App = () => {
729729
/>
730730
<AuthDebugger
731731
sseUrl={sseUrl}
732-
bearerToken={bearerToken}
733-
headerName={headerName}
734-
setBearerToken={setBearerToken}
735-
setHeaderName={setHeaderName}
736732
onBack={() => setShowAuthDebugger(false)}
737733
/>
738734
</>
@@ -742,10 +738,6 @@ const App = () => {
742738
) : showAuthDebugger ? (
743739
<AuthDebugger
744740
sseUrl={sseUrl}
745-
bearerToken={bearerToken}
746-
headerName={headerName}
747-
setBearerToken={setBearerToken}
748-
setHeaderName={setHeaderName}
749741
onBack={() => setShowAuthDebugger(false)}
750742
/>
751743
) : (

client/src/components/AuthDebugger.tsx

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ import { CheckCircle2, Circle, ExternalLink } from "lucide-react";
2222

2323
interface AuthDebuggerProps {
2424
sseUrl: string;
25-
bearerToken: string;
26-
headerName: string;
27-
setBearerToken: (token: string) => void;
28-
setHeaderName: (headerName: string) => void;
2925
onBack: () => void;
3026
}
3127

@@ -46,20 +42,43 @@ class DebugInspectorOAuthClientProvider extends InspectorOAuthClientProvider {
4642
}
4743
}
4844

49-
const AuthDebugger = ({
50-
sseUrl,
51-
bearerToken,
52-
headerName,
53-
setBearerToken,
54-
setHeaderName,
55-
onBack,
56-
}: AuthDebuggerProps) => {
45+
const validateOAuthMetadata = (
46+
metadata: OAuthMetadata | null,
47+
toast: (arg0: object) => void,
48+
): OAuthMetadata => {
49+
if (!metadata) {
50+
toast({
51+
title: "Error",
52+
description: "Can't advance without successfully fetching metadata",
53+
variant: "destructive",
54+
});
55+
throw new Error("OAuth metadata not found");
56+
}
57+
return metadata;
58+
};
59+
60+
const validateClientInformation = async (
61+
provider: DebugInspectorOAuthClientProvider,
62+
toast: (arg0: object) => void,
63+
): Promise<OAuthClientInformation> => {
64+
const clientInformation = await provider.clientInformation();
65+
66+
if (!clientInformation) {
67+
toast({
68+
title: "Error",
69+
description: "Can't advance without successful client registration",
70+
variant: "destructive",
71+
});
72+
throw new Error("OAuth client information not found");
73+
}
74+
return clientInformation;
75+
};
76+
77+
const AuthDebugger = ({ sseUrl, onBack }: AuthDebuggerProps) => {
5778
const { toast } = useToast();
5879
const [isInitiatingAuth, setIsInitiatingAuth] = useState(false);
5980
const [oauthTokens, setOAuthTokens] = useState<OAuthTokens | null>(null);
6081
const [loading, setLoading] = useState(true);
61-
const [localHeaderName, setLocalHeaderName] = useState(headerName);
62-
const [localBearerToken, setLocalBearerToken] = useState(bearerToken);
6382
const [oauthStep, setOAuthStep] = useState<OAuthStep>("not_started");
6483
const [oauthMetadata, setOAuthMetadata] = useState<OAuthMetadata | null>(
6584
null,
@@ -82,7 +101,7 @@ const AuthDebugger = ({
82101
const parsedTokens = await OAuthTokensSchema.parseAsync(
83102
JSON.parse(tokens),
84103
);
85-
setOauthTokens(parsedTokens);
104+
setOAuthTokens(parsedTokens);
86105
setOAuthStep("complete");
87106
}
88107
}
@@ -154,24 +173,30 @@ const AuthDebugger = ({
154173
const parsedMetadata = await OAuthMetadataSchema.parseAsync(metadata);
155174
setOAuthMetadata(parsedMetadata);
156175
} else if (oauthStep === "metadata_discovery") {
176+
const metadata = validateOAuthMetadata(oauthMetadata, toast);
177+
157178
setOAuthStep("client_registration");
158179

159180
const fullInformation = await registerClient(sseUrl, {
160-
metadata: oauthMetadata,
181+
metadata,
161182
clientMetadata: provider.clientMetadata,
162183
});
163184

164185
provider.saveClientInformation(fullInformation);
165186
} else if (oauthStep === "client_registration") {
187+
const metadata = validateOAuthMetadata(oauthMetadata, toast);
188+
const clientInformation = await validateClientInformation(
189+
provider,
190+
toast,
191+
);
166192
setOAuthStep("authorization_redirect");
167193
// This custom implementation captures the OAuth flow step by step
168194
// First, get or register the client
169195
try {
170-
const clientInformation = await provider.clientInformation();
171196
const { authorizationUrl, codeVerifier } = await startAuthorization(
172197
sseUrl,
173198
{
174-
metadata: oauthMetadata,
199+
metadata,
175200
clientInformation,
176201
redirectUrl: provider.redirectUrl,
177202
},
@@ -194,15 +219,27 @@ const AuthDebugger = ({
194219
});
195220
}
196221
} else if (oauthStep === "authorization_code") {
197-
// This is after we enter the code.
222+
if (!authorizationCode || authorizationCode.trim() === "") {
223+
toast({
224+
title: "Error",
225+
description: "You need to provide an authorization code",
226+
variant: "destructive",
227+
});
228+
return;
229+
}
230+
// We have a code, continue to token request
198231
setOAuthStep("token_request");
199232
} else if (oauthStep === "token_request") {
200233
const codeVerifier = provider.codeVerifier();
201-
const clientInformation = await provider.clientInformation();
234+
const metadata = validateOAuthMetadata(oauthMetadata, toast);
235+
const clientInformation = await validateClientInformation(
236+
provider,
237+
toast,
238+
);
202239

203240
// const clientInformation = await provider.clientInformation();
204241
const tokens = await exchangeAuthorization(sseUrl, {
205-
metadata: oauthMetadata,
242+
metadata,
206243
clientInformation,
207244
authorizationCode,
208245
codeVerifier,
@@ -279,34 +316,7 @@ const AuthDebugger = ({
279316
}
280317
};
281318

282-
const handleSaveManualAuth = () => {
283-
setBearerToken(localBearerToken);
284-
setHeaderName(localHeaderName);
285-
toast({
286-
title: "Settings Saved",
287-
description:
288-
"Your authentication settings have been saved for the next connection",
289-
});
290-
};
291-
292-
const getOAuthStatus = () => {
293-
if (!oauthTokens) return "Not authenticated";
294-
295-
if (oauthTokens.expires_at) {
296-
const now = Math.floor(Date.now() / 1000);
297-
if (now > oauthTokens.expires_at) {
298-
return "Token expired";
299-
}
300-
301-
const timeRemaining = oauthTokens.expires_at - now;
302-
return `Authenticated (expires in ${Math.floor(timeRemaining / 60)} minutes)`;
303-
}
304-
305-
return "Authenticated";
306-
};
307319
const renderOAuthFlow = () => {
308-
const provider = new DebugInspectorOAuthClientProvider(sseUrl);
309-
310320
const steps = [
311321
{
312322
key: "not_started",
@@ -534,17 +544,6 @@ const AuthDebugger = ({
534544
<p>Loading authentication status...</p>
535545
) : (
536546
<div className="space-y-4">
537-
<div className="flex items-center justify-between">
538-
<span className="font-medium">Status:</span>
539-
<span
540-
className={
541-
oauthTokens ? "text-green-600" : "text-amber-600"
542-
}
543-
>
544-
{getOAuthStatus()}
545-
</span>
546-
</div>
547-
548547
{oauthTokens && (
549548
<div className="space-y-2">
550549
<p className="text-sm font-medium">Access Token:</p>

0 commit comments

Comments
 (0)