all files / lib/ account.js

36.73% Statements 54/147
32.69% Branches 34/104
43.48% Functions 10/23
28.57% Lines 36/126
1 statement, 1 function, 6 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _request = require('request');
 
var _request2 = _interopRequireDefault(_request);
 
var _cheerio = require('cheerio');
 
var _cheerio2 = _interopRequireDefault(_cheerio);
 
var _package = require('../package.json');
 
var _package2 = _interopRequireDefault(_package);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
/*
 * The ClipperAccount class serves as the wrapper for the account itself and is
 * the entry point for logging in to the Clipper Card. It has profile and
 * payment data bound to it, and provides access to the cards and their
 * statements.
 */
 
var ClipperAccount = function () {
 
  /**
   * Constructor to create a new ClipperAccount
   *
   * @param {object} options - Contains required options for creating the class
   */
 
  function ClipperAccount(options) {
    _classCallCheck(this, ClipperAccount);
 
    this.MAX_LOGIN_ATTEMPTS = 3;
 
    this.uris = {
      login: 'https://www.clippercard.com/ClipperCard/loginFrame.jsf',
      dashboard: 'https://www.clippercard.com/ClipperCard/dashboard.jsf',
      sessionExpired: 'https://www.clippercard.com/ClipperCard/sessionExpired.jsf'
    };
 
    this.userAgent = _package2.default.name + '@' + _package2.default.version;
 
    this.cookies = undefined;
 
    this.email = undefined;
    this.password = undefined;
 
    this.profile = undefined;
    this.cards = undefined;
 
    this._failedLoginCount = 0;
 
    Eif ('object' === (typeof options === 'undefined' ? 'undefined' : _typeof(options))) {
 
      // check we have a valid email address
      if (options.email && 'string' === typeof options.email) {
        this.email = options.email;
      } else {
        throw new Error('Email address must be provided in the class options');
      }
 
      // check we have a valid password
      if (options.password && 'string' === typeof options.password) {
        this.password = options.password;
      } else {
        throw new Error('The password must be provided in the class options');
      }
 
      if (options.userAgent) {
        this.userAgent = options.userAgent;
      }
    }
  }
 
  /**
   * Loads the login form and extracts the form elements, then submits the
   * form. By the time the callback is invoked, the cookie jar is in the right
   * state to be logged in. Once a cookie jar exists, this method will lazy
   * bypass itself unless forceRefresh=true.
   *
   * @param {function} callback - invoked as callback(error);
   * @param {boolean} forceRefresh - set true to refresh cookies [false]
   */
 
 
  _createClass(ClipperAccount, [{
    key: 'login',
    value: function login(callback) {
      var forceRefresh = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
 
      if ('function' !== typeof callback) {
        callback = function callback() {};
      }
 
      if (false === forceRefresh && 'object' === _typeof(this.cookies)) {
        return callback(null);
      }
 
      this._failedLoginAttempts = 0;
      this._login(callback);
    }
 
    /* Processes the actual login request
     *
     * @param {function} callback - invoked as callback(error);
     */
 
  }, {
    key: '_login',
    value: function _login(callback) {
      var self = this;
 
      this.cookies = _request2.default.jar();
 
      (0, _request2.default)({
        method: 'GET',
        uri: this.uris.login,
        gzip: true,
        jar: this.cookies
      }, function (error, response, body) {
        if (error) {
          return callback(error);
        }
 
        var $ = _cheerio2.default.load(body);
        var form = $('form').first().serializeArray();
        var formId = $('form').first().attr('id');
 
        // parse the form elements
        var params = {};
        var _iteratorNormalCompletion = true;
        var _didIteratorError = false;
        var _iteratorError = undefined;
 
        try {
          for (var _iterator = form[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
            var i = _step.value;
 
            if (undefined !== i.name && 'string' === typeof i.name) {
              params[i.name] = i.value;
            }
          }
 
          // manually add in the username and password
        } catch (err) {
          _didIteratorError = true;
          _iteratorError = err;
        } finally {
          try {
            if (!_iteratorNormalCompletion && _iterator.return) {
              _iterator.return();
            }
          } finally {
            if (_didIteratorError) {
              throw _iteratorError;
            }
          }
        }
 
        params[formId + ':username'] = self.email;
        params[formId + ':password'] = self.password;
 
        // and some other parameters that look like they're needed
        params['javax.faces.source'] = formId + ':submitLogin';
        params['javax.faces.partial.event'] = 'click';
        params['javax.faces.partial.execute'] = formId + ':submitLogin ' + formId + ':username' + formId + ':password';
        params['javax.faces.partial.render'] = formId + ':err';
        params['javax.faces.behavior.event'] = 'action';
        params['javax.faces.partial.ajax'] = 'true';
 
        (0, _request2.default)({
          method: 'POST',
          uri: self.uris.login,
          gzip: true,
          jar: self.cookies,
          form: params
        }, function (error, response, body) {
          if (error) {
            return callback(error);
          }
 
          if ('string' === typeof response.headers.location) {
            if (self.uris.dashboard === response.headers.location) {
              self._failedLoginCount = 0;
              return callback(null);
            } else if (self.uris.sessionExpired === resposne.headers.location) {
              if (++self._failedLoginCount >= self.MAX_LOGIN_ATTEMPTS) {
                return callback(new Error('Exceeded maximum number of failed login attempts'));
              } else {
                return self._login(callback, forceRefresh);
              }
            } else {
              return callback(new Error('Redirected to an unexpected location: ' + response.headers.location));
            }
          } else {
            return callback(new Error('Logging in did not redirect as expected: ' + JSON.stringify(response.headers)));
          }
        });
      });
    }
  }, {
    key: 'getProfile',
    value: function getProfile(callback) {
      var forceRefresh = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
 
      if ('function' !== typeof callback) {
        callback = function callback() {};
      }
 
      if (false === forceRefresh && 'object' === _typeof(this.profile)) {
        return callback(null, this.profile);
      }
 
      var self = this;
 
      this._login(function (error) {
        if (error) {
          return callback(error);
        }
 
        self._getProfile(function (error, result) {
          if (error) {
            // we'll force a refreshed login this time
            self._login(function (error) {
              if (error) {
                return callback(error);
              }
              self._getProfile(callback);
            }, true);
          }
          callback(null, result);
        });
      });
    }
  }, {
    key: '_getProfile',
    value: function _getProfile(callback) {
      var self = this;
 
      (0, _request2.default)({
        method: 'GET',
        uri: this.uris.dashboard,
        gzip: true,
        jar: this.cookies
      }, function (error, response, body) {
        if (error) {
          return callback(error);
        }
 
        var $ = _cheerio2.default.load(body);
 
        self.profile = {};
        try {
          self.profile.name = $('div.fieldName:contains("Name on Account:")').next('div.fieldData').text();
          self.profile.email = $('div.fieldName:contains("Email:")').next('div.fieldData').text();
          self.profile.address = $('div.fieldName:contains("Address:")').next('div.fieldData').text();
          self.profile.phone = $('div.fieldName:contains("Phone:")').next('div.fieldData').text();
        } catch (e) {
          if (e instanceof TypeError) {
            // swallow any TypeError exceptions that are thrown
            console.log(e);
          } else {
            return callback(e);
          }
        }
 
        self.profile.payment = {};
        try {
          self.profile.payment.primary = $('div.fieldName:contains("Primary:")').next('div.fieldData').text();
          self.profile.payment.expiry = $('div.fieldData:contains("Exp.")').text().match(/[0-9]{2}\/[0-9]{2}/)[0];
        } catch (e) {
          if (e instanceof TypeError) {
            // swallow any TypeError exceptions that are thrown
            console.log(e);
          } else {
            return callback(e);
          }
        }
 
        callback(null, self.profile);
      });
    }
  }]);
 
  return ClipperAccount;
}();
 
exports.default = ClipperAccount;