Guest User

Untitled

a guest
Sep 3rd, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. ## Coffee
  2. app.get '/test', authenticate, (req, res) ->
  3. res.render 'index', { locals: { title: 'Express' } }
  4.  
  5. authenticate = (req, res, next) ->
  6. DEBUG "headers[authorization]: #{req.headers['authorization']}"
  7. creds = base64_decode(req.headers['authorization'].split(' ')[1])
  8. DEBUG "creds: #{creds}"
  9. [username,password] = creds.split(":")
  10. User.load username, (err,user) ->
  11. if err
  12. TODO "User.load(#{username}) err: #{err}"
  13. next(new Error("User.load(#{username}) err: #{err}"))
  14. else if user and user.password == md5(password)
  15. req.authenticated = user
  16. next()
  17. else
  18. DEBUG "user:password #{username}:#{password} not found"
  19. next(new Error("Authentication Error: #{username}"))
  20.  
  21.  
  22.  
  23. ##js
  24. app.get('/test', authenticate, function(req, res) {
  25. return res.render('index', {
  26. locals: {
  27. title: 'Express'
  28. }
  29. });
  30. });
  31.  
  32. authenticate = function(req, res, next) {
  33. var creds, password, username, _ref;
  34. DEBUG("headers[authorization]: " + req.headers['authorization']);
  35. creds = base64_decode(req.headers['authorization'].split(' ')[1]);
  36. DEBUG("creds: " + creds);
  37. _ref = creds.split(":"), username = _ref[0], password = _ref[1];
  38. return User.load(username, function(err, user) {
  39. if (err) {
  40. TODO("User.load(" + username + ") err: " + err);
  41. return next(new Error("User.load(" + username + ") err: " + err));
  42. } else if (user && user.password === md5(password)) {
  43. req.authenticated = user;
  44. return next();
  45. } else {
  46. DEBUG("user:password " + username + ":" + password + " not found");
  47. return next(new Error("Authentication Error: " + username));
  48. }
  49. });
  50. };
Add Comment
Please, Sign In to add comment