Skip to content

Commit

Permalink
Updated Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
jspears committed Apr 13, 2012
1 parent 90d3d84 commit 7d78691
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
11 changes: 11 additions & 0 deletions plugins/passport/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,14 @@ This plugin provides passport authentication support.
passwordField // the default password field.
strategy // the passport strategy to use defaults to LocalStrategy.
protected // the list of urls to protect, defaults to all.

##Request
To require a route to be authenticated set authrequired.

```javascript

app.all('/your/route', function(req,res, next){
req.authrequired = true
next();
});
```
28 changes: 24 additions & 4 deletions plugins/passport/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ var PassportPlugin = function () {
if (!this.options.authModel) {
throw new Error("authModel option is required");
}
this.usernameField = this.options.usernameField || 'username';
this.passwordField = this.options.passwordField || 'password';
this.app.use(passport.initialize());
this.app.use(passport.session());


passport.use(this.options.strategy || new LocalStrategy(
function (username, password, done) {
this.options.authModel.findOne({username:username, password:password}, done)
var obj = {};
obj[this.usernameField] = username;
obj[this.passwordField] = password;
this.options.authModel.findOne(obj, done)
}.bind(this)
));
passport.serializeUser(function (user, done) {
Expand Down Expand Up @@ -45,16 +50,17 @@ PassportPlugin.prototype.encryptCredentials = function (req, res, next) {
req.body[passfield] = passHash;
next();
};
PassportPlugin.prototype.onAuth = function(req,res){
PassportPlugin.prototype.onAuth = function (req, res) {
res.send({
status:0,
payload:req.user
});
}
PassportPlugin.prototype.filters = function () {
var passfield = this.options.passwordField || 'password';
var authenticate = passport.authenticate('local', { failureRedirect:this.pluginUrl+'/check' });
var app = this.app;
app.post(this.pluginUrl, this.encryptCredentials.bind(this),
passport.authenticate('local', { failureRedirect:'/check' }), this.onAuth.bind(this));
app.post(this.pluginUrl, this.encryptCredentials.bind(this), authenticate, this.onAuth.bind(this));

app.get(this.pluginUrl + '/check', this.ensureAuthenticated.bind(this), this.onAuth.bind(this));

Expand All @@ -63,6 +69,20 @@ PassportPlugin.prototype.filters = function () {
res.redirect(this.baseUrl);
}.bind(this));

app.all(this.baseUrl + '*', function (req, res, next) {
if (req.authrequired) {
if (req.body[passfield])
return authenticate(req, res, next);
}
next();


}.bind(this), function (req, res, next) {
if (req.authrequired)
return this.ensureAuthenticated(req, res, next)
return next();
}.bind(this));

[this.baseUrl + '/rest/*'].concat(this.options.restrict).forEach(function (v) {
if (v)
this.app.all(v, this.ensureAuthenticated.bind(this));
Expand Down

0 comments on commit 7d78691

Please sign in to comment.