Guest User

Untitled

a guest
Aug 3rd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. # test
  2. # @requires connect.cookieParser
  3. # @requires connect.query
  4. # @requires connect.session
  5. url = require 'url'
  6.  
  7. module.exports = class
  8.  
  9. # routing
  10.  
  11. paths_signup: '/signup'
  12. paths_login: '/login'
  13. paths_logout: '/logout'
  14. paths_redirect: '/'
  15.  
  16. # access
  17.  
  18. access_whitelist: []
  19. access_blacklist: []
  20.  
  21. # actions
  22.  
  23. do_signup: (req, res, cb) ->
  24. @users ?= []
  25. if req.body
  26. cb @users.push
  27. username: req.body.username
  28. password: req.body.password
  29. else
  30. cb false
  31.  
  32. do_login: (req, res) ->
  33. req.session.username = req.query.username
  34.  
  35. do_logout: (req, res) ->
  36. req.session.destroy()
  37.  
  38. # verification
  39.  
  40. verify_credentials: (req, res, cb) ->
  41. if Object.keys(req.query).length and @users
  42. for u in @users
  43. if u.username == req.query.username and u.password == req.query.password
  44. return cb u
  45. cb false
  46.  
  47. verify_authorized: (req, res) ->
  48. req.session?.username
  49.  
  50. # render
  51.  
  52. render_unauthorized: (req, res, next) ->
  53. res.writeHead 403, 'Content-Type': 'text/html'
  54. res.end 'unauthorized yano? <a href="' + @paths_login + '">login</a>'
  55.  
  56. render_login: (req, res, next) ->
  57. res.writeHead 200, 'Content-Type': 'text/html'
  58. res.end 'login <form method="GET" action="' + @paths_login + '">
  59. <input name="username" /><input type="password" name="password" />
  60. <input type="submit" name="submit" /></form>'
  61.  
  62. render_signup: (req, res, next) ->
  63. res.writeHead 200, 'Content-Type': 'text/html'
  64. res.end 'login <form method="POST" action="' + @paths_signup + '">
  65. <input name="username" /><input type="password" name="password" />
  66. <input type="submit" name="submit" /></form>'
  67.  
  68. # middleware
  69.  
  70. connect: ->
  71.  
  72. return (req, res, next) =>
  73.  
  74. parsed_url = url.parse req.url, true
  75. path = parsed_url.pathname
  76.  
  77. # logout takes priority over other actions because otherwise
  78. # whitelist may pass on it or authorized check may override it
  79. if path == @paths_logout
  80. @do_logout req, res
  81. res.writeHead 302, 'Location': @paths_login
  82. return res.end()
  83.  
  84. # if it's in whitelist, we will move on regardless
  85. if @access_whitelist.indexOf(path) > -1
  86. return next()
  87.  
  88. # if user is already verified, we will move on regardless
  89. if @verify_authorized req, res
  90. return next()
  91.  
  92. # if they are trying to access login, we will move on regardless
  93. if path == @paths_login
  94. return @verify_credentials req, res, (verified) =>
  95. if verified
  96. @do_login req, res
  97. res.writeHead 302, 'Location': @paths_redirect
  98. res.end()
  99. else
  100. return @render_login req, res, next
  101.  
  102. # if they are trying to access signup, we will move on regardless
  103. if path == @paths_signup
  104. return @do_signup req, res, (created) =>
  105. if created
  106. res.writeHead 302, 'Location': @paths_redirect
  107. res.end()
  108. else
  109. return @render_signup req, res, next
  110.  
  111. # if there is no blacklist, we auth every request
  112. # if there is a blacklist, we only auth requests in the list
  113. if not @access_blacklist.length or @access_blacklist.indexOf(path) > -1
  114. return @render_unauthorized req, res, next
  115.  
  116. # weird config if this happens, it probably shouldn't
  117. return next()
Add Comment
Please, Sign In to add comment