Get JWT token in GO

Hi,

I want to implement logic below(in javascript) to get JWT token for a user in GO (Receiving the JWT token from Cognito UserPools).
I looked at https://github.com/aws/aws-sdk-go but couldn’t find corresponding classes.
Can somebody please guide me how to implement this in GO?

Thanks

 var authenticationData = {
        Username : 'username',
        Password : 'password',
    };
    var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
    var poolData = { UserPoolId : 'us-east-1_xxxxx',
        ClientId : 'xxxxxxxxxxxxxxxx'
    };
    var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
    var userData = {
        Username : 'username',
        Pool : userPool
    };
    var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            console.log('access token + ' + 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*/
            console.log('idToken + ' + result.idToken.jwtToken);
        },

        onFailure: function(err) {
            alert(err);
        },

    });

Do understand your question correctly: you want to port this JavaScript code to Go? Which JS libraries do you use here and what do they do?

Yes. I want to port this code to Go. I am new to GO and learning.

Here is what I am trying to achieve :-
From GO call AWS Cognito API with username and password to get the JWT token.

I got this sample code from amazon docs example for javascript SDK and that is exactly what I want to achieve in GO.
https://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-javascript-examples.html.

In GO SDK I couldn’t find similar classes/methods as shown in javascript example, So trying to find mapped class and its methods which will help me achieve the same.

Check out this repository: seems to solve your issue ( https://github.com/mura123yasu/go-cognito )

1 Like

For JWT I use library github.com/dgrijalva/jwt-go and jwt middleware in Echo framework.

2 Likes

Just to add, lot of popular JWT middlewares actually use this lib as well.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.