awslabs / aws-amplify-identity-broker

A centralized login and SSO application to authenticate several websites and mobile apps.
MIT License
211 stars 68 forks source link

Got unexpected status code 502 when updating token via 'oauth2/token' #537

Open chunfan-3t opened 7 months ago

chunfan-3t commented 7 months ago

@ amplify/backend/function/amplifyIdentityBrokerToken/src/index.js

else if (grant_type === "refresh_token") {

    var cognitoResponse = await cognitoSP.initiateAuth(params).promise(); // Call Cognito with refresh token to get refreshed id and access tokens
    var access_token = cognitoResponse.AuthenticationResult.AccessToken;
    var id_token = cognitoResponse.AuthenticationResult.IdToken;

}

The promise of cognitoSP.initiateAuth(params) may throw an error. IMO, we should add a try catch to properly handle this.

try {
    var cognitoResponse = await cognitoSP.initiateAuth(params).promise();
} catch (error) {
    const errorBody = {
        errorType: error. errorType,
        errorMessage: error. errorMessage,
        time: error.time,
            ...
        stack: error.stack
    }
    return {
        statusCode: error.statusCode,
        body: JSON.stringify(errorBody)
    };
}