wip new metadata
This commit is contained in:
parent
befadc0042
commit
00893afc0b
5 changed files with 19 additions and 38 deletions
|
|
@ -125,7 +125,7 @@ const App = () => {
|
||||||
isInitiatingAuth: false,
|
isInitiatingAuth: false,
|
||||||
oauthTokens: null,
|
oauthTokens: null,
|
||||||
loading: true,
|
loading: true,
|
||||||
oauthStep: "resource_metadata_discovery",
|
oauthStep: "metadata_discovery",
|
||||||
oauthMetadata: null,
|
oauthMetadata: null,
|
||||||
resourceMetadata: null,
|
resourceMetadata: null,
|
||||||
oauthClientInfo: null,
|
oauthClientInfo: null,
|
||||||
|
|
|
||||||
|
|
@ -179,7 +179,7 @@ const AuthDebugger = ({
|
||||||
serverAuthProvider.clear();
|
serverAuthProvider.clear();
|
||||||
updateAuthState({
|
updateAuthState({
|
||||||
oauthTokens: null,
|
oauthTokens: null,
|
||||||
oauthStep: "resource_metadata_discovery",
|
oauthStep: "metadata_discovery",
|
||||||
latestError: null,
|
latestError: null,
|
||||||
oauthClientInfo: null,
|
oauthClientInfo: null,
|
||||||
authorizationCode: "",
|
authorizationCode: "",
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,7 @@ interface OAuthFlowProgressProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
const steps: Array<OAuthStep> = [
|
const steps: Array<OAuthStep> = [
|
||||||
|
"resource_metadata_discovery",
|
||||||
"metadata_discovery",
|
"metadata_discovery",
|
||||||
"client_registration",
|
"client_registration",
|
||||||
"authorization_redirect",
|
"authorization_redirect",
|
||||||
|
|
@ -79,16 +80,6 @@ export const OAuthFlowProgress = ({
|
||||||
null,
|
null,
|
||||||
);
|
);
|
||||||
|
|
||||||
const steps: Array<OAuthStep> = [
|
|
||||||
"resource_metadata_discovery",
|
|
||||||
"metadata_discovery",
|
|
||||||
"client_registration",
|
|
||||||
"authorization_redirect",
|
|
||||||
"authorization_code",
|
|
||||||
"token_request",
|
|
||||||
"complete",
|
|
||||||
];
|
|
||||||
|
|
||||||
const currentStepIdx = steps.findIndex((s) => s === authState.oauthStep);
|
const currentStepIdx = steps.findIndex((s) => s === authState.oauthStep);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -135,8 +126,8 @@ export const OAuthFlowProgress = ({
|
||||||
|
|
||||||
<div className="space-y-3">
|
<div className="space-y-3">
|
||||||
<OAuthStepDetails
|
<OAuthStepDetails
|
||||||
label="Resource Metadata Discovery"
|
label="Metadata Discovery"
|
||||||
{...getStepProps("resource_metadata_discovery")}
|
{...getStepProps("metadata_discovery")}
|
||||||
>
|
>
|
||||||
{authState.resourceMetadata && (
|
{authState.resourceMetadata && (
|
||||||
<details className="text-xs mt-2">
|
<details className="text-xs mt-2">
|
||||||
|
|
@ -148,11 +139,6 @@ export const OAuthFlowProgress = ({
|
||||||
</pre>
|
</pre>
|
||||||
</details>
|
</details>
|
||||||
)}
|
)}
|
||||||
</OAuthStepDetails>
|
|
||||||
<OAuthStepDetails
|
|
||||||
label="Metadata Discovery"
|
|
||||||
{...getStepProps("metadata_discovery")}
|
|
||||||
>
|
|
||||||
{provider.getServerMetadata() && (
|
{provider.getServerMetadata() && (
|
||||||
<details className="text-xs mt-2">
|
<details className="text-xs mt-2">
|
||||||
<summary className="cursor-pointer text-muted-foreground font-medium">
|
<summary className="cursor-pointer text-muted-foreground font-medium">
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import {
|
||||||
|
|
||||||
// OAuth flow steps
|
// OAuth flow steps
|
||||||
export type OAuthStep =
|
export type OAuthStep =
|
||||||
| "resource_metadata_discovery"
|
|
||||||
| "metadata_discovery"
|
| "metadata_discovery"
|
||||||
| "client_registration"
|
| "client_registration"
|
||||||
| "authorization_redirect"
|
| "authorization_redirect"
|
||||||
|
|
|
||||||
|
|
@ -20,31 +20,26 @@ export interface StateTransition {
|
||||||
execute: (context: StateMachineContext) => Promise<void>;
|
execute: (context: StateMachineContext) => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fetchProtectedResourceMetadata = async (serverUrl: string): Promise<object> => {
|
||||||
|
// TODO: use sdk
|
||||||
|
const url = new URL("/.well-known/oauth-protected-resource", serverUrl);
|
||||||
|
const response = await fetch(url);
|
||||||
|
const resourceMetadata = await response.json();
|
||||||
|
|
||||||
|
return resourceMetadata;
|
||||||
|
}
|
||||||
|
|
||||||
// State machine transitions
|
// State machine transitions
|
||||||
export const oauthTransitions: Record<OAuthStep, StateTransition> = {
|
export const oauthTransitions: Record<OAuthStep, StateTransition> = {
|
||||||
resource_metadata_discovery: {
|
metadata_discovery: {
|
||||||
canTransition: async () => true,
|
canTransition: async () => true,
|
||||||
execute: async (context) => {
|
execute: async (context) => {
|
||||||
// TODO: use sdk
|
|
||||||
const url = new URL("/.well-known/oauth-protected-resource", context.serverUrl);
|
|
||||||
const response = await fetch(url);
|
|
||||||
|
|
||||||
const resourceMetadata = await response.json();
|
try {
|
||||||
context.updateState({
|
|
||||||
resourceMetadata: resourceMetadata,
|
|
||||||
oauthStep: "metadata_discovery",
|
|
||||||
});
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
metadata_discovery: {
|
|
||||||
canTransition: async (context) => !!context.state.resourceMetadata,
|
|
||||||
execute: async (context) => {
|
|
||||||
// TODO: use sdk
|
|
||||||
let authServerUrl = context.serverUrl;
|
|
||||||
if (context.state.resourceMetadata?.authorization_servers?.[0]) {
|
|
||||||
authServerUrl = context.state.resourceMetadata.authorization_servers[0];
|
|
||||||
}
|
}
|
||||||
|
const resourceMetadata = fetchProtectedResourceMetadata(serverUrl)
|
||||||
|
|
||||||
const metadata = await discoverOAuthMetadata(authServerUrl);
|
const metadata = await discoverOAuthMetadata(authServerUrl);
|
||||||
if (!metadata) {
|
if (!metadata) {
|
||||||
throw new Error("Failed to discover OAuth metadata");
|
throw new Error("Failed to discover OAuth metadata");
|
||||||
|
|
@ -52,6 +47,7 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
|
||||||
const parsedMetadata = await OAuthMetadataSchema.parseAsync(metadata);
|
const parsedMetadata = await OAuthMetadataSchema.parseAsync(metadata);
|
||||||
context.provider.saveServerMetadata(parsedMetadata);
|
context.provider.saveServerMetadata(parsedMetadata);
|
||||||
context.updateState({
|
context.updateState({
|
||||||
|
resourceMetadata,
|
||||||
oauthMetadata: parsedMetadata,
|
oauthMetadata: parsedMetadata,
|
||||||
oauthStep: "client_registration",
|
oauthStep: "client_registration",
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue