// Licensed under the Apache 2.0 License. See footer for details.

var crypto = require('crypto'),
    algorithm = 'AES-256-CTR';

module.exports.putUser = function(req, res) {
  var app = req.app;
  var cloudant = app.get('cloudant-location-tracker-db');
  if (!cloudant) {
    return res.status(500).json({ error: 'No database server configured' })
  }
  if (!req.body) {
    return res.sendStatus(400);
  }
  if (req.params.id != 'org.couchdb.user:' + req.body.name) {
    // TOOD: Better handle this error
    return res.sendStatus(400);
  }
  var usersDb = cloudant.use('users');
  cloudant.generate_api_key(function(err, api) {
    if (!err) {
      cloudant.set_permissions({database:'location-tracker', username:api.key, roles:['_reader', '_writer']}, function(err, result) {
        if (!err) {
          var cipher = crypto.createCipher(algorithm, req.body.password);
          var encryptedApiPassword = cipher.update(api.password, 'utf8', 'hex');
          encryptedApiPassword += cipher.final('hex');
          var user = {
            _id: req.params.id,
            name: req.body.name,
            api_key: api.key,
            api_password: encryptedApiPassword
          };
          usersDb.insert(user, user._id, function(err, body) {
            if (!err) {
              res.status(201).json({
                ok: true,
                id: user._id,
                rev: body.rev
              });
            } else {
              console.error(err);
              res.status(500).json({error: 'Internal Server Error'});
            }
          });
        } else {
          console.error(err);
          res.status(500).json({error: 'Internal Server Error'});
        }
      });
     } else {
        console.error(err);
        res.status(500).json({error: 'Internal Server Error'});
     }
  });
};

module.exports.postSession = function(req, res) {
  var app = req.app;
  var cloudant = app.get('cloudant-location-tracker-db');
  if (!cloudant) {
    return res.status(500).json({ error: 'No database server configured' })
  }
  if (!req.body) {
    return res.sendStatus(400);
  }
  var usersDb = cloudant.use('users');
  usersDb.get('org.couchdb.user:' + req.body.name, function(err, body) {
    if (!err) {
      var decipher = crypto.createDecipher(algorithm, req.body.password);
      var decryptedApiPassword = decipher.update(body.api_password, 'hex', 'utf8');
      decryptedApiPassword += decipher.final('utf8');
      cloudant.auth(body.api_key, decryptedApiPassword, function(err, body, headers) {
        if (!err) {
          res.setHeader("Set-Cookie", headers['set-cookie']);
          res.json({
            ok: true,
            name: req.body.name,
            roles: body.roles
          });
        } else {
          res.status(500).json({error: 'Internal Server Error'});
        }
      });
    } else {
      res.status(500).json({error: 'Internal Server Error'});
    }
  });
};

//-------------------------------------------------------------------------------
// Copyright IBM Corp. 2015
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//-------------------------------------------------------------------------------

@@ -2566,57 +2566,1368 @@
     
-res.setHeader(%22Set-Cookie%22, headers%5B'set-cookie'%5D
+// TODO: This is a hack to deal with running app locally%0A          var cookieData = %7B%7D;%0A          var cookieKeyValues = headers%5B'set-cookie'%5D%5B0%5D.split(';');%0A          cookieKeyValues.map(function(keyValueString) %7B%0A              var keyValue = keyValueString.trim().split('=');%0A              cookieData%5BkeyValue%5B0%5D%5D = keyValue%5B1%5D;%0A          %7D);%0A          var cookie = '';%0A          for (var key in cookieData) %7B%0A              switch (key) %7B%0A                  case 'AuthSession':%0A                      cookie += 'AuthSession=' + cookieData%5Bkey%5D%0A                      break;%0A                  case 'Version':%0A                      cookie += '; Version=' + cookieData%5Bkey%5D%0A                      break;%0A                  case 'Expires':%0A                      cookie += '; Expires=' + cookieData%5Bkey%5D%0A                      break;%0A                  case 'Max-Age':%0A                      cookie += '; Max-Age=' + cookieData%5Bkey%5D%0A                      break;%0A                  case 'Path':%0A                      cookie += '; Path=' + cookieData%5Bkey%5D%0A                      break;%0A                  case 'HttpOnly':%0A                      cookie += '; HttpOnly'%0A                      break;%0A                  case 'Secure':%0A                      // Intentionally not set%0A                      break;%0A              %7D%0A          %7D%0A          res.setHeader('Set-Cookie', cookie
 );%0A 

Work around cookie issue when running locally without SSL