Setup Sms MFA - nodejs - CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

Viewed 29

I am trying to setup MFA Preference for sms using 'aws-sdk', method adminSetUserMFAPreference (or another setUserMFAPreference): , and getting same the error:

CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG = 1

I have tied to add apiVersion, accessKeyId, accessSecretKey to config, pls see below but error is the same.Also created AIM user with policy AmazonCognitoDeveloperAuthenticatedIdentities and add ~/.aws/credentilas:

[default ]
aws_access_key_id = 'xxxxxxxxxxxx'
aws_secret_access_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

import AWS from 'aws-sdk';
import crypto from 'crypto';
import * as dotenv from 'dotenv';

dotenv.config();

class CognitoService {
  private config = {
    region: process.env.POOL_REGION,
    apiVersion: '2016-04-18',
    accessKeyId: 'xxxxxxxxxxxx',
    accessSecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  };

  private secretHash: string = process.env.SECRET_HASH as string;

  private clientId: string = process.env.CLIENT_ID as string;

  private userPoolId: string = process.env.USER_POOL_ID as string;

  private cognitoIdentity;

  constructor() {
    this.cognitoIdentity = new AWS.CognitoIdentityServiceProvider(this.config);
  }

  ...

  public async setupAdSmsMFA(
    username: string,
    promptedAllSignIs: boolean,
  ): Promise<any> {
    const params = {
      UserPoolId: this.userPoolId,
      Username: username,
      SMSMfaSettings: {
        Enabled: promptedAllSignIs,
        PreferredMfa: true,
      },
      SoftwareTokenMfaSettings: {
        Enabled: false,
        PreferredMfa: false,
      },
    };
    try {
      const data = this.cognitoIdentity.adminSetUserMFAPreference(params).promise();
      return data;
    } catch (error) {
      console.log('##ERROR: ', error);
      return error;
    }
  }

  private generateHash(email: string): string {
    return crypto.createHmac('SHA256', this.secretHash)
      .update(email + this.clientId)
      .digest('base64');
  }
}

export default CognitoService;

0 Answers
Related