<commit_before>/**
 * @module SocketAdapter
 */
'use strict';
var rfr = require('rfr');
var Promise = require('bluebird');
var Iron = Promise.promisifyAll(require('iron'));

var Utility = rfr('app/util/Utility');
var Client = rfr('app/adapters/socket/Client');
var RoomsManager = rfr('app/adapters/socket/RoomsManager');
var ServerConfig = rfr('config/ServerConfig.js');

var logger = Utility.createLogger(__filename);

function SocketAdapter() {
  this.isInitialized = false;
}

var Class = SocketAdapter.prototype;

Class.init = function init(server) {
  this.server = server;
  this.io = require('socket.io')(server.listener);

  this.roomsManager = new RoomsManager(server, this.io);

  this.io.on('connection', (socket) => {
    logger.info('New websocket connection from: ' +
                (socket.conn.request.headers['x-forwarded-for'] ||
                 socket.conn.request.connection.remoteAddress));
    this.__handleIdentifyEvent(socket);
  });

  this.isInitialized = true;
};

/**
 * Handles the `identify` socket.io events that identify a user by cookie
 * @param socket {Socket}
 */
Class.__handleIdentifyEvent = function(socket) {
  socket.on('identify', (cookie) => {
    let cookieData = this.__extractCookieData(cookie);
    Iron.unsealAsync(cookieData, ServerConfig.cookiePassword, Iron.defaults)
    .then((credentials) => {
      if (!credentials || credentials instanceof Error) {
        logger.error('Error decryping cookie from <identify> message');
        return socket.emit('identify', 'ERR');
      }

      let Authenticator = this.server.app.authenticator;
      return Authenticator.validateAccount(this.server, credentials)
      .then((result) => {
        if (!result || result instanceof Error) {
          logger.error(result);
          return socket.emit('identify', 'ERR');
        }
        return this.__createNewClient(socket, credentials);
      });
    }).catch((err) => {
      logger.error(err);
      socket.emit('identify', 'ERR');
    });
  });
};

Class.__extractCookieData = function(cookie) {
  if (!cookie) {
    return '';
  }

  let cookieTokens = cookie.match('sid-worldscope=(.*)');
  if (!cookieTokens || cookieTokens.length <= 0) {
    return '';
  }
<commit_msg>Allow admins with permission to access sockets<commit_after>/**
 * @module SocketAdapter
 */
'use strict';
var rfr = require('rfr');
var Promise = require('bluebird');
var Iron = Promise.promisifyAll(require('iron'));

var Utility = rfr('app/util/Utility');
var Client = rfr('app/adapters/socket/Client');
var RoomsManager = rfr('app/adapters/socket/RoomsManager');
var ServerConfig = rfr('config/ServerConfig.js');

var logger = Utility.createLogger(__filename);

function SocketAdapter() {
  this.isInitialized = false;
}

var Class = SocketAdapter.prototype;

Class.init = function init(server) {
  this.server = server;
  this.io = require('socket.io')(server.listener);

  this.roomsManager = new RoomsManager(server, this.io);

  this.io.on('connection', (socket) => {
    logger.info('New websocket connection from: ' +
                (socket.conn.request.headers['x-forwarded-for'] ||
                 socket.conn.request.connection.remoteAddress));
    this.__handleIdentifyEvent(socket);
  });

  this.isInitialized = true;
};

/**
 * Handles the `identify` socket.io events that identify a user by cookie
 * @param socket {Socket}
 */
Class.__handleIdentifyEvent = function(socket) {
  socket.on('identify', (cookie) => {
    let cookieData = this.__extractCookieData(cookie);
    Iron.unsealAsync(cookieData, ServerConfig.cookiePassword, Iron.defaults)
    .then((credentials) => {
      if (!credentials || credentials instanceof Error) {
        logger.error('Error decrypting cookie from <identify> message');
        return socket.emit('identify', 'ERR');
      }

      let Authenticator = this.server.app.authenticator;

      let request = socket.request;
      if (credentials.scope &&
          credentials.scope.indexOf(Authenticator.SCOPE.ADMIN.STREAMS) > -1) {
        request.headers['x-csrf-token'] = request.headers['cookie'];
      }

      return Authenticator.validateAccount(this.server, credentials, request)
      .then((result) => {
        if (!result || result instanceof Error) {
          logger.error(result);
          return socket.emit('identify', 'ERR');
        }
        return this.__createNewClient(socket, credentials);
      });
    }).catch((err) => {
      logger.error(err);
      socket.emit('identify', 'ERR');
    });
  });
};

Class.__extractCookieData = function(cookie) {
  if (!cookie) {
    return '';
  }

  let cookieTokens = cookie.match('sid-worldscope=(.*)');
  if (!cookieTokens || cookieTokens.length <= 0) {
    return '';
  }

{'fraction_non_alphanumeric': 0.15198983911939035, 'fraction_numerical': 0.0006350550381033022, 'mean_word_length': 3.530201342281879, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, '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': '406627', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1396, 'n_words': 282}