From 3b26f1d369183d573196f96a6eb3e703a2e99573 Mon Sep 17 00:00:00 2001 From: Baptiste Lafontaine Date: Wed, 4 Jan 2012 23:13:15 +0100 Subject: [PATCH] First logic version --- examples/login/app.js | 29 ++-------------------- lib/passport-webid/strategy.js | 44 +++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/examples/login/app.js b/examples/login/app.js index 6812615..3e543e6 100644 --- a/examples/login/app.js +++ b/examples/login/app.js @@ -1,32 +1,7 @@ var express = require('express') , passport = require('passport') , util = require('util') - , LocalStrategy = require('../../../passport-webid').Strategy; - - -var users = [ - { id: 1, username: 'bob', password: 'secret', email: 'bob@example.com' } - , { id: 2, username: 'joe', password: 'birthday', email: 'joe@example.com' } -]; - -function findById(id, fn) { - var idx = id - 1; - if (users[idx]) { - fn(null, users[idx]); - } else { - fn(new Error('User ' + id + ' does not exist')); - } -} - -function findByUsername(username, fn) { - for (var i = 0, len = users.length; i < len; i++) { - var user = users[i]; - if (user.username === username) { - return fn(null, user); - } - } - return fn(null, null); -} + , WebIDStrategy = require('../../../passport-webid').Strategy; // Passport session setup. @@ -50,7 +25,7 @@ passport.deserializeUser(function(id, done) { // credentials (in this case, a username and password), and invoke a callback // with a user object. In the real world, this would query a database; // however, in this example we are using a baked-in set of users. -passport.use(new LocalStrategy( +passport.use(new WebIDStrategy( function(username, password, done) { // asynchronous verification, for effect... process.nextTick(function () { diff --git a/lib/passport-webid/strategy.js b/lib/passport-webid/strategy.js index f6902ae..b9c6841 100644 --- a/lib/passport-webid/strategy.js +++ b/lib/passport-webid/strategy.js @@ -3,9 +3,51 @@ var passport = require('passport'), function Strategy(options, verify) { + if (typeof options == 'function') { + verify = options; + options = {}; + } + passport.Strategy.call(this); + this.name = 'local'; + this.verify = verify; } /** * Inherit from `passport.Strategy`. */ -util.inherits(Strategy, passport.Strategy); \ No newline at end of file +util.inherits(Strategy, passport.Strategy); + +/** + * Authentificate with the given certificate + * + */ +Strategy.prototype.authenticate = function(req) { + var self = this; + if (!req.certificate) { + self.fail(); + } + else { + var certificate = req.certificate; + + // Verifying with node-webid + var verifAgent = new webid.VerificationAgent(certificate); + verifAgent.verify(function (success, result) { + if (success) { + var foaf = new webid.Foaf(result); + self._verify(foaf, function (err, user) { + if (err) { return self.error(err); } + if (!user) { return self.fail(); } + self.success(user); + }); + } + else { + self.error(result); + } + }); + } +} + +/** + * Expose `Strategy`. + */ +module.exports = Strategy; \ No newline at end of file