Created
October 8, 2017 19:19
-
-
Save aaronweaver/cc1c5b65032ed3ac84f3518aa0c1ecb5 to your computer and use it in GitHub Desktop.
Challenge #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
class Login { | |
public Connection getConnection() throws SQLException { | |
DriverManager.registerDriver(new | |
com.microsoft.sqlserver.jdbc.SQLServerDriver()); | |
String dbConnection = | |
PropertyManager.getProperty("db.connection"); | |
// Can hold some value like | |
// "jdbc:microsoft:sqlserver://<HOST>:1433,<UID>,<PWD>" | |
return DriverManager.getConnection(dbConnection); | |
} | |
String hashPassword(char[] password) { | |
// Create hash of password | |
} | |
public void doPrivilegedAction(String username, char[] password) | |
throws SQLException { | |
Connection connection = getConnection(); | |
if (connection == null) { | |
// Handle error | |
} | |
try { | |
String pwd = hashPassword(password); | |
String sqlString = "SELECT * FROM db_user WHERE username = '" | |
+ username + | |
"' AND password = '" + pwd + "'"; | |
Statement stmt = connection.createStatement(); | |
ResultSet rs = stmt.executeQuery(sqlString); | |
if (!rs.next()) { | |
throw new SecurityException( | |
"User name or password incorrect" | |
); | |
} | |
// Authenticated; proceed | |
} finally { | |
try { | |
connection.close(); | |
} catch (SQLException x) { | |
// Forward to handler | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment