Skip to content

Instantly share code, notes, and snippets.

@bmeck
Created June 5, 2012 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmeck/2877622 to your computer and use it in GitHub Desktop.
Save bmeck/2877622 to your computer and use it in GitHub Desktop.
#!/bin/env node
//
// Simple example of a custom out of band strategy (aka one that is missing a response).
// Could be improved with some sort of session storage outside of the req/res
//
// Passing can be achieved by passing in --auth user:pass
//
var passport = require('passport')
, optimist = require('optimist')
, request = require('request')
, Strategy = passport.Strategy;
//
// Simple example strategy
//
var basicStrategy = new Strategy();
basicStrategy.name = 'custom';
basicStrategy.authenticate = function (req) {
if (req.headers.authorization === 'Basic ' + new Buffer('user:pass').toString('base64')) {
this.pass();
return
}
this.fail();
}
passport.use(basicStrategy);
var pass = passport.authenticate('custom')
//
// Shim to override the res result
//
function auth(req, callback) {
var intercept = {
end: function (body) {
callback({status: this.status, body: body});
}
};
pass(req, intercept, function (req, res) {
callback();
});
}
//
// Faked request
//
var req = {
headers: {
authorization:'Basic ' + new Buffer(optimist.argv.auth).toString('base64')
}
}
//
// Use the callback to determine if this was a valid auth
//
auth(req, function (err) {
if (err) {
console.error('failed')
}
else {
console.error('passed')
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment