broker/DBAuthenticator: code formatter used
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
package io.moquette.spi.impl.security;
|
package io.moquette.spi.impl.security;
|
||||||
|
|
||||||
import io.moquette.BrokerConstants;
|
import io.moquette.BrokerConstants;
|
||||||
@@ -6,14 +7,12 @@ import io.moquette.spi.security.IAuthenticator;
|
|||||||
import org.apache.commons.codec.binary.Hex;
|
import org.apache.commons.codec.binary.Hex;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load user credentials from a SQL database.
|
* Load user credentials from a SQL database. sql driver must be provided at runtime
|
||||||
* sql driver must be provided at runtime
|
|
||||||
*
|
*
|
||||||
* @author mackristof
|
* @author mackristof
|
||||||
*/
|
*/
|
||||||
@@ -25,30 +24,34 @@ public class DBAuthenticator implements IAuthenticator {
|
|||||||
private final MessageDigest messageDigest;
|
private final MessageDigest messageDigest;
|
||||||
private final PreparedStatement preparedStatement;
|
private final PreparedStatement preparedStatement;
|
||||||
|
|
||||||
|
public DBAuthenticator(IConfig conf) {
|
||||||
|
this(
|
||||||
|
conf.getProperty(BrokerConstants.DB_AUTHENTICATOR_DRIVER, ""),
|
||||||
public DBAuthenticator(IConfig conf){
|
conf.getProperty(BrokerConstants.DB_AUTHENTICATOR_URL, ""),
|
||||||
this(conf.getProperty(BrokerConstants.DB_AUTHENTICATOR_DRIVER, ""),
|
conf.getProperty(BrokerConstants.DB_AUTHENTICATOR_QUERY, ""),
|
||||||
conf.getProperty(BrokerConstants.DB_AUTHENTICATOR_URL,""),
|
conf.getProperty(BrokerConstants.DB_AUTHENTICATOR_DIGEST, ""));
|
||||||
conf.getProperty(BrokerConstants.DB_AUTHENTICATOR_QUERY, ""),
|
|
||||||
conf.getProperty(BrokerConstants.DB_AUTHENTICATOR_DIGEST,""));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** provide authenticator from SQL database
|
/**
|
||||||
* @param driver : jdbc driver class like : "org.postgresql.Driver"
|
* provide authenticator from SQL database
|
||||||
* @param jdbcUrl : jdbc url like : "jdbc:postgresql://host:port/dbname"
|
*
|
||||||
* @param sqlQuery : sql query like : "SELECT PASSWORD FROM USER WHERE LOGIN=?"
|
* @param driver
|
||||||
* @param digestMethod : password encoding algorithm : "MD5", "SHA-1", "SHA-256"
|
* : jdbc driver class like : "org.postgresql.Driver"
|
||||||
|
* @param jdbcUrl
|
||||||
|
* : jdbc url like : "jdbc:postgresql://host:port/dbname"
|
||||||
|
* @param sqlQuery
|
||||||
|
* : sql query like : "SELECT PASSWORD FROM USER WHERE LOGIN=?"
|
||||||
|
* @param digestMethod
|
||||||
|
* : password encoding algorithm : "MD5", "SHA-1", "SHA-256"
|
||||||
*/
|
*/
|
||||||
public DBAuthenticator(String driver, String jdbcUrl, String sqlQuery, String digestMethod){
|
public DBAuthenticator(String driver, String jdbcUrl, String sqlQuery, String digestMethod) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Class.forName(driver);
|
Class.forName(driver);
|
||||||
final Connection connection = DriverManager.getConnection(jdbcUrl);
|
final Connection connection = DriverManager.getConnection(jdbcUrl);
|
||||||
this.messageDigest = MessageDigest.getInstance(digestMethod);
|
this.messageDigest = MessageDigest.getInstance(digestMethod);
|
||||||
this.preparedStatement = connection.prepareStatement(sqlQuery);
|
this.preparedStatement = connection.prepareStatement(sqlQuery);
|
||||||
} catch (ClassNotFoundException cnfe){
|
} catch (ClassNotFoundException cnfe) {
|
||||||
LOG.error(String.format("Can't find driver %s", driver), cnfe);
|
LOG.error(String.format("Can't find driver %s", driver), cnfe);
|
||||||
throw new RuntimeException(cnfe);
|
throw new RuntimeException(cnfe);
|
||||||
} catch (SQLException sqle) {
|
} catch (SQLException sqle) {
|
||||||
@@ -67,11 +70,11 @@ public class DBAuthenticator implements IAuthenticator {
|
|||||||
LOG.info("username or password was null");
|
LOG.info("username or password was null");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
ResultSet r= null;
|
ResultSet r = null;
|
||||||
try {
|
try {
|
||||||
this.preparedStatement.setString(1,username);
|
this.preparedStatement.setString(1, username);
|
||||||
r = this.preparedStatement.executeQuery();
|
r = this.preparedStatement.executeQuery();
|
||||||
if(r.next()) {
|
if (r.next()) {
|
||||||
final String foundPwq = r.getString(1);
|
final String foundPwq = r.getString(1);
|
||||||
messageDigest.update(password);
|
messageDigest.update(password);
|
||||||
byte[] digest = messageDigest.digest();
|
byte[] digest = messageDigest.digest();
|
||||||
|
|||||||
Reference in New Issue
Block a user