Tuesday, 15 January 2019

publishing Sails to Kongregate , 403 Forbidden

So, finally my MMORTS game built on Sails is going to Kongregate. Had few obstacles, like connecting websockets, but solved now. Probably the last obstacle is to keep authenticated session. I was using frameworks everywhere and i have no idea how does the authentication sessions work under the hood.
The main problem is probably the CSRF or CORS.
I am using Sails v1.0. So, i start with HTML, which I upload to kongregate. I'm taking the simplest possible example:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta charset="utf-8">

    <script src="jquery.js"></script>
    <script src='https://cdn1.kongregate.com/javascripts/kongregate_api.js'></script>

    <script src="sails.io.js"
      autoConnect="false"
      environment="production"
      headers='{ "x-csrf-token": "" }'
    ></script>

    <script type="text/javascript">
      io.sails.url = 'https://my-secret-game.com'; // or where you want
    </script>

  </head>
  <script src="main.js"></script>
</html>

And this is the main.js , which I also upload to kongregate

kongregateAPI.loadAPI(function(){
  window.kongregate = kongregateAPI.getAPI();
  var username = kongregate.services.getUsername();
  var id = kongregate.services.getUserId();
  var token = kongregate.services.getGameAuthToken();

  $.get("https://my-secret-game.com/csrfToken", function (data, jwres) {

    var params = {
      username: username,
      id: id,
      token: token,
      _csrf: data._csrf
    };

    $.post("https://my-secret-game.com/kong", params, function(data, jwr, xhr){

      // cant set the cookie - because of chrome. this doesnt work
      document.cookie = document.cookie + ';authenticated=true;sails.sid=' + data.id;

      $.get("https://my-secret-game.com/csrfToken", function (data, jwres) {
        var msg = {
          testing_authentication: true,
          _csrf: data._csrf
        };
        $.post("https://my-secret-game.com/test", msg, function(data, status){
          // getting the 403 Forbidden, CSRF mismatch. trying to access 
          // the play/test route, which is protected by sessionAUTH
          console.log('data.response', data.response)
        });

      });
    });
  });
});

The problem is, that i am getting 403 Forbidden whenever I try to POST my Sails backend with sessionAUTH. I also cant set cookies - probably because of Chrome. What can I do? When i get CSRF token, on the next request my Sails app responds about CSRF mismatch. It becomes wrong.

And this is the controller on my Sails backend server

module.exports = {
  kong: function (req, res, next) {
    var url = 'https://api.kongregate.com/api/authenticate.json';
    var kong_api_key = 'my-secred-api-key';
    var params = req.allParams();
    var request = require('request');

    var req_form = {
      "api_key": kong_api_key,
      "user_id": params.id,
      "game_auth_token": params.token
    };

    request({
        url: url,
        method: "POST",
        json: true,
        body: req_form,
        timeout: 5000
    }, function (err, response, body){
       if(err) { console.log(err, 'ERR43'); return res.ok(); }
       else {
        if(!response.body.success) {
          console.log('unsuccessful login from kongregate')
          return res.ok();
        }

        // trying to use a existing user and authenticate to it
        User.find({username: 'admin-user'}).exec(function(err, users) {
          var user = users[0];
          req.session.authenticated = true;
          req.session.user = { id: user.id };

          // trying to send session_id, so that i could hold it on kongregates cookies as `sid`
          return res.send({ user: user, id: req.session.id });

        });
       }
    });
  },

Could somoene please help to fix authentication and CSRF of my app?

In case needs more info about my configs, this is the config/session.js

var prefixes = 'dev';

module.exports.session = {
  secret: 'my-secret',

  cookie: {
     secure: false
  },

  adapter: 'redis',

  host: 'localhost',
  port: 6379,
  ttl: 3000000,
  db: 0,
  prefix: prefixes + 'sess:',
};

config/policies.js

module.exports.policies = {
  user: {
    'new':    'flash',
    'create': 'flash',
    'edit':   'rightUser',
    'update': 'rightUser',
    '*':      'sessionAuth'
  },
  play: {
    '*': 'sessionAuth'
  }
};

api/policies/sessionAuth.js

module.exports = function(req, res, next) {
  if (req.session.authenticated) {
    return next();
  } else {
    var requireLoginErr = [
      { name: 'requireLogin', message: 'You must be signed in' }
    ];
    req.session.flash = {
      err: requireLoginErr
    };
    res.redirect('/');
    return;
  }
};

config/security.js

module.exports.security = {
    csrf: true,
  cors: {
    allowRequestMethods: 'GET,PUT,POST,OPTIONS,HEAD',
    allowRequestHeaders: 'content-type,Access-Token',
    allowResponseHeaders: '*',
    allRoutes: true,
    allowOrigins: '*',
    allowCredentials: false,
  },
};



from publishing Sails to Kongregate , 403 Forbidden

No comments:

Post a Comment