Skip to content

Commit 8a26e2d

Browse files
committed
Validate auth flow is supported
1 parent c39c8bf commit 8a26e2d

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/client/auth/auth.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,37 @@ export async function startAuthorization(
7070
const codeVerifier = challenge.code_verifier;
7171
const codeChallenge = challenge.code_challenge;
7272

73-
const authorizationUrl = metadata?.authorization_endpoint
74-
? new URL(metadata?.authorization_endpoint)
75-
: new URL("/authorize", serverUrl);
73+
const responseType = "code";
74+
const codeChallengeMethod = "S256";
7675

77-
// TODO: Validate that these parameters are listed as supported in the metadata, if present.
78-
authorizationUrl.searchParams.set("response_type", "code");
76+
let authorizationUrl: URL;
77+
if (metadata) {
78+
authorizationUrl = new URL(metadata.authorization_endpoint);
79+
80+
if (!(responseType in metadata.response_types_supported)) {
81+
throw new Error(
82+
`Incompatible auth server: does not support response type ${responseType}`,
83+
);
84+
}
85+
86+
if (
87+
!metadata.code_challenge_methods_supported ||
88+
!(codeChallengeMethod in metadata.code_challenge_methods_supported)
89+
) {
90+
throw new Error(
91+
`Incompatible auth server: does not support code challenge method ${codeChallengeMethod}`,
92+
);
93+
}
94+
} else {
95+
authorizationUrl = new URL("/authorize", serverUrl);
96+
}
97+
98+
authorizationUrl.searchParams.set("response_type", responseType);
7999
authorizationUrl.searchParams.set("code_challenge", codeChallenge);
80-
authorizationUrl.searchParams.set("code_challenge_method", "S256");
100+
authorizationUrl.searchParams.set(
101+
"code_challenge_method",
102+
codeChallengeMethod,
103+
);
81104
authorizationUrl.searchParams.set("redirect_uri", String(redirectUrl));
82105

83106
return { authorizationUrl, codeVerifier };

0 commit comments

Comments
 (0)