¿Cómo llamar a AWS API Gateway Endpoint con Cognito Id (+ configuración)?
Quiero llamar a unAWS API Gateway Endpoint
que está protegido conAWS_IAM
utilizando lagenerated JavaScript API SDK
.
tengo unCognito UserPool
y unCognito Identity Pool
. Ambos correctamente sincronizados a través deClientId
.
Yo uso este código paraSign in
y obtener elCognito Identity
AWS.config.region = 'us-east-1'; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-1:XXXXXXXXXXXXXXXXXXXXXXXX' // your identity pool id here
});
AWSCognito.config.region = 'us-east-1';
AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-1:XXXXXXXXXXXXXXXXXXXXXXXX' // your identity pool id here
});
var poolData = {
UserPoolId: 'us-east-1_XXXXXXXX',
ClientId: 'XXXXXXXXXXXXXXXXXXXXXXXX'
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var authenticationData = {
Username: 'user',
Password: '12345678',
};
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
var userData = {
Username: 'user',
Pool: userPool
};
var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
console.log('access token + ' + result.getAccessToken().getJwtToken());
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-1:XXXXXXXXXXXXXXXXXXXX',
IdentityId: AWS.config.credentials.identityId,
Logins: {
'cognito-idp.us-east-1.amazonaws.com/us-east-1_XXXXXX': result.idToken.jwtToken
}
});
AWS.config.credentials.get(function (err) {
// now I'm using authenticated credentials
if(err)
{
console.log('error in autheticatig AWS'+err);
}
else
{
console.log(AWS.config.credentials.identityId);
}
});
},
onFailure: function (err) {
alert(err);
}
});
Todo esto tiene éxito y tengo unauthorized Cognito Identity
ahora.
Ahora trato de llamar alAPI Gateway Endpoint
para ejecutar elLambda Function
señala
var apigClient = apigClientFactory.newClient({
accessKey: AWS.config.credentials.accessKeyId, //'ACCESS_KEY',
secretKey: AWS.config.credentials.secretAccessKey, //'SECRET_KEY',
sessionToken: AWS.config.credentials.sessionToken, // 'SESSION_TOKEN', //OPTIONAL: If you are using temporary credentials you must include the session token
region: 'us-east-1' // OPTIONAL: The region where the API is deployed, by default this parameter is set to us-east-1
});
var params = {
// This is where any modeled request parameters should be added.
// The key is the parameter name, as it is defined in the API in API Gateway.
};
var body = {
// This is where you define the body of the request,
query: '{person {firstName lastName}}'
};
var additionalParams = {
// If there are any unmodeled query parameters or headers that must be
// sent with the request, add them here.
headers: {},
queryParams: {}
};
apigClient.graphqlPost(params, body, additionalParams)
.then(function (result) {
// Add success callback code here.
console.log(result);
}).catch(function (result) {
// Add error callback code here.
console.log(result);
});
Pero desafortunadamente esto falla. losOPTIONS
solicitud tiene éxito con200
pero elPOST
luego falla con403
.
Estoy bastante seguro de que no hayCORS
problema aquí
Estoy bastante seguro de que el problema tiene que ver conIAM Roles
yAWS Resource Configurations
.
Mi pregunta es básicamente, ¿podrían proporcionarme todo lo necesario?AWS Resource Configurations
yIAM Roles
que son necesarios para que esto funcione por favor?
Los recursos que tengo son
Puerta de enlace API: con puntos finales API implementadosFunción Lambda: llamada por el punto finalCognito User Pool: con la aplicación sincronizada con el Identity PoolCognito Identity Pool: con un rol autorizado y no autorizado asignado a él.Roles de IAM: para la función Lambda y el rol autorizado y no autorizado del grupo de identidad de Cognito.Pero no sé cómo estos recursos deben configurarse correctamente para que esto funcione.
Gracias