Skip to content

Commit

Permalink
fixed old auth example
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Jun 21, 2012
1 parent 1379ed4 commit c201db2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 60 deletions.
34 changes: 14 additions & 20 deletions example/auth/client.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
#!/usr/bin/env node
// Connect to the auth server and display a quote from it.

var DNode = require('dnode');
var sys = require('sys');
var dnode = require('dnode');

if (process.argv.length < 4) {
sys.puts('Usage: ./client.js user pass');
process.exit();
return console.error('Usage: ./client.js user pass');
}

var user = process.argv[2];
var pass = process.argv[3];

DNode.connect(7007, function (remote,conn) {
remote.authenticate(user, pass, function (session) {
if (session) {
sys.puts('Authentication success');
session.quote(function (q) {
sys.puts('\nAnd now for a quote by ' + q.who + ':\n\n');
sys.puts(q.quote + '\n\n');
conn.end();
});
}
else {
sys.puts('Authentication failure');
conn.end();
var d = dnode.connect(7007);
d.on('remote', function (remote) {
remote.auth(user, pass, function (err, session) {
if (err) {
console.error(err);
return d.end();
}

session.quote(function (q) {
console.log('And now for a quote by ' + q.who + ':\n');
console.log(q.quote + '\n');
d.end();
});
});
});
65 changes: 25 additions & 40 deletions example/auth/server.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@
#!/usr/bin/env node
// Serve an object with authentication

var DNode = require('dnode');
var sys = require('sys');
var dnode = require('dnode');
var fs = require('fs');
var net = require('net');

var quotes = JSON.parse(
fs.readFileSync(__dirname + '/quotes.json').toString()
);

// Serve up a Session object only after the client supplies the valid user/pass
DNode(function (client, connection) {
this.authenticate = function (user, pass, cb) {
if (user == 'moo' && pass == 'hax') {
sys.puts('Sign in as ' + sys.inspect(user) + ' succeeded!');
cb(new Session({
user : user,
client : client,
connection : connection,
}));
}
else {
sys.puts('Sign in as ' + sys.inspect(user) + ' failed!');
cb(null);
}
};
}).listen(7007);
var secretQuotes = require('./quotes.json');
function randomQuote (cb) {
var ix = Math.floor(Math.random() * secretQuotes.length);
cb(secretQuotes[ix]);
}

// Clients who connect get an instance of this session object:
function Session (params) {
var conn = params.connection;
var user = params.user;
var client = params.client;

conn.addListener('end', function () {
sys.puts('User ' + sys.inspect(user) + ' disconnected');
});
var server = net.createServer(function (stream) {
var d = dnode({ auth : auth });
d.pipe(stream).pipe(d);

this.quote = function (f) {
var i = Math.floor(Math.random() * quotes.length);
f(quotes[i]);
};
}
function auth (user, pass, cb) {
if (typeof cb !== 'function') return;

if (user === 'moo' && pass === 'hax') {
console.log('signed in: ' + user);
d.on('end', function () {
console.log('disconnected: ' + user);
});

cb(null, { quote : randomQuote });
}
else cb('ACCESS DENIED')
}
});
server.listen(7007);

0 comments on commit c201db2

Please sign in to comment.