@@ -2,6 +2,7 @@ import { stateManager, $TSContext } from 'amplify-cli-core';
22import aws from 'aws-sdk' ;
33import _ from 'lodash' ;
44import fetch from 'node-fetch' ;
5+ import { adminLoginFlow } from '../admin-login' ;
56import { AdminAuthConfig , AwsSdkConfig , CognitoAccessToken , CognitoIdToken } from './auth-types' ;
67
78export const adminVerifyUrl = ( appId : string , envName : string , region : string ) : string => {
@@ -27,8 +28,11 @@ export async function isAmplifyAdminApp(appId: string): Promise<{ isAdminApp: bo
2728 return { isAdminApp : ! ! appState . appId , region : appState . region } ;
2829}
2930
30- export async function getTempCredsWithAdminTokens ( appId : string , print : $TSContext [ 'print' ] ) : Promise < AwsSdkConfig > {
31- const authConfig = await getRefreshedTokens ( appId , print ) ;
31+ export async function getTempCredsWithAdminTokens ( context : $TSContext , appId : string ) : Promise < AwsSdkConfig > {
32+ if ( ! doAdminTokensExist ( appId ) ) {
33+ await adminLoginFlow ( context , appId ) ;
34+ }
35+ const authConfig = await getRefreshedTokens ( context , appId ) ;
3236 const { idToken, IdentityId, region } = authConfig ;
3337 // use tokens to get creds and assign to config
3438 const awsConfig = await getAdminCognitoCredentials ( idToken , IdentityId , region ) ;
@@ -82,17 +86,23 @@ async function getAdminStsCredentials(idToken: CognitoIdToken, region: string):
8286 } ;
8387}
8488
85- export async function getRefreshedTokens ( appId : string , print : $TSContext [ 'print' ] ) {
89+ async function getRefreshedTokens ( context : $TSContext , appId : string ) {
8690 // load token, check expiry, refresh if needed
8791 const authConfig : AdminAuthConfig = stateManager . getAmplifyAdminConfigEntry ( appId ) ;
8892
8993 if ( isJwtExpired ( authConfig . idToken ) ) {
90- const refreshedTokens = await refreshJWTs ( authConfig , print ) ;
91- // Refresh stored tokens
92- authConfig . accessToken . jwtToken = refreshedTokens . AccessToken ;
93- authConfig . idToken . jwtToken = refreshedTokens . IdToken ;
94- authConfig . refreshToken . token = refreshedTokens . RefreshToken ;
95- stateManager . setAmplifyAdminConfigEntry ( appId , authConfig ) ;
94+ let refreshedTokens : aws . CognitoIdentityServiceProvider . AuthenticationResultType ;
95+ try {
96+ refreshedTokens = ( await refreshJWTs ( authConfig ) ) . AuthenticationResult ;
97+ // Refresh stored tokens
98+ authConfig . accessToken . jwtToken = refreshedTokens . AccessToken ;
99+ authConfig . idToken . jwtToken = refreshedTokens . IdToken ;
100+ authConfig . refreshToken . token = refreshedTokens . RefreshToken ;
101+ stateManager . setAmplifyAdminConfigEntry ( appId , authConfig ) ;
102+ } catch {
103+ // Refresh failed, fall back to login
104+ await adminLoginFlow ( context , appId , null , authConfig . region ) ;
105+ }
96106 }
97107 return authConfig ;
98108}
@@ -103,21 +113,15 @@ function isJwtExpired(token: CognitoAccessToken | CognitoIdToken) {
103113 return secSinceEpoch >= expiration - 60 ;
104114}
105115
106- async function refreshJWTs ( authConfig : AdminAuthConfig , print : $TSContext [ 'print' ] ) {
116+ async function refreshJWTs ( authConfig : AdminAuthConfig ) {
107117 const CognitoISP = new aws . CognitoIdentityServiceProvider ( { region : authConfig . region } ) ;
108- try {
109- const result = await CognitoISP . initiateAuth ( {
110- AuthFlow : 'REFRESH_TOKEN' ,
111- AuthParameters : {
112- REFRESH_TOKEN : authConfig . refreshToken . token ,
113- } ,
114- ClientId : authConfig . accessToken . payload . client_id , // App client id from identityPool
115- } ) . promise ( ) ;
116- return result . AuthenticationResult ;
117- } catch ( e ) {
118- print . error ( `Failed to refresh tokens: ${ e . message || 'Unknown error occurred' } ` ) ;
119- throw e ;
120- }
118+ return await CognitoISP . initiateAuth ( {
119+ AuthFlow : 'REFRESH_TOKEN' ,
120+ AuthParameters : {
121+ REFRESH_TOKEN : authConfig . refreshToken . token ,
122+ } ,
123+ ClientId : authConfig . accessToken . payload . client_id , // App client id from identityPool
124+ } ) . promise ( ) ;
121125}
122126
123127export const adminBackendMap : {
0 commit comments