API gateway not accepting the token received from @aws-sdk/client-cognito-identity-provider using AdminInitiateAuthCommand
#4866
-
I'm authenticating users via Username/Password on my Nodejs server using the
The After I changed my code from this: import {
CognitoUserPool,
AuthenticationDetails,
CognitoUser,
CognitoUserSession,
} from 'amazon-cognito-identity-js';
export class CognitoService {
async authenticateUser(
username: string,
password: string
): Promise<CognitoUserSession> {
const authenticationData = {
Username: username,
Password: password,
};
const authenticationDetails = new AuthenticationDetails(authenticationData);
const cognitoUser = new CognitoUser({
Username: authenticationData.Username,
Pool: UserPool,
});
return new Promise<CognitoUserSession>((resolve, reject) => {
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: (result) => {
resolve(result);
},
onFailure: (err) => {
reject(err);
},
});
});
} To this: import {
CognitoIdentityProviderClient,
ForgotPasswordCommand,
SignUpCommand,
AuthFlowType,
AdminInitiateAuthCommand,
} from '@aws-sdk/client-cognito-identity-provider';
export class CognitoService {
private cognitoClient: CognitoIdentityProviderClient;
constructor() {
this.cognitoClient = new CognitoIdentityProviderClient({
region: environment.AWS_REGION,
credentials: {
accessKeyId: environment.AWS_ACCESS_KEY_ID,
secretAccessKey: environment.AWS_SECRET_ACCESS_KEY,
},
});
}
async authenticateUser(username: string, password: string) {
const command = new AdminInitiateAuthCommand({
ClientId: userPoolData.ClientId,
UserPoolId: userPoolData.UserPoolId,
AuthFlow: AuthFlowType.ADMIN_USER_PASSWORD_AUTH,
AuthParameters: { USERNAME: username, PASSWORD: password },
});
return this.cognitoClient.send(command);
}
} I had to change the code to get the tokens from previous and new responses from the Old response handling of const authentication = await this.cognitoService.authenticateUser(
email,
password
);
const refreshToken = authentication.getRefreshToken().getToken();
const authToken = authentication.getIdToken().getJwtToken();
// other operations... And new way:
I even manually checked in API Gateway test authorizer to see if the generated token works or not, there also I go 401 Unauthorized response: One change I had to make in change Cognito App client was to add the one more Authentication Flow: Can anyone help me what am I doing wrong here? or should I just stay with Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi @hirenchauhan2, The error you are seeing is returned from your lambda authorizer. After the auth process, you are given a JWT token. You can decode that token manually and examine the contents. Is it different than the one returned by using the other library? From what I can tell in your first implementation you are using the IdToken, and in the second example you are providing the access token (which normally you would use), is this your intention? // old:
const authToken = authentication.getIdToken().getJwtToken(); // ID token
// new:
const authToken = authentication.AuthenticationResult.AccessToken; // access token
// new with ID Token:
const idToken = authentication.AuthenticationResult.IdToken; // ID token In a lambda authorizer you'd want to use the access token because it contains important information like scope and claims which is normally needed for authorization, whereas an ID token would be more for getting user information. So maybe your lambda authorizer is authorizing based on user info found in the ID token? This would be up to you to determine how you are handling auth on your lambda authorizer. I suggest you give this article a read as it highlights how a Lambda Authorizer works. All the best, |
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
Hi @hirenchauhan2,
The error you are seeing is returned from your lambda authorizer. After the auth process, you are given a JWT token. You can decode that token manually and examine the contents. Is it different than the one returned by using the other library?
From what I can tell in your first implementation you are using the IdToken, and in the second example you are providing the access token (which normally you would use), is this your intention?