<commit_before>var cookie = require('cookie'),
  crypto = require('crypto'),
  Promise = require('bluebird'),
  qs = require('querystring'),
  _ = require('lodash');

var noop = function(x) { return x };

var PlaySession = function(request, opts) {
  opts = opts || {};
  this.secret = opts.secret || process.env.PLAY_APP_SECRET;
  if (request.headers && request.headers.cookie) {
    var sessionString = cookie.parse(request.headers.cookie, {decode: noop}).PLAY_SESSION;
    if (sessionString) {
      var i = sessionString.indexOf('-');
      this.signature = sessionString.slice(0, i);
      this.body = sessionString.slice(i + 1);
      this.data = qs.decode(this.body);
    }
  }
};

_.extend(PlaySession.prototype, {
  isValid: function() {
    if (!this.signature) return false;

    var hash = crypto.createHmac('sha1', this.secret).update(this.body).digest('hex');
    return this.signature == hash;
  },
  isExpired: function() {
    return this.data['pa.u.exp'] < new Date().getTime();
  },
  fetchUser: function(callback) {
    var providerKey = this.providerKey(), providerId = this.providerId();

    if (!providerKey || !providerId)
      return Promise.resolve(null);

    return LinkedAccount.query(function(qb) {
      qb.where({
          provider_key: providerKey,
          provider_user_id: providerId,
          "users.active": true
      }).leftJoin("users", function() {
        this.on("users.id", "=", "linked_account.user_id");
      });
    }).fetch().then(function (account) {
      if (account) {
        return account.activeUser().fetch();
      } else {
        sails.log.error("PlaySession failed to retrieve linkedAccount", providerKey, providerId);
        return null;
      }
    });
  },
  providerKey: function() {
    return this.data['pa.p.id'];
  },<commit_msg>fix regression in password login<commit_after>var cookie = require('cookie'),
  crypto = require('crypto'),
  Promise = require('bluebird'),
  qs = require('querystring'),
  _ = require('lodash');

var noop = function(x) { return x };

var PlaySession = function(request, opts) {
  opts = opts || {};
  this.secret = opts.secret || process.env.PLAY_APP_SECRET;
  if (request.headers && request.headers.cookie) {
    var sessionString = cookie.parse(request.headers.cookie, {decode: noop}).PLAY_SESSION;
    if (sessionString) {
      var i = sessionString.indexOf('-');
      this.signature = sessionString.slice(0, i);
      this.body = sessionString.slice(i + 1);
      this.data = qs.decode(this.body);
    }
  }
};

_.extend(PlaySession.prototype, {
  isValid: function() {
    if (!this.signature) return false;

    var hash = crypto.createHmac('sha1', this.secret).update(this.body).digest('hex');
    return this.signature == hash;
  },
  isExpired: function() {
    return this.data['pa.u.exp'] < new Date().getTime();
  },
  fetchUser: function(callback) {
    var providerKey = this.providerKey(), providerId = this.providerId();

    if (!providerKey || !providerId)
      return Promise.resolve(null);

    if (providerKey == 'password') {
      return User.where({email: providerId, active: true}).fetch();
    }

    return LinkedAccount.query(function(qb) {
      qb.where({
          provider_key: providerKey,
          provider_user_id: providerId,
          "users.active": true
      }).leftJoin("users", function() {
        this.on("users.id", "=", "linked_account.user_id");
      });
    }).fetch().then(function (account) {
      if (account) {
        return account.activeUser().fetch();
      } else {
        sails.log.error("PlaySession failed to retrieve linkedAccount", providerKey, providerId);
        return null;
      }
    });
  },
  providerKey: function() {
    return this.data['pa.p.id'];
  },
{'fraction_non_alphanumeric': 0.16145415664260893, 'fraction_numerical': 0.0016038492381716118, 'mean_word_length': 3.1716833890746936, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5495425', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1138, 'n_words': 232}