Skip to content

Commit befadc0

Browse files
committed
very wip draft auth spec support
1 parent 46d3d84 commit befadc0

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

client/src/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ const App = () => {
125125
isInitiatingAuth: false,
126126
oauthTokens: null,
127127
loading: true,
128-
oauthStep: "metadata_discovery",
128+
oauthStep: "resource_metadata_discovery",
129129
oauthMetadata: null,
130+
resourceMetadata: null,
130131
oauthClientInfo: null,
131132
authorizationUrl: null,
132133
authorizationCode: "",

client/src/components/AuthDebugger.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ const AuthDebugger = ({
179179
serverAuthProvider.clear();
180180
updateAuthState({
181181
oauthTokens: null,
182-
oauthStep: "metadata_discovery",
182+
oauthStep: "resource_metadata_discovery",
183183
latestError: null,
184184
oauthClientInfo: null,
185185
authorizationCode: "",

client/src/components/OAuthFlowProgress.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ export const OAuthFlowProgress = ({
7979
null,
8080
);
8181

82+
const steps: Array<OAuthStep> = [
83+
"resource_metadata_discovery",
84+
"metadata_discovery",
85+
"client_registration",
86+
"authorization_redirect",
87+
"authorization_code",
88+
"token_request",
89+
"complete",
90+
];
91+
8292
const currentStepIdx = steps.findIndex((s) => s === authState.oauthStep);
8393

8494
useEffect(() => {
@@ -124,6 +134,21 @@ export const OAuthFlowProgress = ({
124134
</p>
125135

126136
<div className="space-y-3">
137+
<OAuthStepDetails
138+
label="Resource Metadata Discovery"
139+
{...getStepProps("resource_metadata_discovery")}
140+
>
141+
{authState.resourceMetadata && (
142+
<details className="text-xs mt-2">
143+
<summary className="cursor-pointer text-muted-foreground font-medium">
144+
Retrieved OAuth Resource Metadata from {(new URL('/.well-known/oauth-protected-resource', serverUrl)).href}
145+
</summary>
146+
<pre className="mt-2 p-2 bg-muted rounded-md overflow-auto max-h-[300px]">
147+
{JSON.stringify(authState.resourceMetadata, null, 2)}
148+
</pre>
149+
</details>
150+
)}
151+
</OAuthStepDetails>
127152
<OAuthStepDetails
128153
label="Metadata Discovery"
129154
{...getStepProps("metadata_discovery")}

client/src/lib/auth-types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77

88
// OAuth flow steps
99
export type OAuthStep =
10+
| "resource_metadata_discovery"
1011
| "metadata_discovery"
1112
| "client_registration"
1213
| "authorization_redirect"
@@ -28,6 +29,8 @@ export interface AuthDebuggerState {
2829
oauthTokens: OAuthTokens | null;
2930
loading: boolean;
3031
oauthStep: OAuthStep;
32+
// TODO: use sdk type
33+
resourceMetadata: object | null;
3134
oauthMetadata: OAuthMetadata | null;
3235
oauthClientInfo: OAuthClientInformationFull | OAuthClientInformation | null;
3336
authorizationUrl: string | null;

client/src/lib/oauth-state-machine.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,34 @@ export interface StateMachineContext {
1818
export interface StateTransition {
1919
canTransition: (context: StateMachineContext) => Promise<boolean>;
2020
execute: (context: StateMachineContext) => Promise<void>;
21-
nextStep: OAuthStep;
2221
}
2322

2423
// State machine transitions
2524
export const oauthTransitions: Record<OAuthStep, StateTransition> = {
26-
metadata_discovery: {
25+
resource_metadata_discovery: {
2726
canTransition: async () => true,
2827
execute: async (context) => {
29-
const metadata = await discoverOAuthMetadata(context.serverUrl);
28+
// TODO: use sdk
29+
const url = new URL("/.well-known/oauth-protected-resource", context.serverUrl);
30+
const response = await fetch(url);
31+
32+
const resourceMetadata = await response.json();
33+
context.updateState({
34+
resourceMetadata: resourceMetadata,
35+
oauthStep: "metadata_discovery",
36+
});
37+
},
38+
},
39+
40+
metadata_discovery: {
41+
canTransition: async (context) => !!context.state.resourceMetadata,
42+
execute: async (context) => {
43+
// TODO: use sdk
44+
let authServerUrl = context.serverUrl;
45+
if (context.state.resourceMetadata?.authorization_servers?.[0]) {
46+
authServerUrl = context.state.resourceMetadata.authorization_servers[0];
47+
}
48+
const metadata = await discoverOAuthMetadata(authServerUrl);
3049
if (!metadata) {
3150
throw new Error("Failed to discover OAuth metadata");
3251
}
@@ -37,7 +56,6 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
3756
oauthStep: "client_registration",
3857
});
3958
},
40-
nextStep: "client_registration",
4159
},
4260

4361
client_registration: {
@@ -62,7 +80,6 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
6280
oauthStep: "authorization_redirect",
6381
});
6482
},
65-
nextStep: "authorization_redirect",
6683
},
6784

6885
authorization_redirect: {
@@ -93,7 +110,6 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
93110
oauthStep: "authorization_code",
94111
});
95112
},
96-
nextStep: "authorization_code",
97113
},
98114

99115
authorization_code: {
@@ -114,7 +130,6 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
114130
oauthStep: "token_request",
115131
});
116132
},
117-
nextStep: "token_request",
118133
},
119134

120135
token_request: {
@@ -144,15 +159,13 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
144159
oauthStep: "complete",
145160
});
146161
},
147-
nextStep: "complete",
148162
},
149163

150164
complete: {
151165
canTransition: async () => false,
152166
execute: async () => {
153167
// No-op for complete state
154168
},
155-
nextStep: "complete",
156169
},
157170
};
158171

0 commit comments

Comments
 (0)