Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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')
- }
- })
Add Comment
Please, Sign In to add comment