1
1
import { Response } from "express" ;
2
2
import { OAuthRegisteredClientsStore } from "./clients.js" ;
3
- import {
4
- OAuthClientInformationFull ,
5
- OAuthClientInformationFullSchema ,
6
- OAuthTokenRevocationRequest ,
3
+ import {
4
+ OAuthClientInformationFull ,
5
+ OAuthClientInformationFullSchema ,
6
+ OAuthTokenRevocationRequest ,
7
7
OAuthTokens ,
8
8
OAuthTokensSchema ,
9
9
} from "./../../shared/auth.js" ;
@@ -12,8 +12,8 @@ import { AuthorizationParams, OAuthServerProvider } from "./provider.js";
12
12
import { ServerError } from "./errors.js" ;
13
13
14
14
export type ProxyEndpoints = {
15
- authorizationUrl ? : string ;
16
- tokenUrl ? : string ;
15
+ authorizationUrl : string ;
16
+ tokenUrl : string ;
17
17
revocationUrl ?: string ;
18
18
registrationUrl ?: string ;
19
19
} ;
@@ -24,14 +24,14 @@ export type ProxyOptions = {
24
24
*/
25
25
endpoints : ProxyEndpoints ;
26
26
27
- /**
28
- * Function to verify access tokens and return auth info
29
- */
30
- verifyAccessToken : ( token : string ) => Promise < AuthInfo > ;
27
+ /**
28
+ * Function to verify access tokens and return auth info
29
+ */
30
+ verifyAccessToken : ( token : string ) => Promise < AuthInfo > ;
31
31
32
- /**
33
- * Function to fetch client information from the upstream server
34
- */
32
+ /**
33
+ * Function to fetch client information from the upstream server
34
+ */
35
35
getClient : ( clientId : string ) => Promise < OAuthClientInformationFull | undefined > ;
36
36
37
37
} ;
@@ -45,7 +45,7 @@ export class ProxyOAuthServerProvider implements OAuthServerProvider {
45
45
protected readonly _getClient : ( clientId : string ) => Promise < OAuthClientInformationFull | undefined > ;
46
46
47
47
revokeToken ?: (
48
- client : OAuthClientInformationFull ,
48
+ client : OAuthClientInformationFull ,
49
49
request : OAuthTokenRevocationRequest
50
50
) => Promise < void > ;
51
51
@@ -55,15 +55,15 @@ export class ProxyOAuthServerProvider implements OAuthServerProvider {
55
55
this . _getClient = options . getClient ;
56
56
if ( options . endpoints ?. revocationUrl ) {
57
57
this . revokeToken = async (
58
- client : OAuthClientInformationFull ,
58
+ client : OAuthClientInformationFull ,
59
59
request : OAuthTokenRevocationRequest
60
60
) => {
61
61
const revocationUrl = this . _endpoints . revocationUrl ;
62
-
62
+
63
63
if ( ! revocationUrl ) {
64
64
throw new Error ( "No revocation endpoint configured" ) ;
65
65
}
66
-
66
+
67
67
const params = new URLSearchParams ( ) ;
68
68
params . set ( "token" , request . token ) ;
69
69
params . set ( "client_id" , client . client_id ) ;
@@ -73,15 +73,15 @@ export class ProxyOAuthServerProvider implements OAuthServerProvider {
73
73
if ( request . token_type_hint ) {
74
74
params . set ( "token_type_hint" , request . token_type_hint ) ;
75
75
}
76
-
76
+
77
77
const response = await fetch ( revocationUrl , {
78
78
method : "POST" ,
79
79
headers : {
80
80
"Content-Type" : "application/x-www-form-urlencoded" ,
81
81
} ,
82
82
body : params . toString ( ) ,
83
83
} ) ;
84
-
84
+
85
85
if ( ! response . ok ) {
86
86
throw new ServerError ( `Token revocation failed: ${ response . status } ` ) ;
87
87
}
@@ -115,18 +115,12 @@ export class ProxyOAuthServerProvider implements OAuthServerProvider {
115
115
}
116
116
117
117
async authorize (
118
- client : OAuthClientInformationFull ,
119
- params : AuthorizationParams ,
118
+ client : OAuthClientInformationFull ,
119
+ params : AuthorizationParams ,
120
120
res : Response
121
121
) : Promise < void > {
122
- const authorizationUrl = this . _endpoints . authorizationUrl ;
123
-
124
- if ( ! authorizationUrl ) {
125
- throw new Error ( "No authorization endpoint configured" ) ;
126
- }
127
-
128
122
// Start with required OAuth parameters
129
- const targetUrl = new URL ( authorizationUrl ) ;
123
+ const targetUrl = new URL ( this . _endpoints . authorizationUrl ) ;
130
124
const searchParams = new URLSearchParams ( {
131
125
client_id : client . client_id ,
132
126
response_type : "code" ,
@@ -144,7 +138,7 @@ export class ProxyOAuthServerProvider implements OAuthServerProvider {
144
138
}
145
139
146
140
async challengeForAuthorizationCode (
147
- _client : OAuthClientInformationFull ,
141
+ _client : OAuthClientInformationFull ,
148
142
_authorizationCode : string
149
143
) : Promise < string > {
150
144
// In a proxy setup, we don't store the code challenge ourselves
@@ -153,16 +147,10 @@ export class ProxyOAuthServerProvider implements OAuthServerProvider {
153
147
}
154
148
155
149
async exchangeAuthorizationCode (
156
- client : OAuthClientInformationFull ,
150
+ client : OAuthClientInformationFull ,
157
151
authorizationCode : string ,
158
152
codeVerifier ?: string
159
153
) : Promise < OAuthTokens > {
160
- const tokenUrl = this . _endpoints . tokenUrl ;
161
-
162
- if ( ! tokenUrl ) {
163
- throw new Error ( "No token endpoint configured" ) ;
164
- }
165
-
166
154
const params = new URLSearchParams ( {
167
155
grant_type : "authorization_code" ,
168
156
client_id : client . client_id ,
@@ -177,7 +165,7 @@ export class ProxyOAuthServerProvider implements OAuthServerProvider {
177
165
params . append ( "code_verifier" , codeVerifier ) ;
178
166
}
179
167
180
- const response = await fetch ( tokenUrl , {
168
+ const response = await fetch ( this . _endpoints . tokenUrl , {
181
169
method : "POST" ,
182
170
headers : {
183
171
"Content-Type" : "application/x-www-form-urlencoded" ,
@@ -195,15 +183,10 @@ export class ProxyOAuthServerProvider implements OAuthServerProvider {
195
183
}
196
184
197
185
async exchangeRefreshToken (
198
- client : OAuthClientInformationFull ,
186
+ client : OAuthClientInformationFull ,
199
187
refreshToken : string ,
200
188
scopes ?: string [ ]
201
189
) : Promise < OAuthTokens > {
202
- const tokenUrl = this . _endpoints . tokenUrl ;
203
-
204
- if ( ! tokenUrl ) {
205
- throw new Error ( "No token endpoint configured" ) ;
206
- }
207
190
208
191
const params = new URLSearchParams ( {
209
192
grant_type : "refresh_token" ,
@@ -219,7 +202,7 @@ export class ProxyOAuthServerProvider implements OAuthServerProvider {
219
202
params . set ( "scope" , scopes . join ( " " ) ) ;
220
203
}
221
204
222
- const response = await fetch ( tokenUrl , {
205
+ const response = await fetch ( this . _endpoints . tokenUrl , {
223
206
method : "POST" ,
224
207
headers : {
225
208
"Content-Type" : "application/x-www-form-urlencoded" ,
@@ -237,5 +220,5 @@ export class ProxyOAuthServerProvider implements OAuthServerProvider {
237
220
238
221
async verifyAccessToken ( token : string ) : Promise < AuthInfo > {
239
222
return this . _verifyAccessToken ( token ) ;
240
- }
223
+ }
241
224
}
0 commit comments