@@ -3,7 +3,6 @@ import { requireBearerAuth } from "./bearerAuth.js";
3
3
import { AuthInfo } from "../types.js" ;
4
4
import { InsufficientScopeError , InvalidTokenError , OAuthError , ServerError } from "../errors.js" ;
5
5
import { OAuthTokenVerifier } from "../provider.js" ;
6
- import { LATEST_PROTOCOL_VERSION , DEFAULT_NEGOTIATED_PROTOCOL_VERSION } from '../../../types.js' ;
7
6
8
7
// Mock verifier
9
8
const mockVerifyAccessToken = jest . fn ( ) ;
@@ -43,13 +42,12 @@ describe("requireBearerAuth middleware", () => {
43
42
44
43
mockRequest . headers = {
45
44
authorization : "Bearer valid-token" ,
46
- 'mcp-protocol-version' : LATEST_PROTOCOL_VERSION ,
47
45
} ;
48
46
49
47
const middleware = requireBearerAuth ( { verifier : mockVerifier } ) ;
50
48
await middleware ( mockRequest as Request , mockResponse as Response , nextFunction ) ;
51
49
52
- expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" , LATEST_PROTOCOL_VERSION ) ;
50
+ expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" ) ;
53
51
expect ( mockRequest . auth ) . toEqual ( validAuthInfo ) ;
54
52
expect ( nextFunction ) . toHaveBeenCalled ( ) ;
55
53
expect ( mockResponse . status ) . not . toHaveBeenCalled ( ) ;
@@ -89,13 +87,12 @@ describe("requireBearerAuth middleware", () => {
89
87
90
88
mockRequest . headers = {
91
89
authorization : "Bearer expired-token" ,
92
- 'mcp-protocol-version' : LATEST_PROTOCOL_VERSION ,
93
90
} ;
94
91
95
92
const middleware = requireBearerAuth ( { verifier : mockVerifier } ) ;
96
93
await middleware ( mockRequest as Request , mockResponse as Response , nextFunction ) ;
97
94
98
- expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "expired-token" , LATEST_PROTOCOL_VERSION ) ;
95
+ expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "expired-token" ) ;
99
96
expect ( mockResponse . status ) . toHaveBeenCalledWith ( 401 ) ;
100
97
expect ( mockResponse . set ) . toHaveBeenCalledWith (
101
98
"WWW-Authenticate" ,
@@ -118,13 +115,12 @@ describe("requireBearerAuth middleware", () => {
118
115
119
116
mockRequest . headers = {
120
117
authorization : "Bearer valid-token" ,
121
- 'mcp-protocol-version' : LATEST_PROTOCOL_VERSION ,
122
118
} ;
123
119
124
120
const middleware = requireBearerAuth ( { verifier : mockVerifier } ) ;
125
121
await middleware ( mockRequest as Request , mockResponse as Response , nextFunction ) ;
126
122
127
- expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" , LATEST_PROTOCOL_VERSION ) ;
123
+ expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" ) ;
128
124
expect ( mockRequest . auth ) . toEqual ( nonExpiredAuthInfo ) ;
129
125
expect ( nextFunction ) . toHaveBeenCalled ( ) ;
130
126
expect ( mockResponse . status ) . not . toHaveBeenCalled ( ) ;
@@ -141,7 +137,6 @@ describe("requireBearerAuth middleware", () => {
141
137
142
138
mockRequest . headers = {
143
139
authorization : "Bearer valid-token" ,
144
- 'mcp-protocol-version' : LATEST_PROTOCOL_VERSION ,
145
140
} ;
146
141
147
142
const middleware = requireBearerAuth ( {
@@ -151,7 +146,7 @@ describe("requireBearerAuth middleware", () => {
151
146
152
147
await middleware ( mockRequest as Request , mockResponse as Response , nextFunction ) ;
153
148
154
- expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" , LATEST_PROTOCOL_VERSION ) ;
149
+ expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" ) ;
155
150
expect ( mockResponse . status ) . toHaveBeenCalledWith ( 403 ) ;
156
151
expect ( mockResponse . set ) . toHaveBeenCalledWith (
157
152
"WWW-Authenticate" ,
@@ -173,7 +168,6 @@ describe("requireBearerAuth middleware", () => {
173
168
174
169
mockRequest . headers = {
175
170
authorization : "Bearer valid-token" ,
176
- 'mcp-protocol-version' : LATEST_PROTOCOL_VERSION ,
177
171
} ;
178
172
179
173
const middleware = requireBearerAuth ( {
@@ -183,7 +177,7 @@ describe("requireBearerAuth middleware", () => {
183
177
184
178
await middleware ( mockRequest as Request , mockResponse as Response , nextFunction ) ;
185
179
186
- expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" , LATEST_PROTOCOL_VERSION ) ;
180
+ expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" ) ;
187
181
expect ( mockRequest . auth ) . toEqual ( authInfo ) ;
188
182
expect ( nextFunction ) . toHaveBeenCalled ( ) ;
189
183
expect ( mockResponse . status ) . not . toHaveBeenCalled ( ) ;
@@ -232,15 +226,14 @@ describe("requireBearerAuth middleware", () => {
232
226
it ( "should return 401 when token verification fails with InvalidTokenError" , async ( ) => {
233
227
mockRequest . headers = {
234
228
authorization : "Bearer invalid-token" ,
235
- 'mcp-protocol-version' : LATEST_PROTOCOL_VERSION ,
236
229
} ;
237
230
238
231
mockVerifyAccessToken . mockRejectedValue ( new InvalidTokenError ( "Token expired" ) ) ;
239
232
240
233
const middleware = requireBearerAuth ( { verifier : mockVerifier } ) ;
241
234
await middleware ( mockRequest as Request , mockResponse as Response , nextFunction ) ;
242
235
243
- expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "invalid-token" , LATEST_PROTOCOL_VERSION ) ;
236
+ expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "invalid-token" ) ;
244
237
expect ( mockResponse . status ) . toHaveBeenCalledWith ( 401 ) ;
245
238
expect ( mockResponse . set ) . toHaveBeenCalledWith (
246
239
"WWW-Authenticate" ,
@@ -255,15 +248,14 @@ describe("requireBearerAuth middleware", () => {
255
248
it ( "should return 403 when access token has insufficient scopes" , async ( ) => {
256
249
mockRequest . headers = {
257
250
authorization : "Bearer valid-token" ,
258
- 'mcp-protocol-version' : LATEST_PROTOCOL_VERSION ,
259
251
} ;
260
252
261
253
mockVerifyAccessToken . mockRejectedValue ( new InsufficientScopeError ( "Required scopes: read, write" ) ) ;
262
254
263
255
const middleware = requireBearerAuth ( { verifier : mockVerifier } ) ;
264
256
await middleware ( mockRequest as Request , mockResponse as Response , nextFunction ) ;
265
257
266
- expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" , LATEST_PROTOCOL_VERSION ) ;
258
+ expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" ) ;
267
259
expect ( mockResponse . status ) . toHaveBeenCalledWith ( 403 ) ;
268
260
expect ( mockResponse . set ) . toHaveBeenCalledWith (
269
261
"WWW-Authenticate" ,
@@ -278,15 +270,14 @@ describe("requireBearerAuth middleware", () => {
278
270
it ( "should return 500 when a ServerError occurs" , async ( ) => {
279
271
mockRequest . headers = {
280
272
authorization : "Bearer valid-token" ,
281
- 'mcp-protocol-version' : LATEST_PROTOCOL_VERSION ,
282
273
} ;
283
274
284
275
mockVerifyAccessToken . mockRejectedValue ( new ServerError ( "Internal server issue" ) ) ;
285
276
286
277
const middleware = requireBearerAuth ( { verifier : mockVerifier } ) ;
287
278
await middleware ( mockRequest as Request , mockResponse as Response , nextFunction ) ;
288
279
289
- expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" , LATEST_PROTOCOL_VERSION ) ;
280
+ expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" ) ;
290
281
expect ( mockResponse . status ) . toHaveBeenCalledWith ( 500 ) ;
291
282
expect ( mockResponse . json ) . toHaveBeenCalledWith (
292
283
expect . objectContaining ( { error : "server_error" , error_description : "Internal server issue" } )
@@ -297,15 +288,14 @@ describe("requireBearerAuth middleware", () => {
297
288
it ( "should return 400 for generic OAuthError" , async ( ) => {
298
289
mockRequest . headers = {
299
290
authorization : "Bearer valid-token" ,
300
- 'mcp-protocol-version' : LATEST_PROTOCOL_VERSION ,
301
291
} ;
302
292
303
293
mockVerifyAccessToken . mockRejectedValue ( new OAuthError ( "custom_error" , "Some OAuth error" ) ) ;
304
294
305
295
const middleware = requireBearerAuth ( { verifier : mockVerifier } ) ;
306
296
await middleware ( mockRequest as Request , mockResponse as Response , nextFunction ) ;
307
297
308
- expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" , LATEST_PROTOCOL_VERSION ) ;
298
+ expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" ) ;
309
299
expect ( mockResponse . status ) . toHaveBeenCalledWith ( 400 ) ;
310
300
expect ( mockResponse . json ) . toHaveBeenCalledWith (
311
301
expect . objectContaining ( { error : "custom_error" , error_description : "Some OAuth error" } )
@@ -316,15 +306,14 @@ describe("requireBearerAuth middleware", () => {
316
306
it ( "should return 500 when unexpected error occurs" , async ( ) => {
317
307
mockRequest . headers = {
318
308
authorization : "Bearer valid-token" ,
319
- 'mcp-protocol-version' : LATEST_PROTOCOL_VERSION ,
320
309
} ;
321
310
322
311
mockVerifyAccessToken . mockRejectedValue ( new Error ( "Unexpected error" ) ) ;
323
312
324
313
const middleware = requireBearerAuth ( { verifier : mockVerifier } ) ;
325
314
await middleware ( mockRequest as Request , mockResponse as Response , nextFunction ) ;
326
315
327
- expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" , LATEST_PROTOCOL_VERSION ) ;
316
+ expect ( mockVerifyAccessToken ) . toHaveBeenCalledWith ( "valid-token" ) ;
328
317
expect ( mockResponse . status ) . toHaveBeenCalledWith ( 500 ) ;
329
318
expect ( mockResponse . json ) . toHaveBeenCalledWith (
330
319
expect . objectContaining ( { error : "server_error" , error_description : "Internal Server Error" } )
0 commit comments