I developed a nodejs code in my lambda function that retrieve token from AWS.
I succeed to get token, but when I am using this token to request any endpoint I got 401 Unauthorized.
I decoded it with JWT and I got:
{
"sub": "*****",
"iss": "https://cognito-idp.us-east-1.amazonaws.com/us-east-*****",
"client_id": "*****",
"origin_jti": "*****",
"event_id": "******",
"token_use": "access",
"scope": "aws.cognito.signin.user.admin",
"auth_time": 1719906953,
"exp": 1719910553,
"iat": 1719906953,
"jti": "*****",
"username": "*****"
}
with postman I am getting the following token
{
"sub": "*****",
"iss": "https://cognito-idp.us-east-1.amazonaws.com/us-east-*****",
"version": 2,
"client_id": "*****",
"origin_jti": "*****",
"token_use": "access",
"scope": "aws.cognito.signin.user.admin phone openid mpfapi/fullaccess email",
"auth_time": 1719917974,
"exp": 1719921574,
"iat": 1719917974,
"jti": "*****",
"username": "*****"
}
if I use the token retrieved from my nodejs code then AWS will return 401
if I use the token retrieved from postman then it works
here after my nodejs source code:
const getSecretHash = (username, clientId, clientSecret) => {
return crypto.createHmac('SHA256', clientSecret)
.update(username + clientId)
.digest('base64');
};
const authenticateUser = async (username, password) => {
const client = new CognitoIdentityProviderClient({ region: RYSEregion });
const secretHash = getSecretHash(username, poolData.ClientId, poolData.ClientSecret);
const params = {
AuthFlow: 'USER_PASSWORD_AUTH',
AuthParameters: {
'USERNAME': username,
'PASSWORD': password,
'SECRET_HASH': secretHash
},
ClientId: poolData.ClientId
};
try {
const command = new InitiateAuthCommand(params);
const response = await client.send(command);
return response.AuthenticationResult.IdToken;
} catch (error) {
console.error("Error: ", error);
throw error;
}
};
what I am missing ?