function getCredentials()

in lambda/apigw-sap-idoc-authorizer/index.js [87:122]


function getCredentials(authData){
  return new Promise(resolve => {
    var response = {
      success: false,
      authResult: {}
    } 
    try{
      // Create credentials object
      var creds = new AWS.Credentials({
        accessKeyId: authData.username, 
        secretAccessKey: authData.password
      });
      // Try to access the S3 bucket for IDOC.
      var s3 = new AWS.S3({
        credentials: creds
      })
      var s3params = {
        Bucket: authData.bucket,
        MaxKeys: 1
      }
      //List objects in the S3 bucket. If the credentials are incorrect, then this will fail
      s3.listObjects(s3params,function(err,data){
        if(err){
          console.log("Error in accessing bucket", err);
          resolve(response) 
        }else{
          response.success = true
          resolve(response)
        }
      })
    }catch(e){
      console.log("Error in getting creds", e);
      resolve(response) 
    }
  })
}