Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.3 KiB

  1. //file io
  2. const utils = require('../../utils/utils.js');
  3. //update db
  4. const sql = require('../../utils/sql');
  5. const qs = require('querystring');
  6. /**
  7. * Processes post data to see if the user has successfully
  8. * logged in. If the user has logged in successfully, a session
  9. * variable is set and the page is refreshed.
  10. *
  11. * @param request
  12. * @returns {Promise}
  13. */
  14. const processLogin = function(request, clientAddress)
  15. {
  16. return new Promise(function(resolve, reject)
  17. {
  18. utils.getPostData(request).then(function(postData)
  19. {
  20. const post = qs.parse(postData);
  21. if(!post.username && !post.password)
  22. {
  23. resolve("");
  24. return(false); // no login attempted
  25. }
  26. return sql.checkLogin(postData);
  27. }).then(function(loginResult)
  28. {
  29. if(loginResult !== false)
  30. {
  31. if(loginResult.pass)
  32. {
  33. //what actually logs in the user
  34. request.session.user = loginResult.user;
  35. console.log("user has logged in");
  36. resolve("<meta http-equiv=\"refresh\" content=\"0\">");
  37. }
  38. else
  39. {
  40. banIP(clientAddress);
  41. console.log("Invader!");
  42. resolve("Wrong!");
  43. }
  44. }
  45. }).catch(function(err)
  46. {
  47. reject(err);
  48. })
  49. });
  50. };
  51. /** Global Containing Ban Data **/
  52. var banData = {};
  53. /** Number of incorrect login attempts permitted per ip */
  54. const LOGIN_LIMIT = 5;
  55. /**
  56. * Determines if a client is banned from the server
  57. * or not.
  58. *
  59. * @param clientAddress
  60. */
  61. const isBanned = function(clientAddress)
  62. {
  63. if(clientAddress in banData)
  64. {
  65. user = banData[clientAddress];
  66. return user.incorrectLogins > LOGIN_LIMIT;
  67. }
  68. return false;
  69. };
  70. /**
  71. * Increments the user's incorrect login attempt
  72. * counter.
  73. *
  74. * @param clientAddress
  75. */
  76. const banIP = function(clientAddress)
  77. {
  78. if(clientAddress in banData)
  79. {
  80. user = banData[clientAddress];
  81. user.incorrectLogins++;
  82. }
  83. else
  84. {
  85. var newUser = new Object();
  86. newUser.incorrectLogins = 1;
  87. banData[clientAddress] = newUser;
  88. }
  89. };
  90. module.exports=
  91. {
  92. /**
  93. * Renders the contents of the login page of the website
  94. *
  95. * @param request express request containing post data
  96. * @returns {Promise} resolves html of login page
  97. */
  98. main: function(request, clientAddress)
  99. {
  100. if(isBanned(clientAddress))
  101. {
  102. return utils.printBannedPage();
  103. }
  104. else
  105. {
  106. return new Promise(function(resolve, reject)
  107. {
  108. Promise.all([utils.include("./admin/login/login.html"),
  109. require("../../sidebar/sidebar.js").main(),
  110. processLogin(request, clientAddress)]).then(function(html)
  111. {
  112. resolve(html.join('') + "</div>");
  113. }).catch(function(err)
  114. {
  115. reject(err);
  116. })
  117. });
  118. }
  119. },
  120. };