Created
June 5, 2012 20:32
-
-
Save bmeck/2877622 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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