Skip to content

Commit

Permalink
First logic version
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetik committed Jan 4, 2012
1 parent 7b94bfd commit 3b26f1d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
29 changes: 2 additions & 27 deletions examples/login/app.js
Original file line number Diff line number Diff line change
@@ -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: '[email protected]' }
, { id: 2, username: 'joe', password: 'birthday', email: '[email protected]' }
];

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.
Expand All @@ -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 () {
Expand Down
44 changes: 43 additions & 1 deletion lib/passport-webid/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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;

0 comments on commit 3b26f1d

Please sign in to comment.