Guest User

Untitled

a guest
Jul 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <regex>
  6. #include <iterator>
  7. #include <sstream>
  8. #include "auth.h"
  9. #include "md5.h"
  10.  
  11. using namespace std;
  12.  
  13. string trim(string& str);
  14. vector<string> split(string str, char del);
  15.  
  16. // Start of auth class implementation
  17. const string auth::salt = "lolmd5";
  18. const string auth::loginFilename = "logins.txt";
  19.  
  20. auth::auth(){
  21.  
  22. }
  23. vector<string> auth::loadFile(){
  24. vector<string> lines;
  25. ifstream inDocument;
  26. inDocument.open(auth::loginFilename);
  27.  
  28. // Read the document to be checked
  29. if (!inDocument){
  30. cerr << "Unable to open file " << auth::loginFilename << endl;
  31. } else {
  32. string readLine;
  33.  
  34. while(inDocument.eof() == false){
  35. getline(inDocument,readLine);
  36. lines.push_back(readLine);
  37. }
  38. }
  39.  
  40. inDocument.close();
  41. return lines;
  42. }
  43. void auth::saveFile(vector<string> logins){
  44. ofstream oFile;
  45. vector<string>::iterator vi;
  46.  
  47. // Open the output file
  48. oFile.open(auth::loginFilename.c_str());
  49.  
  50. // Write the lines to the file
  51. for (vi=logins.begin();vi!=logins.end();vi++){
  52. if (trim(*vi) != ""){
  53. oFile << *vi << endl;
  54. }
  55. }
  56.  
  57. oFile.close();
  58. }
  59. bool auth::removeUser(string username, string password){
  60. vector<string> logins = auth::loadFile();
  61. vector<string>::iterator vi;
  62. bool found = false;
  63.  
  64. // Loop through the logins file for a matching username. If one is found, return false
  65. // If one is not found, return false.
  66. for (vi=logins.begin();vi!=logins.end();vi++){
  67. vector<string> loginVec = split(*vi,',');
  68. string loginUsername = "";
  69. string loginHash = "";
  70.  
  71. if (trim(*vi) != ""){
  72. loginUsername = trim(loginVec[0]);
  73. loginHash = trim(loginVec[1]);
  74.  
  75. if (loginUsername == username && auth::login(username,password)){
  76. found = true;
  77. *vi = "";
  78. break;
  79. }
  80. }
  81. }
  82.  
  83. if (found){
  84. auth::saveFile(logins);
  85. }
  86.  
  87. return found;
  88. }
  89. bool auth::addUser(string username, string password){
  90. vector<string> logins = auth::loadFile();
  91. vector<string>::iterator vi;
  92.  
  93. // Loop through the logins file for a matching username. If one is found, return false
  94. // If one is not found, return false.
  95. for (vi=logins.begin();vi!=logins.end();vi++){
  96. vector<string> loginVec = split(*vi,',');
  97. string loginUsername = "";
  98. string loginHash = "";
  99.  
  100. if (trim(*vi) != ""){
  101. loginUsername = trim(loginVec[0]);
  102. loginHash = trim(loginVec[1]);
  103.  
  104. if (username == loginUsername){
  105. return false; // User already exists
  106. }
  107. }
  108. }
  109.  
  110. logins.push_back(username + "," + md5(auth::salt+trim(username)+trim(password)));
  111. auth::saveFile(logins);
  112. return true;
  113. }
  114. bool auth::changePassword(string username, string oldPassword, string newPassword1, string newPassword2){
  115. if (newPassword1 == newPassword2){
  116. return auth::changePassword(username, oldPassword, newPassword1);
  117. } else {
  118. return false;
  119. }
  120. }
  121. bool auth::changePassword(string username, string oldPassword, string newPassword){
  122. vector<string> logins = auth::loadFile();
  123. vector<string>::iterator vi;
  124.  
  125. // Loop through the logins file for a matching username and hash. If one is found, consider the user authenticated
  126. // If one is not found, return false.
  127. for (vi=logins.begin();vi!=logins.end();vi++){
  128. vector<string> loginVec = split(*vi,',');
  129. string loginUsername = "";
  130. string loginHash = "";
  131.  
  132. if (trim(*vi) != ""){
  133. loginUsername = trim(loginVec[0]);
  134. loginHash = trim(loginVec[1]);
  135. }
  136.  
  137. if (username == loginUsername){
  138. if (auth::login(username,oldPassword)){
  139. *vi = username + "," + md5(auth::salt+trim(username)+trim(newPassword));
  140. auth::saveFile(logins);
  141. return true;
  142. } else {
  143. cerr << "Invalid password for user \"" << username << "\"" << endl;
  144. return false;
  145. }
  146. }
  147. }
  148.  
  149. cerr << "Invalid username/password" << endl;
  150. return false;
  151. }
  152. bool auth::login(string username, string password){
  153. // Construct passwords using the md5 algorithm
  154. // The input is a concatenated string consisting of the salt, the username entered, and the password given.
  155. // e.g. md5("lolmd5chrispassword") = "53ec5bbab50d715f882485af18358b4e"
  156. // This stops people from simply switching passwords around to login as different users or using a lookup table
  157. string hash = md5(auth::salt+trim(username)+trim(password));
  158.  
  159. vector<string> logins = auth::loadFile();
  160. vector<string>::iterator vi;
  161.  
  162. // Loop through the logins file for a matching username and hash. If one is found, consider the user authenticated
  163. // If one is not found, return false.
  164. for (vi=logins.begin();vi!=logins.end();vi++){
  165. if (trim(*vi) != ""){
  166. vector<string> loginVec = split(*vi,',');
  167. string loginUsername = trim(loginVec[0]);
  168. string loginHash = trim(loginVec[1]);
  169.  
  170. if (loginUsername == username && loginHash == hash){
  171. return true;
  172. }
  173. }
  174. }
  175.  
  176. cerr << "Bad username/password" << endl;
  177. return false;
  178. }
  179. // End of auth class
  180.  
  181. string trim(string& str){
  182. const string blank = "";
  183. regex rgxTrim("^\\s+|\\s+$");
  184. str = std::tr1::regex_replace(str, rgxTrim, blank);
  185. return str;
  186. }
  187. vector<string> split(string str, char del){
  188. string token, text(str);
  189. istringstream iss(text);
  190. vector<string> vec = vector<string>();
  191.  
  192. while (getline(iss, token, del)){
  193. vec.push_back(token);
  194. }
  195.  
  196. return vec;
  197. }
Add Comment
Please, Sign In to add comment