<commit_before>var express  = require('express');
var app = express();
var uid = require('uid2');
var sessionSecret = uid(30);
var server = require('http').Server(app);
var io = require('socket.io')(server);
var session = require('express-session');
var sessionStore = new session.MemoryStore();
var cookieParser = require('cookie-parser')(sessionSecret)
var sessionSocketIo = require('session.socket.io');
var sessionSockets = new sessionSocketIo(io, sessionStore, cookieParser);
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var restClient = require('node-rest-client');
var path = require('path');
var cons = require('consolidate');

// general config ==============================================================

app.engine('html', cons.underscore);

app.set('views', './app')
app.set('view engine', 'html')
app.use(express.static(path.join(__dirname, 'app/static')));
app.use(cookieParser);
app.use(session({
  genid: function(req) {
    return uid(30);
  },
  secret: sessionSecret,
  resave: true,
  saveUninitialized: true,
  cookie: {},
  store: sessionStore
}))
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(methodOverride());

// oauth2 and identity server config ===========================================

var environmentSet = true;
var clientId = process.env.ULU_OAUTH_CLIENT_ID;
var clientSecret = process.env.ULU_OAUTH_CLIENT_SECRET;
var identityServerAddress = process.env.ULU_IDENTITY_ADDRESS
var sultansAddress = process.env.ULU_SULTANS_ADDRESS
var cloudbreakAddress = process.env.ULU_CLOUDBREAK_ADDRESS
var hostAddress = process.env.ULU_HOST_ADDRESS;
var clientScopes = 'openid' +
    '+cloudbreak.templates' +
    '+cloudbreak.credentials' +
    '+cloudbreak.blueprints' +
    '+cloudbreak.stacks' +
    '+periscope.cluster' +
    '+cloudbreak.events+cloudbreak.usages.account+cloudbreak.usages.user';


var periscopeAddress = process.env.ULU_PERISCOPE_ADDRESS;
var subscriptionAddress = null;

if (!periscopeAddress || (periscopeAddress.substring(0, 7) !== "http://" && periscopeAddress.substring(0, 8) !== "https://")){
  console.log("ULU_PERISCOPE_ADDRESS must be specified and it must be a standard URL: 'http[s]://host[:port]/'");
  environmentSet = false;
}

if (!clientSecret || !hostAddress || !clientId) {
  console.log("ULU_HOST_ADDRESS, ULU_OAUTH_CLIENT_ID and ULU_OAUTH_CLIENT_SECRET must be specified!");
  environmentSet = false;
}

if (!identityServerAddress || (identityServerAddress.substring(0, 7) !== "http://" && identityServerAddress.substring(0, 8) !== "https://")){
  console.log("ULU_IDENTITY_ADDRESS must be specified and it must be a standard URL: 'http[s]://host[:port]/'");
  environmentSet = false;
}

if (!cloudbreakAddress || (cloudbreakAddress.substring(0, 7) !== "http://" && cloudbreakAddress.substring(0, 8) !== "https://")){
  console.log("ULU_CLOUDBREAK_ADDRESS must be specified and it must be a standard URL: 'http[s]://host[:port]/'");
  environmentSet = false;
}

if (!sultansAddress || (sultansAddress.substring(0, 7) !== "http://" && sultansAddress.substring(0, 8) !== "https://")){
  console.log("ULU_SULTANS_ADDRESS must be specified and it must be a standard URL: 'http[s]://host[:port]/'");
  environmentSet = false;
}

if (!environmentSet) {
  process.exit(1);
}

if (hostAddress.slice(-1) !== '/') {
  hostAddress += '/';
}

if (identityServerAddress.slice(-1) !== '/') {
  identityServerAddress += '/';
}

if (cloudbreakAddress.slice(-1) !== '/') {
  cloudbreakAddress += '/';
}

if (sultansAddress.slice(-1) !== '/') {
  sultansAddress += '/';
}

if (periscopeAddress.slice(-1) !== '/') {
  periscopeAddress += '/';
}

var redirectUri = hostAddress + 'authorize';

var optionsAuth={user: clientId, password: <PASSWORD>Secret};
var identityServerClient = new restClient.Client(optionsAuth);
identityServerClient.registerMethod("retrieveToken", identityServerAddress + "oauth/token", "POST");

var proxyRestClient = new restClient.Client();
proxyRestClient.registerMethod("subscribe", cloudbreakAddress + "subscriptions", "POST");

// cloudbreak config ===========================================================

var cbRequestArgs = {
  headers:{
    "Content-Type": "application/json"
  }
}

// routes ======================================================================

// oauth  ======================================================================

app.get('/authorize', function(req, res, next){
  var args = {
    headers:{
      "Content-Type": "application/x-www-form-urlencoded",
    },
    data:
      'grant_type=authorization_code'
      + '&redirect_uri=' + redirectUri
      + '&code=' + req.query.code
  }
  identityServerClient.methods.retrieveToken(args, function(data, response){
    if (response.statusCode != 200 || !data.access_token){
      var error = new Error("Couldn't retrieve access token from identity server.");
      error.status = 500;
      return next(error);
    }
    console.log(data)
    req.session.token=data.access_token;
    res.redirect('/');
  });
});
<commit_msg>CLOUD-649 added /info endpoint<commit_after>var express  = require('express');
var app = express();
var uid = require('uid2');
var sessionSecret = uid(30);
var server = require('http').Server(app);
var io = require('socket.io')(server);
var session = require('express-session');
var sessionStore = new session.MemoryStore();
var cookieParser = require('cookie-parser')(sessionSecret)
var sessionSocketIo = require('session.socket.io');
var sessionSockets = new sessionSocketIo(io, sessionStore, cookieParser);
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var restClient = require('node-rest-client');
var path = require('path');
var cons = require('consolidate');

// general config ==============================================================

app.engine('html', cons.underscore);

app.set('views', './app')
app.set('view engine', 'html')
app.use(express.static(path.join(__dirname, 'app/static')));
app.use(cookieParser);
app.use(session({
  genid: function(req) {
    return uid(30);
  },
  secret: sessionSecret,
  resave: true,
  saveUninitialized: true,
  cookie: {},
  store: sessionStore
}))
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(methodOverride());

// oauth2 and identity server config ===========================================

var environmentSet = true;
var clientId = process.env.ULU_OAUTH_CLIENT_ID;
var clientSecret = process.env.ULU_OAUTH_CLIENT_SECRET;
var identityServerAddress = process.env.ULU_IDENTITY_ADDRESS
var sultansAddress = process.env.ULU_SULTANS_ADDRESS
var cloudbreakAddress = process.env.ULU_CLOUDBREAK_ADDRESS
var hostAddress = process.env.ULU_HOST_ADDRESS;
var clientScopes = 'openid' +
    '+cloudbreak.templates' +
    '+cloudbreak.credentials' +
    '+cloudbreak.blueprints' +
    '+cloudbreak.stacks' +
    '+periscope.cluster' +
    '+cloudbreak.events+cloudbreak.usages.account+cloudbreak.usages.user';


var periscopeAddress = process.env.ULU_PERISCOPE_ADDRESS;
var subscriptionAddress = null;

if (!periscopeAddress || (periscopeAddress.substring(0, 7) !== "http://" && periscopeAddress.substring(0, 8) !== "https://")){
  console.log("ULU_PERISCOPE_ADDRESS must be specified and it must be a standard URL: 'http[s]://host[:port]/'");
  environmentSet = false;
}

if (!clientSecret || !hostAddress || !clientId) {
  console.log("ULU_HOST_ADDRESS, ULU_OAUTH_CLIENT_ID and ULU_OAUTH_CLIENT_SECRET must be specified!");
  environmentSet = false;
}

if (!identityServerAddress || (identityServerAddress.substring(0, 7) !== "http://" && identityServerAddress.substring(0, 8) !== "https://")){
  console.log("ULU_IDENTITY_ADDRESS must be specified and it must be a standard URL: 'http[s]://host[:port]/'");
  environmentSet = false;
}

if (!cloudbreakAddress || (cloudbreakAddress.substring(0, 7) !== "http://" && cloudbreakAddress.substring(0, 8) !== "https://")){
  console.log("ULU_CLOUDBREAK_ADDRESS must be specified and it must be a standard URL: 'http[s]://host[:port]/'");
  environmentSet = false;
}

if (!sultansAddress || (sultansAddress.substring(0, 7) !== "http://" && sultansAddress.substring(0, 8) !== "https://")){
  console.log("ULU_SULTANS_ADDRESS must be specified and it must be a standard URL: 'http[s]://host[:port]/'");
  environmentSet = false;
}

if (!environmentSet) {
  process.exit(1);
}

if (hostAddress.slice(-1) !== '/') {
  hostAddress += '/';
}

if (identityServerAddress.slice(-1) !== '/') {
  identityServerAddress += '/';
}

if (cloudbreakAddress.slice(-1) !== '/') {
  cloudbreakAddress += '/';
}

if (sultansAddress.slice(-1) !== '/') {
  sultansAddress += '/';
}

if (periscopeAddress.slice(-1) !== '/') {
  periscopeAddress += '/';
}

var redirectUri = hostAddress + 'authorize';

var optionsAuth={user: clientId, password: <PASSWORD>};
var identityServerClient = new restClient.Client(optionsAuth);
identityServerClient.registerMethod("retrieveToken", identityServerAddress + "oauth/token", "POST");

var proxyRestClient = new restClient.Client();
proxyRestClient.registerMethod("subscribe", cloudbreakAddress + "subscriptions", "POST");

// cloudbreak config ===========================================================

var cbRequestArgs = {
  headers:{
    "Content-Type": "application/json"
  }
}

// routes ======================================================================

// info ========================================================================

var pjson = require('./package.json');

app.get('/info', function(req, res) {
    res.json({name: pjson.name, version: pjson.version, sultansAddress: sultansAddress});
});


// oauth  ======================================================================

app.get('/authorize', function(req, res, next){
  var args = {
    headers:{
      "Content-Type": "application/x-www-form-urlencoded",
    },
    data:
      'grant_type=authorization_code'
      + '&redirect_uri=' + redirectUri
      + '&code=' + req.query.code
  }
  identityServerClient.methods.retrieveToken(args, function(data, response){
    if (response.statusCode != 200 || !data.access_token){
      var error = new Error("Couldn't retrieve access token from identity server.");
      error.status = 500;
      return next(error);
    }
    console.log(data)
    req.session.token=data.access_token;
    res.redirect('/');
  });
});

{'fraction_non_alphanumeric': 0.22331737538721486, 'fraction_numerical': 0.006664789261240965, 'mean_word_length': 5.456969696969697, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 8, 'lorem ipsum': 0, 'www.': 2, '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': '1907382', 'n_tokens_mistral': 3328, 'n_tokens_neox': 3232, 'n_words': 658}