Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions client/src/components/AuthDebugger.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useMemo, useEffect } from "react";
import { Button } from "@/components/ui/button";
import { DebugInspectorOAuthClientProvider } from "../lib/auth";
import { DebugInspectorOAuthClientProvider, InspectorOAuthClientProvider } from "../lib/auth";
import { AlertCircle } from "lucide-react";
import { AuthDebuggerState, EMPTY_DEBUGGER_STATE } from "../lib/auth-types";
import { OAuthFlowProgress } from "./OAuthFlowProgress";
Expand Down Expand Up @@ -149,11 +149,13 @@ const AuthDebugger = ({
latestError: null,
};

// Use regular provider for quick flow (automatic callback)
const provider = new InspectorOAuthClientProvider(serverUrl);
const oauthMachine = new OAuthStateMachine(serverUrl, (updates) => {
// Update our temporary state during the process
currentState = { ...currentState, ...updates };
// But don't call updateAuthState yet
});
}, provider);

// Manually step through each stage of the OAuth flow
while (currentState.oauthStep !== "complete") {
Expand Down
22 changes: 16 additions & 6 deletions client/src/lib/oauth-state-machine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OAuthStep, AuthDebuggerState } from "./auth-types";
import { DebugInspectorOAuthClientProvider } from "./auth";
import { DebugInspectorOAuthClientProvider, InspectorOAuthClientProvider } from "./auth";
import {
discoverAuthorizationServerMetadata,
registerClient,
Expand All @@ -17,7 +17,7 @@ import { generateOAuthState } from "@/utils/oauthUtils";
export interface StateMachineContext {
state: AuthDebuggerState;
serverUrl: string;
provider: DebugInspectorOAuthClientProvider;
provider: DebugInspectorOAuthClientProvider | InspectorOAuthClientProvider;
updateState: (updates: Partial<AuthDebuggerState>) => void;
}

Expand Down Expand Up @@ -62,7 +62,10 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
throw new Error("Failed to discover OAuth metadata");
}
const parsedMetadata = await OAuthMetadataSchema.parseAsync(metadata);
context.provider.saveServerMetadata(parsedMetadata);
// Only save server metadata if provider supports it (debug provider)
if ('saveServerMetadata' in context.provider) {
context.provider.saveServerMetadata(parsedMetadata);
}
context.updateState({
resourceMetadata,
resource,
Expand Down Expand Up @@ -160,15 +163,20 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {

token_request: {
canTransition: async (context) => {
const hasServerMetadata = 'getServerMetadata' in context.provider
? !!context.provider.getServerMetadata()
: !!context.state.oauthMetadata;
return (
!!context.state.authorizationCode &&
!!context.provider.getServerMetadata() &&
hasServerMetadata &&
!!(await context.provider.clientInformation())
);
},
execute: async (context) => {
const codeVerifier = context.provider.codeVerifier();
const metadata = context.provider.getServerMetadata()!;
const metadata = 'getServerMetadata' in context.provider
? context.provider.getServerMetadata()!
: context.state.oauthMetadata!;
const clientInformation = (await context.provider.clientInformation())!;

const tokens = await exchangeAuthorization(context.serverUrl, {
Expand Down Expand Up @@ -200,10 +208,12 @@ export class OAuthStateMachine {
constructor(
private serverUrl: string,
private updateState: (updates: Partial<AuthDebuggerState>) => void,
private provider?: DebugInspectorOAuthClientProvider | InspectorOAuthClientProvider,
) {}

async executeStep(state: AuthDebuggerState): Promise<void> {
const provider = new DebugInspectorOAuthClientProvider(this.serverUrl);
// Use provided provider or default to debug provider for backward compatibility
const provider = this.provider || new DebugInspectorOAuthClientProvider(this.serverUrl);
const context: StateMachineContext = {
state,
serverUrl: this.serverUrl,
Expand Down