dorita980-node18 – Rev 1

Subversion Repositories:
Rev:
#!/usr/bin/env node

'use strict';

const request = require('request');

if (!process.argv[2] || !process.argv[3]) {
  console.log('Usage: npm run get-password-cloud   [Gigya API Key]');
  process.exit();
}

const username = process.argv[2];
const password = process.argv[3];
const apiKey = process.argv[4] || process.env.GIGYA_API_KEY || '3_rWtvxmUKwgOzu3AUPTMLnM46lj-LxURGflmu5PcE_sGptTbD-wMeshVbLvYpq01K';

const gigyaLoginOptions = {
  'method': 'POST',
  'uri': 'https://accounts.us1.gigya.com/accounts.login',
  'json': true,
  'qs': {
    'apiKey': apiKey,
    'targetenv': 'mobile',
    'loginID': username,
    'password': password,
    'format': 'json',
    'targetEnv': 'mobile'
  },
  'headers': {
    'Connection': 'close'
  }
};

request(gigyaLoginOptions, loginGigyaResponseHandler);

function loginGigyaResponseHandler (error, response, body) {
  if (error) {
    console.log('Fatal error login into Gigya API. Please check your credentials or Gigya API Key.');
    console.log(error);
    process.exit(1);
  }

  if (response.statusCode === 401 || response.statusCode === 403) {
    console.log('Authentication error. Check your credentials.');
    console.log(response);
    process.exit(1);
  } else if (response.statusCode === 400) {
    console.log(response);
    process.exit(1);
  } else if (response.statusCode === 200) {
    if (body && body.statusCode && body.statusCode === 403) {
      console.log('Authentication error. Please check your credentials.');
      console.log(body);
      process.exit(1);
    }
    if (body && body.statusCode && body.statusCode === 400) {
      console.log('Error login into Gigya API.');
      console.log(body);
      process.exit(1);
    }
    if (body && body.statusCode && body.statusCode === 200 && body.errorCode === 0 && body.UID && body.UIDSignature && body.signatureTimestamp && body.sessionInfo && body.sessionInfo.sessionToken) {
      const iRobotLoginOptions = {
        'method': 'POST',
        'uri': 'https://unauth2.prod.iot.irobotapi.com/v2/login',
        'json': true,
        'body': {
          'app_id': 'ANDROID-C7FB240E-DF34-42D7-AE4E-A8C17079A294',
          'assume_robot_ownership': 0,
          'gigya': {
            'signature': body.UIDSignature,
            'timestamp': body.signatureTimestamp,
            'uid': body.UID
          }
        },
        'headers': {
          'Connection': 'close'
        }
      };
      request(iRobotLoginOptions, loginIrobotResponseHandler);
    } else {
      console.log('Error login into iRobot account. Missing fields in login response.');
      console.log(body);
      process.exit(1);
    }
  } else {
    console.log('Unespected response. Checking again...');
  }
}

function loginIrobotResponseHandler (error, response, body) {
  if (error) {
    console.log('Fatal error login into iRobot account. Please check your credentials or API Key.');
    console.log(error);
    process.exit(1);
  }
  if (body && body.robots) {
    const robotCount = Object.keys(body.robots).length;
    console.log('Found ' + robotCount + ' robot(s)!');
    Object.keys(body.robots).map(function (r) {
      console.log('Robot "' + body.robots[r].name + '" (sku: ' + body.robots[r].sku + ' SoftwareVer: ' + body.robots[r].softwareVer + '):');
      console.log('BLID=> ' + r);
      console.log('Password=> ' + body.robots[r].password + ' <= Yes, all this string.');
      console.log('');
    });
    console.log('Use this credentials in dorita980 lib :)');
  } else {
    console.log('Fatal error login into iRobot account. Please check your credentials or API Key.');
    console.log(body);
    process.exit(1);
  }
}