Guest User

Untitled

a guest
Jan 5th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.82 KB | None | 0 0
  1. package PasswordGenerator;
  2.  
  3. import java.sql.*;
  4.  
  5. /**
  6. *
  7. * @author emil
  8. */
  9. public class LoginDB {
  10.  
  11. public static final int USER_NOT_IN_DB = 0;
  12. public static final int SUCCESS = 1;
  13. public static final int DUPLICATE_USERS_EXIST = 2;
  14. public static final int PATIENT_ACCSESS = 3;
  15. public static final int DOCTOR_ACCSESS = 4;
  16. public static final int ADMIN_ACCESS = 5;
  17.  
  18. static final int MAX_ONE_TIME_PASSWORD_NBR = 100;
  19.  
  20. public static int checkLogin(String username, String password) throws SQLException {
  21.  
  22. Connection connection = DB.kopplaUpp();
  23. PreparedStatement ps = null;
  24. String query = null;
  25. int result = -1;
  26.  
  27. query = "SELECT loginPatient(?,?)";
  28. ps = connection.prepareStatement(query);
  29. ps.setString(1, username);
  30. ps.setString(2, password);
  31. ResultSet rs = ps.executeQuery();
  32.  
  33. while (rs.next()) {
  34. result = rs.getInt(1);
  35. }
  36.  
  37. if (result == SUCCESS) {
  38. return PATIENT_ACCSESS;
  39. } else if (result >= DUPLICATE_USERS_EXIST) {
  40. //Logg
  41. } else {
  42. query = "SELECT loginDoctor(?,?)";
  43. ps = connection.prepareStatement(query);
  44. ps.setString(1, username);
  45. ps.setString(2, password);
  46. rs = ps.executeQuery();
  47.  
  48. while (rs.next()) {
  49. result = rs.getInt(1);
  50. }
  51.  
  52. if (result == SUCCESS) {
  53. return DOCTOR_ACCSESS;
  54. } else if (result >= DUPLICATE_USERS_EXIST) {
  55. //Logg
  56. } else {
  57. query = "SELECT loginAdmin(?,?)";
  58. ps = connection.prepareStatement(query);
  59. ps.setString(1, username);
  60. ps.setString(2, password);
  61. rs = ps.executeQuery();
  62.  
  63. while (rs.next()) {
  64. result = rs.getInt(1);
  65. }
  66.  
  67. if (result == SUCCESS) {
  68. return ADMIN_ACCESS;
  69. } else if (result >= DUPLICATE_USERS_EXIST) {
  70. //Logg
  71. }
  72. }
  73. }
  74.  
  75. DB.kopplaNer();
  76.  
  77. return USER_NOT_IN_DB;
  78. }
  79.  
  80. public static int loginPatientOneTimePasswords(int patientId, String password) throws SQLException {
  81. Connection connection = DB.kopplaUpp();
  82. PreparedStatement ps = null;
  83. String query = null;
  84. int result = -1;
  85.  
  86. query = "SELECT loginPatientOneTimePasswords(?,SHA1(?))";
  87. ps = connection.prepareStatement(query);
  88. ps.setInt(1, patientId);
  89. ps.setString(2, password);
  90. ResultSet rs = ps.executeQuery();
  91.  
  92. if (rs.next()) {
  93. result = rs.getInt(1);
  94. }
  95.  
  96. DBUtil.closePreparedStatement(ps);
  97. DBUtil.closeResultSet(rs);
  98. DB.kopplaNer();
  99.  
  100. if (result == SUCCESS) {
  101. return PATIENT_ACCSESS;
  102. }
  103.  
  104. return USER_NOT_IN_DB;
  105. }
  106.  
  107. public static int loginDoctorOneTimePasswords(int doctorId, String password) throws SQLException {
  108. Connection connection = DB.kopplaUpp();
  109. PreparedStatement ps = null;
  110. String query = null;
  111. int result = -1;
  112.  
  113. query = "SELECT loginDoctorOneTimePasswords(?,SHA1(?))";
  114. ps = connection.prepareStatement(query);
  115. ps.setInt(1, doctorId);
  116. ps.setString(2, password);
  117. ResultSet rs = ps.executeQuery();
  118.  
  119. if (rs.next()) {
  120. result = rs.getInt(1);
  121. }
  122.  
  123. DBUtil.closePreparedStatement(ps);
  124. DBUtil.closeResultSet(rs);
  125. DB.kopplaNer();
  126.  
  127. if (result == SUCCESS) {
  128. return DOCTOR_ACCSESS;
  129. }
  130.  
  131. return USER_NOT_IN_DB;
  132. }
  133.  
  134. public static int loginAdminOneTimePasswords(int adminId, String password) throws SQLException {
  135. Connection connection = DB.kopplaUpp();
  136. PreparedStatement ps = null;
  137. String query = null;
  138. int result = -1;
  139.  
  140. query = "SELECT loginAdminOneTimePasswords(?,SHA1(?))";
  141. ps = connection.prepareStatement(query);
  142. ps.setInt(1, adminId);
  143. ps.setString(2, password);
  144. ResultSet rs = ps.executeQuery();
  145.  
  146. if (rs.next()) {
  147. result = rs.getInt(1);
  148. }
  149.  
  150. DBUtil.closePreparedStatement(ps);
  151. DBUtil.closeResultSet(rs);
  152. DB.kopplaNer();
  153.  
  154. if (result == SUCCESS) {
  155. return ADMIN_ACCESS;
  156. }
  157.  
  158. return USER_NOT_IN_DB;
  159. }
  160.  
  161. public static boolean checkIfPaientsLatsOneTimePasswordIsUsed(int patientId) throws SQLException {
  162. Connection connection = DB.kopplaUpp();
  163. PreparedStatement ps = null;
  164. String query = null;
  165. int result = -1;
  166.  
  167. query = "SELECT MIN(passwordNumber) FROM PatientOneTimePasswords WHERE patientId=?";
  168. ps = connection.prepareStatement(query);
  169. ps.setInt(1, patientId);
  170. ResultSet rs = ps.executeQuery();
  171.  
  172. if (rs.next()) {
  173. result = rs.getInt(1);
  174. }
  175.  
  176. DBUtil.closePreparedStatement(ps);
  177. DBUtil.closeResultSet(rs);
  178. DB.kopplaNer();
  179.  
  180. return result == MAX_ONE_TIME_PASSWORD_NBR;
  181. }
  182.  
  183. public static boolean checkIfDoctorsLatsOneTimePasswordIsUsed(int doctorId) throws SQLException {
  184. Connection connection = DB.kopplaUpp();
  185. PreparedStatement ps = null;
  186. String query = null;
  187. int result = -1;
  188.  
  189. query = "SELECT MIN(passwordNumber) FROM DoctorOneTimePasswords WHERE doctorId=?";
  190. ps = connection.prepareStatement(query);
  191. ps.setInt(1, doctorId);
  192. ResultSet rs = ps.executeQuery();
  193.  
  194. if (rs.next()) {
  195. result = rs.getInt(1);
  196. }
  197.  
  198. DBUtil.closePreparedStatement(ps);
  199. DBUtil.closeResultSet(rs);
  200. DB.kopplaNer();
  201.  
  202. return result == MAX_ONE_TIME_PASSWORD_NBR;
  203. }
  204.  
  205. public static int checkLoginHashedPassword(String username, String password) throws SQLException {
  206.  
  207. Connection connection = DB.kopplaUpp();
  208. PreparedStatement ps = null;
  209. String query = null;
  210. int result = -1;
  211.  
  212. query = "SELECT loginPatient(?,SHA1(?))";
  213. ps = connection.prepareStatement(query);
  214. ps.setString(1, username);
  215. ps.setString(2, password);
  216. ResultSet rs = ps.executeQuery();
  217.  
  218. while (rs.next()) {
  219. result = rs.getInt(1);
  220. }
  221.  
  222. if (result == SUCCESS) {
  223. return PATIENT_ACCSESS;
  224. } else if (result >= DUPLICATE_USERS_EXIST) {
  225. //Logg
  226. } else {
  227. query = "SELECT loginDoctor(?,SHA1(?))";
  228. ps = connection.prepareStatement(query);
  229. ps.setString(1, username);
  230. ps.setString(2, password);
  231. rs = ps.executeQuery();
  232.  
  233. while (rs.next()) {
  234. result = rs.getInt(1);
  235. }
  236.  
  237. if (result == SUCCESS) {
  238. return DOCTOR_ACCSESS;
  239. } else if (result >= DUPLICATE_USERS_EXIST) {
  240. //Logg
  241. } else {
  242. query = "SELECT loginAdmin(?,SHA1(?))";
  243. ps = connection.prepareStatement(query);
  244. ps.setString(1, username);
  245. ps.setString(2, password);
  246. rs = ps.executeQuery();
  247.  
  248. while (rs.next()) {
  249. result = rs.getInt(1);
  250. }
  251.  
  252. if (result == SUCCESS) {
  253. return ADMIN_ACCESS;
  254. } else if (result >= DUPLICATE_USERS_EXIST) {
  255. //Logg
  256. }
  257. }
  258. }
  259.  
  260. DBUtil.closePreparedStatement(ps);
  261. DBUtil.closeResultSet(rs);
  262. DB.kopplaNer();
  263.  
  264. return USER_NOT_IN_DB;
  265. }
  266.  
  267. public static int getCurrOneTimePasswordNumberPatient(int patientId) throws SQLException {
  268. Connection connection = DB.kopplaUpp();
  269. PreparedStatement ps = null;
  270. ResultSet rs = null;
  271. int passwordNumber = 0;
  272.  
  273. ps = connection.prepareStatement("SELECT MIN(passwordNumber) FROM PatientOneTimePasswords WHERE patientId=?");
  274. ps.setInt(1, patientId);
  275. rs = ps.executeQuery();
  276.  
  277. if (rs.next()) {
  278. passwordNumber = rs.getInt(1);
  279. }
  280.  
  281. DBUtil.closePreparedStatement(ps);
  282. DBUtil.closeResultSet(rs);
  283. DB.kopplaNer();
  284.  
  285. return passwordNumber;
  286.  
  287. }
  288.  
  289. public static int getCurrOneTimePasswordNumberDoctor(int doctorId) throws SQLException {
  290. Connection connection = DB.kopplaUpp();
  291. PreparedStatement ps = null;
  292. ResultSet rs = null;
  293. int passwordNumber = 0;
  294.  
  295. ps = connection.prepareStatement("SELECT MIN(passwordNumber) FROM DoctorOneTimePasswords WHERE doctorId=?");
  296. ps.setInt(1, doctorId);
  297. rs = ps.executeQuery();
  298.  
  299. if (rs.next()) {
  300. passwordNumber = rs.getInt(1);
  301. }
  302.  
  303. DBUtil.closePreparedStatement(ps);
  304. DBUtil.closeResultSet(rs);
  305. DB.kopplaNer();
  306.  
  307. return passwordNumber;
  308.  
  309. }
  310.  
  311. public static int getCurrOneTimePasswordNumberAdmin(int adminId) throws SQLException {
  312. Connection connection = DB.kopplaUpp();
  313. PreparedStatement ps = null;
  314. ResultSet rs = null;
  315. int passwordNumber = 0;
  316.  
  317. ps = connection.prepareStatement("SELECT MIN(passwordNumber) FROM AdminOneTimePasswords WHERE adminId=?");
  318. ps.setInt(1, adminId);
  319. rs = ps.executeQuery();
  320.  
  321. if (rs.next()) {
  322. passwordNumber = rs.getInt(1);
  323. }
  324.  
  325. DBUtil.closePreparedStatement(ps);
  326. DBUtil.closeResultSet(rs);
  327. DB.kopplaNer();
  328.  
  329. return passwordNumber;
  330.  
  331. }
  332.  
  333. public static String getHashedOneTimePasswordPatient(String patientUsername) throws SQLException {
  334. Connection connection = DB.kopplaUpp();
  335. PreparedStatement ps = null;
  336. ResultSet rs = null;
  337. String hashedPassword = null;
  338.  
  339. ps = connection.prepareStatement("SELECT tempPatientOneTimePassword From Patient WHERE patientUsername=?");
  340. ps.setString(1, patientUsername);
  341. rs = ps.executeQuery();
  342.  
  343. if (rs.next()) {
  344. hashedPassword = rs.getString(1);
  345. }
  346.  
  347. DBUtil.closePreparedStatement(ps);
  348. DBUtil.closeResultSet(rs);
  349. DB.kopplaNer();
  350.  
  351. return hashedPassword;
  352.  
  353. }
  354.  
  355. public static String getHashedOneTimePasswordDoctor(String doctorUsername) throws SQLException {
  356. Connection connection = DB.kopplaUpp();
  357. PreparedStatement ps = null;
  358. ResultSet rs = null;
  359. String hashedPassword = null;
  360.  
  361. ps = connection.prepareStatement("SELECT tempDoctorOneTimePassword From Doctor WHERE doctorUsername=?");
  362. ps.setString(1, doctorUsername);
  363. rs = ps.executeQuery();
  364.  
  365. if (rs.next()) {
  366. hashedPassword = rs.getString(1);
  367. }
  368.  
  369. DBUtil.closePreparedStatement(ps);
  370. DBUtil.closeResultSet(rs);
  371. DB.kopplaNer();
  372.  
  373. return hashedPassword;
  374.  
  375. }
  376.  
  377. public static int checkAccessRights(String username, String oneTimePassword) throws SQLException {
  378. Connection connection = DB.kopplaUpp();
  379. PreparedStatement ps = null;
  380. String query = null;
  381. int result = -1;
  382.  
  383. query = "SELECT checkPatientCurrOneTimePassword(?,?)";
  384. ps = connection.prepareStatement(query);
  385. ps.setString(1, username);
  386. ps.setString(2, oneTimePassword);
  387. ResultSet rs = ps.executeQuery();
  388.  
  389. while (rs.next()) {
  390. result = rs.getInt(1);
  391. }
  392.  
  393. if (result == SUCCESS) {
  394. return PATIENT_ACCSESS;
  395.  
  396. } else {
  397. query = "SELECT checkDoctorCurrOneTimePassword(?,?)";
  398. ps = connection.prepareStatement(query);
  399. ps.setString(1, username);
  400. ps.setString(2, oneTimePassword);
  401. rs = ps.executeQuery();
  402.  
  403. while (rs.next()) {
  404. result = rs.getInt(1);
  405. }
  406.  
  407. if (result == SUCCESS) {
  408. return DOCTOR_ACCSESS;
  409.  
  410. } else {
  411. // query = "SELECT checkDoctorCurrOneTimePassword(?,?)";
  412. // ps = connection.prepareStatement(query);
  413. // ps.setString(1, username);
  414. // ps.setString(2, oneTimePassword);
  415. // rs = ps.executeQuery();
  416. //
  417. // while (rs.next()) {
  418. // result = rs.getInt(1);
  419. // }
  420.  
  421. }
  422.  
  423. }
  424.  
  425. DBUtil.closePreparedStatement(ps);
  426. DBUtil.closeResultSet(rs);
  427. DB.kopplaNer();
  428.  
  429. return USER_NOT_IN_DB;
  430.  
  431. }
  432.  
  433. public static String getHashedPasswordDoctor(String doctorUsername, String doctorPassword) throws SQLException {
  434. Connection connection = DB.kopplaUpp();
  435. PreparedStatement ps = null;
  436. ResultSet rs = null;
  437. String hashedPassword = null;
  438.  
  439. ps = connection.prepareStatement("SELECT doctorPassword From Doctor WHERE doctorUsername=? AND doctorPassword=SHA1(?)");
  440. ps.setString(1, doctorUsername);
  441. ps.setString(2, doctorPassword);
  442. rs = ps.executeQuery();
  443.  
  444. if (rs.next()) {
  445. hashedPassword = rs.getString(1);
  446. }
  447.  
  448. DBUtil.closePreparedStatement(ps);
  449. DBUtil.closeResultSet(rs);
  450. DB.kopplaNer();
  451.  
  452. return hashedPassword;
  453.  
  454. }
  455.  
  456. public static String getHashedPasswordAdmin(String adminUsername, String adminPassword) throws SQLException {
  457. Connection connection = DB.kopplaUpp();
  458. PreparedStatement ps = null;
  459. ResultSet rs = null;
  460. String hashedPassword = null;
  461.  
  462. ps = connection.prepareStatement("SELECT adminPassword From Admin WHERE adminUsername=? AND adminPassword=SHA1(?)");
  463. ps.setString(1, adminUsername);
  464. ps.setString(2, adminPassword);
  465. rs = ps.executeQuery();
  466.  
  467. if (rs.next()) {
  468. hashedPassword = rs.getString(1);
  469. }
  470.  
  471. DBUtil.closePreparedStatement(ps);
  472. DBUtil.closeResultSet(rs);
  473. DB.kopplaNer();
  474.  
  475. return hashedPassword;
  476.  
  477. }
  478.  
  479. public static void setPatientLastLoggedInTimestamp(String username, String password) throws SQLException {
  480. Connection connection = DB.kopplaUpp();
  481. PreparedStatement ps = null;
  482.  
  483. ps = connection.prepareStatement("UPDATE Patient SET lastLoggedIn=now() WHERE patientUsername=? AND patientPassword=SHA1(?)");
  484. ps.setString(1, username);
  485. ps.setString(2, password);
  486. ps.executeUpdate();
  487.  
  488. DBUtil.closePreparedStatement(ps);
  489. DB.kopplaNer();
  490.  
  491. }
  492.  
  493. public static String getPatientLastLoggedInTimestamp(String username) throws SQLException {
  494. Connection connection = DB.kopplaUpp();
  495. PreparedStatement ps = null;
  496. ResultSet rs = null;
  497. String lastLoggedInTimestamp = null;
  498.  
  499. ps = connection.prepareStatement("SELECT lastLoggedIn FROM Patient WHERE patientUsername=?");
  500. ps.setString(1, username);
  501. rs = ps.executeQuery();
  502.  
  503. if (rs.next()) {
  504. lastLoggedInTimestamp = rs.getString(1);
  505. }
  506.  
  507. DBUtil.closePreparedStatement(ps);
  508. DBUtil.closeResultSet(rs);
  509. DB.kopplaNer();
  510.  
  511. return lastLoggedInTimestamp;
  512.  
  513. }
  514.  
  515. public static void changePasswordPatient(String username, String newPassword) throws SQLException {
  516. Connection connection = DB.kopplaUpp();
  517. PreparedStatement ps = null;
  518.  
  519. ps = connection.prepareStatement("UPDATE Patient SET patientPassword=SHA1(?) where patientUsername=?");
  520. ps.setString(1, newPassword);
  521. ps.setString(2, username);
  522. ps.executeUpdate();
  523.  
  524. DBUtil.closePreparedStatement(ps);
  525. DB.kopplaNer();
  526.  
  527. }
  528.  
  529. public static void changePasswordDoctor(String username, String newPassword) throws SQLException {
  530. Connection connection = DB.kopplaUpp();
  531. PreparedStatement ps = null;
  532.  
  533. ps = connection.prepareStatement("UPDATE Doctor SET doctorPassword=SHA1(?) where doctorUsername=?");
  534. ps.setString(1, newPassword);
  535. ps.setString(2, username);
  536. ps.executeUpdate();
  537.  
  538. DBUtil.closePreparedStatement(ps);
  539. DB.kopplaNer();
  540.  
  541. }
  542.  
  543. public static void changePasswordAdmin(String username, String newPassword) throws SQLException {
  544. Connection connection = DB.kopplaUpp();
  545. PreparedStatement ps = null;
  546.  
  547. ps = connection.prepareStatement("UPDATE Admin SET adminPassword=SHA1(?) where adminUsername=?");
  548. ps.setString(1, newPassword);
  549. ps.setString(2, username);
  550. ps.executeUpdate();
  551.  
  552. DBUtil.closePreparedStatement(ps);
  553. DB.kopplaNer();
  554.  
  555. }
  556. }
Add Comment
Please, Sign In to add comment