0
votes

I am using Congnito User Pool to perform API Gateway authorization which is working fine. Now, I am trying to add Instagram as one of the login provider, for that I created Custom Authentication Provider in Federated Identities and using code below:

var cognitoidentity = new AWS.CognitoIdentity({ apiVersion: '2014-06-30' });

var params = {
    IdentityPoolId: 'us-east-1:7d99e750-.....',
    Logins: {
        'login.instagram': 'Access-Token-Returned-By-Instagram',
    },
    TokenDuration: 60
};

cognitoidentity.getOpenIdTokenForDeveloperIdentity(params, function (err, data) {
    if (err) {
        console.log(err, err.stack);
    } else {
        console.log(data);
        var idParams = {
            IdentityId: data['IdentityId'],
            Logins: {
                'cognito-identity.amazonaws.com': data['Token']
            }
        };

        cognitoidentity.getCredentialsForIdentity(idParams, function (err2, data2) {
            if (err2) console.log(err2, err2.stack); // an error occurred
            else console.log(data2);           // successful response
        });
    }
});

I am able to get accessToken and sessionToken, however, I am still unable to find a way to get idToken and accessToken which is required by API Gateway to authorize the incoming request.

I tried looking into SDK as well as AWS forum, but I am still unable to find a way to use custom federated identity provider to authorize API Gateway which use a cognito user pool.

1

1 Answers

0
votes

I am going to take a whirl at this....

  1. Do you have a cognito user?

    • No
import { CognitoUser, CognitoUserPool, AuthenticationDetails } from "amazon-cognito-identity-js";

let cognitoUser;

const userPool = new CognitoUserPool({
   UserPoolId: config.USER_POOL.pool_Id, //your userpool id
   ClientId: config.appClientId, //your appClient 
});

const userData = {
       Username: 'user name',
       Pool: userPool
 };

cognitoUser = new CognitoUser(userData);
/* Should now have a cognitoUser */
  1. Authenticate your cognito user
const authenticationData = {
    Username : payload.userName,
    Password : payload.password,
};

const authenticationDetails = new AuthenticationDetails(authenticationData);

cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
        const accessToken = result.getAccessToken().getJwtToken();

        /* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer*/
         const idToken = result.idToken.jwtToken;

          /*
            Do something with 
              idToken
              accessToken

           I would write them to a file or encrypt them and store them on the localStorage OR encrypt and save in REDUX store.
          */


    },

    onFailure: function(err) {
         //handle error
   },

});