package pt.iscte.ci.authentication; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import net.sourceforge.fenixedu._development.PropertiesManager; import net.sourceforge.fenixedu.applicationTier.Servico.ExcepcaoAutenticacao; import net.sourceforge.fenixedu.applicationTier.security.PasswordEncryptor; import net.sourceforge.fenixedu.domain.exceptions.DomainException; public class BlackboardUtils { private static final String CHECK_BLACKBOARD_SERVER; private static final String PASSWD = "passwd"; private static String databaseDriver; private static String databaseName; private static String databaseUrl; private static String databaseUsername; private static String databasePassword; private static final String BLACKBOARD_DATABASE_DRIVER = "blackboard.database.driver"; private static final String BLACKBOARD_DATABASE_NAME = "blackboard.database.name"; private static final String BLACKBOARD_DATABASE_URL = "blackboard.database.url"; private static final String BLACKBOARD_DATABASE_USERNAME = "blackboard.database.username"; private static final String BLACKBOARD_DATABASE_PASSWORD = "blackboard.database.password"; private static final String GET_USERNAME_AND_PASSWORD_STATEMENT; private static final String GET_USERNAME_STATEMENT; private static final String UPDATE_PASSWORD_STATEMENT; static { databaseDriver = PropertiesManager.getProperty(BLACKBOARD_DATABASE_DRIVER); databaseName = PropertiesManager.getProperty(BLACKBOARD_DATABASE_NAME); databaseUrl = PropertiesManager.getProperty(BLACKBOARD_DATABASE_URL); databaseUsername = PropertiesManager.getProperty(BLACKBOARD_DATABASE_USERNAME); databasePassword = PropertiesManager.getProperty(BLACKBOARD_DATABASE_PASSWORD); GET_USERNAME_AND_PASSWORD_STATEMENT = "SELECT user_id, passwd FROM " + databaseName + ".users WHERE user_id = ?"; UPDATE_PASSWORD_STATEMENT = "UPDATE " + databaseName + ".users SET passwd = ? WHERE user_id = ?"; GET_USERNAME_STATEMENT = "SELECT user_id FROM " + databaseName + ".users WHERE user_id = ?"; CHECK_BLACKBOARD_SERVER = PropertiesManager.getProperty("blackboard.check.enabled"); try { Class.forName(databaseDriver); } catch (ClassNotFoundException e) { System.out.println("ClassNotFoundException " + e); } } private static Connection openAndConfigureConnection() throws SQLException { Connection connection; connection = DriverManager.getConnection(databaseUrl, databaseUsername, databasePassword); return connection; } private static void closeConnection(final Connection connection) { try { if (connection != null && !connection.isClosed()) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); throw new DomainException("error.sql.close.connection.failed", e); } } /** * * @param connection * @param username * The username whose access is to be checked. * @param cryptedPasswordInUpperCase * The password the user has introduced in MD5 format, with all * characters in uppercase. * @return * @throws SQLException */ private static boolean authenticate(final Connection connection, final String username, final String cryptedPasswordInUpperCase) throws SQLException { final PreparedStatement stm = connection.prepareStatement(GET_USERNAME_AND_PASSWORD_STATEMENT); stm.setString(1, username); final ResultSet rs = stm.executeQuery(); if (rs.next()) { final String userPassword = rs.getString(PASSWD); return userPassword.equals(cryptedPasswordInUpperCase); } return false; } public static void authenticate(final String username, final String password) throws ExcepcaoAutenticacao { if (!checkBlackboard()) { return; } Connection connection = null; try { connection = openAndConfigureConnection(); if (!authenticate(connection, username, PasswordEncryptor.generatePasswordForBlackboard(password))) { throw new ExcepcaoAutenticacao(); } } catch (SQLException e) { e.printStackTrace(); throw new DomainException("error.blackboard.sql.exception"); } finally { closeConnection(connection); } } public static void changePassword(final String username, final String newPassword) { if (!checkBlackboard()) { return; } Connection connection = null; try { connection = openAndConfigureConnection(); final PreparedStatement stm = connection.prepareStatement(UPDATE_PASSWORD_STATEMENT); stm.setString(1, PasswordEncryptor.generatePasswordForBlackboard(newPassword)); stm.setString(2, username); stm.executeUpdate(); System.out.println("Blackboard password changed successfully for username " + username); connection.commit(); } catch (SQLException e) { e.printStackTrace(); } finally { closeConnection(connection); } } public static boolean isUserInBlackboard(final String username) { if (!checkBlackboard()) { return false; } Connection connection = null; try { connection = openAndConfigureConnection(); final PreparedStatement stm = connection.prepareStatement(GET_USERNAME_STATEMENT); stm.setString(1, username); final ResultSet rs = stm.executeQuery(); if (rs.next()) { return true; } return false; } catch (SQLException e) { e.printStackTrace(); } finally { closeConnection(connection); } return false; } /** * * @return Returns true if an access to the email server database should be * performed. It returns false otherwise. This method returns the * property set in the build configuration file, where one specifies * if this should be enabled or not. */ public static boolean checkBlackboard() { return CHECK_BLACKBOARD_SERVER == null || "true".equals(CHECK_BLACKBOARD_SERVER); } }