centrifugal / jscent

Node.js client to interact with Centrifugo v1 HTTP API
MIT License
11 stars 5 forks source link

Invalid token #4

Closed synw closed 7 years ago

synw commented 7 years ago

Hi. I always get an invalid token response when I use this server library. I may have missed something. Here is the code:

index.js:

var express = require("express");
var Client = require("jscent");
var path = require('path');

var host = "0.0.0.0";
var port = 8080;

var app = express();
app.use(express.static(__dirname + "/static"));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

var timestamp = parseInt(new Date().getTime()/1000).toString();
var user = "42";
var Token = new Client.Token("secret_key");
Token = Token.clientToken(user, timestamp, "");

app.get('/', function(req, res){ 
    res.render('index',{Token: Token});
});

app.listen(port, host);
console.log("Server ready at "+host+" "+port)

views/index.ejs:

<html>
<head>
    <title>Centrifugo js</title>
    <script type="text/javascript" src="js/sockjs.min.js"></script>
    <script type="text/javascript" src="js/centrifuge.min.js"></script>
</head>
<body>
<div><%= Token %></div>
<script>
    var timestamp = parseInt(new Date().getTime()/1000).toString();

    var centrifuge = new Centrifuge({
        url: "http://localhost:8001/connection",
        user: "42",
        timestamp: timestamp,
        token: "<%= Token %>"
    });

    var public_callbacks = {
        "message": function(dataset) {
            console.log("MSG ");
            console.log('DATASET: '+JSON.stringify(dataset));
        },
        "join": function(message) {
            console.log('JOIN: '+JSON.stringify(message));
        },
        "leave": function(message) {
            console.log('LEAVE: '+JSON.stringify(message));
        },
        "subscribe": function(context) {
            console.log('SUBSCRIBE: '+JSON.stringify(context));
        },
        "error": function(errContext) {
            console.log('ERROR: '+JSON.stringify(errContext));
        },
        "unsubscribe": function(context) {
            console.log('UNSUBSCRIBE: '+JSON.stringify(context));
        }
    }

    var subscription = centrifuge.subscribe("public:test", public_callbacks);

    centrifuge.on('connect', function(context) {
        console.log("Connection ("+context.latency+"ms)");
    });

    centrifuge.on('disconnect', function(context) {
        console.log("Disconnection: "+context.reason);
    });

    centrifuge.connect();
</script>
</body>
</html>

Tested with Centrifugo 1.7.1

FZambia commented 7 years ago

You are passing timestamp generated on client side, it most probably differs from server side used to generate token, so just pass timestamp to template

synw commented 7 years ago

That was it, thank you.

I can say that some example/bootstrap code can be useful as you noticed in another issue. I can make an example with the code above if you wish, now that it works, if you consider it correct.

FZambia commented 7 years ago

@synw of course any example will help, especially for NodeJS and other languages/frameworks we have no examples at all, feel free to make pull requests to https://github.com/centrifugal/examples repo

deresegetachew commented 7 years ago

In case anyone is running their Centrifugo instance from docker and are running on a MAC please be aware of this issue. https://forums.docker.com/t/time-in-container-is-out-of-sync/16566. basically the problem is your docker container's time will be out of Sync with your host machine which will give us an invalid token in our case. you can find the solution on that link above and on that thread or on the following link https://github.com/arunvelsriram/docker-time-sync-agent/.

FZambia commented 7 years ago

@deresegetachew in addition I suppose that it should only affect Centrifugo users who have connection_lifetime option enabled - in other cases Centrifugo does not rely on system timestamp value

FZambia commented 7 years ago

Btw starting from Docker 17.05 issue with time difference was fixed - though it was not yet released as stable release.