Guest User

Untitled

a guest
Oct 17th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #!/bin/env node
  2.  
  3. //
  4. // Simple example of a custom out of band strategy (aka one that is missing a response).
  5. // Could be improved with some sort of session storage outside of the req/res
  6. //
  7. // Passing can be achieved by passing in --auth user:pass
  8. //
  9. var passport = require('passport')
  10. , optimist = require('optimist')
  11. , request = require('request')
  12. , Strategy = passport.Strategy;
  13.  
  14. //
  15. // Simple example strategy
  16. //
  17. var basicStrategy = new Strategy();
  18. basicStrategy.name = 'custom';
  19. basicStrategy.authenticate = function (req) {
  20. if (req.headers.authorization === 'Basic ' + new Buffer('user:pass').toString('base64')) {
  21. this.pass();
  22. return
  23. }
  24. this.fail();
  25. }
  26.  
  27. passport.use(basicStrategy);
  28.  
  29. var pass = passport.authenticate('custom')
  30.  
  31. //
  32. // Shim to override the res result
  33. //
  34. function auth(req, callback) {
  35.  
  36. var intercept = {
  37. end: function (body) {
  38. callback({status: this.status, body: body});
  39. }
  40. };
  41.  
  42. pass(req, intercept, function (req, res) {
  43. callback();
  44. });
  45. }
  46.  
  47. //
  48. // Faked request
  49. //
  50. var req = {
  51. headers: {
  52. authorization:'Basic ' + new Buffer(optimist.argv.auth).toString('base64')
  53. }
  54. }
  55.  
  56. //
  57. // Use the callback to determine if this was a valid auth
  58. //
  59. auth(req, function (err) {
  60. if (err) {
  61. console.error('failed')
  62. }
  63. else {
  64. console.error('passed')
  65. }
  66. })
Add Comment
Please, Sign In to add comment