session-file-helpers.js 11.2 KB
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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
var fs = require('fs-extra');
var writeFileAtomic = require('write-file-atomic');
var path = require('path');
var retry = require('retry');
var childProcess = require('child_process');
var Bagpipe = require('bagpipe');
var objectAssign = require('object-assign');
var isWindows = process.platform === 'win32';

var helpers = {

  isSecret: function (secret) {
    return secret !== undefined && secret != null;
  },

  sessionPath: function (options, sessionId) {
    //return path.join(basepath, sessionId + '.json');
    return path.join(options.path, sessionId + options.fileExtension);
  },

  sessionId: function (options, file) {
    //return file.substring(0, file.lastIndexOf('.json'));
    if (options.fileExtension.length === 0) return file;
    var id = file.replace(options.filePattern, '');
    return id === file ? '' : id;
  },

  getLastAccess: function (session) {
    return session.__lastAccess;
  },

  setLastAccess: function (session) {
    session.__lastAccess = new Date().getTime();
  },

  escapeForRegExp: function (str) {
    return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
  },

  getFilePatternFromFileExtension: function (fileExtension) {
    return new RegExp(helpers.escapeForRegExp(fileExtension) + '$');
  },

  DEFAULTS: {
    path: './sessions',
    ttl: 3600,
    retries: 5,
    factor: 1,
    minTimeout: 50,
    maxTimeout: 100,
    reapInterval: 3600,
    reapMaxConcurrent: 10,
    reapAsync: false,
    reapSyncFallback: false,
    logFn: console.log || function () {
    },
    encoding: 'utf8',
    encoder: JSON.stringify,
    decoder: JSON.parse,
    encryptEncoding: 'hex',
    fileExtension: '.json',
    crypto: {
      algorithm: "aes-256-gcm",
      hashing: "sha512",
      use_scrypt: true
    },
    keyFunction: function (secret, sessionId) {
      return secret + sessionId;
    },
  },

  defaults: function (userOptions) {
    var options = objectAssign({}, helpers.DEFAULTS, userOptions);
    options.path = path.normalize(options.path);
    options.filePattern = helpers.getFilePatternFromFileExtension(options.fileExtension);

    if (helpers.isSecret(options.secret))
      options.kruptein = require('kruptein')(options.crypto);

    return options;
  },

  destroyIfExpired: function (sessionId, options, callback) {
    helpers.expired(sessionId, options, function (err, expired) {
      if (err == null && expired) {
        helpers.destroy(sessionId, options, callback);
      } else if (callback) {
        err ? callback(err) : callback();
      }
    });
  },

  scheduleReap: function (options) {
    if (options.reapInterval !== -1) {
      options.reapIntervalObject = setInterval(function () {
        if (options.reapAsync) {
          options.logFn('[session-file-store] Starting reap worker thread');
          helpers.asyncReap(options);
        } else {
          options.logFn('[session-file-store] Deleting expired sessions');
          helpers.reap(options);
        }
      }, options.reapInterval * 1000).unref();
    }
  },

  asyncReap: function (options, callback) {
    callback || (callback = function () {
    });

    function execCallback(err) {
      if (err && options.reapSyncFallback) {
        helpers.reap(options, callback);
      } else {
        err ? callback(err) : callback();
      }
    }

    if (isWindows) {
      childProcess.execFile('node', [path.join(__dirname, 'reap-worker.js'), options.path, options.ttl], execCallback);
    } else {
      childProcess.execFile(path.join(__dirname, 'reap-worker.js'), [options.path, options.ttl], execCallback);
    }
  },

  reap: function (options, callback) {
    callback || (callback = function () {
    });
    helpers.list(options, function (err, files) {
      if (err) return callback(err);
      if (files.length === 0) return callback();

      var bagpipe = new Bagpipe(options.reapMaxConcurrent);

      var errors = [];
      files.forEach(function (file, i) {
        bagpipe.push(helpers.destroyIfExpired,
          helpers.sessionId(options, file),
          options,
          function (err) {
            if (err) {
              errors.push(err);
            }
            if (i >= files.length - 1) {
              errors.length > 0 ? callback(errors) : callback();
            }
          });
      });
    });
  },

  /**
   * Attempts to fetch session from a session file by the given `sessionId`
   *
   * @param  {String}   sessionId
   * @param  {Object}   options
   * @param  {Function} callback
   *
   * @api public
   */
  get: function (sessionId, options, callback) {
    var sessionPath = helpers.sessionPath(options, sessionId);

    var operation = retry.operation({
      retries: options.retries,
      factor: options.factor,
      minTimeout: options.minTimeout,
      maxTimeout: options.maxTimeout
    });

    operation.attempt(function () {

      fs.readFile(sessionPath, helpers.isSecret(options.secret) && !options.encryptEncoding ? null : options.encoding, function readCallback(err, data) {

        if (!err) {
          var json;

          if (helpers.isSecret(options.secret))
            data = options.decoder(helpers.decrypt(options, data, sessionId));

          try {
            json = options.decoder(data);
          } catch (parseError) {
            return fs.remove(sessionPath, function (removeError) {
              if (removeError) {
                return callback(removeError);
              }

              callback(parseError);
            });
          }
          if (!err) {
            return callback(null, helpers.isExpired(json, options) ? null : json);
          }
        }

        if (operation.retry(err)) {
          options.logFn('[session-file-store] will retry, error on last attempt: ' + err);
        } else if (options.fallbackSessionFn) {
          var session = options.fallbackSessionFn(sessionId);
          helpers.setLastAccess(session);
          callback(null, session);
        } else {
          callback(err);
        }
      });
    });
  },

  /**
   * Attempts to commit the given `session` associated with the given `sessionId` to a session file
   *
   * @param {String}   sessionId
   * @param {Object}   session
   * @param  {Object}  options
   * @param {Function} callback (optional)
   *
   * @api public
   */
  set: function (sessionId, session, options, callback) {
    try {
      helpers.setLastAccess(session);

      var sessionPath = helpers.sessionPath(options, sessionId);
      var json = options.encoder(session);
      if (helpers.isSecret(options.secret)) {
        json = helpers.encrypt(options, json, sessionId);
      }
      writeFileAtomic(sessionPath, json, function (err) {
        if (callback) {
          err ? callback(err) : callback(null, session);
        }
      });
    } catch (err) {
      if (callback) callback(err);
    }
  },

  /**
   * Update the last access time and the cookie of given `session` associated with the given `sessionId` in session file.
   * Note: Do not change any other session data.
   *
   * @param {String}   sessionId
   * @param {Object}   session
   * @param {Object}   options
   * @param {Function} callback (optional)
   *
   * @api public
   */
  touch: function (sessionId, session, options, callback) {
    helpers.get(sessionId, options, function (err, originalSession) {
      if (err) {
        callback(err, null);
        return;
      }

      if (!originalSession) {
        originalSession = {};
      }

      if (session.cookie) {
        // Update cookie details
        originalSession.cookie = session.cookie;
      }
      // Update `__lastAccess` property and save to store 
      helpers.set(sessionId, originalSession, options, callback);
    });
  },

  /**
   * Attempts to unlink a given session by its id
   *
   * @param  {String}   sessionId   Files are serialized to disk by their sessionId
   * @param  {Object}   options
   * @param  {Function} callback
   *
   * @api public
   */
  destroy: function (sessionId, options, callback) {
    var sessionPath = helpers.sessionPath(options, sessionId);
    fs.remove(sessionPath, callback || function () {
    });
  },

  /**
   * Attempts to fetch number of the session files
   *
   * @param  {Object}   options
   * @param  {Function} callback
   *
   * @api public
   */
  length: function (options, callback) {
    fs.readdir(options.path, function (err, files) {
      if (err) return callback(err);

      var result = 0;
      files.forEach(function (file) {
        if (options.filePattern.exec(file)) {
          ++result;
        }
      });

      callback(null, result);
    });
  },

  /**
   * Attempts to clear out all of the existing session files
   *
   * @param  {Object}   options
   * @param  {Function} callback
   *
   * @api public
   */
  clear: function (options, callback) {
    fs.readdir(options.path, function (err, files) {
      if (err) return callback([err]);
      if (files.length <= 0) return callback();

      var errors = [];
      files.forEach(function (file, i) {
        if (options.filePattern.exec(file)) {
          fs.remove(path.join(options.path, file), function (err) {
            if (err) {
              errors.push(err);
            }
            // TODO: wrong call condition (call after all completed attempts to remove instead of after completed attempt with last index)
            if (i >= files.length - 1) {
              errors.length > 0 ? callback(errors) : callback();
            }
          });
        } else {
          // TODO: wrong call condition (call after all completed attempts to remove instead of after completed attempt with last index)
          if (i >= files.length - 1) {
            errors.length > 0 ? callback(errors) : callback();
          }
        }
      });
    });
  },

  /**
   * Attempts to find all of the session files
   *
   * @param  {Object}   options
   * @param  {Function} callback
   *
   * @api public
   */
  list: function (options, callback) {
    fs.readdir(options.path, function (err, files) {
      if (err) return callback(err);

      files = files.filter(function (file) {
        return options.filePattern.exec(file);
      });

      callback(null, files);
    });
  },

  /**
   * Attempts to detect whether a session file is already expired or not
   *
   * @param  {String}   sessionId
   * @param  {Object}   options
   * @param  {Function} callback
   *
   * @api public
   */
  expired: function (sessionId, options, callback) {
    helpers.get(sessionId, options, function (err, session) {
      if (err) return callback(err);

      err ? callback(err) : callback(null, helpers.isExpired(session, options));
    });
  },

  isExpired: function (session, options) {
    if (!session) return true;

    var ttl = session.cookie && session.cookie.originalMaxAge ? session.cookie.originalMaxAge : options.ttl * 1000;
    return !ttl || helpers.getLastAccess(session) + ttl < new Date().getTime();
  },

  encrypt: function (options, data, sessionId) {
    var ciphertext = null;

    options.kruptein.set(options.secret, data, function(err, ct) {
      if (err)
        throw err;

      ciphertext = ct;
    });

    return ciphertext;
  },

  decrypt: function (options, data, sessionId) {
    var plaintext = null;

    options.kruptein.get(options.secret, data, function(err, pt) {
      if (err)
        throw err;

      plaintext = pt;
    });
    
    return plaintext;
  }
};

module.exports = helpers;