broker/ResourceAuthenticator: code formatter used

This commit is contained in:
Andreas Mager
2017-02-01 17:13:40 +01:00
parent 92f4492fc3
commit 3bbec46585
@@ -13,6 +13,7 @@
* *
* You may elect to redistribute this code under either of these licenses. * You may elect to redistribute this code under either of these licenses.
*/ */
package io.moquette.spi.impl.security; package io.moquette.spi.impl.security;
import io.moquette.server.config.IResourceLoader; import io.moquette.server.config.IResourceLoader;
@@ -20,7 +21,6 @@ import io.moquette.spi.security.IAuthenticator;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
@@ -31,14 +31,17 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
* Load user credentials from a text resource. * Load user credentials from a text resource. Each line of the file is formatted as
* Each line of the file is formatted as "[username]:[sha256(password)]". The username mustn't contains : char. * "[username]:[sha256(password)]". The username mustn't contains : char.
* *
* To encode your password from command line on Linux systems, you could use: * To encode your password from command line on Linux systems, you could use:
*
* <pre> * <pre>
* echo -n "yourpassword" | sha256sum * echo -n "yourpassword" | sha256sum
* </pre> * </pre>
* NB -n is important because echo append a newline by default at the of string. -n avoid this behaviour. *
* NB -n is important because echo append a newline by default at the of string. -n avoid this
* behaviour.
* *
* @author andrea * @author andrea
*/ */
@@ -63,24 +66,23 @@ public class ResourceAuthenticator implements IAuthenticator {
reader = resourceLoader.loadResource(resourceName); reader = resourceLoader.loadResource(resourceName);
if (reader == null) { if (reader == null) {
LOG.warn(String.format("Parsing not existing %s %s", resourceLoader.getName(), resourceName)); LOG.warn(String.format("Parsing not existing %s %s", resourceLoader.getName(), resourceName));
} } else {
else {
parse(reader); parse(reader);
} }
} } catch (IResourceLoader.ResourceIsDirectoryException e) {
catch (IResourceLoader.ResourceIsDirectoryException e) {
LOG.warn(String.format("Trying to parse directory %s", resourceName)); LOG.warn(String.format("Trying to parse directory %s", resourceName));
} } catch (ParseException pex) {
catch (ParseException pex) { LOG.warn(
LOG.warn(String.format("Format error in parsing password %s %s", resourceLoader.getName(), resourceName), pex); String.format("Format error in parsing password %s %s", resourceLoader.getName(), resourceName),
pex);
} }
} }
private void parse(Reader reader) throws ParseException { private void parse(Reader reader) throws ParseException {
if (reader == null) { if (reader == null) {
return; return;
} }
BufferedReader br = new BufferedReader(reader); BufferedReader br = new BufferedReader(reader);
String line; String line;
try { try {
@@ -88,23 +90,23 @@ public class ResourceAuthenticator implements IAuthenticator {
int commentMarker = line.indexOf('#'); int commentMarker = line.indexOf('#');
if (commentMarker != -1) { if (commentMarker != -1) {
if (commentMarker == 0) { if (commentMarker == 0) {
//skip its a comment // skip its a comment
continue; continue;
} else { } else {
//it's a malformed comment // it's a malformed comment
throw new ParseException(line, commentMarker); throw new ParseException(line, commentMarker);
} }
} else { } else {
if (line.isEmpty() || line.matches("^\\s*$")) { if (line.isEmpty() || line.matches("^\\s*$")) {
//skip it's a black line // skip it's a black line
continue; continue;
} }
//split till the first space // split till the first space
int delimiterIdx = line.indexOf(':'); int delimiterIdx = line.indexOf(':');
String username = line.substring(0, delimiterIdx).trim(); String username = line.substring(0, delimiterIdx).trim();
String password = line.substring(delimiterIdx + 1).trim(); String password = line.substring(delimiterIdx + 1).trim();
m_identities.put(username, password); m_identities.put(username, password);
} }
} }
@@ -126,5 +128,5 @@ public class ResourceAuthenticator implements IAuthenticator {
String encodedPasswd = DigestUtils.sha256Hex(password); String encodedPasswd = DigestUtils.sha256Hex(password);
return foundPwq.equals(encodedPasswd); return foundPwq.equals(encodedPasswd);
} }
} }