5

Chcę zadzwonić pod numer AWS API Gateway Endpoint, który jest chroniony przy użyciu AWS_IAM przy użyciu generated JavaScript API SDK.Jak wywołać punkt końcowy bramy AWS API z identyfikatorem Cognito (konfiguracja +)?

Mam Cognito UserPool i Cognito Identity Pool. Oba poprawnie zsynchronizowane przez ClientId.

Używam tego kodu do Sign in i uzyskać Cognito 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); 
    } 

}); 

Wszystko to powiedzie i mam authorized Cognito Identity teraz.

Teraz próbuję wywołać API Gateway Endpoint, aby wykonać Lambda Function wskazuje na to.

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); 
    }); 

Ale niestety to się nie udaje. Żądanie OPTIONS kończy się pomyślnie: 200, ale POST następnie kończy się niepowodzeniem z 403.

Jestem prawie pewien, że nie ma tutaj problemu z CORS.

Jestem prawie pewien, że problem ma związek z IAM Roles i AWS Resource Configurations.

Moje pytanie brzmi w zasadzie, czy możesz podać mi wszystkie niezbędne AWS Resource Configurations i IAM Roles, które są niezbędne do tego, aby działał?

Resources Mam są

  • API Gateway - z wdrożonych Endpoints API
  • Lambda Function - nazywany przez punkt końcowy
  • Basen
  • Cognito użytkownika - z App synchronizowane tożsamość puli
  • Cognito Tożsamości Pula - z przypisaną do niej uprawnioną i nieautoryzowaną rolą.
  • Role IAM - dla funkcji Lambda i autoryzowanej i nieautoryzowanej roli puli tożsamości Cognito.

Ale nie wiem, jak te zasoby muszą być odpowiednio skonfigurowane, aby to działało.

Dziękuję

Odpowiedz

3

Jakie uprawnienia ma dostęp rola Tożsamości Cognito mieć? Upewnij się, że ma on dostęp do funkcji execute-api:Invoke w swoim interfejsie API.

{ 
    "Version": "2012-10-17", 
    "Statement": [ 
    { 
     "Effect": "Allow", 
     "Action": [ 
     "execute-api:Invoke"   
     ], 
     "Resource": [ 
     "arn:aws:execute-api:us-east-1:<account>:<rest-api>/*/POST/graphql" 
     ] 
    } 
    ] 
} 

Możesz uzyskać dokładny ARN zasobów ze strony ustawień metod w konsoli internetowej.

+0

Awesome, dziękuję. To był brakujący element układanki. – Christine

1

Nawet po podążaniu za wszystkim otrzymywałem ten sam błąd. Powodem było pominięcie "znacznika sesji" podczas inicjowania apigClient.

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 }); 

// Opcjonalne: Jeśli używasz tymczasowe poświadczenia należy dołączyć sesji tokena - nie jest to opcja

+0

W Cognito używasz tymczasowych danych uwierzytelniających. Ale użycie Cognito jest opcjonalne, wystarczy użyć standardowych poświadczeń, ale nie jest to zalecane. W każdym razie możesz *, * dlatego właśnie ten komentarz mówi, że jest opcjonalny. –

Powiązane problemy