Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Cognito Identity rather than AWS credentials ? #69

Open
BerndWessels opened this issue Apr 26, 2018 · 2 comments
Open

How to use Cognito Identity rather than AWS credentials ? #69

BerndWessels opened this issue Apr 26, 2018 · 2 comments

Comments

@BerndWessels
Copy link

Hi @mhart
I'd like to call AWS services via https but rather than using fixed AWS credentials I want to use the Cognito Identity credentials of the currently logged in Cognito User.
How can this be done?

@rasovica
Copy link

You can just do
AWS.config.credentials = new AWS.CognitoIdentityCredentials(params)

@CyberCyclone
Copy link

CyberCyclone commented Mar 23, 2020

You can just do
AWS.config.credentials = new AWS.CognitoIdentityCredentials(params)

This doesn't work. It ignores it and tries to use the AWS creds from the environment variable, which in my case is undefined and fails.

This seems to work:

    var params = {
            "IdentityId" : "CognitoIdentity",
            "Logins" : {
                'cognito-identity.amazonaws.com': "CognitoToken"
            }
        }
        var cognitoidentity = new AWS.CognitoIdentity();
        var tmp = await cognitoidentity.getCredentialsForIdentity(params).promise();
        tmp.region = "us-east-1";
        tmp.IdentityPoolId = "userPoolId"; 
        tmp.Logins = {
            'cognito-identity.amazonaws.com' :  "CognitoToken",
        }

        aws4.sign(
            {
                host: ('bucket-name.s3.amazonaws.com'),
                service: 's3',
                region: "us-east-1",
                path: "/bucketKey/path,
                method: 'PUT',
                signQuery: true
            },
            {
                accessKeyId: tmp.Credentials.AccessKeyId,
                secretAccessKey: tmp.Credentials.SecretKey,
            }
        );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants