@@ -67,6 +67,23 @@ describe.sequential('e2e: BYO agent with CUSTOM_JWT auth', () => {
6767 const cognitoClient = new CognitoIdentityProviderClient ( { region } ) ;
6868 const cfnClient = new CloudFormationClient ( { region } ) ;
6969
70+ /** Fetch a Cognito access token via client_credentials flow. */
71+ async function fetchCognitoAccessToken ( ) : Promise < string > {
72+ const tokenUrl = `https://${ domainPrefix } .auth.${ region } .amazoncognito.com/oauth2/token` ;
73+ const tokenRes = await fetch ( tokenUrl , {
74+ method : 'POST' ,
75+ headers : {
76+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
77+ Authorization : `Basic ${ Buffer . from ( `${ clientId } :${ clientSecret } ` ) . toString ( 'base64' ) } ` ,
78+ } ,
79+ body : 'grant_type=client_credentials&scope=agentcore/invoke' ,
80+ } ) ;
81+ expect ( tokenRes . ok , `Token fetch failed: ${ tokenRes . status } ` ) . toBe ( true ) ;
82+ const tokenJson = ( await tokenRes . json ( ) ) as { access_token : string } ;
83+ expect ( tokenJson . access_token , 'Should have received an access token' ) . toBeTruthy ( ) ;
84+ return tokenJson . access_token ;
85+ }
86+
7087 beforeAll ( async ( ) => {
7188 if ( ! canRun ) return ;
7289
@@ -267,20 +284,7 @@ describe.sequential('e2e: BYO agent with CUSTOM_JWT auth', () => {
267284 async ( ) => {
268285 expect ( projectPath , 'Project should have been deployed' ) . toBeTruthy ( ) ;
269286
270- // Fetch a Cognito access token via client_credentials flow
271- const tokenUrl = `https://${ domainPrefix } .auth.${ region } .amazoncognito.com/oauth2/token` ;
272- const tokenRes = await fetch ( tokenUrl , {
273- method : 'POST' ,
274- headers : {
275- 'Content-Type' : 'application/x-www-form-urlencoded' ,
276- Authorization : `Basic ${ Buffer . from ( `${ clientId } :${ clientSecret } ` ) . toString ( 'base64' ) } ` ,
277- } ,
278- body : 'grant_type=client_credentials&scope=agentcore/invoke' ,
279- } ) ;
280- expect ( tokenRes . ok , `Token fetch failed: ${ tokenRes . status } ` ) . toBe ( true ) ;
281- const tokenJson = ( await tokenRes . json ( ) ) as { access_token : string } ;
282- const accessToken = tokenJson . access_token ;
283- expect ( accessToken , 'Should have received an access token' ) . toBeTruthy ( ) ;
287+ const accessToken = await fetchCognitoAccessToken ( ) ;
284288
285289 // Invoke with bearer token — should NOT get auth mismatch
286290 const result = await runLocalCLI (
@@ -313,18 +317,7 @@ describe.sequential('e2e: BYO agent with CUSTOM_JWT auth', () => {
313317 async ( ) => {
314318 expect ( projectPath , 'Project should have been deployed' ) . toBeTruthy ( ) ;
315319
316- const tokenUrl = `https://${ domainPrefix } .auth.${ region } .amazoncognito.com/oauth2/token` ;
317- const tokenRes = await fetch ( tokenUrl , {
318- method : 'POST' ,
319- headers : {
320- 'Content-Type' : 'application/x-www-form-urlencoded' ,
321- Authorization : `Basic ${ Buffer . from ( `${ clientId } :${ clientSecret } ` ) . toString ( 'base64' ) } ` ,
322- } ,
323- body : 'grant_type=client_credentials&scope=agentcore/invoke' ,
324- } ) ;
325- expect ( tokenRes . ok , `Token fetch failed: ${ tokenRes . status } ` ) . toBe ( true ) ;
326- const tokenJson = ( await tokenRes . json ( ) ) as { access_token : string } ;
327- const accessToken = tokenJson . access_token ;
320+ const accessToken = await fetchCognitoAccessToken ( ) ;
328321
329322 const result = await runLocalCLI (
330323 [ 'invoke' , '--agent' , mcpAgentName , 'list-tools' , '--bearer-token' , accessToken , '--json' ] ,
@@ -342,18 +335,7 @@ describe.sequential('e2e: BYO agent with CUSTOM_JWT auth', () => {
342335 async ( ) => {
343336 expect ( projectPath , 'Project should have been deployed' ) . toBeTruthy ( ) ;
344337
345- const tokenUrl = `https://${ domainPrefix } .auth.${ region } .amazoncognito.com/oauth2/token` ;
346- const tokenRes = await fetch ( tokenUrl , {
347- method : 'POST' ,
348- headers : {
349- 'Content-Type' : 'application/x-www-form-urlencoded' ,
350- Authorization : `Basic ${ Buffer . from ( `${ clientId } :${ clientSecret } ` ) . toString ( 'base64' ) } ` ,
351- } ,
352- body : 'grant_type=client_credentials&scope=agentcore/invoke' ,
353- } ) ;
354- expect ( tokenRes . ok , `Token fetch failed: ${ tokenRes . status } ` ) . toBe ( true ) ;
355- const tokenJson = ( await tokenRes . json ( ) ) as { access_token : string } ;
356- const accessToken = tokenJson . access_token ;
338+ const accessToken = await fetchCognitoAccessToken ( ) ;
357339
358340 const result = await runLocalCLI (
359341 [
0 commit comments