@@ -16,6 +16,18 @@ jest.mock('pkce-challenge', () => ({
16
16
} )
17
17
} ) ) ;
18
18
19
+ const mockTokens = {
20
+ access_token : 'mock_access_token' ,
21
+ token_type : 'bearer' ,
22
+ expires_in : 3600 ,
23
+ refresh_token : 'mock_refresh_token'
24
+ } ;
25
+
26
+ const mockTokensWithIdToken = {
27
+ ...mockTokens ,
28
+ id_token : 'mock_id_token'
29
+ }
30
+
19
31
describe ( 'Token Handler' , ( ) => {
20
32
// Mock client data
21
33
const validClient : OAuthClientInformationFull = {
@@ -58,12 +70,7 @@ describe('Token Handler', () => {
58
70
59
71
async exchangeAuthorizationCode ( client : OAuthClientInformationFull , authorizationCode : string ) : Promise < OAuthTokens > {
60
72
if ( authorizationCode === 'valid_code' ) {
61
- return {
62
- access_token : 'mock_access_token' ,
63
- token_type : 'bearer' ,
64
- expires_in : 3600 ,
65
- refresh_token : 'mock_refresh_token'
66
- } ;
73
+ return mockTokens ;
67
74
}
68
75
throw new InvalidGrantError ( 'The authorization code is invalid or has expired' ) ;
69
76
} ,
@@ -291,18 +298,36 @@ describe('Token Handler', () => {
291
298
) ;
292
299
} ) ;
293
300
301
+ it ( 'returns id token in code exchange if provided' , async ( ) => {
302
+ mockProvider . exchangeAuthorizationCode = async ( client : OAuthClientInformationFull , authorizationCode : string ) : Promise < OAuthTokens > => {
303
+ if ( authorizationCode === 'valid_code' ) {
304
+ return mockTokensWithIdToken ;
305
+ }
306
+ throw new InvalidGrantError ( 'The authorization code is invalid or has expired' ) ;
307
+ } ;
308
+
309
+ const response = await supertest ( app )
310
+ . post ( '/token' )
311
+ . type ( 'form' )
312
+ . send ( {
313
+ client_id : 'valid-client' ,
314
+ client_secret : 'valid-secret' ,
315
+ grant_type : 'authorization_code' ,
316
+ code : 'valid_code' ,
317
+ code_verifier : 'valid_verifier'
318
+ } ) ;
319
+
320
+ expect ( response . status ) . toBe ( 200 ) ;
321
+ expect ( response . body . id_token ) . toBe ( 'mock_id_token' ) ;
322
+ } ) ;
323
+
294
324
it ( 'passes through code verifier when using proxy provider' , async ( ) => {
295
325
const originalFetch = global . fetch ;
296
326
297
327
try {
298
328
global . fetch = jest . fn ( ) . mockResolvedValue ( {
299
329
ok : true ,
300
- json : ( ) => Promise . resolve ( {
301
- access_token : 'mock_access_token' ,
302
- token_type : 'bearer' ,
303
- expires_in : 3600 ,
304
- refresh_token : 'mock_refresh_token'
305
- } )
330
+ json : ( ) => Promise . resolve ( mockTokens )
306
331
} ) ;
307
332
308
333
const proxyProvider = new ProxyOAuthServerProvider ( {
@@ -359,12 +384,7 @@ describe('Token Handler', () => {
359
384
try {
360
385
global . fetch = jest . fn ( ) . mockResolvedValue ( {
361
386
ok : true ,
362
- json : ( ) => Promise . resolve ( {
363
- access_token : 'mock_access_token' ,
364
- token_type : 'bearer' ,
365
- expires_in : 3600 ,
366
- refresh_token : 'mock_refresh_token'
367
- } )
387
+ json : ( ) => Promise . resolve ( mockTokens )
368
388
} ) ;
369
389
370
390
const proxyProvider = new ProxyOAuthServerProvider ( {
0 commit comments