@@ -70,14 +70,37 @@ export async function startAuthorization(
70
70
const codeVerifier = challenge . code_verifier ;
71
71
const codeChallenge = challenge . code_challenge ;
72
72
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" ;
76
75
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 ) ;
79
99
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
+ ) ;
81
104
authorizationUrl . searchParams . set ( "redirect_uri" , String ( redirectUrl ) ) ;
82
105
83
106
return { authorizationUrl, codeVerifier } ;
0 commit comments