Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add authKey generator for private/presence channels

  • Loading branch information...
commit 43eb2060a222f476b27a13e5ddbac833d73cba46 1 parent c8e8c69
@daraosn authored
View
29 lib/mustekala.js
@@ -5,6 +5,8 @@ module.exports=function() {
,config: {}
,io: {}
,socket: {}
+ ,authKeyExpiry: 2 // seconds
+ ,authKeys: {}
}
try {
@@ -14,6 +16,8 @@ module.exports=function() {
process.exit(1);
}
+ m.config.port = m.config.port || 3000;
+
m.trigger=function(password, channel, action, data) {
if(password==mustekala.config.password) {
if(channel) {
@@ -27,21 +31,28 @@ module.exports=function() {
}
}
- m.socketPresencePreauthorize=function(password) {
+ m.initialize=function() {
+ require('./configure');
+ require('./socket');
+ require('./routes');
+ require('./utils');
+
+ m.generateAuthKey(m.config.password);
+ setInterval(function() {
+ console.log(m.authKeys);
+ }, 1000);
+ }
+
+ m.generateAuthKey = function(password) {
if(password==mustekala.config.password) {
- do { var token = global.token(); } while(mustekala.authKeys[token]);
- mustekala.authKeys[token] = new Date().getTime() + presenceKeyExpiry;
- console.log('@@@@@'+global.dump(mustekala.authKeys));
+ do { var token = Utils.token(); } while(m.authKeys[token]);
+ m.authKeys[token] = new Date().getTime() + m.authKeyExpiry;
+ setTimeout(function() { delete m.authKeys[token] }, m.authKeyExpiry*1000);
return token;
} else {
return false;
}
}
- m.initialize=function() {
- require('./routes');
- require('./configure');
- require('./socket');
- }
return m;
}
View
31 lib/routes.js
@@ -8,11 +8,18 @@ app.post('/mustekala/trigger', function(req, res) {
res.send(JSON.stringify({'success':result, 'channel': channel, 'action': action, 'data': data}));
});
-app.post('/mustekala/authentificate', function(req, res) {
+app.post('/mustekala/authenticate', function(req, res) {
var password=req.body.password;
- var channel=req.body.channel;
- var user=req.body.channel;
+ var user=req.body.user;
+
+ console.log('*******',req.body);
+ // TODO:
+ var response=JSON.stringify({'authKey':mustekala.generateAuthKey(password)}).toString();
+ //console.log(typeof response);
+ // console.log(res);
+ // res['headers']['content-type'] = 'text/plain'
+ res.end(response+'');
});
app.get('/mustekala.js', function(req, res) {
@@ -60,14 +67,18 @@ app.post('/example/auth', function(req, res) {
,nickname: 'joe'
}
}
+
+ var body={
+ password: mustekala.config.password
+ ,user: exampleUser
+ };
+
+ // note: if not using json, you must provide header 'content-type: application/x-www-form-urlencoded'
require('request').post({
- url: '/mustekala/authentificate'
- ,body: {
- password: mustekala.config.password
- ,user: exampleUser
- }
+ url: "http://localhost:"+mustekala.config.port+'/mustekala/authenticate'
+ , headers: {'content-type': 'application/x-www-form-urlencoded'}
+ , body: require('qs').stringify(body)
},function(error, response, body) {
- console.log(body);
+ res.end(body);
});
- res.end(JSON.stringify({'authKey': '12345'}));
});
View
14 utils.js → lib/utils.js
@@ -1,4 +1,5 @@
-global.dump = function(arr,level) {
+var Utils={};
+Utils.dump = function(arr,level) {
var dumped_text = "";
if(!level) level = 0;
@@ -23,15 +24,16 @@ global.dump = function(arr,level) {
return dumped_text;
}
-global.debug=function(params,alive) {
+Utils.debug=function(params,alive) {
console.log(params);
if(!alive) process.exit(0);
}
-global.rand_str = function() {
+Utils.rand_str = function() {
return Math.random().toString(36).substr(2);
};
-global.token = function() {
- return global.rand_str() + global.rand_str();
-};
+Utils.token = function() {
+ return Utils.rand_str() + Utils.rand_str();
+};
+global.Utils=Utils;
View
2  mustekala.js
@@ -4,5 +4,5 @@ global.app = module.exports = express.createServer();
global.mustekala = new Mustekala();
mustekala.initialize();
-app.listen(mustekala.config.port || 3000);
+app.listen(mustekala.config.port);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
View
3  package.json
@@ -12,10 +12,11 @@
, "jade": ">= 0.0.1"
, "socket.io": "0.9.6"
, "request": "2.9.x"
+ , "qs": "0.5.0"
},
"devDependencies": {},
"optionalDependencies": {},
"engines": {
- "node": "*"
+ "node": "0.6.x"
}
}
View
2  public/example.html
@@ -79,7 +79,7 @@
<div>
<b>SUBSCRIBE</b>
<br>
- channel: <input type="text" name="channel" id="subscribe-channel">
+ channel: <input type="text" name="channel" id="subscribe-channel" value="presence@123">
<input type="button" id="subscribe-button" value="subscribe">
</div>
<div>
Please sign in to comment.
Something went wrong with that request. Please try again.