Guest User

Untitled

a guest
May 20th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. package xenon.security;
  2.  
  3. import java.security.GeneralSecurityException;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. import xenon.sql.SQLEngine;
  10.  
  11. /**
  12. * Represents a persons account.
  13. *
  14. * @author Colby
  15. */
  16. public class Account {
  17.  
  18. public Account(SQLEngine sql, String attemptUsername, String attemptPassword) throws SQLException, GeneralSecurityException {
  19. username = attemptUsername;
  20. password = attemptPassword;
  21. rights = Rights.Administrator;
  22. if (true) {
  23. return;
  24. }
  25.  
  26. Connection dedi = null;
  27. try {
  28. dedi = sql.getDedicatedConnection();
  29. PreparedStatement stmt = getCredStatement(dedi);
  30. stmt.setString(1, attemptUsername);
  31. ResultSet set = stmt.executeQuery();
  32. if (set.next()) {
  33. String actualHash = set.getString("password");
  34. String actualSalt = set.getString("salt");
  35.  
  36. String attemptHash = MD5.getHash(attemptPassword, actualSalt);
  37. if (!actualHash.equals(attemptHash)) {
  38. throw new GeneralSecurityException("User: " + attemptUsername
  39. + " Invalid password: " + attemptPassword);
  40. }
  41.  
  42. rights = rightsForName(nameForId(translateVBIDToServer(set.getInt("usergroupid"))));
  43. username = attemptUsername;
  44. password = attemptHash;
  45.  
  46. } else {
  47. throw new GeneralSecurityException("Invalid username: "
  48. + attemptUsername);
  49. }
  50.  
  51. } finally {
  52. if (dedi != null) {
  53. dedi.close();
  54. }
  55. }
  56. }
  57.  
  58. @Override
  59. public boolean equals(Object o) {
  60. if (o instanceof Account) {
  61. Account other = (Account) o;
  62. return other.username.equals(username);
  63. }
  64. return false;
  65. }
  66.  
  67. public String getPassword() {
  68. return password;
  69. }
  70.  
  71. public Rights getRights() {
  72. return rights;
  73. }
  74.  
  75. public String getUsername() {
  76. return username;
  77. }
  78.  
  79. public void setPassword(String password) {
  80. this.password = password;
  81. }
  82.  
  83. public void setRights(Rights rights) {
  84. this.rights = rights;
  85. }
  86.  
  87. public void setUsername(String username) {
  88. this.username = username;
  89. }
  90.  
  91. /**
  92. * Translates the vbulliten usergroup id into a number usable by the server
  93. *
  94. * @param VBID
  95. * The number to convert
  96. * @return The converted number
  97. */
  98. public static byte translateVBIDToServer(int VBID) {
  99.  
  100. switch (VBID) {
  101. case 2:
  102. return 0;// Member
  103. case 39:
  104. return 0;// Platinum
  105. case 42:
  106. return 0;// Gold
  107. case 10:
  108. return 1;// Mod
  109. case 9:
  110. return 2;// Admin
  111. case 12:
  112. return 2;// Developer
  113. case 6:
  114. return 2;// Executive
  115.  
  116. default:
  117. return 0;// Undefined
  118. }
  119. }
  120.  
  121. /**
  122. * Translates the specified id into the name of the Rights that holds that
  123. * id
  124. *
  125. * @param id
  126. * The id to get the parents name of
  127. * @return The name of the id's parent
  128. * @throws IllegalArgumentException
  129. * If there is no Rights associated with the specified id
  130. */
  131. public static String nameForId(int id) {
  132. for (Rights r : Rights.values()) {
  133. if (r.getId() == id) {
  134. return r.toString();
  135. }
  136. }
  137. return Rights.Player.toString();
  138. }
  139.  
  140. /**
  141. * Translates the specified name into its parent Rights
  142. *
  143. * @param name
  144. * The name to get the parent
  145. * @return The parent Rights with the specified name
  146. * @see Rights.valueOf(String)
  147. * @throws IllegalArgumentException
  148. * If there is no parent Rights with the specified name
  149. */
  150. public static Rights rightsForName(String name) {
  151. Rights result = Rights.valueOf(name);
  152. return result != null ? result : Rights.Player;
  153. }
  154.  
  155. public enum Rights {
  156.  
  157. Player(0), Moderator(1), Administrator(2);
  158.  
  159. Rights(int id) {
  160. this.id = id;
  161. }
  162.  
  163. public int getId() {
  164. return id;
  165. }
  166. private int id;
  167. }
  168.  
  169. @SuppressWarnings("unused")
  170. private static PreparedStatement getCredStatement(Connection con) {
  171. if (credCheck != null) {
  172. return credCheck;
  173. }
  174.  
  175. try {
  176. return con.prepareStatement("SELECT * FROM user WHERE username=?");
  177.  
  178. } catch (SQLException e) {
  179. throw new RuntimeException(e);
  180. }
  181. }
  182. private String username;
  183. private String password;
  184. private Rights rights;
  185. private static PreparedStatement credCheck;
  186. }
Add Comment
Please, Sign In to add comment