How to use the code returned from Cognito to get AWS credentials?

Now I'm trying to understand AWS Cognito, so maybe someone can help me. I set up a domain to serve the Cognito UI for my user pool, as described here . Therefore, when I https://<my-domain>.auth.us-east-1.amazoncognito.com/login?response_type=code&client_id=<MY_POOL_CLIENT_ID>&redirect_uri=https://localhost:8080log in I get a login page where my users can log in to my application using Google. This part works just fine.

I am confused about what to do with the code that returns from this page when my user logs in. Therefore, when I redirect to Google and authorize the application to view my information, I am redirected back to one of my URLs with the code in the request parameters. I am currently redirecting to localhost, so the redirect URL looks like this:

https://localhost:8080/?code=XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX

What exactly is this code? Also, how can I use it to access AWS resources for my user?

+10
source share
3 answers

AWS (id_token, access_token refresh_token) , oath2, - Cognito User Pool /oauth2/token, https://docs.aws.amazon. com/cognito/latest/developerguide/token-endpoint.html.

HTTP Basic Authorization, Cognito App client_id client_secret, invalid_client.

, , URL. , response_type=token, .

+6

, 10 , , .

response_type=token ( Oauth = implicit grant = openid) Cognito. id_token & access_token. , id_token. , , , , . ( , javascript), javascript Cognito. -

function getAccessToken(idToken, identityPoolId, userPool) {
        let provider = "cognito-idp.us-east-2.amazonaws.com/" + userPool;
        let login = {};

        login[provider] = idToken;

        // Add the User Id Token to the Cognito credentials login map.
        let credentials = new AWS.CognitoIdentityCredentials({
            IdentityPoolId: identityPoolId,
            Logins: login
        });

        //call refresh method in order to authenticate user and get new temp credentials
        credentials.get((error) => {
            if (error) {
                console.error(error);

                let response = {
                    statusCode: 500,
                    body: JSON.stringify(error)
                };

                return response;

            } else {
                console.log('Successfully logged!');
                console.log('AKI:'+ credentials.accessKeyId);
                console.log('AKS:'+ credentials.secretAccessKey);
                console.log('token:' + credentials.sessionToken);

                let response = {
                    statusCode: 200,
                    body: JSON.stringify({
                        'AKI': credentials.accessKeyId,
                        'AKS': credentials.secretAccessKey,
                        'token': credentials.sessionToken
                    })
                };

                return response;
            }
        });
    }

.

+3
source

Source: https://habr.com/ru/post/1684068/


All Articles